sharedb 4.1.2 → 4.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -61,7 +61,9 @@ Presence.prototype.destroy = function(callback) {
61
61
  [
62
62
  function(next) {
63
63
  async.each(localIds, function(presenceId, next) {
64
- presence.localPresences[presenceId].destroy(next);
64
+ var localPresence = presence.localPresences[presenceId];
65
+ if (!localPresence) return next();
66
+ localPresence.destroy(next);
65
67
  }, next);
66
68
  },
67
69
  function(next) {
@@ -0,0 +1,26 @@
1
+ exports.messageChannel = function() {
2
+ var triggerCallback = createNextTickTrigger(arguments);
3
+ var channel = new MessageChannel();
4
+ channel.port1.onmessage = function() {
5
+ triggerCallback();
6
+ channel.port1.close();
7
+ };
8
+ channel.port2.postMessage('');
9
+ };
10
+
11
+ exports.setTimeout = function() {
12
+ var triggerCallback = createNextTickTrigger(arguments);
13
+ setTimeout(triggerCallback);
14
+ };
15
+
16
+ function createNextTickTrigger(args) {
17
+ var callback = args[0];
18
+ var _args = [];
19
+ for (var i = 1; i < args.length; i++) {
20
+ _args[i - 1] = args[i];
21
+ }
22
+
23
+ return function triggerCallback() {
24
+ callback.apply(null, _args);
25
+ };
26
+ }
package/lib/op-stream.js CHANGED
@@ -1,4 +1,3 @@
1
- var inherits = require('util').inherits;
2
1
  var Readable = require('stream').Readable;
3
2
  var util = require('./util');
4
3
 
@@ -10,7 +9,7 @@ function OpStream() {
10
9
  }
11
10
  module.exports = OpStream;
12
11
 
13
- inherits(OpStream, Readable);
12
+ util.inherits(OpStream, Readable);
14
13
 
15
14
  // This function is for notifying us that the stream is empty and needs data.
16
15
  // For now, we'll just ignore the signal and assume the reader reads as fast
@@ -1,5 +1,4 @@
1
1
  var Duplex = require('stream').Duplex;
2
- var inherits = require('util').inherits;
3
2
  var logger = require('./logger');
4
3
  var util = require('./util');
5
4
 
@@ -47,7 +46,7 @@ function ServerStream(socket) {
47
46
  socket.close('stopped');
48
47
  });
49
48
  }
50
- inherits(ServerStream, Duplex);
49
+ util.inherits(ServerStream, Duplex);
51
50
 
52
51
  ServerStream.prototype.isServer = true;
53
52
 
package/lib/util.js CHANGED
@@ -1,3 +1,4 @@
1
+ var nextTickImpl = require('./next-tick');
1
2
 
2
3
  exports.doNothing = doNothing;
3
4
  function doNothing() {}
@@ -85,20 +86,13 @@ exports.truthy = function(arg) {
85
86
  return !!arg;
86
87
  };
87
88
 
88
- exports.nextTick = function(callback) {
89
- if (typeof process !== 'undefined' && process.nextTick) {
90
- return process.nextTick.apply(null, arguments);
91
- }
92
-
93
- var args = [];
94
- for (var i = 1; i < arguments.length; i++) {
95
- args[i - 1] = arguments[i];
96
- }
97
-
98
- setTimeout(function() {
99
- callback.apply(null, args);
100
- });
101
- };
89
+ if (typeof process !== 'undefined' && typeof process.nextTick === 'function') {
90
+ exports.nextTick = process.nextTick;
91
+ } else if (typeof MessageChannel !== 'undefined') {
92
+ exports.nextTick = nextTickImpl.messageChannel;
93
+ } else {
94
+ exports.nextTick = nextTickImpl.setTimeout;
95
+ }
102
96
 
103
97
  exports.clone = function(obj) {
104
98
  return (obj === undefined) ? undefined : JSON.parse(JSON.stringify(obj));
@@ -113,3 +107,15 @@ Object.getOwnPropertyNames(Object.prototype).forEach(function(prop) {
113
107
  exports.isDangerousProperty = function(propName) {
114
108
  return propName === '__proto__' || objectProtoPropNames[propName];
115
109
  };
110
+
111
+ try {
112
+ var util = require('util');
113
+ if (typeof util.inherits !== 'function') throw new Error('Could not find util.inherits()');
114
+ exports.inherits = util.inherits;
115
+ } catch (e) {
116
+ try {
117
+ exports.inherits = require('inherits');
118
+ } catch (e) {
119
+ throw new Error('If running sharedb in a browser, please install the "inherits" or "util" package');
120
+ }
121
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sharedb",
3
- "version": "4.1.2",
3
+ "version": "4.1.4",
4
4
  "description": "JSON OT database backend",
5
5
  "main": "lib/index.js",
6
6
  "dependencies": {
@@ -146,6 +146,32 @@ describe('Presence', function() {
146
146
  ], errorHandler(done));
147
147
  });
148
148
 
149
+ it('does not throw if LocalPresence destroys before Presence', function(done) {
150
+ var localPresence1 = presence1.create('presence-1');
151
+
152
+ async.series([
153
+ presence1.subscribe.bind(presence1),
154
+ function(next) {
155
+ // Middleware that ensures the presence update replies before the
156
+ // unsubscribe, so the local presence is destroyed first
157
+ var replies = [];
158
+ backend.use('reply', function(message, cb) {
159
+ if (!replies) return cb();
160
+ if (message.reply.a !== 'p') return replies.push(cb);
161
+ var _replies = replies;
162
+ replies = null;
163
+ cb();
164
+ _replies.forEach(function(reply) {
165
+ reply();
166
+ });
167
+ });
168
+
169
+ presence1.destroy(next);
170
+ localPresence1.destroy(errorHandler(done));
171
+ }
172
+ ], done);
173
+ });
174
+
149
175
  it('throws if trying to create local presence when wanting destroy', function(done) {
150
176
  presence2.destroy(errorHandler(done));
151
177
  expect(function() {
@@ -0,0 +1,26 @@
1
+ var nextTickImpl = require('../lib/next-tick');
2
+ var expect = require('chai').expect;
3
+
4
+ describe('nextTick', function() {
5
+ ['messageChannel', 'setTimeout'].forEach(function(name) {
6
+ var tick = nextTickImpl[name];
7
+
8
+ it('passes args', function(done) {
9
+ tick(function(arg1, arg2, arg3) {
10
+ expect(arg1).to.equal('foo');
11
+ expect(arg2).to.equal(123);
12
+ expect(arg3).to.be.undefined;
13
+ done();
14
+ }, 'foo', 123);
15
+ });
16
+
17
+ it('calls asynchronously', function(done) {
18
+ var called = false;
19
+ tick(function() {
20
+ called = true;
21
+ done();
22
+ });
23
+ expect(called).to.be.false;
24
+ });
25
+ });
26
+ });
package/test/util-test.js DELETED
@@ -1,50 +0,0 @@
1
- var util = require('../lib/util');
2
- var expect = require('chai').expect;
3
-
4
- describe('util', function() {
5
- describe('nextTick', function() {
6
- it('uses process.nextTick if present', function(done) {
7
- expect(process.nextTick).to.be.ok;
8
-
9
- util.nextTick(function(arg1, arg2, arg3) {
10
- expect(arg1).to.equal('foo');
11
- expect(arg2).to.equal(123);
12
- expect(arg3).to.be.undefined;
13
- done();
14
- }, 'foo', 123);
15
- });
16
-
17
- describe('without nextTick', function() {
18
- var nextTick;
19
-
20
- before(function() {
21
- nextTick = process.nextTick;
22
- delete process.nextTick;
23
- });
24
-
25
- after(function() {
26
- process.nextTick = nextTick;
27
- });
28
-
29
- it('uses a ponyfill if process.nextTick is not present', function(done) {
30
- expect(process.nextTick).to.be.undefined;
31
-
32
- util.nextTick(function(arg1, arg2, arg3) {
33
- expect(arg1).to.equal('foo');
34
- expect(arg2).to.equal(123);
35
- expect(arg3).to.be.undefined;
36
- done();
37
- }, 'foo', 123);
38
- });
39
-
40
- it('calls asynchronously', function(done) {
41
- var called = false;
42
- util.nextTick(function() {
43
- called = true;
44
- done();
45
- });
46
- expect(called).to.be.false;
47
- });
48
- });
49
- });
50
- });