-
-
Notifications
You must be signed in to change notification settings - Fork 592
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
Don't do /keys/changes on incremental sync #625
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4f17352
Don't do /keys/changes on incremental sync
dbkr 727ad57
lint
dbkr a0578ef
fix tests
dbkr 3d1fcc6
One day I'll learn to spell guaranteed
dbkr facfcf6
DeviceList: bring save forward if necessary
dbkr 5a23927
Move comment up
dbkr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,6 +92,12 @@ export default class DeviceList { | |
|
||
// Promise resolved when device data is saved | ||
this._savePromise = null; | ||
// Function that resolves the save promise | ||
this._resolveSavePromise = null; | ||
// The time the save is scheduled for | ||
this._savePromiseTime = null; | ||
// The timer used to delay the save | ||
this._saveTimer = null; | ||
} | ||
|
||
/** | ||
|
@@ -146,38 +152,68 @@ export default class DeviceList { | |
* The actual save will be delayed by a short amount of time to | ||
* aggregate multiple writes to the database. | ||
* | ||
* @param {integer} delay Time in ms before which the save actually happens. | ||
* By default, the save is delayed for a short period in order to batch | ||
* multiple writes, but this behaviour can be disabled by passing 0. | ||
* | ||
* @return {Promise<bool>} true if the data was saved, false if | ||
* it was not (eg. because no changes were pending). The promise | ||
* will only resolve once the data is saved, so may take some time | ||
* to resolve. | ||
*/ | ||
async saveIfDirty() { | ||
async saveIfDirty(delay) { | ||
if (!this._dirty) return Promise.resolve(false); | ||
if (delay === undefined) delay = 500; | ||
|
||
const targetTime = Date.now + delay; | ||
if (this._savePromiseTime && targetTime < this._savePromiseTime) { | ||
// There's a save scheduled but for after we would like: cancel | ||
// it & schedule one for the time we want | ||
clearTimeout(this._saveTimer); | ||
this._saveTimer = null; | ||
this._savePromiseTime = null; | ||
// (but keep the save promise since whatever called save before | ||
// will still want to know when the save is done) | ||
} | ||
|
||
let savePromise = this._savePromise; | ||
if (savePromise === null) { | ||
savePromise = new Promise((resolve, reject) => { | ||
this._resolveSavePromise = resolve; | ||
}); | ||
this._savePromise = savePromise; | ||
} | ||
|
||
if (this._savePromise === null) { | ||
if (this._saveTimer === null) { | ||
// Delay saves for a bit so we can aggregate multiple saves that happen | ||
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 comment probably needs to move up now |
||
// in quick succession (eg. when a whole room's devices are marked as known) | ||
this._savePromise = Promise.delay(500).then(() => { | ||
const resolveSavePromise = this._resolveSavePromise; | ||
this._savePromiseTime = targetTime; | ||
this._saveTimer = setTimeout(() => { | ||
console.log('Saving device tracking data at token ' + this._syncToken); | ||
// null out savePromise now (after the delay but before the write), | ||
// otherwise we could return the existing promise when the save has | ||
// actually already happened. Likewise for the dirty flag. | ||
this._savePromiseTime = null; | ||
this._saveTimer = null; | ||
this._savePromise = null; | ||
this._resolveSavePromise = null; | ||
|
||
this._dirty = false; | ||
return this._cryptoStore.doTxn( | ||
this._cryptoStore.doTxn( | ||
'readwrite', [IndexedDBCryptoStore.STORE_DEVICE_DATA], (txn) => { | ||
this._cryptoStore.storeEndToEndDeviceData({ | ||
devices: this._devices, | ||
trackingStatus: this._deviceTrackingStatus, | ||
syncToken: this._syncToken, | ||
}, txn); | ||
}, | ||
); | ||
}).then(() => { | ||
return true; | ||
}); | ||
).then(() => { | ||
resolveSavePromise(); | ||
}); | ||
}, delay); | ||
} | ||
return this._savePromise; | ||
return savePromise; | ||
} | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I'm a bit concerned that, if there is already a save scheduled, it might not happen for 500ms even if delay is 0. can you reorganise this somehow so that it will happen immediately?
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.
I was wondering whether this was worth worrying about. Have added it (although since Promise.delay can only be left to run or canceled outright, it's all had to change to a setTimeout)