-
Notifications
You must be signed in to change notification settings - Fork 830
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bg sync replay fix #601
Bg sync replay fix #601
Changes from all commits
774e941
bfbd46d
93c1b24
dc316e1
583837f
1f9f5bf
129f4f5
280e3cb
8a66894
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,6 @@ class RequestManager { | |
* attaches event handler | ||
* @param {Object=} config | ||
* | ||
* @memberOf RequestManager | ||
* @private | ||
*/ | ||
constructor({callbacks, queue}) { | ||
|
@@ -29,7 +28,6 @@ class RequestManager { | |
* attaches sync handler to replay requests when | ||
* sync event is fired | ||
* | ||
* @memberOf RequestManager | ||
* @private | ||
*/ | ||
attachSyncHandler() { | ||
|
@@ -42,49 +40,67 @@ class RequestManager { | |
} | ||
|
||
/** | ||
* function to start playing requests | ||
* in sequence | ||
* @return {void} | ||
* function to play one single request given its hash | ||
* | ||
* @param {String} hash | ||
* | ||
* @return {Promise} Resolves if the request corresponding to the hash is | ||
* played successfully, rejects if it fails during the replay | ||
* | ||
* | ||
* @memberOf RequestManager | ||
* @private | ||
*/ | ||
replayRequests() { | ||
return this._queue.queue.reduce((promise, hash) => { | ||
return promise | ||
.then(async (item) => { | ||
const reqData = await this._queue.getRequestFromQueue({hash}); | ||
if(reqData.response) { | ||
// check if request is not played already | ||
return; | ||
} | ||
|
||
const request = await getFetchableRequest({ | ||
idbRequestObject: reqData.request, | ||
}); | ||
|
||
return fetch(request) | ||
.then((response)=>{ | ||
if(!response.ok) { | ||
return Promise.resolve(); | ||
} else { | ||
// not blocking on putResponse. | ||
putResponse({ | ||
hash, | ||
idbObject: reqData, | ||
response: response.clone(), | ||
idbQDb: this._queue.idbQDb, | ||
}); | ||
this._globalCallbacks.onResponse | ||
&& this._globalCallbacks.onResponse(hash, response); | ||
} | ||
}) | ||
.catch((err)=>{ | ||
this._globalCallbacks.onRetryFailure | ||
&& this._globalCallbacks.onRetryFailure(hash, err); | ||
}); | ||
async replayRequest(hash) { | ||
try { | ||
const reqData = await this._queue.getRequestFromQueue({hash}); | ||
if(reqData.response) { | ||
return; | ||
} | ||
const request = await getFetchableRequest({ | ||
idbRequestObject: reqData.request, | ||
}); | ||
const response = await fetch(request); | ||
if(!response.ok) { | ||
return Promise.reject(response); | ||
} else { | ||
// not blocking on putResponse. | ||
putResponse({ | ||
hash, | ||
idbObject: reqData, | ||
response: response.clone(), | ||
idbQDb: this._queue.idbQDb, | ||
}); | ||
}, Promise.resolve()); | ||
if (this._globalCallbacks.onResponse) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not to be address in this PR, but I can't help but feel like this should be an event that is emitted rather than a callback function. |
||
this._globalCallbacks.onResponse(hash, response); | ||
} | ||
} catch(err) { | ||
return Promise.reject(err); | ||
} | ||
} | ||
|
||
/** | ||
* This function is to be called to replay all the requests | ||
* in the current queue. It will play all the requests and return a promise | ||
* based on the successfull execution of the requests. | ||
* | ||
* @return {Promise} Resolves if all requests are played successfully, | ||
* rejects if any of the request fails during the replay | ||
* | ||
* @private | ||
*/ | ||
async replayRequests() { | ||
const failedItems = []; | ||
for (let hash of this._queue.queue) { | ||
try { | ||
await this.replayRequest(hash); | ||
} catch (err) { | ||
if(this._globalCallbacks.onRetryFailure) | ||
this._globalCallbacks.onRetryFailure(hash, err); | ||
failedItems.push(err); | ||
} | ||
} | ||
return failedItems.length > 0 ? | ||
Promise.reject(failedItems) : Promise.resolve(); | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -140,7 +140,8 @@ class RequestQueue { | |
assert.isType({hash}, 'string'); | ||
|
||
if(this._queue.includes(hash)) { | ||
return await this._idbQDb.get(hash); | ||
const req = await this._idbQDb.get(hash); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gauntface just wanted to discuss this small change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nope nothing from me. cc @jeffposnick as FYI |
||
return req; | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jeffposnick this prevents replaying an already "successfully" played request
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotcha.