1
1
var MilestoneDB = require ( './index' ) ;
2
+ var ShareDBError = require ( '../error' ) ;
2
3
3
4
/**
4
5
* In-memory ShareDB milestone database
@@ -22,6 +23,11 @@ function MemoryMilestoneDB(options) {
22
23
MemoryMilestoneDB . prototype = Object . create ( MilestoneDB . prototype ) ;
23
24
24
25
MemoryMilestoneDB . prototype . getMilestoneSnapshot = function ( collection , id , version , callback ) {
26
+ if ( ! callback ) callback = function ( ) { } ;
27
+ if ( ! collection ) return process . nextTick ( callback , new ShareDBError ( 4001 , 'Missing collection' ) ) ;
28
+ if ( ! id ) return process . nextTick ( callback , new ShareDBError ( 4001 , 'Missing ID' ) ) ;
29
+ if ( ! this . _isValidVersion ( version ) ) return process . nextTick ( callback , new ShareDBError ( 4001 , 'Invalid version' ) ) ;
30
+
25
31
var milestoneSnapshots = this . _getMilestoneSnapshotsSync ( collection , id ) ;
26
32
27
33
var milestoneSnapshot ;
@@ -38,21 +44,21 @@ MemoryMilestoneDB.prototype.getMilestoneSnapshot = function (collection, id, ver
38
44
} ;
39
45
40
46
MemoryMilestoneDB . prototype . saveMilestoneSnapshot = function ( collection , snapshot , callback ) {
41
- var saved = false ;
42
- if ( ! snapshot ) {
43
- if ( callback ) return process . nextTick ( callback , null , saved ) ;
44
- this . emit ( 'save' , saved , collection , snapshot ) ;
45
- }
47
+ callback = callback || function ( error ) {
48
+ if ( error ) return this . emit ( 'error' , error ) ;
49
+ this . emit ( 'save' , collection , snapshot ) ;
50
+ } . bind ( this ) ;
51
+
52
+ if ( ! collection ) return callback ( new ShareDBError ( 4001 , 'Missing collection' ) ) ;
53
+ if ( ! snapshot ) return callback ( new ShareDBError ( 4001 , 'Missing snapshot' ) ) ;
46
54
47
55
var milestoneSnapshots = this . _getMilestoneSnapshotsSync ( collection , snapshot . id ) ;
48
56
milestoneSnapshots . push ( snapshot ) ;
49
57
milestoneSnapshots . sort ( function ( a , b ) {
50
58
return a . v - b . v ;
51
59
} ) ;
52
60
53
- saved = true ;
54
- if ( callback ) return process . nextTick ( callback , null , saved ) ;
55
- this . emit ( 'save' , saved , collection , snapshot ) ;
61
+ process . nextTick ( callback , null ) ;
56
62
} ;
57
63
58
64
MemoryMilestoneDB . prototype . _getMilestoneSnapshotsSync = function ( collection , id ) {
0 commit comments