-
Notifications
You must be signed in to change notification settings - Fork 822
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
Background sync queue callbacks #666
Changes from 5 commits
90cd2f0
6f05e67
f190b28
9739578
ac3d74a
1bf91ec
509245d
8af00af
1827413
21f7ad2
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
Copyright 2017 Google Inc. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
/** | ||
* Warns users that an old object method is deprecated in favor of a new method | ||
* and aliases the old name to the new name. | ||
* @param {Object} obj The object containing the methods. | ||
* @param {string} base The base project/object to identify the method names. | ||
* @param {string} oldMethod The method to deprecate. | ||
* @param {string} newMethod The new method replacing the deprecated method. | ||
*/ | ||
export default (obj, base, oldMethod, newMethod) => { | ||
/* eslint-disable no-console */ | ||
console.warn(`In ${base}: ` + | ||
`${oldMethod} is deprecated, use ${newMethod} instead`); | ||
/* eslint-enable no-console */ | ||
|
||
if (obj[oldMethod] && !obj[newMethod]) { | ||
obj[newMethod] = obj[oldMethod]; | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,12 +32,14 @@ class RequestQueue { | |
queueName = defaultQueueName + '_' + _queueCounter++, | ||
idbQDb, | ||
broadcastChannel, | ||
callbacks, | ||
}) { | ||
this._isQueueNameAddedToAllQueue = false; | ||
this._queueName = queueName; | ||
this._config = config; | ||
this._idbQDb = idbQDb; | ||
this._broadcastChannel = broadcastChannel; | ||
this._globalCallbacks = callbacks || {}; | ||
this._queue = []; | ||
this.initQueue(); | ||
} | ||
|
@@ -86,6 +88,7 @@ class RequestQueue { | |
* preferably when network comes back | ||
* | ||
* @param {Request} request request object to be queued by this | ||
* @return {Promise} | ||
* | ||
* @memberOf Queue | ||
* @private | ||
|
@@ -94,17 +97,23 @@ class RequestQueue { | |
isInstance({request}, Request); | ||
|
||
const hash = `${request.url}!${Date.now()}!${_requestCounter++}`; | ||
const queuableRequest = | ||
await getQueueableRequest({ | ||
request, | ||
config: this._config, | ||
}); | ||
const reqData = await getQueueableRequest({ | ||
request, | ||
config: this._config, | ||
}); | ||
|
||
// Apply the `requestWillEnqueue` callback so plugins can modify the | ||
// request data before it's stored in IndexedDB. | ||
if (this._globalCallbacks.requestWillEnqueue) { | ||
this._globalCallbacks.requestWillEnqueue(reqData); | ||
} | ||
|
||
try{ | ||
this._queue.push(hash); | ||
|
||
// add to queue | ||
this.saveQueue(); | ||
this._idbQDb.put(hash, queuableRequest); | ||
this._idbQDb.put(hash, reqData); | ||
await this.addQueueNameToAllQueues(); | ||
// register sync | ||
self.registration && | ||
|
@@ -117,31 +126,42 @@ class RequestQueue { | |
id: hash, | ||
url: request.url, | ||
}); | ||
} catch(e) { | ||
|
||
return hash; | ||
} catch (err) { | ||
// broadcast the failure of request added to the queue | ||
broadcastMessage({ | ||
broadcastChannel: this._broadcastChannel, | ||
type: broadcastMessageFailedType, | ||
id: hash, | ||
url: request.url, | ||
}); | ||
|
||
return err; | ||
} | ||
} | ||
|
||
/** | ||
* get the Request from the queue at a particular index | ||
* | ||
* @param {string} hash hash of the request at the given index | ||
* @return {Request} request object corresponding to given hash | ||
* @return {Promise} request object corresponding to given 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.
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. This doesn't fulfill to a I could update it to: /** @return {Promise<Object>} */ But I didn't see much of that in the existing code. 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. my bad it is indeed
|
||
* @memberOf Queue | ||
* @private | ||
*/ | ||
async getRequestFromQueue({hash}) { | ||
isType({hash}, 'string'); | ||
|
||
if(this._queue.includes(hash)) { | ||
const req = await this._idbQDb.get(hash); | ||
return req; | ||
const reqData = await this._idbQDb.get(hash); | ||
|
||
// Apply the `requestWillDequeue` callback so plugins can modify the | ||
// stored data before it's converted back into a request to be replayed. | ||
if (this._globalCallbacks.requestWillDequeue) { | ||
this._globalCallbacks.requestWillDequeue(reqData); | ||
} | ||
|
||
return reqData; | ||
} | ||
} | ||
|
||
|
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.
Could you make these calls to
deprecate
conditional, and only invoke each one if the correspondingcallbacks.onResponse
/callbacks.onRetryFailure
are set?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.
Yeah, this was an oversight and noticed it when updating the GA stuff. It should be fixed now.