@@ -32,10 +32,63 @@ function Backend(options) {
3232 // The number of open agents for monitoring and testing memory leaks
3333 this . agentsCount = 0 ;
3434 this . remoteAgentsCount = 0 ;
35+
36+ // The below shims are for backwards compatibility. These options will be
37+ // removed in a future major version
38+ if ( ! options . disableDocAction ) {
39+ this . _shimDocAction ( ) ;
40+ }
41+ if ( ! options . disableSpaceDelimitedActions ) {
42+ this . _shimAfterSubmit ( ) ;
43+ }
3544}
3645module . exports = Backend ;
3746emitter . mixin ( Backend ) ;
3847
48+ Backend . prototype . MIDDLEWARE_ACTIONS = {
49+ // An operation was successfully submitted to the database.
50+ afterSubmit : 'afterSubmit' ,
51+ // DEPRECATED: Synonym for 'afterSubmit'
52+ 'after submit' : 'after submit' ,
53+ // An operation is about to be applied to a snapshot before being committed to the database
54+ apply : 'apply' ,
55+ // An operation was applied to a snapshot; The operation and new snapshot are about to be written to the database.
56+ commit : 'commit' ,
57+ // A new client connected to the server.
58+ connect : 'connect' ,
59+ // DEPRECATED: A snapshot was loaded from the database.
60+ doc : 'doc' ,
61+ // An operation was loaded from the database
62+ op : 'op' ,
63+ // A query is about to be sent to the database
64+ query : 'query' ,
65+ // Received a message from a client
66+ receive : 'receive' ,
67+ // Snapshot(s) were received from the database and are about to be returned to a client
68+ readSnapshots : 'readSnapshots' ,
69+ // An operation is about to be submitted to the database
70+ submit : 'submit'
71+ } ;
72+
73+ Backend . prototype . _shimDocAction = function ( ) {
74+ var backend = this ;
75+ this . use ( this . MIDDLEWARE_ACTIONS . readSnapshots , function ( request , callback ) {
76+ async . each ( request . snapshots , function ( snapshot , eachCb ) {
77+ var docRequest = { collection : request . collection , id : snapshot . id , snapshot : snapshot } ;
78+ backend . trigger ( backend . MIDDLEWARE_ACTIONS . doc , request . agent , docRequest , eachCb ) ;
79+ } , callback ) ;
80+ } ) ;
81+ } ;
82+
83+ // Shim for backwards compatibility with deprecated middleware action name.
84+ // The action 'after submit' is now 'afterSubmit'.
85+ Backend . prototype . _shimAfterSubmit = function ( ) {
86+ var backend = this ;
87+ this . use ( backend . MIDDLEWARE_ACTIONS . afterSubmit , function ( request , callback ) {
88+ backend . trigger ( backend . MIDDLEWARE_ACTIONS [ 'after submit' ] , request . agent , request , callback ) ;
89+ } ) ;
90+ } ;
91+
3992Backend . prototype . close = function ( callback ) {
4093 var wait = 3 ;
4194 var backend = this ;
@@ -82,7 +135,7 @@ Backend.prototype.connect = function(connection, req) {
82135 */
83136Backend . prototype . listen = function ( stream , req ) {
84137 var agent = new Agent ( this , stream ) ;
85- this . trigger ( ' connect' , agent , { stream : stream , req : req } , function ( err ) {
138+ this . trigger ( this . MIDDLEWARE_ACTIONS . connect , agent , { stream : stream , req : req } , function ( err ) {
86139 if ( err ) return agent . close ( err ) ;
87140 agent . _open ( ) ;
88141 } ) ;
@@ -155,11 +208,11 @@ Backend.prototype.submit = function(agent, index, id, op, options, callback) {
155208 if ( err ) return callback ( err ) ;
156209 var request = new SubmitRequest ( this , agent , index , id , op , options ) ;
157210 var backend = this ;
158- backend . trigger ( ' submit' , agent , request , function ( err ) {
211+ backend . trigger ( backend . MIDDLEWARE_ACTIONS . submit , agent , request , function ( err ) {
159212 if ( err ) return callback ( err ) ;
160213 request . submit ( function ( err ) {
161214 if ( err ) return callback ( err ) ;
162- backend . trigger ( 'after submit' , agent , request , function ( err ) {
215+ backend . trigger ( backend . MIDDLEWARE_ACTIONS . afterSubmit , agent , request , function ( err ) {
163216 if ( err ) return callback ( err ) ;
164217 backend . _sanitizeOps ( agent , request . projection , request . collection , id , request . ops , function ( err ) {
165218 if ( err ) return callback ( err ) ;
@@ -179,7 +232,7 @@ Backend.prototype._sanitizeOp = function(agent, projection, collection, id, op,
179232 return callback ( err ) ;
180233 }
181234 }
182- this . trigger ( 'op' , agent , { collection : collection , id : id , op : op } , callback ) ;
235+ this . trigger ( this . MIDDLEWARE_ACTIONS . op , agent , { collection : collection , id : id , op : op } , callback ) ;
183236} ;
184237Backend . prototype . _sanitizeOps = function ( agent , projection , collection , id , ops , callback ) {
185238 var backend = this ;
@@ -194,33 +247,31 @@ Backend.prototype._sanitizeOpsBulk = function(agent, projection, collection, ops
194247 } , callback ) ;
195248} ;
196249
197- Backend . prototype . _sanitizeSnapshot = function ( agent , projection , collection , id , snapshot , callback ) {
250+ Backend . prototype . _sanitizeSnapshots = function ( agent , projection , collection , snapshots , callback ) {
198251 if ( projection ) {
199252 try {
200- projections . projectSnapshot ( projection . fields , snapshot ) ;
253+ projections . projectSnapshots ( projection . fields , snapshots ) ;
201254 } catch ( err ) {
202255 return callback ( err ) ;
203256 }
204257 }
205- this . trigger ( 'doc' , agent , { collection : collection , id : id , snapshot : snapshot } , callback ) ;
206- } ;
207- Backend . prototype . _sanitizeSnapshots = function ( agent , projection , collection , snapshots , callback ) {
208- var backend = this ;
209- async . each ( snapshots , function ( snapshot , eachCb ) {
210- backend . _sanitizeSnapshot ( agent , projection , collection , snapshot . id , snapshot , eachCb ) ;
211- } , callback ) ;
212- } ;
213- Backend . prototype . _sanitizeSnapshotBulk = function ( agent , projection , collection , snapshotMap , callback ) {
214- var backend = this ;
215- async . forEachOf ( snapshotMap , function ( snapshot , id , eachCb ) {
216- backend . _sanitizeSnapshot ( agent , projection , collection , id , snapshot , eachCb ) ;
217- } , callback ) ;
258+ var request = { collection : collection , snapshots : snapshots } ;
259+ this . trigger ( this . MIDDLEWARE_ACTIONS . readSnapshots , agent , request , callback ) ;
218260} ;
219261
220262Backend . prototype . _getSnapshotProjection = function ( db , projection ) {
221263 return ( db . projectsSnapshots ) ? null : projection ;
222264} ;
223265
266+ Backend . prototype . _getSnapshotsFromMap = function ( ids , snapshotMap ) {
267+ var snapshots = new Array ( ids . length ) ;
268+ for ( var i = 0 ; i < ids . length ; i ++ ) {
269+ var id = ids [ i ] ;
270+ snapshots [ i ] = snapshotMap [ id ] ;
271+ }
272+ return snapshots ;
273+ } ;
274+
224275// Non inclusive - gets ops from [from, to). Ie, all relevant ops. If to is
225276// not defined (null or undefined) then it returns all ops.
226277Backend . prototype . getOps = function ( agent , index , id , from , to , callback ) {
@@ -283,7 +334,8 @@ Backend.prototype.fetch = function(agent, index, id, callback) {
283334 backend . db . getSnapshot ( collection , id , fields , null , function ( err , snapshot ) {
284335 if ( err ) return callback ( err ) ;
285336 var snapshotProjection = backend . _getSnapshotProjection ( backend . db , projection ) ;
286- backend . _sanitizeSnapshot ( agent , snapshotProjection , collection , id , snapshot , function ( err ) {
337+ var snapshots = [ snapshot ] ;
338+ backend . _sanitizeSnapshots ( agent , snapshotProjection , collection , snapshots , function ( err ) {
287339 if ( err ) return callback ( err ) ;
288340 backend . emit ( 'timing' , 'fetch' , Date . now ( ) - start , request ) ;
289341 callback ( null , snapshot ) ;
@@ -306,7 +358,8 @@ Backend.prototype.fetchBulk = function(agent, index, ids, callback) {
306358 backend . db . getSnapshotBulk ( collection , ids , fields , null , function ( err , snapshotMap ) {
307359 if ( err ) return callback ( err ) ;
308360 var snapshotProjection = backend . _getSnapshotProjection ( backend . db , projection ) ;
309- backend . _sanitizeSnapshotBulk ( agent , snapshotProjection , collection , snapshotMap , function ( err ) {
361+ var snapshots = backend . _getSnapshotsFromMap ( ids , snapshotMap ) ;
362+ backend . _sanitizeSnapshots ( agent , snapshotProjection , collection , snapshots , function ( err ) {
310363 if ( err ) return callback ( err ) ;
311364 backend . emit ( 'timing' , 'fetchBulk' , Date . now ( ) - start , request ) ;
312365 callback ( null , snapshotMap ) ;
@@ -479,7 +532,7 @@ Backend.prototype._triggerQuery = function(agent, index, query, options, callbac
479532 snapshotProjection : null ,
480533 } ;
481534 var backend = this ;
482- backend . trigger ( ' query' , agent , request , function ( err ) {
535+ backend . trigger ( backend . MIDDLEWARE_ACTIONS . query , agent , request , function ( err ) {
483536 if ( err ) return callback ( err ) ;
484537 // Set the DB reference for the request after the middleware trigger so
485538 // that the db option can be changed in middleware
0 commit comments