Comparing version 1.6.0 to 1.7.0
@@ -207,7 +207,13 @@ var async = require('async'); | ||
// 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('submitRequestEnd', 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) { | ||
@@ -214,0 +220,0 @@ if (err) return callback(err); |
{ | ||
"name": "sharedb", | ||
"version": "1.6.0", | ||
"version": "1.7.0", | ||
"description": "JSON OT database backend", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -631,2 +631,12 @@ _This README is for `sharedb@1.x`. For `sharedb@1.x-beta`, see [the 1.x-beta branch](https://github.com/share/sharedb/tree/1.x-beta). To upgrade, see [the upgrade guide](https://github.com/share/sharedb/wiki/Upgrading-to-sharedb@1.0.0-from-1.0.0-beta)._ | ||
#### `on` | ||
```javascript | ||
backend.on('submitRequestEnd', callback): void; | ||
``` | ||
A submit request is about to return to the client. This is called whether the request was a success or a failure. | ||
* `callback` _Function_: callback for handling the event: `function (error, request): void;` | ||
### Class: `ShareDB.Agent` | ||
@@ -633,0 +643,0 @@ |
@@ -103,3 +103,43 @@ var Backend = require('../lib/backend'); | ||
}); | ||
describe('submitRequestEnd', function() { | ||
it('emits after write', function(done) { | ||
var afterWriteCalled = false; | ||
backend.use(backend.MIDDLEWARE_ACTIONS.afterWrite, function(request, next) { | ||
afterWriteCalled = true; | ||
next(); | ||
}); | ||
backend.on('submitRequestEnd', 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('submitRequestEnd', 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 | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
2595333
15812
892