Skip to content

Commit cfca37f

Browse files
committed
Make destroy wait for unsubscribe
1 parent 15cdd1d commit cfca37f

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

lib/client/doc.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,15 @@ Doc.prototype.destroy = function(callback) {
105105
var doc = this;
106106
doc.whenNothingPending(function() {
107107
if (doc.wantSubscribe) {
108-
doc.unsubscribe();
108+
doc.unsubscribe(function(err) {
109+
if (!err) doc.connection._destroyDoc(doc);
110+
if (callback) return callback(err);
111+
if (err) this.emit('error', err);
112+
});
113+
} else {
114+
doc.connection._destroyDoc(doc);
115+
if (callback) callback();
109116
}
110-
doc.connection._destroyDoc(doc);
111-
if (callback) callback();
112117
});
113118
};
114119

test/client/subscribe.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ describe('client subscribe', function() {
414414
doc2.subscribe(function(err) {
415415
if (err) return done(err);
416416
doc2.on('op', function(op, context) {
417-
done();
417+
done(new Error('Should not get op event'));
418418
});
419419
doc2.destroy(function(err) {
420420
if (err) return done(err);
@@ -425,6 +425,17 @@ describe('client subscribe', function() {
425425
});
426426
});
427427

428+
it('doc destroy removes doc from connection when doc is not subscribed', function(done) {
429+
var connection = this.backend.connect();
430+
var doc = connection.get('dogs', 'fido');
431+
expect(connection.getExisting('dogs', 'fido')).equal(doc);
432+
doc.destroy(function(err) {
433+
if (err) return done(err);
434+
expect(connection.getExisting('dogs', 'fido')).equal(undefined);
435+
done();
436+
});
437+
});
438+
428439
it('bulk unsubscribe stops op updates', function(done) {
429440
var connection = this.backend.connect();
430441
var connection2 = this.backend.connect();

0 commit comments

Comments
 (0)