Skip to content

Commit

Permalink
Merge pull request #434 from share/next-tick
Browse files Browse the repository at this point in the history
Ponyfill `nextTick`
  • Loading branch information
alecgibson committed Feb 17, 2021
2 parents 10b8372 + 4826757 commit fa9179d
Show file tree
Hide file tree
Showing 18 changed files with 120 additions and 44 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const SHAREDB_RULES = {
'no-unused-vars': ['error', {vars: 'all', args: 'after-used'}],
// It's more readable to ensure we only have one statement per line
'max-statements-per-line': ['error', {max: 1}],
// ES3 doesn't support spread
'prefer-spread': 'off',
// as-needed quote props are easier to write
'quote-props': ['error', 'as-needed'],
'require-jsdoc': 'off',
Expand Down
10 changes: 5 additions & 5 deletions lib/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ Agent.prototype._onOp = function(collection, id, op) {
// precaution against op middleware breaking query subscriptions, we delay
// before calling into projection and middleware code
var agent = this;
process.nextTick(function() {
util.nextTick(function() {
var copy = shallowCopy(op);
agent.backend.sanitizeOp(agent, collection, id, copy, function(err) {
if (err) {
Expand Down Expand Up @@ -571,7 +571,7 @@ Agent.prototype._queryUnsubscribe = function(queryId, callback) {
emitter.destroy();
delete this.subscribedQueries[queryId];
}
process.nextTick(callback);
util.nextTick(callback);
};

Agent.prototype._fetch = function(collection, id, version, callback) {
Expand Down Expand Up @@ -679,18 +679,18 @@ Agent.prototype._unsubscribe = function(collection, id, callback) {
var docs = this.subscribedDocs[collection];
var stream = docs && docs[id];
if (stream) stream.destroy();
process.nextTick(callback);
util.nextTick(callback);
};

Agent.prototype._unsubscribeBulk = function(collection, ids, callback) {
var docs = this.subscribedDocs[collection];
if (!docs) return process.nextTick(callback);
if (!docs) return util.nextTick(callback);
for (var i = 0; i < ids.length; i++) {
var id = ids[i];
var stream = docs[id];
if (stream) stream.destroy();
}
process.nextTick(callback);
util.nextTick(callback);
};

Agent.prototype._submit = function(collection, id, op, callback) {
Expand Down
3 changes: 2 additions & 1 deletion lib/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var Snapshot = require('./snapshot');
var StreamSocket = require('./stream-socket');
var SubmitRequest = require('./submit-request');
var ReadSnapshotsRequest = require('./read-snapshots-request');
var util = require('./util');

var ERROR_CODE = ShareDBError.CODES;

Expand Down Expand Up @@ -251,7 +252,7 @@ Backend.prototype._sanitizeOps = function(agent, projection, collection, id, ops
var backend = this;
async.each(ops, function(op, eachCb) {
backend._sanitizeOp(agent, projection, collection, id, op, function(err) {
process.nextTick(eachCb, err);
util.nextTick(eachCb, err);
});
}, callback);
};
Expand Down
6 changes: 3 additions & 3 deletions lib/client/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ Connection.prototype.bindToSocket = function(socket) {
try {
connection.handleMessage(request.data);
} catch (err) {
process.nextTick(function() {
util.nextTick(function() {
connection.emit('error', err);
});
}
Expand Down Expand Up @@ -605,12 +605,12 @@ Connection.prototype.whenNothingPending = function(callback) {
return;
}
// Call back when no pending operations
process.nextTick(callback);
util.nextTick(callback);
};
Connection.prototype._nothingPendingRetry = function(callback) {
var connection = this;
return function() {
process.nextTick(function() {
util.nextTick(function() {
connection.whenNothingPending(callback);
});
};
Expand Down
6 changes: 3 additions & 3 deletions lib/client/doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ Doc.prototype.ingestSnapshot = function(snapshot, callback) {

Doc.prototype.whenNothingPending = function(callback) {
var doc = this;
process.nextTick(function() {
util.nextTick(function() {
if (doc.hasPending()) {
doc.once('nothing pending', callback);
return;
Expand Down Expand Up @@ -470,7 +470,7 @@ Doc.prototype._flushSubscribe = function() {
if (!this.pendingSubscribe[0].wantSubscribe) {
this.inflightSubscribe = this.pendingSubscribe.shift();
var doc = this;
process.nextTick(function() {
util.nextTick(function() {
doc._handleSubscribe();
});
}
Expand Down Expand Up @@ -753,7 +753,7 @@ Doc.prototype._submit = function(op, source, callback) {
// The call to flush is delayed so if submit() is called multiple times
// synchronously, all the ops are combined before being sent to the server.
var doc = this;
process.nextTick(function() {
util.nextTick(function() {
doc.flush();
});
};
Expand Down
3 changes: 2 additions & 1 deletion lib/client/presence/local-presence.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var emitter = require('../../emitter');
var util = require('../../util');

module.exports = LocalPresence;
function LocalPresence(presence, presenceId) {
Expand Down Expand Up @@ -73,6 +74,6 @@ LocalPresence.prototype._getCallback = function(presenceVersion) {
};

LocalPresence.prototype._callbackOrEmit = function(error, callback) {
if (callback) return process.nextTick(callback, error);
if (callback) return util.nextTick(callback, error);
if (error) this.emit('error', error);
};
2 changes: 1 addition & 1 deletion lib/client/presence/presence.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ Presence.prototype._subscriptionCallback = function(seq) {
};

Presence.prototype._callbackOrEmit = function(error, callback) {
if (callback) return process.nextTick(callback, error);
if (callback) return util.nextTick(callback, error);
if (error) this.emit('error', error);
};

Expand Down
4 changes: 3 additions & 1 deletion lib/client/presence/remote-presence.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var util = require('../../util');

module.exports = RemotePresence;
function RemotePresence(presence, presenceId) {
this.presence = presence;
Expand All @@ -18,5 +20,5 @@ RemotePresence.prototype.receiveUpdate = function(message) {
RemotePresence.prototype.destroy = function(callback) {
delete this.presence._remotePresenceInstances[this.presenceId];
delete this.presence.remotePresences[this.presenceId];
if (callback) process.nextTick(callback);
if (callback) util.nextTick(callback);
};
3 changes: 2 additions & 1 deletion lib/client/query.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var emitter = require('../emitter');
var util = require('../util');

// Queries are live requests to the database for particular sets of fields.
//
Expand Down Expand Up @@ -80,7 +81,7 @@ Query.prototype.destroy = function(callback) {
this.connection._destroyQuery(this);
// There is a callback for consistency, but we don't actually wait for the
// server's unsubscribe message currently
if (callback) process.nextTick(callback);
if (callback) util.nextTick(callback);
};

Query.prototype._onConnectionStateChanged = function() {
Expand Down
9 changes: 5 additions & 4 deletions lib/db/memory.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var DB = require('./index');
var Snapshot = require('../snapshot');
var util = require('../util');

// In-memory ShareDB database
//
Expand Down Expand Up @@ -38,7 +39,7 @@ MemoryDB.prototype.close = function(callback) {
MemoryDB.prototype.commit = function(collection, id, op, snapshot, options, callback) {
var db = this;
if (typeof callback !== 'function') throw new Error('Callback required');
process.nextTick(function() {
util.nextTick(function() {
var version = db._getVersionSync(collection, id);
if (snapshot.v !== version + 1) {
var succeeded = false;
Expand All @@ -61,7 +62,7 @@ MemoryDB.prototype.getSnapshot = function(collection, id, fields, options, callb
var includeMetadata = (fields && fields.$submit) || (options && options.metadata);
var db = this;
if (typeof callback !== 'function') throw new Error('Callback required');
process.nextTick(function() {
util.nextTick(function() {
var snapshot = db._getSnapshotSync(collection, id, includeMetadata);
callback(null, snapshot);
});
Expand All @@ -80,7 +81,7 @@ MemoryDB.prototype.getOps = function(collection, id, from, to, options, callback
var includeMetadata = options && options.metadata;
var db = this;
if (typeof callback !== 'function') throw new Error('Callback required');
process.nextTick(function() {
util.nextTick(function() {
var opLog = db._getOpLogSync(collection, id);
if (to == null) {
to = opLog.length;
Expand All @@ -101,7 +102,7 @@ MemoryDB.prototype.query = function(collection, query, fields, options, callback
var includeMetadata = options && options.metadata;
var db = this;
if (typeof callback !== 'function') throw new Error('Callback required');
process.nextTick(function() {
util.nextTick(function() {
var collectionDocs = db.docs[collection];
var snapshots = [];
for (var id in collectionDocs || {}) {
Expand Down
4 changes: 2 additions & 2 deletions lib/milestone-db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function MilestoneDB(options) {
emitter.mixin(MilestoneDB);

MilestoneDB.prototype.close = function(callback) {
if (callback) process.nextTick(callback);
if (callback) util.nextTick(callback);
};

/**
Expand Down Expand Up @@ -73,6 +73,6 @@ MilestoneDB.prototype._isValidTimestamp = function(timestamp) {
};

MilestoneDB.prototype._callBackOrEmitError = function(error, callback) {
if (callback) return process.nextTick(callback, error);
if (callback) return util.nextTick(callback, error);
this.emit('error', error);
};
19 changes: 10 additions & 9 deletions lib/milestone-db/memory.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var MilestoneDB = require('./index');
var ShareDBError = require('../error');
var util = require('../util');

var ERROR_CODE = ShareDBError.CODES;

Expand All @@ -26,7 +27,7 @@ MemoryMilestoneDB.prototype = Object.create(MilestoneDB.prototype);

MemoryMilestoneDB.prototype.getMilestoneSnapshot = function(collection, id, version, callback) {
if (!this._isValidVersion(version)) {
return process.nextTick(callback, new ShareDBError(ERROR_CODE.ERR_MILESTONE_ARGUMENT_INVALID, 'Invalid version'));
return util.nextTick(callback, new ShareDBError(ERROR_CODE.ERR_MILESTONE_ARGUMENT_INVALID, 'Invalid version'));
}

var predicate = versionLessThanOrEqualTo(version);
Expand All @@ -48,12 +49,12 @@ MemoryMilestoneDB.prototype.saveMilestoneSnapshot = function(collection, snapsho
return a.v - b.v;
});

process.nextTick(callback, null);
util.nextTick(callback, null);
};

MemoryMilestoneDB.prototype.getMilestoneSnapshotAtOrBeforeTime = function(collection, id, timestamp, callback) {
if (!this._isValidTimestamp(timestamp)) {
return process.nextTick(callback, new ShareDBError(ERROR_CODE.ERR_MILESTONE_ARGUMENT_INVALID, 'Invalid timestamp'));
return util.nextTick(callback, new ShareDBError(ERROR_CODE.ERR_MILESTONE_ARGUMENT_INVALID, 'Invalid timestamp'));
}

var filter = timestampLessThanOrEqualTo(timestamp);
Expand All @@ -62,29 +63,29 @@ MemoryMilestoneDB.prototype.getMilestoneSnapshotAtOrBeforeTime = function(collec

MemoryMilestoneDB.prototype.getMilestoneSnapshotAtOrAfterTime = function(collection, id, timestamp, callback) {
if (!this._isValidTimestamp(timestamp)) {
return process.nextTick(callback, new ShareDBError(ERROR_CODE.ERR_MILESTONE_ARGUMENT_INVALID, 'Invalid timestamp'));
return util.nextTick(callback, new ShareDBError(ERROR_CODE.ERR_MILESTONE_ARGUMENT_INVALID, 'Invalid timestamp'));
}

var filter = timestampGreaterThanOrEqualTo(timestamp);
this._findMilestoneSnapshot(collection, id, filter, function(error, snapshot) {
if (error) return process.nextTick(callback, error);
if (error) return util.nextTick(callback, error);

var mtime = snapshot && snapshot.m && snapshot.m.mtime;
if (timestamp !== null && mtime < timestamp) {
snapshot = undefined;
}

process.nextTick(callback, null, snapshot);
util.nextTick(callback, null, snapshot);
});
};

MemoryMilestoneDB.prototype._findMilestoneSnapshot = function(collection, id, breakCondition, callback) {
if (!collection) {
return process.nextTick(
return util.nextTick(
callback, new ShareDBError(ERROR_CODE.ERR_MILESTONE_ARGUMENT_INVALID, 'Missing collection')
);
}
if (!id) return process.nextTick(callback, new ShareDBError(ERROR_CODE.ERR_MILESTONE_ARGUMENT_INVALID, 'Missing ID'));
if (!id) return util.nextTick(callback, new ShareDBError(ERROR_CODE.ERR_MILESTONE_ARGUMENT_INVALID, 'Missing ID'));

var milestoneSnapshots = this._getMilestoneSnapshotsSync(collection, id);

Expand All @@ -98,7 +99,7 @@ MemoryMilestoneDB.prototype._findMilestoneSnapshot = function(collection, id, br
}
}

process.nextTick(callback, null, milestoneSnapshot);
util.nextTick(callback, null, milestoneSnapshot);
};

MemoryMilestoneDB.prototype._getMilestoneSnapshotsSync = function(collection, id) {
Expand Down
9 changes: 5 additions & 4 deletions lib/milestone-db/no-op.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var MilestoneDB = require('./index');
var util = require('../util');

/**
* A no-op implementation of the MilestoneDB class.
Expand All @@ -15,20 +16,20 @@ NoOpMilestoneDB.prototype = Object.create(MilestoneDB.prototype);

NoOpMilestoneDB.prototype.getMilestoneSnapshot = function(collection, id, version, callback) {
var snapshot = undefined;
process.nextTick(callback, null, snapshot);
util.nextTick(callback, null, snapshot);
};

NoOpMilestoneDB.prototype.saveMilestoneSnapshot = function(collection, snapshot, callback) {
if (callback) return process.nextTick(callback, null);
if (callback) return util.nextTick(callback, null);
this.emit('save', collection, snapshot);
};

NoOpMilestoneDB.prototype.getMilestoneSnapshotAtOrBeforeTime = function(collection, id, timestamp, callback) {
var snapshot = undefined;
process.nextTick(callback, null, snapshot);
util.nextTick(callback, null, snapshot);
};

NoOpMilestoneDB.prototype.getMilestoneSnapshotAtOrAfterTime = function(collection, id, timestamp, callback) {
var snapshot = undefined;
process.nextTick(callback, null, snapshot);
util.nextTick(callback, null, snapshot);
};
10 changes: 5 additions & 5 deletions lib/pubsub/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ PubSub.prototype.close = function(callback) {
map[id].destroy();
}
}
if (callback) process.nextTick(callback);
if (callback) util.nextTick(callback);
};

PubSub.prototype._subscribe = function(channel, callback) {
process.nextTick(function() {
util.nextTick(function() {
callback(new ShareDBError(
ERROR_CODE.ERR_DATABASE_METHOD_NOT_IMPLEMENTED,
'_subscribe PubSub method unimplemented'
Expand All @@ -48,7 +48,7 @@ PubSub.prototype._subscribe = function(channel, callback) {
};

PubSub.prototype._unsubscribe = function(channel, callback) {
process.nextTick(function() {
util.nextTick(function() {
callback(new ShareDBError(
ERROR_CODE.ERR_DATABASE_METHOD_NOT_IMPLEMENTED,
'_unsubscribe PubSub method unimplemented'
Expand All @@ -57,7 +57,7 @@ PubSub.prototype._unsubscribe = function(channel, callback) {
};

PubSub.prototype._publish = function(channels, data, callback) {
process.nextTick(function() {
util.nextTick(function() {
callback(new ShareDBError(ERROR_CODE.ERR_DATABASE_METHOD_NOT_IMPLEMENTED, '_publish PubSub method unimplemented'));
});
};
Expand All @@ -70,7 +70,7 @@ PubSub.prototype.subscribe = function(channel, callback) {

var pubsub = this;
if (this.subscribed[channel]) {
process.nextTick(function() {
util.nextTick(function() {
var stream = pubsub._createStream(channel);
callback(null, stream);
});
Expand Down
Loading

0 comments on commit fa9179d

Please sign in to comment.