Skip to content

Commit

Permalink
Merge 3b50450 into 0986b1b
Browse files Browse the repository at this point in the history
  • Loading branch information
alecgibson committed Jan 25, 2021
2 parents 0986b1b + 3b50450 commit e75d0f7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,17 @@ Backend.prototype.trigger = function(action, agent, request, callback) {
// Submit an operation on the named collection/docname. op should contain a
// {op:}, {create:} or {del:} field. It should probably contain a v: field (if
// it doesn't, it defaults to the current version).
Backend.prototype.submit = function(agent, index, id, op, options, callback) {
Backend.prototype.submit = function(agent, index, id, op, options, originalCallback) {
var backend = this;
var request = new SubmitRequest(this, agent, index, id, op, options);

var callback = function(error, ops) {
backend.emit('after submit', error, request);
originalCallback(error, ops);
};

var err = ot.checkOp(op);
if (err) return callback(err);
var request = new SubmitRequest(this, agent, index, id, op, options);
var backend = this;
backend.trigger(backend.MIDDLEWARE_ACTIONS.submit, agent, request, function(err) {
if (err) return callback(err);
request.submit(function(err) {
Expand Down
40 changes: 40 additions & 0 deletions test/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,45 @@ describe('Backend', function() {
});
});
});

describe('after submit', function() {
it('emits after write', function(done) {
var afterWriteCalled = false;

backend.use(backend.MIDDLEWARE_ACTIONS.afterWrite, function(request, next) {
afterWriteCalled = true;
next();
});

backend.on('after submit', function(error, request) {
expect(error).not.to.be.ok;
expect(request).to.be.ok;
expect(afterWriteCalled).to.be.true;
done();
});

var op = {op: {p: ['publicationYear'], oi: 1949}};
backend.submit(null, 'books', '1984', op, null, function(error) {
if (error) done(error);
});
});

it('emits after an error is raised in the middleware', function(done) {
backend.use(backend.MIDDLEWARE_ACTIONS.submit, function(request, next) {
next(new Error());
});

backend.on('after submit', function(error, request) {
expect(error).to.be.ok;
expect(request).to.be.ok;
done();
});

var op = {op: {p: ['publicationYear'], oi: 1949}};
backend.submit(null, 'books', '1984', op, null, function() {
// Swallow the error
});
});
});
});
});

0 comments on commit e75d0f7

Please sign in to comment.