Skip to content

Commit

Permalink
startObserving: serialize database operations, and ignore current tra…
Browse files Browse the repository at this point in the history
…nsaction context when setting heartbeat, poll timeouts
  • Loading branch information
jayaddison committed Oct 7, 2020
1 parent 0035e27 commit 5237d33
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions addons/Dexie.Observable/src/Dexie.Observable.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ function Observable(db) {
}
// Add new sync node or if this is a reopening of the database after a close() call, update it.
return db.transaction('rw', '_syncNodes', () => {
return db._syncNodes
var currentMaster = db._syncNodes
.where('isMaster').equals(1)
.first(currentMaster => {
if (!currentMaster) {
Expand All @@ -256,18 +256,23 @@ function Observable(db) {
currentMaster.isMaster = 0;
return db._syncNodes.put(currentMaster);
}
}).then(()=>{
// Add our node to DB and start subscribing to events
return db._syncNodes.add(mySyncNode.node).then(function() {
Observable.on('latestRevisionIncremented', onLatestRevisionIncremented); // Wakeup when a new revision is available.
Observable.on('beforeunload', onBeforeUnload);
Observable.on('suicideNurseCall', onSuicide);
Observable.on('intercomm', onIntercomm);
// Start polling for changes and do cleanups:
pollHandle = setTimeout(poll, LOCAL_POLL);
// Start heartbeat
heartbeatHandle = setTimeout(heartbeat, HEARTBEAT_INTERVAL);
});
});
Promise.all([currentMaster]);

// Add our node to DB and start subscribing to events
var currentNode = db._syncNodes.add(mySyncNode.node).then(function() {
Observable.on('latestRevisionIncremented', onLatestRevisionIncremented); // Wakeup when a new revision is available.
Observable.on('beforeunload', onBeforeUnload);
Observable.on('suicideNurseCall', onSuicide);
Observable.on('intercomm', onIntercomm);
});
Promise.all([currentNode]);

return Dexie.ignoreTransaction(function() {
// Start polling for changes and do cleanups:
pollHandle = setTimeout(poll, LOCAL_POLL);
// Start heartbeat
heartbeatHandle = setTimeout(heartbeat, HEARTBEAT_INTERVAL);
});
}).then(function () {
cleanup();
Expand Down

0 comments on commit 5237d33

Please sign in to comment.