sharedb 4.1.0 → 4.1.1

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.
package/lib/agent.js CHANGED
@@ -453,6 +453,8 @@ Agent.prototype._handleMessage = function(request, callback) {
453
453
  return this._subscribePresence(request.ch, request.seq, callback);
454
454
  case ACTIONS.presenceUnsubscribe:
455
455
  return this._unsubscribePresence(request.ch, request.seq, callback);
456
+ case ACTIONS.presenceRequest:
457
+ return this._requestPresence(request.ch, callback);
456
458
  case ACTIONS.pingPong:
457
459
  return this._pingPong(callback);
458
460
  default:
@@ -807,19 +809,17 @@ Agent.prototype._createPresence = function(request) {
807
809
  };
808
810
  };
809
811
 
810
- Agent.prototype._subscribePresence = function(channel, seq, callback) {
812
+ Agent.prototype._subscribePresence = function(channel, seq, cb) {
811
813
  var agent = this;
812
814
 
813
- function requestPresence() {
814
- agent._requestPresence(channel, function(error) {
815
- callback(error, {ch: channel, seq: seq});
816
- });
815
+ function callback(error) {
816
+ cb(error, {ch: channel, seq: seq});
817
817
  }
818
818
 
819
819
  var existingStream = agent.subscribedPresences[channel];
820
820
  if (existingStream) {
821
821
  agent.presenceSubscriptionSeq[channel] = seq;
822
- return requestPresence();
822
+ return callback();
823
823
  }
824
824
 
825
825
  var presenceChannel = this._getPresenceChannel(channel);
@@ -827,12 +827,14 @@ Agent.prototype._subscribePresence = function(channel, seq, callback) {
827
827
  if (error) return callback(error);
828
828
  if (seq < agent.presenceSubscriptionSeq[channel]) {
829
829
  stream.destroy();
830
- return callback(null, {ch: channel, seq: seq});
830
+ return callback();
831
831
  }
832
832
  agent.presenceSubscriptionSeq[channel] = seq;
833
833
  agent.subscribedPresences[channel] = stream;
834
834
  agent._subscribeToPresenceStream(channel, stream);
835
- requestPresence();
835
+ agent._requestPresence(channel, function(error) {
836
+ callback(error);
837
+ });
836
838
  });
837
839
  };
838
840
 
@@ -809,6 +809,10 @@ Connection.prototype._addPresence = function(presence) {
809
809
  });
810
810
  };
811
811
 
