Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
} else {
request.resolve(msg.message.result);
}
delete requestsWaitingForReply[msg.requestId.toString()];
}
} else {
// This is a message or request. Fire the event and add an API for the subscriber to use if reply is requested
msg.resolve = function(result) {
db.sendMessage('response', { result: result }, msg.sender, { requestId: msg.id });
};
msg.reject = function(error) {
db.sendMessage('response', { error: error.toString() }, msg.sender, { isFailure: true, requestId: msg.id });
};
var message = msg.message;
delete msg.message;
Dexie.extend(msg, message);
db.on.message.fire(msg);
}
}
} else {
request.resolve(msg.message.result);
}
delete requestsWaitingForReply[msg.requestId.toString()];
}
} else {
// This is a message or request. Fire the event and add an API for the subscriber to use if reply is requested
msg.resolve = function(result) {
db.sendMessage('response', { result: result }, msg.sender, { requestId: msg.id });
};
msg.reject = function(error) {
db.sendMessage('response', { error: error.toString() }, msg.sender, { isFailure: true, requestId: msg.id });
};
var message = msg.message;
delete msg.message;
Dexie.extend(msg, message);
db.on.message.fire(msg);
}
}
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;
return db._intercomm.add(msg)
});
});
} else {
db.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.
if (!mySyncNode) return Promise.reject("Database closed");
options = options || {};
var msg = { message: message, destinationNode: destinationNode, sender: mySyncNode.id, type: type };
Dexie.extend(msg, options); // wantReply: wantReply, success: !isFailure, requestId: ...
var tables = ["_intercomm"];
if (options.wantReply) tables.push("_syncNodes"); // If caller wants a reply, include "_syncNodes" in transaction to check that there's a reciever there. Otherwise, new master will get it.
return db.transaction('rw?', tables, function() {
if (options.wantReply) {
// Check that there is a reciever there to take the request.
return db._syncNodes.where('id').equals(destinationNode).count(function(recieverAlive) {
if (recieverAlive)
return addMessage(msg);
else
return db._syncNodes.where('isMaster').above(0).first(function(masterNode) {
msg.destinationNode = masterNode.id;
return addMessage(msg);
});
});
} else {
addMessage(msg); // No need to return Promise. Caller dont need a reply.
} else {
request.resolve(msg.message.result);
}
delete requestsWaitingForReply[msg.requestId.toString()];
}
} else {
// This is a message or request. Fire the event and add an API for the subscriber to use if reply is requested
msg.resolve = function (result) {
db.sendMessage('response', { result: result }, msg.sender, { requestId: msg.id });
};
msg.reject = function (error) {
db.sendMessage('response', { error: error.toString() }, msg.sender, { isFailure: true, requestId: msg.id });
};
var message = msg.message;
delete msg.message;
Dexie.extend(msg, message);
db.on.message.fire(msg);
}
}
db.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.
if (!mySyncNode) return Promise.reject("Database closed");
options = options || {};
var msg = { message: message, destinationNode: destinationNode, sender: mySyncNode.id, type: type };
Dexie.extend(msg, options); // wantReply: wantReply, success: !isFailure, requestId: ...
var tables = ["_intercomm"];
if (options.wantReply) tables.push("_syncNodes"); // If caller wants a reply, include "_syncNodes" in transaction to check that there's a reciever there. Otherwise, new master will get it.
return db.transaction('rw?', tables, function () {
if (options.wantReply) {
// Check that there is a reciever there to take the request.
return db._syncNodes.where('id').equals(destinationNode).count(function (recieverAlive) {
if (recieverAlive) return addMessage(msg);else return db._syncNodes.where('isMaster').above(0).first(function (masterNode) {
msg.destinationNode = masterNode.id;
return addMessage(msg);
});
});
} else {
addMessage(msg); // No need to return Promise. Caller dont need a reply.
}
function addMessage(msg) {
db.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.
if (!mySyncNode) return Promise.reject("Database closed");
options = options || {};
var msg = { message: message, destinationNode: destinationNode, sender: mySyncNode.id, type: type };
Dexie.extend(msg, options); // wantReply: wantReply, success: !isFailure, requestId: ...
var tables = ["_intercomm"];
if (options.wantReply) tables.push("_syncNodes"); // If caller wants a reply, include "_syncNodes" in transaction to check that there's a reciever there. Otherwise, new master will get it.
return db.transaction('rw?', tables, function() {
if (options.wantReply) {
// Check that there is a reciever there to take the request.
return db._syncNodes.where('id').equals(destinationNode).count(function(recieverAlive) {
if (recieverAlive)
return addMessage(msg);
else
return db._syncNodes.where('isMaster').above(0).first(function(masterNode) {
msg.destinationNode = masterNode.id;
return addMessage(msg);
});
});
} else {
addMessage(msg); // No need to return Promise. Caller dont need a reply.
function PersistedContext(nodeID, otherProps) {
this.nodeID = nodeID;
if (otherProps) Dexie.extend(this, otherProps);
}