-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathbase.js
190 lines (167 loc) · 6.12 KB
/
base.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
var util = require('util'),
EventEmitter = require('events').EventEmitter,
prequire = require('parent-require'),
_ = require('lodash'),
uuid = require('uuid').v4;
/**
* Store constructor
* @param {Object} options The options can have information like host, port, etc. [optional]
*/
function Store(options) {
options = options || {};
EventEmitter.call(this);
}
util.inherits(Store, EventEmitter);
function implementError (callback) {
var err = new Error('Please implement this function!');
if (callback) callback(err);
throw err;
}
function silentWarning(callback) {
console.warn('Snapshot cleaning is not implemented for this kind of store');
callback();
}
_.extend(Store.prototype, {
/**
* Initiate communication with the queue.
* @param {Function} callback The function, that will be called when the this action is completed. [optional]
* `function(err, queue){}`
*/
connect: implementError,
/**
* Terminate communication with the queue.
* @param {Function} callback The function, that will be called when the this action is completed. [optional]
* `function(err){}`
*/
disconnect: implementError,
/**
* Use this function to obtain a new id.
* @param {Function} callback The function, that will be called when the this action is completed.
* `function(err, id){}` id is of type String.
*/
getNewId: function (callback) {
var id = uuid().toString();
if (callback) callback(null, id);
},
/**
* Use this function to an array containing the next position numbers
* @param {number} positins Number of positions to provide.
* @param {Function} callback The function, that will be called when the this action is completed.
* `function(err, positions){}` positions is either undefined if option is not enabled/supported or array with positions
*/
getNextPositions: function(positions, callback) {
callback(null);
},
/**
* loads the events
* @param {Object} query the query object
* @param {Number} skip how many events should be skipped?
* @param {Number} limit how many events do you want in the result?
* @param {Function} callback the function that will be called when this action has finished
* `function(err, events){}`
*/
getEvents: function (query, skip, limit, callback) {
implementError(callback);
},
/**
* loads all the events since passed commitStamp
* @param {Date} commitStamp the date object
* @param {Number} skip how many events should be skipped? [optional]
* @param {Number} limit how many events do you want in the result? [optional]
* @param {Function} callback the function that will be called when this action has finished
* `function(err, events){}`
*/
getEventsSince: function (commitStamp, skip, limit, callback) {
implementError(callback);
},
/**
* loads the events
* @param {Object} query the query object
* @param {Number} revMin revision start point
* @param {Number} revMax revision end point (hint: -1 = to end)
* @param {Function} callback the function that will be called when this action has finished
* `function(err, events){}`
*/
getEventsByRevision: function (query, revMin, revMax, callback) {
implementError(callback);
},
/**
* loads the next snapshot back from given max revision
* @param {Object} query the query object
* @param {Number} revMax revision end point (hint: -1 = to end)
* @param {Function} callback the function that will be called when this action has finished
* `function(err, snapshot){}`
*/
getSnapshot: function (query, revMax, callback) {
implementError(callback);
},
/**
* stores a new snapshot
* @param {Object} snap the snapshot data
* @param {Function} callback the function that will be called when this action has finished [optional]
*/
addSnapshot: function(snap, callback) {
implementError(callback);
},
/**
* stores a new snapshot
* @param {Object} query the query object
* @param {Function} callback the function that will be called when this action has finished [optional]
*/
cleanSnapshots: function(query, callback) {
silentWarning(callback);
},
/**
* stores the passed events
* @param {Array} evts the events
* @param {Function} callback the function that will be called when this action has finished [optional]
*/
addEvents: function (evts, callback) {
implementError(callback);
},
/**
* loads the last event
* @param {Object} query the query object [optional]
* @param {Function} callback the function that will be called when this action has finished
* `function(err, event){}`
*/
getLastEvent: function (query, callback) {
implementError(callback);
},
/**
* loads all undispatched events
* @param {Object} query the query object [optional]
* @param {Function} callback the function that will be called when this action has finished
* `function(err, events){}`
*/
getUndispatchedEvents: function (query, callback) {
implementError(callback);
},
/**
* Sets the given event to dispatched.
* @param {String} id the event id
* @param {Function} callback the function that will be called when this action has finished [optional]
*/
setEventToDispatched: function (id, callback) {
implementError(callback);
},
/**
* NEVER USE THIS FUNCTION!!! ONLY FOR TESTS!
* clears the complete store...
* @param {Function} callback the function that will be called when this action has finished [optional]
*/
clear: function (callback) {
implementError(callback);
}
});
Store.use = function (toRequire) {
var required;
try {
required = require(toRequire);
} catch (e) {
// workaround when `npm link`'ed for development
required = prequire(toRequire);
}
return required;
};
module.exports = Store;