sw-background-sync-queue
Advanced tools
Comparing version 0.0.13 to 0.0.15
@@ -401,2 +401,34 @@ /* | ||
function isArrayOfType(object, expectedType) { | ||
const parameter = Object.keys(object).pop(); | ||
const message = `The '${parameter}' parameter should be an array containing | ||
one or more '${expectedType}' elements.`; | ||
if (!Array.isArray(object[parameter])) { | ||
throwError(message); | ||
} | ||
for (let item of object[parameter]) { | ||
if (typeof item !== expectedType) { | ||
throwError(message); | ||
} | ||
} | ||
} | ||
function isArrayOfClass(object, expectedClass) { | ||
const parameter = Object.keys(object).pop(); | ||
const message = `The '${parameter}' parameter should be an array containing | ||
one or more '${expectedClass.name}' instances.`; | ||
if (!Array.isArray(object[parameter])) { | ||
throwError(message); | ||
} | ||
for (let item of object[parameter]) { | ||
if (!(item instanceof expectedClass)) { | ||
throwError(message); | ||
} | ||
} | ||
} | ||
function isSWEnv() { | ||
@@ -439,3 +471,5 @@ return 'ServiceWorkerGlobalScope' in self && self instanceof ServiceWorkerGlobalScope; | ||
isSWEnv, | ||
isValue | ||
isValue, | ||
isArrayOfType, | ||
isArrayOfClass | ||
}; | ||
@@ -1025,3 +1059,4 @@ | ||
method: request.method, | ||
redirect: request.redirect | ||
redirect: request.redirect, | ||
credentials: request.credentials | ||
}; | ||
@@ -1055,3 +1090,4 @@ const requestBody = yield request.text(); | ||
redirect: idbRequestObject.redirect, | ||
headers: new Headers(JSON.parse(idbRequestObject.headers)) | ||
headers: new Headers(JSON.parse(idbRequestObject.headers)), | ||
credentials: idbRequestObject.credentials | ||
}; | ||
@@ -1058,0 +1094,0 @@ if (idbRequestObject.body) { |
@@ -20,3 +20,5 @@ /* | ||
${G}, actual: ${I})`)},isSWEnv:function isSWEnv(){return'ServiceWorkerGlobalScope'in self&&self instanceof ServiceWorkerGlobalScope},isValue:function isValue(F,G){const H=Object.keys(F).pop(),I=F[H];I!==G&&throwError(`The '${H}' parameter has the wrong value. (Expected: | ||
${G}, actual: ${I})`)}};let t='bgQueueSyncDB';var u=createCommonjsModule(function(F){'use strict';(function(){function toArray(H){return Array.prototype.slice.call(H)}function promisifyRequest(H){return new Promise(function(I,J){H.onsuccess=function(){I(H.result)},H.onerror=function(){J(H.error)}})}function promisifyRequestCall(H,I,J){var K,L=new Promise(function(M,N){K=H[I].apply(H,J),promisifyRequest(K).then(M,N)});return L.request=K,L}function promisifyCursorRequestCall(H,I,J){var K=promisifyRequestCall(H,I,J);return K.then(function(L){return L?new Cursor(L,K.request):void 0})}function proxyProperties(H,I,J){J.forEach(function(K){Object.defineProperty(H.prototype,K,{get:function(){return this[I][K]},set:function(L){this[I][K]=L}})})}function proxyRequestMethods(H,I,J,K){K.forEach(function(L){L in J.prototype&&(H.prototype[L]=function(){return promisifyRequestCall(this[I],L,arguments)})})}function proxyMethods(H,I,J,K){K.forEach(function(L){L in J.prototype&&(H.prototype[L]=function(){return this[I][L].apply(this[I],arguments)})})}function proxyCursorRequestMethods(H,I,J,K){K.forEach(function(L){L in J.prototype&&(H.prototype[L]=function(){return promisifyCursorRequestCall(this[I],L,arguments)})})}function Index(H){this._index=H}function Cursor(H,I){this._cursor=H,this._request=I}function ObjectStore(H){this._store=H}function Transaction(H){this._tx=H,this.complete=new Promise(function(I,J){H.oncomplete=function(){I()},H.onerror=function(){J(H.error)},H.onabort=function(){J(H.error)}})}function UpgradeDB(H,I,J){this._db=H,this.oldVersion=I,this.transaction=new Transaction(J)}function DB(H){this._db=H}proxyProperties(Index,'_index',['name','keyPath','multiEntry','unique']),proxyRequestMethods(Index,'_index',IDBIndex,['get','getKey','getAll','getAllKeys','count']),proxyCursorRequestMethods(Index,'_index',IDBIndex,['openCursor','openKeyCursor']),proxyProperties(Cursor,'_cursor',['direction','key','primaryKey','value']),proxyRequestMethods(Cursor,'_cursor',IDBCursor,['update','delete']),['advance','continue','continuePrimaryKey'].forEach(function(H){H in IDBCursor.prototype&&(Cursor.prototype[H]=function(){var I=this,J=arguments;return Promise.resolve().then(function(){return I._cursor[H].apply(I._cursor,J),promisifyRequest(I._request).then(function(K){return K?new Cursor(K,I._request):void 0})})})}),ObjectStore.prototype.createIndex=function(){return new Index(this._store.createIndex.apply(this._store,arguments))},ObjectStore.prototype.index=function(){return new Index(this._store.index.apply(this._store,arguments))},proxyProperties(ObjectStore,'_store',['name','keyPath','indexNames','autoIncrement']),proxyRequestMethods(ObjectStore,'_store',IDBObjectStore,['put','add','delete','clear','get','getAll','getKey','getAllKeys','count']),proxyCursorRequestMethods(ObjectStore,'_store',IDBObjectStore,['openCursor','openKeyCursor']),proxyMethods(ObjectStore,'_store',IDBObjectStore,['deleteIndex']),Transaction.prototype.objectStore=function(){return new ObjectStore(this._tx.objectStore.apply(this._tx,arguments))},proxyProperties(Transaction,'_tx',['objectStoreNames','mode']),proxyMethods(Transaction,'_tx',IDBTransaction,['abort']),UpgradeDB.prototype.createObjectStore=function(){return new ObjectStore(this._db.createObjectStore.apply(this._db,arguments))},proxyProperties(UpgradeDB,'_db',['name','version','objectStoreNames']),proxyMethods(UpgradeDB,'_db',IDBDatabase,['deleteObjectStore','close']),DB.prototype.transaction=function(){return new Transaction(this._db.transaction.apply(this._db,arguments))},proxyProperties(DB,'_db',['name','version','objectStoreNames']),proxyMethods(DB,'_db',IDBDatabase,['close']),['openCursor','openKeyCursor'].forEach(function(H){[ObjectStore,Index].forEach(function(I){I.prototype[H.replace('open','iterate')]=function(){var J=toArray(arguments),K=J[J.length-1],L=this._store||this._index,M=L[H].apply(L,J.slice(0,-1));M.onsuccess=function(){K(M.result)}}})}),[Index,ObjectStore].forEach(function(H){H.prototype.getAll||(H.prototype.getAll=function(I,J){var K=this,L=[];return new Promise(function(M){K.iterateCursor(I,function(N){return N?(L.push(N.value),void 0!==J&&L.length==J?void M(L):void N.continue()):void M(L)})})})});F.exports={open:function(H,I,J){var K=promisifyRequestCall(indexedDB,'open',[H,I]),L=K.request;return L.onupgradeneeded=function(M){J&&J(new UpgradeDB(L.result,M.oldVersion,L.transaction))},K.then(function(M){return new DB(M)})},delete:function(H){return promisifyRequestCall(indexedDB,'deleteDatabase',[H])}}})()});class IDBHelper{constructor(F,G,H){if(F==void 0||G==void 0||H==void 0)throw Error('name, version, storeName must be passed to the constructor.');this._name=F,this._version=G,this._storeName=H}_getDb(){return this._dbPromise?this._dbPromise:(this._dbPromise=u.open(this._name,this._version,(F)=>{F.createObjectStore(this._storeName)}).then((F)=>{return F}),this._dbPromise)}close(){return this._dbPromise?this._dbPromise.then((F)=>{F.close(),this._dbPromise=null}):void 0}put(F,G){return this._getDb().then((H)=>{const I=H.transaction(this._storeName,'readwrite'),J=I.objectStore(this._storeName);return J.put(G,F),I.complete})}delete(F){return this._getDb().then((G)=>{const H=G.transaction(this._storeName,'readwrite'),I=H.objectStore(this._storeName);return I.delete(F),H.complete})}get(F){return this._getDb().then((G)=>{return G.transaction(this._storeName).objectStore(this._storeName).get(F)})}getAllValues(){return this._getDb().then((F)=>{return F.transaction(this._storeName).objectStore(this._storeName).getAll()})}getAllKeys(){return this._getDb().then((F)=>{return F.transaction(this._storeName).objectStore(this._storeName).getAllKeys()})}}var w=function(F){return function(){var G=F.apply(this,arguments);return new Promise(function(H,I){function step(J,K){try{var L=G[J](K),M=L.value}catch(N){return void I(N)}return L.done?void H(M):Promise.resolve(M).then(function(N){step('next',N)},function(N){step('throw',N)})}return step('next')})}};let x=(()=>{var F=w(function*({hash:G,idbObject:H,response:I,idbQDb:J}){H.response={headers:JSON.stringify([...I.headers]),status:I.status,body:yield I.blob()},J.put(G,H)});return function putResponse(){return F.apply(this,arguments)}})(),y=(()=>{var F=w(function*({id:G}){const H=new IDBHelper(getDbName(),1,'QueueStore'),I=yield H.get(G);return I&&I.response?I.response:null});return function getResponse(){return F.apply(this,arguments)}})(),z=(()=>{var F=w(function*({request:G,config:H}){let I={config:H,metadata:{creationTimestamp:Date.now()},request:{url:G.url,headers:JSON.stringify([...G.headers]),mode:G.mode,method:G.method,redirect:G.redirect}};const J=yield G.text();return 0<J.length&&(I.request.body=J),I});return function getQueueableRequest(){return F.apply(this,arguments)}})(),A=(()=>{var F=w(function*({idbRequestObject:G}){let H={mode:G.mode,method:G.method,redirect:G.redirect,headers:new Headers(JSON.parse(G.headers))};return G.body&&(H.body=G.body),new Request(G.url,H)});return function getFetchableRequest(){return F.apply(this,arguments)}})(),B=(()=>{var F=w(function*(){let G=new IDBHelper(getDbName(),1,'QueueStore'),H=yield G.get(m);return H?void(yield Promise.all(H.map((()=>{var I=w(function*(J){const K=yield G.get(J);let L=[],M=[];yield Promise.all(K.map((()=>{var N=w(function*(O){const P=yield G.get(O);P&&P.metadata&&P.metadata.creationTimestamp+P.config.maxAge<=Date.now()?M.push(G.delete(O)):L.push(O)});return function(){return N.apply(this,arguments)}})())),yield Promise.all(M),G.put(J,L)});return function(){return I.apply(this,arguments)}})()))):null});return function cleanupQueue(){return F.apply(this,arguments)}})();class RequestManager{constructor({callbacks:F,queue:G}){this._globalCallbacks=F||{},this._queue=G,this.attachSyncHandler()}attachSyncHandler(){self.addEventListener('sync',(F)=>{F.tag===h+this._queue.queueName&&F.waitUntil(this.replayRequests())})}replayRequests(){var F=this;return this._queue.queue.reduce((G,H)=>{return G.then((()=>{var I=w(function*(){const K=yield F._queue.getRequestFromQueue({hash:H});if(!K.response){const L=yield A({idbRequestObject:K.request});return fetch(L).then(function(M){return M.ok?void(x({hash:H,idbObject:K,response:M.clone(),idbQDb:F._queue.idbQDb}),F._globalCallbacks.onResponse&&F._globalCallbacks.onResponse(H,M)):Promise.resolve()}).catch(function(M){F._globalCallbacks.onRetryFailure&&F._globalCallbacks.onRetryFailure(H,M)})}});return function(){return I.apply(this,arguments)}})())},Promise.resolve())}}let C=0,D=0;class RequestQueue{constructor({config:F,queueName:I='DEFAULT_QUEUE'+'_'+D++,idbQDb:G,broadcastChannel:H}){this._isQueueNameAddedToAllQueue=!1,this._queueName=I,this._config=F,this._idbQDb=G,this._broadcastChannel=H,this._queue=[],this.initQueue()}initQueue(){var F=this;return w(function*(){const G=yield F._idbQDb.get(F._queueName);F._queue.concat(G)})()}addQueueNameToAllQueues(){var F=this;return w(function*(){if(!F._isQueueNameAddedToAllQueue){let G=yield F._idbQDb.get(m);G=G||[],G.includes(F._queueName)||G.push(F._queueName),F._idbQDb.put(m,G),F._isQueueNameAddedToAllQueue=!0}})()}saveQueue(){var F=this;return w(function*(){yield F._idbQDb.put(F._queueName,F._queue)})()}push({request:F}){var G=this;return w(function*(){s.isInstance({request:F},Request);const H=`${F.url}!${Date.now()}!${C++}`,I=yield z({request:F,config:G._config});try{G._queue.push(H),G.saveQueue(),G._idbQDb.put(H,I),yield G.addQueueNameToAllQueues(),self.registration&&self.registration.sync.register(h+G._queueName),broadcastMessage({broadcastChannel:G._broadcastChannel,type:'BACKGROUND_REQUESTED_ADDED',id:H,url:F.url})}catch(J){broadcastMessage({broadcastChannel:G._broadcastChannel,type:'BACKGROUND_REQUESTED_FAILED',id:H,url:F.url})}})()}getRequestFromQueue({hash:F}){var G=this;return w(function*(){if(s.isType({hash:F},'string'),G._queue.includes(F))return yield G._idbQDb.get(F)})()}get queue(){return Object.assign([],this._queue)}get queueName(){return this._queueName}get idbQDb(){return this._idbQDb}}let E=(()=>{var F=w(function*({dbName:G}={}){G&&(s.isType({dbName:G},'string'),setDbName(G)),yield B()});return function initialize(){return F.apply(this,arguments)}})();a.initialize=E,a.getResponse=y,a.BackgroundSyncQueue=class BackgroundSyncQueue{constructor({maxRetentionTime:I=432000000,callbacks:F,queueName:G,broadcastChannel:H}={}){G&&s.isType({queueName:G},'string'),I&&s.isType({maxRetentionTime:I},'number'),H&&s.isInstance({broadcastChannel:H},BroadcastChannel),this._queue=new RequestQueue({config:{maxAge:I},queueName:G,idbQDb:new IDBHelper(getDbName(),1,'QueueStore'),broadcastChannel:H}),this._requestManager=new RequestManager({callbacks:F,queue:this._queue})}pushIntoQueue({request:F}){return s.isInstance({request:F},Request),this._queue.push({request:F})}fetchDidFail({request:F}){return this.pushIntoQueue({request:F})}},Object.defineProperty(a,'__esModule',{value:!0})}); | ||
${G}, actual: ${I})`)},isArrayOfType:function isArrayOfType(F,G){const H=Object.keys(F).pop(),I=`The '${H}' parameter should be an array containing | ||
one or more '${G}' elements.`;Array.isArray(F[H])||throwError(I);for(let J of F[H])typeof J!==G&&throwError(I)},isArrayOfClass:function isArrayOfClass(F,G){const H=Object.keys(F).pop(),I=`The '${H}' parameter should be an array containing | ||
one or more '${G.name}' instances.`;Array.isArray(F[H])||throwError(I);for(let J of F[H])J instanceof G||throwError(I)}};let t='bgQueueSyncDB';var u=createCommonjsModule(function(F){'use strict';(function(){function toArray(H){return Array.prototype.slice.call(H)}function promisifyRequest(H){return new Promise(function(I,J){H.onsuccess=function(){I(H.result)},H.onerror=function(){J(H.error)}})}function promisifyRequestCall(H,I,J){var K,L=new Promise(function(M,N){K=H[I].apply(H,J),promisifyRequest(K).then(M,N)});return L.request=K,L}function promisifyCursorRequestCall(H,I,J){var K=promisifyRequestCall(H,I,J);return K.then(function(L){return L?new Cursor(L,K.request):void 0})}function proxyProperties(H,I,J){J.forEach(function(K){Object.defineProperty(H.prototype,K,{get:function(){return this[I][K]},set:function(L){this[I][K]=L}})})}function proxyRequestMethods(H,I,J,K){K.forEach(function(L){L in J.prototype&&(H.prototype[L]=function(){return promisifyRequestCall(this[I],L,arguments)})})}function proxyMethods(H,I,J,K){K.forEach(function(L){L in J.prototype&&(H.prototype[L]=function(){return this[I][L].apply(this[I],arguments)})})}function proxyCursorRequestMethods(H,I,J,K){K.forEach(function(L){L in J.prototype&&(H.prototype[L]=function(){return promisifyCursorRequestCall(this[I],L,arguments)})})}function Index(H){this._index=H}function Cursor(H,I){this._cursor=H,this._request=I}function ObjectStore(H){this._store=H}function Transaction(H){this._tx=H,this.complete=new Promise(function(I,J){H.oncomplete=function(){I()},H.onerror=function(){J(H.error)},H.onabort=function(){J(H.error)}})}function UpgradeDB(H,I,J){this._db=H,this.oldVersion=I,this.transaction=new Transaction(J)}function DB(H){this._db=H}proxyProperties(Index,'_index',['name','keyPath','multiEntry','unique']),proxyRequestMethods(Index,'_index',IDBIndex,['get','getKey','getAll','getAllKeys','count']),proxyCursorRequestMethods(Index,'_index',IDBIndex,['openCursor','openKeyCursor']),proxyProperties(Cursor,'_cursor',['direction','key','primaryKey','value']),proxyRequestMethods(Cursor,'_cursor',IDBCursor,['update','delete']),['advance','continue','continuePrimaryKey'].forEach(function(H){H in IDBCursor.prototype&&(Cursor.prototype[H]=function(){var I=this,J=arguments;return Promise.resolve().then(function(){return I._cursor[H].apply(I._cursor,J),promisifyRequest(I._request).then(function(K){return K?new Cursor(K,I._request):void 0})})})}),ObjectStore.prototype.createIndex=function(){return new Index(this._store.createIndex.apply(this._store,arguments))},ObjectStore.prototype.index=function(){return new Index(this._store.index.apply(this._store,arguments))},proxyProperties(ObjectStore,'_store',['name','keyPath','indexNames','autoIncrement']),proxyRequestMethods(ObjectStore,'_store',IDBObjectStore,['put','add','delete','clear','get','getAll','getKey','getAllKeys','count']),proxyCursorRequestMethods(ObjectStore,'_store',IDBObjectStore,['openCursor','openKeyCursor']),proxyMethods(ObjectStore,'_store',IDBObjectStore,['deleteIndex']),Transaction.prototype.objectStore=function(){return new ObjectStore(this._tx.objectStore.apply(this._tx,arguments))},proxyProperties(Transaction,'_tx',['objectStoreNames','mode']),proxyMethods(Transaction,'_tx',IDBTransaction,['abort']),UpgradeDB.prototype.createObjectStore=function(){return new ObjectStore(this._db.createObjectStore.apply(this._db,arguments))},proxyProperties(UpgradeDB,'_db',['name','version','objectStoreNames']),proxyMethods(UpgradeDB,'_db',IDBDatabase,['deleteObjectStore','close']),DB.prototype.transaction=function(){return new Transaction(this._db.transaction.apply(this._db,arguments))},proxyProperties(DB,'_db',['name','version','objectStoreNames']),proxyMethods(DB,'_db',IDBDatabase,['close']),['openCursor','openKeyCursor'].forEach(function(H){[ObjectStore,Index].forEach(function(I){I.prototype[H.replace('open','iterate')]=function(){var J=toArray(arguments),K=J[J.length-1],L=this._store||this._index,M=L[H].apply(L,J.slice(0,-1));M.onsuccess=function(){K(M.result)}}})}),[Index,ObjectStore].forEach(function(H){H.prototype.getAll||(H.prototype.getAll=function(I,J){var K=this,L=[];return new Promise(function(M){K.iterateCursor(I,function(N){return N?(L.push(N.value),void 0!==J&&L.length==J?void M(L):void N.continue()):void M(L)})})})});F.exports={open:function(H,I,J){var K=promisifyRequestCall(indexedDB,'open',[H,I]),L=K.request;return L.onupgradeneeded=function(M){J&&J(new UpgradeDB(L.result,M.oldVersion,L.transaction))},K.then(function(M){return new DB(M)})},delete:function(H){return promisifyRequestCall(indexedDB,'deleteDatabase',[H])}}})()});class IDBHelper{constructor(F,G,H){if(F==void 0||G==void 0||H==void 0)throw Error('name, version, storeName must be passed to the constructor.');this._name=F,this._version=G,this._storeName=H}_getDb(){return this._dbPromise?this._dbPromise:(this._dbPromise=u.open(this._name,this._version,(F)=>{F.createObjectStore(this._storeName)}).then((F)=>{return F}),this._dbPromise)}close(){return this._dbPromise?this._dbPromise.then((F)=>{F.close(),this._dbPromise=null}):void 0}put(F,G){return this._getDb().then((H)=>{const I=H.transaction(this._storeName,'readwrite'),J=I.objectStore(this._storeName);return J.put(G,F),I.complete})}delete(F){return this._getDb().then((G)=>{const H=G.transaction(this._storeName,'readwrite'),I=H.objectStore(this._storeName);return I.delete(F),H.complete})}get(F){return this._getDb().then((G)=>{return G.transaction(this._storeName).objectStore(this._storeName).get(F)})}getAllValues(){return this._getDb().then((F)=>{return F.transaction(this._storeName).objectStore(this._storeName).getAll()})}getAllKeys(){return this._getDb().then((F)=>{return F.transaction(this._storeName).objectStore(this._storeName).getAllKeys()})}}var w=function(F){return function(){var G=F.apply(this,arguments);return new Promise(function(H,I){function step(J,K){try{var L=G[J](K),M=L.value}catch(N){return void I(N)}return L.done?void H(M):Promise.resolve(M).then(function(N){step('next',N)},function(N){step('throw',N)})}return step('next')})}};let x=(()=>{var F=w(function*({hash:G,idbObject:H,response:I,idbQDb:J}){H.response={headers:JSON.stringify([...I.headers]),status:I.status,body:yield I.blob()},J.put(G,H)});return function putResponse(){return F.apply(this,arguments)}})(),y=(()=>{var F=w(function*({id:G}){const H=new IDBHelper(getDbName(),1,'QueueStore'),I=yield H.get(G);return I&&I.response?I.response:null});return function getResponse(){return F.apply(this,arguments)}})(),z=(()=>{var F=w(function*({request:G,config:H}){let I={config:H,metadata:{creationTimestamp:Date.now()},request:{url:G.url,headers:JSON.stringify([...G.headers]),mode:G.mode,method:G.method,redirect:G.redirect,credentials:G.credentials}};const J=yield G.text();return 0<J.length&&(I.request.body=J),I});return function getQueueableRequest(){return F.apply(this,arguments)}})(),A=(()=>{var F=w(function*({idbRequestObject:G}){let H={mode:G.mode,method:G.method,redirect:G.redirect,headers:new Headers(JSON.parse(G.headers)),credentials:G.credentials};return G.body&&(H.body=G.body),new Request(G.url,H)});return function getFetchableRequest(){return F.apply(this,arguments)}})(),B=(()=>{var F=w(function*(){let G=new IDBHelper(getDbName(),1,'QueueStore'),H=yield G.get(m);return H?void(yield Promise.all(H.map((()=>{var I=w(function*(J){const K=yield G.get(J);let L=[],M=[];yield Promise.all(K.map((()=>{var N=w(function*(O){const P=yield G.get(O);P&&P.metadata&&P.metadata.creationTimestamp+P.config.maxAge<=Date.now()?M.push(G.delete(O)):L.push(O)});return function(){return N.apply(this,arguments)}})())),yield Promise.all(M),G.put(J,L)});return function(){return I.apply(this,arguments)}})()))):null});return function cleanupQueue(){return F.apply(this,arguments)}})();class RequestManager{constructor({callbacks:F,queue:G}){this._globalCallbacks=F||{},this._queue=G,this.attachSyncHandler()}attachSyncHandler(){self.addEventListener('sync',(F)=>{F.tag===h+this._queue.queueName&&F.waitUntil(this.replayRequests())})}replayRequests(){var F=this;return this._queue.queue.reduce((G,H)=>{return G.then((()=>{var I=w(function*(){const K=yield F._queue.getRequestFromQueue({hash:H});if(!K.response){const L=yield A({idbRequestObject:K.request});return fetch(L).then(function(M){return M.ok?void(x({hash:H,idbObject:K,response:M.clone(),idbQDb:F._queue.idbQDb}),F._globalCallbacks.onResponse&&F._globalCallbacks.onResponse(H,M)):Promise.resolve()}).catch(function(M){F._globalCallbacks.onRetryFailure&&F._globalCallbacks.onRetryFailure(H,M)})}});return function(){return I.apply(this,arguments)}})())},Promise.resolve())}}let C=0,D=0;class RequestQueue{constructor({config:F,queueName:I='DEFAULT_QUEUE'+'_'+D++,idbQDb:G,broadcastChannel:H}){this._isQueueNameAddedToAllQueue=!1,this._queueName=I,this._config=F,this._idbQDb=G,this._broadcastChannel=H,this._queue=[],this.initQueue()}initQueue(){var F=this;return w(function*(){const G=yield F._idbQDb.get(F._queueName);F._queue.concat(G)})()}addQueueNameToAllQueues(){var F=this;return w(function*(){if(!F._isQueueNameAddedToAllQueue){let G=yield F._idbQDb.get(m);G=G||[],G.includes(F._queueName)||G.push(F._queueName),F._idbQDb.put(m,G),F._isQueueNameAddedToAllQueue=!0}})()}saveQueue(){var F=this;return w(function*(){yield F._idbQDb.put(F._queueName,F._queue)})()}push({request:F}){var G=this;return w(function*(){s.isInstance({request:F},Request);const H=`${F.url}!${Date.now()}!${C++}`,I=yield z({request:F,config:G._config});try{G._queue.push(H),G.saveQueue(),G._idbQDb.put(H,I),yield G.addQueueNameToAllQueues(),self.registration&&self.registration.sync.register(h+G._queueName),broadcastMessage({broadcastChannel:G._broadcastChannel,type:'BACKGROUND_REQUESTED_ADDED',id:H,url:F.url})}catch(J){broadcastMessage({broadcastChannel:G._broadcastChannel,type:'BACKGROUND_REQUESTED_FAILED',id:H,url:F.url})}})()}getRequestFromQueue({hash:F}){var G=this;return w(function*(){if(s.isType({hash:F},'string'),G._queue.includes(F))return yield G._idbQDb.get(F)})()}get queue(){return Object.assign([],this._queue)}get queueName(){return this._queueName}get idbQDb(){return this._idbQDb}}let E=(()=>{var F=w(function*({dbName:G}={}){G&&(s.isType({dbName:G},'string'),setDbName(G)),yield B()});return function initialize(){return F.apply(this,arguments)}})();a.initialize=E,a.getResponse=y,a.BackgroundSyncQueue=class BackgroundSyncQueue{constructor({maxRetentionTime:I=432000000,callbacks:F,queueName:G,broadcastChannel:H}={}){G&&s.isType({queueName:G},'string'),I&&s.isType({maxRetentionTime:I},'number'),H&&s.isInstance({broadcastChannel:H},BroadcastChannel),this._queue=new RequestQueue({config:{maxAge:I},queueName:G,idbQDb:new IDBHelper(getDbName(),1,'QueueStore'),broadcastChannel:H}),this._requestManager=new RequestManager({callbacks:F,queue:this._queue})}pushIntoQueue({request:F}){return s.isInstance({request:F},Request),this._queue.push({request:F})}fetchDidFail({request:F}){return this.pushIntoQueue({request:F})}},Object.defineProperty(a,'__esModule',{value:!0})}); | ||
//# sourceMappingURL=background-sync-queue.min.js.map |
@@ -394,2 +394,34 @@ /* | ||
function isArrayOfType(object, expectedType) { | ||
const parameter = Object.keys(object).pop(); | ||
const message = `The '${parameter}' parameter should be an array containing | ||
one or more '${expectedType}' elements.`; | ||
if (!Array.isArray(object[parameter])) { | ||
throwError(message); | ||
} | ||
for (let item of object[parameter]) { | ||
if (typeof item !== expectedType) { | ||
throwError(message); | ||
} | ||
} | ||
} | ||
function isArrayOfClass(object, expectedClass) { | ||
const parameter = Object.keys(object).pop(); | ||
const message = `The '${parameter}' parameter should be an array containing | ||
one or more '${expectedClass.name}' instances.`; | ||
if (!Array.isArray(object[parameter])) { | ||
throwError(message); | ||
} | ||
for (let item of object[parameter]) { | ||
if (!(item instanceof expectedClass)) { | ||
throwError(message); | ||
} | ||
} | ||
} | ||
function isSWEnv() { | ||
@@ -432,3 +464,5 @@ return 'ServiceWorkerGlobalScope' in self && self instanceof ServiceWorkerGlobalScope; | ||
isSWEnv, | ||
isValue | ||
isValue, | ||
isArrayOfType, | ||
isArrayOfClass | ||
}; | ||
@@ -435,0 +469,0 @@ |
@@ -401,2 +401,34 @@ /* | ||
function isArrayOfType(object, expectedType) { | ||
const parameter = Object.keys(object).pop(); | ||
const message = `The '${parameter}' parameter should be an array containing | ||
one or more '${expectedType}' elements.`; | ||
if (!Array.isArray(object[parameter])) { | ||
throwError(message); | ||
} | ||
for (let item of object[parameter]) { | ||
if (typeof item !== expectedType) { | ||
throwError(message); | ||
} | ||
} | ||
} | ||
function isArrayOfClass(object, expectedClass) { | ||
const parameter = Object.keys(object).pop(); | ||
const message = `The '${parameter}' parameter should be an array containing | ||
one or more '${expectedClass.name}' instances.`; | ||
if (!Array.isArray(object[parameter])) { | ||
throwError(message); | ||
} | ||
for (let item of object[parameter]) { | ||
if (!(item instanceof expectedClass)) { | ||
throwError(message); | ||
} | ||
} | ||
} | ||
function isSWEnv() { | ||
@@ -439,3 +471,5 @@ return 'ServiceWorkerGlobalScope' in self && self instanceof ServiceWorkerGlobalScope; | ||
isSWEnv, | ||
isValue | ||
isValue, | ||
isArrayOfType, | ||
isArrayOfClass | ||
}; | ||
@@ -987,3 +1021,4 @@ | ||
method: request.method, | ||
redirect: request.redirect | ||
redirect: request.redirect, | ||
credentials: request.credentials | ||
}; | ||
@@ -1017,3 +1052,4 @@ const requestBody = yield request.text(); | ||
redirect: idbRequestObject.redirect, | ||
headers: new Headers(JSON.parse(idbRequestObject.headers)) | ||
headers: new Headers(JSON.parse(idbRequestObject.headers)), | ||
credentials: idbRequestObject.credentials | ||
}; | ||
@@ -1020,0 +1056,0 @@ if (idbRequestObject.body) { |
@@ -394,2 +394,34 @@ /* | ||
function isArrayOfType(object, expectedType) { | ||
const parameter = Object.keys(object).pop(); | ||
const message = `The '${parameter}' parameter should be an array containing | ||
one or more '${expectedType}' elements.`; | ||
if (!Array.isArray(object[parameter])) { | ||
throwError(message); | ||
} | ||
for (let item of object[parameter]) { | ||
if (typeof item !== expectedType) { | ||
throwError(message); | ||
} | ||
} | ||
} | ||
function isArrayOfClass(object, expectedClass) { | ||
const parameter = Object.keys(object).pop(); | ||
const message = `The '${parameter}' parameter should be an array containing | ||
one or more '${expectedClass.name}' instances.`; | ||
if (!Array.isArray(object[parameter])) { | ||
throwError(message); | ||
} | ||
for (let item of object[parameter]) { | ||
if (!(item instanceof expectedClass)) { | ||
throwError(message); | ||
} | ||
} | ||
} | ||
function isSWEnv() { | ||
@@ -432,3 +464,5 @@ return 'ServiceWorkerGlobalScope' in self && self instanceof ServiceWorkerGlobalScope; | ||
isSWEnv, | ||
isValue | ||
isValue, | ||
isArrayOfType, | ||
isArrayOfClass | ||
}; | ||
@@ -435,0 +469,0 @@ |
@@ -876,3 +876,4 @@ /* | ||
method: request.method, | ||
redirect: request.redirect | ||
redirect: request.redirect, | ||
credentials: request.credentials | ||
}; | ||
@@ -906,3 +907,4 @@ const requestBody = yield request.text(); | ||
redirect: idbRequestObject.redirect, | ||
headers: new Headers(JSON.parse(idbRequestObject.headers)) | ||
headers: new Headers(JSON.parse(idbRequestObject.headers)), | ||
credentials: idbRequestObject.credentials | ||
}; | ||
@@ -909,0 +911,0 @@ if (idbRequestObject.body) { |
@@ -746,3 +746,4 @@ /* | ||
redirect: idbRequestObject.redirect, | ||
headers: new Headers(JSON.parse(idbRequestObject.headers)) | ||
headers: new Headers(JSON.parse(idbRequestObject.headers)), | ||
credentials: idbRequestObject.credentials | ||
}; | ||
@@ -749,0 +750,0 @@ if (idbRequestObject.body) { |
@@ -726,2 +726,34 @@ /* | ||
function isArrayOfType(object, expectedType) { | ||
const parameter = Object.keys(object).pop(); | ||
const message = `The '${parameter}' parameter should be an array containing | ||
one or more '${expectedType}' elements.`; | ||
if (!Array.isArray(object[parameter])) { | ||
throwError(message); | ||
} | ||
for (let item of object[parameter]) { | ||
if (typeof item !== expectedType) { | ||
throwError(message); | ||
} | ||
} | ||
} | ||
function isArrayOfClass(object, expectedClass) { | ||
const parameter = Object.keys(object).pop(); | ||
const message = `The '${parameter}' parameter should be an array containing | ||
one or more '${expectedClass.name}' instances.`; | ||
if (!Array.isArray(object[parameter])) { | ||
throwError(message); | ||
} | ||
for (let item of object[parameter]) { | ||
if (!(item instanceof expectedClass)) { | ||
throwError(message); | ||
} | ||
} | ||
} | ||
function isSWEnv() { | ||
@@ -764,3 +796,5 @@ return 'ServiceWorkerGlobalScope' in self && self instanceof ServiceWorkerGlobalScope; | ||
isSWEnv, | ||
isValue | ||
isValue, | ||
isArrayOfType, | ||
isArrayOfClass | ||
}; | ||
@@ -822,3 +856,4 @@ | ||
method: request.method, | ||
redirect: request.redirect | ||
redirect: request.redirect, | ||
credentials: request.credentials | ||
}; | ||
@@ -825,0 +860,0 @@ const requestBody = yield request.text(); |
@@ -401,2 +401,34 @@ /* | ||
function isArrayOfType(object, expectedType) { | ||
const parameter = Object.keys(object).pop(); | ||
const message = `The '${parameter}' parameter should be an array containing | ||
one or more '${expectedType}' elements.`; | ||
if (!Array.isArray(object[parameter])) { | ||
throwError(message); | ||
} | ||
for (let item of object[parameter]) { | ||
if (typeof item !== expectedType) { | ||
throwError(message); | ||
} | ||
} | ||
} | ||
function isArrayOfClass(object, expectedClass) { | ||
const parameter = Object.keys(object).pop(); | ||
const message = `The '${parameter}' parameter should be an array containing | ||
one or more '${expectedClass.name}' instances.`; | ||
if (!Array.isArray(object[parameter])) { | ||
throwError(message); | ||
} | ||
for (let item of object[parameter]) { | ||
if (!(item instanceof expectedClass)) { | ||
throwError(message); | ||
} | ||
} | ||
} | ||
function isSWEnv() { | ||
@@ -439,3 +471,5 @@ return 'ServiceWorkerGlobalScope' in self && self instanceof ServiceWorkerGlobalScope; | ||
isSWEnv, | ||
isValue | ||
isValue, | ||
isArrayOfType, | ||
isArrayOfClass | ||
}; | ||
@@ -1025,3 +1059,4 @@ | ||
method: request.method, | ||
redirect: request.redirect | ||
redirect: request.redirect, | ||
credentials: request.credentials | ||
}; | ||
@@ -1055,3 +1090,4 @@ const requestBody = yield request.text(); | ||
redirect: idbRequestObject.redirect, | ||
headers: new Headers(JSON.parse(idbRequestObject.headers)) | ||
headers: new Headers(JSON.parse(idbRequestObject.headers)), | ||
credentials: idbRequestObject.credentials | ||
}; | ||
@@ -1058,0 +1094,0 @@ if (idbRequestObject.body) { |
{ | ||
"name": "sw-background-sync-queue", | ||
"version": "0.0.13", | ||
"version": "0.0.15", | ||
"description": "Queues failed requests and uses the Background Sync API to replay those requests at a later time when the network state has changed.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -29,2 +29,3 @@ import IDBHelper from '../../../../lib/idb-helper'; | ||
redirect: request.redirect, | ||
credentials: request.credentials, | ||
}; | ||
@@ -51,2 +52,3 @@ const requestBody = await request.text(); | ||
headers: new Headers(JSON.parse(idbRequestObject.headers)), | ||
credentials: idbRequestObject.credentials, | ||
}; | ||
@@ -53,0 +55,0 @@ if(idbRequestObject.body) { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
577605
10984