Skip to content

Commit

Permalink
Merge pull request #314 from share/after-write
Browse files Browse the repository at this point in the history
Rename `afterSubmit` middleware hook to `afterWrite`
  • Loading branch information
alecgibson committed Oct 21, 2019
2 parents f01cf0e + 52e94ba commit b9a72eb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ Register a new middleware.
before being committed to the database
* `'commit'`: An operation was applied to a snapshot; The operation
and new snapshot are about to be written to the database.
* `'afterSubmit'`: An operation was successfully submitted to
* `'afterWrite'`: An operation was successfully written to
the database.
* `'receive'`: Received a message from a client
* `'reply'`: About to send a non-error reply to a client message
Expand Down Expand Up @@ -321,7 +321,7 @@ Populate the fields on `doc` with a snapshot of the document from the server.
Populate the fields on `doc` with a snapshot of the document from the server, and
fire events on subsequent changes.

`doc.unsubscribe(function (err) {...})`
`doc.unsubscribe(function (err) {...})`
Stop listening for document updates. The document data at the time of unsubscribing remains in memory, but no longer stays up-to-date. Resubscribe with `doc.subscribe`.

`doc.ingestSnapshot(snapshot, callback)`
Expand Down
23 changes: 14 additions & 9 deletions lib/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ var warnDeprecatedAfterSubmit = true;
var DOC_ACTION_DEPRECATION_WARNING = 'DEPRECATED: "doc" middleware action. Use "readSnapshots" instead. ' +
'Pass `disableDocAction: true` option to ShareDB to disable the "doc" action and this warning.';

var AFFTER_SUBMIT_ACTION_DEPRECATION_WARNING = 'DEPRECATED: "after submit" middleware action. ' +
'Use "afterSubmit" instead. Pass `disableSpaceDelimitedActions: true` option to ShareDB to ' +
'disable the "after submit" action and this warning.';
var AFTER_SUBMIT_ACTION_DEPRECATION_WARNING = 'DEPRECATED: "after submit" and "afterSubmit" middleware actions. ' +
'Use "afterWrite" instead. Pass `disableSpaceDelimitedActions: true` option to ShareDB to ' +
'disable the "after submit" and "afterSubmit" actions and this warning.';

function Backend(options) {
if (!(this instanceof Backend)) return new Backend(options);
Expand Down Expand Up @@ -59,10 +59,12 @@ module.exports = Backend;
emitter.mixin(Backend);

Backend.prototype.MIDDLEWARE_ACTIONS = {
// An operation was successfully submitted to the database.
// DEPRECATED: Synonym for 'afterWrite'
afterSubmit: 'afterSubmit',
// DEPRECATED: Synonym for 'afterSubmit'
// DEPRECATED: Synonym for 'afterWrite'
'after submit': 'after submit',
// An operation was successfully written to the database.
afterWrite: 'afterWrite',
// An operation is about to be applied to a snapshot before being committed to the database
apply: 'apply',
// An operation was applied to a snapshot; The operation and new snapshot are about to be written to the database.
Expand Down Expand Up @@ -114,17 +116,20 @@ Backend.prototype._shimDocAction = function() {
};

// Shim for backwards compatibility with deprecated middleware action name.
// The action 'after submit' is now 'afterSubmit'.
// The actions 'after submit' and 'afterSubmit' are now 'afterWrite'.
Backend.prototype._shimAfterSubmit = function() {
if (warnDeprecatedAfterSubmit) {
warnDeprecatedAfterSubmit = false;
console.warn(AFFTER_SUBMIT_ACTION_DEPRECATION_WARNING);
console.warn(AFTER_SUBMIT_ACTION_DEPRECATION_WARNING);
}

var backend = this;
this.use(backend.MIDDLEWARE_ACTIONS.afterSubmit, function(request, callback) {
this.use(backend.MIDDLEWARE_ACTIONS.afterWrite, function(request, callback) {
backend.trigger(backend.MIDDLEWARE_ACTIONS['after submit'], request.agent, request, callback);
});
this.use(backend.MIDDLEWARE_ACTIONS.afterWrite, function(request, callback) {
backend.trigger(backend.MIDDLEWARE_ACTIONS['afterSubmit'], request.agent, request, callback);
});
};

Backend.prototype.close = function(callback) {
Expand Down Expand Up @@ -251,7 +256,7 @@ Backend.prototype.submit = function(agent, index, id, op, options, callback) {
if (err) return callback(err);
request.submit(function(err) {
if (err) return callback(err);
backend.trigger(backend.MIDDLEWARE_ACTIONS.afterSubmit, agent, request, function(err) {
backend.trigger(backend.MIDDLEWARE_ACTIONS.afterWrite, agent, request, function(err) {
if (err) return callback(err);
backend._sanitizeOps(agent, request.projection, request.collection, id, request.ops, function(err) {
if (err) return callback(err);
Expand Down
4 changes: 2 additions & 2 deletions test/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,8 @@ describe('middleware', function() {
});

describe('submit lifecycle', function() {
// DEPRECATED: 'after submit' is a synonym for 'afterSubmit'
['submit', 'apply', 'commit', 'afterSubmit', 'after submit'].forEach(function(action) {
// DEPRECATED: 'after submit' and 'afterSubmit' are synonyms for 'afterWrite'
['submit', 'apply', 'commit', 'afterWrite', 'afterSubmit', 'after submit'].forEach(function(action) {
it(action + ' gets options passed to backend.submit', function(done) {
var doneAfter = util.callAfter(1, done);
this.backend.use(action, function(request, next) {
Expand Down

0 comments on commit b9a72eb

Please sign in to comment.