812
+ Connection.prototype._requestRemotePresence = function(channel) {
813
+ this.send({a: ACTIONS.presenceRequest, ch: channel});
814
+ };
815
+
812
816
  Connection.prototype._handlePresenceSubscribe = function(error, message) {
813
817
  var presence = util.dig(this._presences, message.ch);
814
818
  if (presence) presence._handleSubscribe(error, message.seq);
@@ -28,12 +28,12 @@ function Presence(connection, channel) {
28
28
  }
29
29
  emitter.mixin(Presence);
30
30
 
31
- Presence.prototype.subscribe = function(options, callback) {
32
- this._sendSubscriptionAction(true, options, callback);
31
+ Presence.prototype.subscribe = function(callback) {
32
+ this._sendSubscriptionAction(true, callback);
33
33
  };
34
34
 
35
- Presence.prototype.unsubscribe = function(options, callback) {
36
- this._sendSubscriptionAction(false, options, callback);
35
+ Presence.prototype.unsubscribe = function(callback) {
36
+ this._sendSubscriptionAction(false, callback);
37
37
  };
38
38
 
39
39
  Presence.prototype.create = function(id) {
@@ -83,14 +83,9 @@ Presence.prototype.destroy = function(callback) {
83
83
  });
84
84
  };
85
85
 
86
- Presence.prototype._sendSubscriptionAction = function(wantSubscribe, options, callback) {
87
- if (typeof options === 'function') {
88
- callback = options;
89
- options = {};
90
- }
91
- options = options || {};
86
+ Presence.prototype._sendSubscriptionAction = function(wantSubscribe, callback) {
92
87
  wantSubscribe = !!wantSubscribe;
93
- if (!options._force && wantSubscribe === this.wantSubscribe) {
88
+ if (wantSubscribe === this.wantSubscribe) {
94
89
  if (!callback) return;
95
90
  if (wantSubscribe === this.subscribed) return util.nextTick(callback);
96
91
  if (Object.keys(this._subscriptionCallbacksBySeq).length) {
@@ -106,6 +101,10 @@ Presence.prototype._sendSubscriptionAction = function(wantSubscribe, options, ca
106
101
  }
107
102
  };
108
103
 
104
+ Presence.prototype._requestRemotePresence = function() {
105
+ this.connection._requestRemotePresence(this.channel);
106
+ };
107
+
109
108
  Presence.prototype._handleSubscribe = function(error, seq) {
110
109
  if (this.wantSubscribe) this.subscribed = true;
111
110
  var callback = this._subscriptionCallback(seq);
@@ -105,9 +105,7 @@ RemoteDocPresence.prototype._catchUpStalePresence = function() {
105
105
  if (!this._opCache) {
106
106
  this._startCachingOps();
107
107
  this._doc.fetch();
108
- // We're already subscribed, but we send another subscribe message
109
- // to force presence updates from other clients
110
- this.presence.subscribe({_force: true});
108
+ this.presence._requestRemotePresence();
111
109
  return false;
112
110
  }
113
111
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sharedb",
3
- "version": "4.1.0",
3
+ "version": "4.1.1",
4
4
  "description": "JSON OT database backend",
5
5
  "main": "lib/index.js",
6
6
  "dependencies": {
@@ -645,20 +645,14 @@ describe('DocPresence', function() {
645
645
  presence1.subscribe.bind(presence1),
646
646
  presence2.subscribe.bind(presence2),
647
647
  function(next) {
648
- connection1._setState('disconnected');
649
- next();
650
- },
651
- function(next) {
652
- localPresence2.submit({index: 0}, errorHandler(done));
653
- // We've not _actually_ disconnected the connection, so this
654
- // event will still fire.
655
- presence1.once('receive', function() {
648
+ connection1.once('closed', function() {
656
649
  next();
657
650
  });
651
+ connection1.close();
658
652
  },
653
+ localPresence2.submit.bind(localPresence2, {index: 0}),
659
654
  function(next) {
660
- connection1._setState('connecting');
661
- connection1._setState('connected');
655
+ backend.connect(connection1);
662
656
  presence1.once('receive', function(id, presence) {
663
657
  expect(presence).to.eql({index: 0});
664
658
  next();
@@ -344,9 +344,11 @@ describe('Presence', function() {
344
344
  presence1.subscribe.bind(presence1),
345
345
  presence2.subscribe.bind(presence2),
346
346
  function(next) {
347
- connection2._setState('disconnected');
347
+ connection2.once('closed', function() {
348
+ next();
349
+ });
350
+ connection2.close();
348
351
  expect(connection2.canSend).to.be.false;
349
- next();
350
352
  },
351
353
  localPresence1.submit.bind(localPresence1, {index: 1}),
352
354
  function(next) {
@@ -355,8 +357,7 @@ describe('Presence', function() {
355
357
  expect(presence).to.eql({index: 1});
356
358
  next();
357
359
  });
358
- connection2._setState('connecting');
359
- connection2._setState('connected');
360
+ backend.connect(connection2);
360
361
  }
361
362
  ], done);
362
363
  });
@@ -444,12 +445,14 @@ describe('Presence', function() {
444
445
  it('does not leak Streams when subscribing the same presence multiple times', function(done) {
445
446
  var streamsCount;
446
447
  async.series([
447
- presence1.subscribe.bind(presence1, {force: true}),
448
+ presence1.subscribe.bind(presence1),
448
449
  function(next) {
449
450
  streamsCount = backend.pubsub.streamsCount;
451
+ // Trick it into sending a duplicate request
452
+ presence1.wantSubscribe = false;
450
453
  next();
451
454
  },
452
- presence1.subscribe.bind(presence1, {force: true}),
455
+ presence1.subscribe.bind(presence1),
453
456
  function(next) {
454
457
  expect(backend.pubsub.streamsCount).to.equal(streamsCount);
455
458
  next();