workbox-background-sync
Advanced tools
Comparing version 3.0.0-beta.0 to 3.0.0-beta.1
this.workbox = this.workbox || {}; | ||
this.workbox.backgroundSync = (function (WorkboxError_mjs,logger_mjs,DBWrapper_mjs) { | ||
this.workbox.backgroundSync = (function (DBWrapper_mjs,WorkboxError_mjs,logger_mjs,assert_mjs,getFriendlyURL_mjs) { | ||
'use strict'; | ||
try { | ||
self.workbox.v['workbox:background-sync:3.0.0-beta.0'] = 1; | ||
self.workbox.v['workbox:background-sync:3.0.0-beta.1'] = 1; | ||
} catch (e) {} // eslint-disable-line | ||
@@ -127,2 +127,23 @@ | ||
} | ||
/** | ||
* Creates and returns a deep clone of the instance. | ||
* | ||
* @return {StorableRequest} | ||
* | ||
* @private | ||
*/ | ||
clone() { | ||
const requestInit = Object.assign({}, this.requestInit); | ||
requestInit.headers = Object.assign({}, this.requestInit.headers); | ||
if (this.requestInit.body) { | ||
requestInit.body = this.requestInit.body.slice(); | ||
} | ||
return new StorableRequest({ | ||
url: this.url, | ||
timestamp: this.timestamp, | ||
requestInit | ||
}); | ||
} | ||
} | ||
@@ -323,2 +344,11 @@ | ||
return babelHelpers.asyncToGenerator(function* () { | ||
{ | ||
assert_mjs.assert.isInstance(request, Request, { | ||
moduleName: 'workbox-background-sync', | ||
className: 'Queue', | ||
funcName: 'addRequest', | ||
paramName: 'request' | ||
}); | ||
} | ||
const storableRequest = yield StorableRequest.fromRequest(request.clone()); | ||
@@ -328,2 +358,6 @@ yield _this._runCallback('requestWillEnqueue', storableRequest); | ||
yield _this._registerSync(); | ||
{ | ||
logger_mjs.logger.log(`Request for '${getFriendlyURL_mjs.getFriendlyURL(storableRequest.url)}' has been | ||
added to background sync queue '${_this._name}'.`); | ||
} | ||
})(); | ||
@@ -349,2 +383,6 @@ } | ||
while (storableRequest = yield _this2._queueStore.getAndRemoveOldestEntry()) { | ||
// Make a copy so the unmodified request can be stored | ||
// in the event of a replay failure. | ||
const storableRequestClone = storableRequest.clone(); | ||
// Ignore requests older than maxRetentionTime. | ||
@@ -363,5 +401,13 @@ const maxRetentionTimeInMs = _this2._maxRetentionTime * 60 * 1000; | ||
replay.response = yield fetch(replay.request.clone()); | ||
{ | ||
logger_mjs.logger.log(`Request for '${getFriendlyURL_mjs.getFriendlyURL(storableRequest.url)}' | ||
has been replayed`); | ||
} | ||
} catch (err) { | ||
{ | ||
logger_mjs.logger.log(`Request for '${getFriendlyURL_mjs.getFriendlyURL(storableRequest.url)}' | ||
failed to replay`); | ||
} | ||
replay.error = err; | ||
failedRequests.push(storableRequest); | ||
failedRequests.push(storableRequestClone); | ||
} | ||
@@ -413,5 +459,14 @@ | ||
self.addEventListener('sync', event => { | ||
event.waitUntil(this.replayRequests()); | ||
if (event.tag === `${TAG_PREFIX}:${this._name}`) { | ||
{ | ||
logger_mjs.logger.log(`Background sync for tag '${event.tag}' | ||
has been received, starting replay now`); | ||
} | ||
event.waitUntil(this.replayRequests()); | ||
} | ||
}); | ||
} else { | ||
{ | ||
logger_mjs.logger.log(`Background sync replaying without background sync event`); | ||
} | ||
// If the browser doesn't support background sync, retry | ||
@@ -521,4 +576,3 @@ // every time the service worker starts up as a fallback. | ||
var publicAPI$1 = Object.freeze({ | ||
var publicAPI = Object.freeze({ | ||
Queue: Queue, | ||
@@ -543,6 +597,6 @@ Plugin: Plugin | ||
return publicAPI$1; | ||
return publicAPI; | ||
}(workbox.core._private,workbox.core._private,workbox.core._private)); | ||
}(workbox.core._private,workbox.core._private,workbox.core._private,workbox.core._private,workbox.core._private)); | ||
//# sourceMappingURL=workbox-background-sync.dev.js.map |
@@ -1,3 +0,3 @@ | ||
this.workbox=this.workbox||{},this.workbox.backgroundSync=function(e,t){"use strict";try{self.workbox.v["workbox:background-sync:3.0.0-beta.0"]=1}catch(e){}const r=["method","referrer","referrerPolicy","mode","credentials","cache","redirect","integrity","keepalive"];class n{static fromRequest(e){return babelHelpers.asyncToGenerator(function*(){const t={headers:{}};"GET"!==e.method&&(t.body=yield e.clone().blob());for(const[r,n]of e.headers.entries())t.headers[r]=n;for(const n of r)void 0!==e[n]&&(t[n]=e[n]);return new n({url:e.url,requestInit:t})})()}constructor({url:e,requestInit:t,timestamp:r=Date.now()}){this.url=e,this.requestInit=t,this.e=r}get timestamp(){return this.e}toObject(){return{url:this.url,timestamp:this.timestamp,requestInit:this.requestInit}}toRequest(){return new Request(this.url,this.requestInit)}}const s="workbox-background-sync",i="requests",u="queueName",c="workbox-background-sync",o=10080;class l{constructor(e){this.t=e,this.r=new t.DBWrapper(s,1,{onupgradeneeded:e=>e.target.result.createObjectStore(i,{autoIncrement:!0}).createIndex(u,u,{unique:!1})})}addEntry(e){var t=this;return babelHelpers.asyncToGenerator(function*(){yield t.r.add(i,{queueName:t.t.name,storableRequest:e.toObject()})})()}getAndRemoveOldestEntry(){var e=this;return babelHelpers.asyncToGenerator(function*(){const[t]=yield e.r.getAllMatching(i,{index:u,query:IDBKeyRange.only(e.t.name),count:1,includeKeys:!0});if(t)return yield e.r.delete(i,t.primaryKey),new n(t.value.storableRequest)})()}}const a=new Set;class h{constructor(t,{callbacks:r={},maxRetentionTime:n=o}={}){if(a.has(t))throw new e.WorkboxError("duplicate-queue-name",{name:t});a.add(t),this.n=t,this.s=r,this.i=n,this.u=new l(this),this.c()}get name(){return this.n}addRequest(e){var t=this;return babelHelpers.asyncToGenerator(function*(){const r=yield n.fromRequest(e.clone());yield t.o("requestWillEnqueue",r),yield t.u.addEntry(r),yield t.l()})()}replayRequests(){var t=this;return babelHelpers.asyncToGenerator(function*(){const r=Date.now(),n=[],s=[];let i;for(;i=yield t.u.getAndRemoveOldestEntry();){const e=60*t.i*1e3;if(r-i.timestamp>e)continue;yield t.o("requestWillReplay",i);const u={request:i.toRequest()};try{u.response=yield fetch(u.request.clone())}catch(e){u.error=e,s.push(i)}n.push(u)}if(yield t.o("queueDidReplay",n),s.length)throw yield Promise.all(s.map(function(e){return t.u.addEntry(e)})),new e.WorkboxError("queue-replay-failed",{name:t.n,count:s.length})})()}o(e,...t){var r=this;return babelHelpers.asyncToGenerator(function*(){"function"==typeof r.s[e]&&(yield r.s[e].apply(null,t))})()}c(){"sync"in registration?self.addEventListener("sync",e=>{e.waitUntil(this.replayRequests())}):this.replayRequests()}l(){var e=this;return babelHelpers.asyncToGenerator(function*(){if("sync"in registration)try{yield registration.sync.register(`${c}:${e.n}`)}catch(e){}})()}static get a(){return a}}return Object.freeze({Queue:h,Plugin:class{constructor(...e){this.t=new h(...e),this.fetchDidFail=this.fetchDidFail.bind(this)}fetchDidFail({request:e}){var t=this;return babelHelpers.asyncToGenerator(function*(){yield t.t.addRequest(e)})()}}})}(workbox.core._private,workbox.core._private); | ||
this.workbox=this.workbox||{},this.workbox.backgroundSync=function(e,t){"use strict";try{self.workbox.v["workbox:background-sync:3.0.0-beta.1"]=1}catch(e){}const r=["method","referrer","referrerPolicy","mode","credentials","cache","redirect","integrity","keepalive"];class s{static fromRequest(e){return babelHelpers.asyncToGenerator(function*(){const t={headers:{}};"GET"!==e.method&&(t.body=yield e.clone().blob());for(const[r,s]of e.headers.entries())t.headers[r]=s;for(const s of r)void 0!==e[s]&&(t[s]=e[s]);return new s({url:e.url,requestInit:t})})()}constructor({url:e,requestInit:t,timestamp:r=Date.now()}){this.url=e,this.requestInit=t,this.e=r}get timestamp(){return this.e}toObject(){return{url:this.url,timestamp:this.timestamp,requestInit:this.requestInit}}toRequest(){return new Request(this.url,this.requestInit)}clone(){const e=Object.assign({},this.requestInit);return e.headers=Object.assign({},this.requestInit.headers),this.requestInit.body&&(e.body=this.requestInit.body.slice()),new s({url:this.url,timestamp:this.timestamp,requestInit:e})}}const i="workbox-background-sync",n="requests",u="queueName",c="workbox-background-sync",o=10080;class l{constructor(t){this.t=t,this.r=new e.DBWrapper(i,1,{onupgradeneeded:e=>e.target.result.createObjectStore(n,{autoIncrement:!0}).createIndex(u,u,{unique:!1})})}addEntry(e){var t=this;return babelHelpers.asyncToGenerator(function*(){yield t.r.add(n,{queueName:t.t.name,storableRequest:e.toObject()})})()}getAndRemoveOldestEntry(){var e=this;return babelHelpers.asyncToGenerator(function*(){const[t]=yield e.r.getAllMatching(n,{index:u,query:IDBKeyRange.only(e.t.name),count:1,includeKeys:!0});if(t)return yield e.r.delete(n,t.primaryKey),new s(t.value.storableRequest)})()}}const a=new Set;class h{constructor(e,{callbacks:r={},maxRetentionTime:s=o}={}){if(a.has(e))throw new t.WorkboxError("duplicate-queue-name",{name:e});a.add(e),this.s=e,this.i=r,this.n=s,this.u=new l(this),this.c()}get name(){return this.s}addRequest(e){var t=this;return babelHelpers.asyncToGenerator(function*(){const r=yield s.fromRequest(e.clone());yield t.o("requestWillEnqueue",r),yield t.u.addEntry(r),yield t.l()})()}replayRequests(){var e=this;return babelHelpers.asyncToGenerator(function*(){const r=Date.now(),s=[],i=[];let n;for(;n=yield e.u.getAndRemoveOldestEntry();){const t=n.clone(),u=60*e.n*1e3;if(r-n.timestamp>u)continue;yield e.o("requestWillReplay",n);const c={request:n.toRequest()};try{c.response=yield fetch(c.request.clone())}catch(e){c.error=e,i.push(t)}s.push(c)}if(yield e.o("queueDidReplay",s),i.length)throw yield Promise.all(i.map(function(t){return e.u.addEntry(t)})),new t.WorkboxError("queue-replay-failed",{name:e.s,count:i.length})})()}o(e,...t){var r=this;return babelHelpers.asyncToGenerator(function*(){"function"==typeof r.i[e]&&(yield r.i[e].apply(null,t))})()}c(){"sync"in registration?self.addEventListener("sync",e=>{e.tag===`${c}:${this.s}`&&e.waitUntil(this.replayRequests())}):this.replayRequests()}l(){var e=this;return babelHelpers.asyncToGenerator(function*(){if("sync"in registration)try{yield registration.sync.register(`${c}:${e.s}`)}catch(e){}})()}static get a(){return a}}return Object.freeze({Queue:h,Plugin:class{constructor(...e){this.t=new h(...e),this.fetchDidFail=this.fetchDidFail.bind(this)}fetchDidFail({request:e}){var t=this;return babelHelpers.asyncToGenerator(function*(){yield t.t.addRequest(e)})()}}})}(workbox.core._private,workbox.core._private); | ||
//# sourceMappingURL=workbox-background-sync.prod.js.map |
{ | ||
"name": "workbox-background-sync", | ||
"version": "3.0.0-beta.0", | ||
"version": "3.0.0-beta.1", | ||
"license": "Apache-2.0", | ||
@@ -30,4 +30,4 @@ "author": "Google's Web DevRel Team", | ||
"dependencies": { | ||
"workbox-core": "^3.0.0-beta.0" | ||
"workbox-core": "^3.0.0-beta.1" | ||
} | ||
} |
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
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
64886
1076
8
Updatedworkbox-core@^3.0.0-beta.1