Skip to content

Commit c9e773d

Browse files
authored
Merge pull request #580 from share/destroying-docs
🐛 Allow getting a `Doc` while it's being destroyed
2 parents 5176283 + d121a3e commit c9e773d

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

lib/client/connection.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,7 @@ Connection.prototype.get = function(collection, id) {
521521
this.emit('doc', doc);
522522
}
523523

524+
doc._wantsDestroy = false;
524525
return doc;
525526
};
526527

@@ -531,7 +532,9 @@ Connection.prototype.get = function(collection, id) {
531532
* @private
532533
*/
533534
Connection.prototype._destroyDoc = function(doc) {
535+
if (!doc._wantsDestroy) return;
534536
util.digAndRemove(this.collections, doc.collection, doc.id);
537+
doc.emit('destroy');
535538
};
536539

537540
Connection.prototype._addDoc = function(doc) {

lib/client/doc.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ function Doc(connection, collection, id) {
7676
// Whether to re-establish the subscription on reconnect
7777
this.wantSubscribe = false;
7878

79+
this._wantsDestroy = false;
80+
7981
// The op that is currently roundtripping to the server, or null.
8082
//
8183
// When the connection reconnects, the inflight op is resubmitted.
@@ -122,6 +124,7 @@ function Doc(connection, collection, id) {
122124
emitter.mixin(Doc);
123125

124126
Doc.prototype.destroy = function(callback) {
127+
this._wantsDestroy = true;
125128
var doc = this;
126129
doc.whenNothingPending(function() {
127130
if (doc.wantSubscribe) {
@@ -131,12 +134,10 @@ Doc.prototype.destroy = function(callback) {
131134
return doc.emit('error', err);
132135
}
133136
doc.connection._destroyDoc(doc);
134-
doc.emit('destroy');
135137
if (callback) callback();
136138
});
137139
} else {
138140
doc.connection._destroyDoc(doc);
139-
doc.emit('destroy');
140141
if (callback) callback();
141142
}
142143
});

test/client/doc.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,31 @@ describe('Doc', function() {
4343
if (err) return done(err);
4444
var doc2 = connection.get('dogs', 'fido');
4545
expect(doc).not.equal(doc2);
46-
expect(doc).eql(doc2);
4746
done();
4847
});
4948
});
5049

50+
it('destroying then getting synchronously does not destroy the new doc', function(done) {
51+
var connection = this.connection;
52+
var doc = connection.get('dogs', 'fido');
53+
var doc2;
54+
55+
doc.create({name: 'fido'}, function(error) {
56+
if (error) return done(error);
57+
58+
doc.destroy(function(error) {
59+
if (error) return done(error);
60+
var doc3 = connection.get('dogs', 'fido');
61+
async.parallel([
62+
doc2.submitOp.bind(doc2, [{p: ['snacks'], oi: true}]),
63+
doc3.submitOp.bind(doc3, [{p: ['color'], oi: 'gray'}])
64+
], done);
65+
});
66+
67+
doc2 = connection.get('dogs', 'fido');
68+
});
69+
});
70+
5171
it('doc.destroy() works without a callback', function() {
5272
var doc = this.connection.get('dogs', 'fido');
5373
doc.destroy();

0 commit comments

Comments
 (0)