@@ -25,15 +25,8 @@ MemoryMilestoneDB.prototype = Object.create(MilestoneDB.prototype);
25
25
MemoryMilestoneDB . prototype . getMilestoneSnapshot = function ( collection , id , version , callback ) {
26
26
if ( ! this . _isValidVersion ( version ) ) return process . nextTick ( callback , new ShareDBError ( 4001 , 'Invalid version' ) ) ;
27
27
28
- var shouldBreak = function ( currentSnapshot , nextSnapshot ) {
29
- if ( version === null ) {
30
- return false ;
31
- }
32
-
33
- return nextSnapshot . v > version ;
34
- } ;
35
-
36
- this . _findMilestoneSnapshot ( collection , id , shouldBreak , callback ) ;
28
+ var predicate = versionLessThanOrEqualTo ( version ) ;
29
+ this . _findMilestoneSnapshot ( collection , id , predicate , callback ) ;
37
30
} ;
38
31
39
32
MemoryMilestoneDB . prototype . saveMilestoneSnapshot = function ( collection , snapshot , callback ) {
@@ -57,31 +50,15 @@ MemoryMilestoneDB.prototype.saveMilestoneSnapshot = function (collection, snapsh
57
50
MemoryMilestoneDB . prototype . getMilestoneSnapshotAtOrBeforeTime = function ( collection , id , timestamp , callback ) {
58
51
if ( ! this . _isValidTimestamp ( timestamp ) ) return process . nextTick ( callback , new ShareDBError ( 4001 , 'Invalid timestamp' ) ) ;
59
52
60
- var shouldBreak = function ( currentSnapshot , nextSnapshot ) {
61
- if ( timestamp === null ) {
62
- return ! ! currentSnapshot ;
63
- }
64
-
65
- var mtime = nextSnapshot && nextSnapshot . m && nextSnapshot . m . mtime ;
66
- return mtime > timestamp ;
67
- } ;
68
-
69
- this . _findMilestoneSnapshot ( collection , id , shouldBreak , callback ) ;
53
+ var filter = timestampLessThanOrEqualTo ( timestamp ) ;
54
+ this . _findMilestoneSnapshot ( collection , id , filter , callback ) ;
70
55
} ;
71
56
72
57
MemoryMilestoneDB . prototype . getMilestoneSnapshotAtOrAfterTime = function ( collection , id , timestamp , callback ) {
73
58
if ( ! this . _isValidTimestamp ( timestamp ) ) return process . nextTick ( callback , new ShareDBError ( 4001 , 'Invalid timestamp' ) ) ;
74
59
75
- var shouldBreak = function ( currentSnapshot ) {
76
- if ( timestamp === null ) {
77
- return false ;
78
- }
79
-
80
- var mtime = currentSnapshot && currentSnapshot . m && currentSnapshot . m . mtime ;
81
- return mtime >= timestamp ;
82
- }
83
-
84
- this . _findMilestoneSnapshot ( collection , id , shouldBreak , function ( error , snapshot ) {
60
+ var filter = timestampGreaterThanOrEqualTo ( timestamp ) ;
61
+ this . _findMilestoneSnapshot ( collection , id , filter , function ( error , snapshot ) {
85
62
if ( error ) return process . nextTick ( callback , error ) ;
86
63
87
64
var mtime = snapshot && snapshot . m && snapshot . m . mtime ;
@@ -93,7 +70,7 @@ MemoryMilestoneDB.prototype.getMilestoneSnapshotAtOrAfterTime = function (collec
93
70
} ) ;
94
71
} ;
95
72
96
- MemoryMilestoneDB . prototype . _findMilestoneSnapshot = function ( collection , id , shouldBreak , callback ) {
73
+ MemoryMilestoneDB . prototype . _findMilestoneSnapshot = function ( collection , id , breakCondition , callback ) {
97
74
if ( ! collection ) return process . nextTick ( callback , new ShareDBError ( 4001 , 'Missing collection' ) ) ;
98
75
if ( ! id ) return process . nextTick ( callback , new ShareDBError ( 4001 , 'Missing ID' ) ) ;
99
76
@@ -102,7 +79,7 @@ MemoryMilestoneDB.prototype._findMilestoneSnapshot = function (collection, id, s
102
79
var milestoneSnapshot ;
103
80
for ( var i = 0 ; i < milestoneSnapshots . length ; i ++ ) {
104
81
var nextMilestoneSnapshot = milestoneSnapshots [ i ] ;
105
- if ( shouldBreak ( milestoneSnapshot , nextMilestoneSnapshot ) ) {
82
+ if ( breakCondition ( milestoneSnapshot , nextMilestoneSnapshot ) ) {
106
83
break ;
107
84
} else {
108
85
milestoneSnapshot = nextMilestoneSnapshot ;
@@ -116,3 +93,35 @@ MemoryMilestoneDB.prototype._getMilestoneSnapshotsSync = function (collection, i
116
93
var collectionSnapshots = this . _milestoneSnapshots [ collection ] || ( this . _milestoneSnapshots [ collection ] = { } ) ;
117
94
return collectionSnapshots [ id ] || ( collectionSnapshots [ id ] = [ ] ) ;
118
95
} ;
96
+
97
+ function versionLessThanOrEqualTo ( version ) {
98
+ return function ( currentSnapshot , nextSnapshot ) {
99
+ if ( version === null ) {
100
+ return false ;
101
+ }
102
+
103
+ return nextSnapshot . v > version ;
104
+ } ;
105
+ }
106
+
107
+ function timestampGreaterThanOrEqualTo ( timestamp ) {
108
+ return function ( currentSnapshot ) {
109
+ if ( timestamp === null ) {
110
+ return false ;
111
+ }
112
+
113
+ var mtime = currentSnapshot && currentSnapshot . m && currentSnapshot . m . mtime ;
114
+ return mtime >= timestamp ;
115
+ } ;
116
+ }
117
+
118
+ function timestampLessThanOrEqualTo ( timestamp ) {
119
+ return function ( currentSnapshot , nextSnapshot ) {
120
+ if ( timestamp === null ) {
121
+ return ! ! currentSnapshot ;
122
+ }
123
+
124
+ var mtime = nextSnapshot && nextSnapshot . m && nextSnapshot . m . mtime ;
125
+ return mtime > timestamp ;
126
+ } ;
127
+ }
0 commit comments