Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function consumeIntercommMessages() {
// Check if we got messages:
if (!mySyncNode.node) return Promise.reject(new Dexie.DatabaseClosedError());
return Dexie.ignoreTransaction(()=> {
return db.transaction('rw', '_intercomm', function() {
return db._intercomm.where({destinationNode: mySyncNode.node.id}).toArray(messages => {
messages.forEach(msg => consumeMessage(msg));
return db._intercomm.where('id').anyOf(messages.map(msg => msg.id)).delete();
});
});
});
}
return enqueue(applyRemoteChanges, function () {
if (!stillAlive()) return Promise.reject(new Dexie.DatabaseClosedError());
// FIXTHIS: Check what to do if clear() is true!
return (partial ? saveToUncommittedChanges(remoteChanges, remoteRevision) : finallyCommitAllChanges(remoteChanges, remoteRevision))
.catch(function (error) {
abortTheProvider(error);
return Promise.reject(error);
});
}, dbAliveID);
}
delete context.ongoingOperation;
});
} else {
context.ongoingOperation = context.ongoingOperation.then(function () {
return enqueue(context, fn, instanceID);
});
}
return context.ongoingOperation;
}
if (!instanceID) {
// Caller wants to enqueue it until database becomes open.
if (db.isOpen()) {
return _enqueue();
} else {
return Dexie.Promise.reject(new Dexie.DatabaseClosedError());
}
} else if (db._localSyncNode && instanceID === db._localSyncNode.id) {
// DB is already open but queue doesn't want it to be queued if database has been closed (request bound to current instance of DB)
return _enqueue();
} else {
return Dexie.Promise.reject(new Dexie.DatabaseClosedError());
}
};
}
return connect(protocolInstance, protocolName, url, options, db._localSyncNode.id);
} else {
// We are not master node
// Request master node to do the connect:
return db.table('_syncNodes').where('isMaster').above(0).first(function (masterNode) {
// There will always be a master node. In theory we may self have become master node when we come here. But that's ok. We'll request ourselves.
return db.observable.sendMessage('connect', {
protocolName: protocolName,
url: url,
options: options
}, masterNode.id, {wantReply: true});
});
}
} else if (db.hasBeenClosed()) {
// Database has been closed.
return Promise.reject(new Dexie.DatabaseClosedError());
} else if (db.hasFailed()) {
// Database has failed to open
return Promise.reject(new Dexie.InvalidStateError(
"Dexie.Syncable: Cannot connect. Database has failed to open"));
} else {
// Database not yet open. It may be on its way to open, or open() hasn't yet been called.
// Wait for it to open, then connect.
var promise = new Promise(function (resolve, reject) {
db.on("ready", () => {
// First, check if this is the very first time we connect to given URL.
// Need to know, because if it is, we should stall the promise returned to
// db.on('ready') to not be fulfilled until the initial sync has succeeded.
return db._syncNodes.get({url}, node => {
// Ok, now we know whether we should await the connect promise or not.
// No matter, we should now connect (will maybe create the SyncNode instance
// representing the given URL)
db.observable.sendMessage = function (type, message, destinationNode, options) {
/// Type of message
/// Message to send
/// ID of destination node
/// {wantReply: Boolean, isFailure: Boolean, requestId: Number}. If wantReply, the returned promise will complete with the reply from remote. Otherwise it will complete when message has been successfully sent.
options = options || {};
if (!mySyncNode.node)
return options.wantReply ?
Promise.reject(new Dexie.DatabaseClosedError()) :
Promise.resolve(); // If caller doesn't want a reply, it won't catch errors either.
var msg = {message: message, destinationNode: destinationNode, sender: mySyncNode.node.id, type: type};
Dexie.extend(msg, options); // wantReply: wantReply, success: !isFailure, requestId: ...
return Dexie.ignoreTransaction(()=> {
var tables = ["_intercomm"];
if (options.wantReply) tables.push("_syncNodes"); // If caller wants a reply, include "_syncNodes" in transaction to check that there's a receiver there. Otherwise, new master will get it.
var promise = db.transaction('rw', tables, () => {
if (options.wantReply) {
// Check that there is a receiver there to take the request.
return db._syncNodes.where('id').equals(destinationNode).count(receiverAlive => {
if (receiverAlive)
return db._intercomm.add(msg);
else // If we couldn't find a node -> send to master
return db._syncNodes.where('isMaster').above(0).first(function (masterNode) {
msg.destinationNode = masterNode.id;