Skip to content
This repository was archived by the owner on Apr 2, 2024. It is now read-only.

Commit 61bf178

Browse files
authored
Merge pull request share#244 from share/null-milestone-db
Add a `NoOpMilestoneDB` implementation
2 parents fb906ef + cb94f33 commit 61bf178

File tree

8 files changed

+207
-93
lines changed

8 files changed

+207
-93
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -420,3 +420,5 @@ The `41xx` and `51xx` codes are reserved for use by ShareDB DB adapters, and the
420420
* 5016 - _unsubscribe PubSub method unimplemented
421421
* 5017 - _publish PubSub method unimplemented
422422
* 5018 - Required QueryEmitter listener not assigned
423+
* 5019 - getMilestoneSnapshot MilestoneDB method unimplemented
424+
* 5020 - saveMilestoneSnapshot MilestoneDB method unimplemented

lib/backend.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var Agent = require('./agent');
33
var Connection = require('./client/connection');
44
var emitter = require('./emitter');
55
var MemoryDB = require('./db/memory');
6-
var MilestoneDB = require('./milestone-db');
6+
var NoOpMilestoneDB = require('./milestone-db/no-op');
77
var MemoryPubSub = require('./pubsub/memory');
88
var ot = require('./ot');
99
var projections = require('./projections');
@@ -25,7 +25,7 @@ function Backend(options) {
2525
this.pubsub = options.pubsub || new MemoryPubSub();
2626
// This contains any extra databases that can be queried
2727
this.extraDbs = options.extraDbs || {};
28-
this.milestoneDb = options.milestoneDb || new MilestoneDB();
28+
this.milestoneDb = options.milestoneDb || new NoOpMilestoneDB();
2929

3030
// Map from projected collection -> {type, fields}
3131
this.projections = {};

lib/client/snapshot-request.js

+1-13
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function SnapshotRequest(connection, requestId, collection, id, version, callbac
1111
throw new Error('Callback is required for SnapshotRequest');
1212
}
1313

14-
if (!this.isValidVersion(version)) {
14+
if (!util.isValidVersion(version)) {
1515
throw new Error('Snapshot version must be a positive integer or null');
1616
}
1717

@@ -26,18 +26,6 @@ function SnapshotRequest(connection, requestId, collection, id, version, callbac
2626
}
2727
emitter.mixin(SnapshotRequest);
2828

29-
SnapshotRequest.prototype.isValidVersion = function (version) {
30-
if (version === null) {
31-
return true;
32-
}
33-
34-
if (!util.isInteger(version)) {
35-
return false;
36-
}
37-
38-
return version >= 0;
39-
}
40-
4129
SnapshotRequest.prototype.send = function () {
4230
if (!this.connection.canSend) {
4331
return;

lib/milestone-db/index.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
var emitter = require('../emitter');
2+
var ShareDBError = require('../error');
3+
var util = require('../util');
24

35
module.exports = MilestoneDB;
46
function MilestoneDB(options) {
@@ -23,17 +25,23 @@ MilestoneDB.prototype.close = function(callback) {
2325
* the signature (error, snapshot) => void;
2426
*/
2527
MilestoneDB.prototype.getMilestoneSnapshot = function (collection, id, version, callback) {
26-
process.nextTick(callback, null, undefined);
28+
var error = new ShareDBError(5019, 'getMilestoneSnapshot MilestoneDB method unimplemented');
29+
if (callback) return process.nextTick(callback, error);
30+
this.emit('error', error);
2731
};
2832

2933
/**
3034
* @param {string} collection - name of the snapshot's collection
3135
* @param {Snapshot} snapshot - the milestone snapshot to save
3236
* @param {Function} callback (optional) - a callback to invoke after the snapshot has been saved.
33-
* Should have the signature (error, wasSaved) => void;
37+
* Should have the signature (error) => void;
3438
*/
3539
MilestoneDB.prototype.saveMilestoneSnapshot = function (collection, snapshot, callback) {
36-
var saved = false;
37-
if (callback) return process.nextTick(callback, null, saved);
38-
this.emit('save', saved, collection, snapshot);
40+
var error = new ShareDBError(5020, 'saveMilestoneSnapshot MilestoneDB method unimplemented');
41+
if (callback) return process.nextTick(callback, error);
42+
this.emit('error', error);
43+
};
44+
45+
MilestoneDB.prototype._isValidVersion = function (version) {
46+
return util.isValidVersion(version);
3947
};

lib/milestone-db/memory.js

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var MilestoneDB = require('./index');
2+
var ShareDBError = require('../error');
23

34
/**
45
* In-memory ShareDB milestone database
@@ -22,6 +23,11 @@ function MemoryMilestoneDB(options) {
2223
MemoryMilestoneDB.prototype = Object.create(MilestoneDB.prototype);
2324

2425
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+
2531
var milestoneSnapshots = this._getMilestoneSnapshotsSync(collection, id);
2632

2733
var milestoneSnapshot;
@@ -38,21 +44,21 @@ MemoryMilestoneDB.prototype.getMilestoneSnapshot = function (collection, id, ver
3844
};
3945

4046
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'));
4654

4755
var milestoneSnapshots = this._getMilestoneSnapshotsSync(collection, snapshot.id);
4856
milestoneSnapshots.push(snapshot);
4957
milestoneSnapshots.sort(function (a, b) {
5058
return a.v - b.v;
5159
});
5260

53-
saved = true;
54-
if (callback) return process.nextTick(callback, null, saved);
55-
this.emit('save', saved, collection, snapshot);
61+
process.nextTick(callback, null);
5662
};
5763

5864
MemoryMilestoneDB.prototype._getMilestoneSnapshotsSync = function (collection, id) {

lib/milestone-db/no-op.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var MilestoneDB = require('./index');
2+
3+
/**
4+
* A no-op implementation of the MilestoneDB class.
5+
*
6+
* This class exists as a simple, silent default drop-in for ShareDB, which allows the backend to call its methods with
7+
* no effect.
8+
*/
9+
module.exports = NoOpMilestoneDB;
10+
function NoOpMilestoneDB(options) {
11+
MilestoneDB.call(this, options);
12+
}
13+
14+
NoOpMilestoneDB.prototype = Object.create(MilestoneDB.prototype);
15+
16+
NoOpMilestoneDB.prototype.getMilestoneSnapshot = function (collection, id, version, callback) {
17+
var snapshot = undefined;
18+
process.nextTick(callback, null, snapshot);
19+
};
20+
21+
NoOpMilestoneDB.prototype.saveMilestoneSnapshot = function (collection, snapshot, callback) {
22+
if (callback) return process.nextTick(callback, null);
23+
this.emit('save', collection, snapshot);
24+
};

lib/util.js

+5
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,8 @@ exports.isInteger = Number.isInteger || function (value) {
1313
isFinite(value) &&
1414
Math.floor(value) === value;
1515
};
16+
17+
exports.isValidVersion = function (version) {
18+
if (version === null) return true;
19+
return exports.isInteger(version) && version >= 0;
20+
};

0 commit comments

Comments
 (0)