Skip to content

Commit 945d69e

Browse files
committed
💥 Rename Doc op events
The `Doc` op events are currently a little confusing: a `json0` "op" can be shattered into multiple "component ops". In the current naming, the "op" emits a `'op batch'` event, and the "op component" emits an `'op'` event. However, calling the op an "op batch" is a little bit misleading, because the "batch" is always an op representing a single version number increase, and potentially considered a single conceptual change. For example, a remote client might submit a single op: ```js [ {p: ['a'], oi: 'foo'}, {p: ['b'], oi: 'bar'}, ] ``` Under the **current** naming scheme, this emits the following events: 1 `'before op batch'`: `[{p: ['a'], oi: 'foo'}, {p: ['b'], oi: 'bar'}]` 2 `'before op'`: `[{p: ['a'], oi: 'foo'}]` 3 `'op'`: `[{p: ['a'], oi: 'foo'}]` 4 `'before op'`: `[{p: ['b'], oi: 'bar'}]` 5 `'op'`: `[{p: ['b'], oi: 'bar'}]` 6 `'op batch'`: `[{p: ['a'], oi: 'foo'}, {p: ['b'], oi: 'bar'}]` This can be considered a little surprising, and you may expect the `'op'` event to be emitted after the whole op (not its shattered components) have been applied. Under the **new** naming scheme, the following events are emitted: 1 `'beforeOp'`: `[{p: ['a'], oi: 'foo'}, {p: ['b'], oi: 'bar'}]` 2 `'beforeOpComponent'`: `[{p: ['a'], oi: 'foo'}]` 3 `'opComponent'`: `[{p: ['a'], oi: 'foo'}]` 4 `'beforeOpComponent'`: `[{p: ['b'], oi: 'bar'}]` 5 `'opComponent'`: `[{p: ['b'], oi: 'bar'}]` 6 `'op'`: `[{p: ['a'], oi: 'foo'}, {p: ['b'], oi: 'bar'}]` This way, you get the `'op'` event after the whole op has been applied. It also makes it more explicit that you're actively listening out for the shattered op components where applicable. Note that we also move the events to camelCase to be consistent with the Backend middleware actions.
1 parent 087eda3 commit 945d69e

File tree

9 files changed

+56
-50
lines changed

9 files changed

+56
-50
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
### Breaking changes
44

5+
* Rename `Doc` op events:
6+
* `before op batch` -> `beforeOp`
7+
* `before op` -> `beforeOpComponent`
8+
* `op` -> `opComponent`
9+
* `op batch` -> `op`
10+
511
## v1.0-beta
612

713
### Breaking changes

docs/api/doc.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -372,12 +372,12 @@ The document was created. The doc will now have a [`type`](#type--type)
372372
373373
> {{ page.copy.event_source }}
374374
375-
### `'before op'`
375+
### `'beforeOpComponent'`
376376
377377
An operation is about to be applied to the [`data`](#data--object)
378378
379379
```js
380-
doc.on('before op', function(op, source) { ... })
380+
doc.on('beforeOpComponent', function(op, source) { ... })
381381
```
382382
383383
`op` -- Object
@@ -388,20 +388,20 @@ doc.on('before op', function(op, source) { ... })
388388
389389
> {{ page.copy.event_source }}
390390
391-
### `'op'`
391+
### `'opComponent'`
392392
393393
An operation was applied to the data.
394394
395395
```js
396-
doc.on('op', function(op, source) { ... })
396+
doc.on('opComponent', function(op, source) { ... })
397397
```
398398
399399
{: .info }
400-
The difference between this event and [`'op batch'`](#op-batch) is that for [`json0`]({{ site.baseurl }}{% link types/json0.md %}), the op will be shattered into its constituent parts.
400+
The difference between this event and [`'op'`](#op) is that for [`json0`]({{ site.baseurl }}{% link types/json0.md %}), the op will be shattered into its constituent parts.
401401
<br/>
402402
For example, `[{p: ['list', 0], li: 'a'}, {p: ['list', 1], li: 'b'}]` would be split into two components: `[{p: ['list', 0], li: 'a'}]` and `[{p: ['list', 1], li: 'b'}]`.
403403
<br/>
404-
The `'op'` event will be called once for each of these op components, but `'op batch'` will only be called once.
404+
The `'opComponent'` event will be called once for each of these op components, but `'op'` will only be called once.
405405
406406
`op` -- Object
407407
@@ -411,12 +411,12 @@ The `'op'` event will be called once for each of these op components, but `'op b
411411
412412
> {{ page.copy.event_source }}
413413
414-
### `'before op batch'`
414+
### `'beforeOp'`
415415
416416
A potentially multi-part operation is about to be applied to the [`data`](#data--object).
417417
418418
```js
419-
doc.on('before op batch', function(op, source) { ... })
419+
doc.on('beforeOp', function(op, source) { ... })
420420
```
421421
422422
`op` -- Object
@@ -427,12 +427,12 @@ doc.on('before op batch', function(op, source) { ... })
427427
428428
> {{ page.copy.event_source }}
429429
430-
### `'op batch'`
430+
### `'op'`
431431
432432
A potentially multi-part operation was applied to the [`data`](#data--object)
433433
434434
```js
435-
doc.on('op batch', function(op, source) { ... })
435+
doc.on('op', function(op, source) { ... })
436436
```
437437
438438
`op` -- Object

lib/client/doc.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ Doc.prototype._otApply = function(op, source) {
589589

590590
// NB: If we need to add another argument to this event, we should consider
591591
// the fact that the 'op' event has op.src as its 3rd argument
592-
this.emit('before op batch', op.op, source);
592+
this.emit('beforeOp', op.op, source);
593593

594594
// Iteratively apply multi-component remote operations and rollback ops
595595
// (source === false) for the default JSON0 OT type. It could use
@@ -620,28 +620,28 @@ Doc.prototype._otApply = function(op, source) {
620620
if (transformErr) return this._hardRollback(transformErr);
621621
}
622622
// Apply the individual op component
623-
this.emit('before op', componentOp.op, source, op.src);
623+
this.emit('beforeOpComponent', componentOp.op, source, op.src);
624624
this.data = this.type.apply(this.data, componentOp.op);
625-
this.emit('op', componentOp.op, source, op.src);
625+
this.emit('opComponent', componentOp.op, source, op.src);
626626
}
627-
this.emit('op batch', op.op, source);
627+
this.emit('op', op.op, source);
628628
// Pop whatever was submitted since we started applying this op
629629
this._popApplyStack(stackLength);
630630
return;
631631
}
632632

633-
// The 'before op' event enables clients to pull any necessary data out of
633+
// The 'beforeOpComponent' event enables clients to pull any necessary data out of
634634
// the snapshot before it gets changed
635-
this.emit('before op', op.op, source, op.src);
635+
this.emit('beforeOpComponent', op.op, source, op.src);
636636
// Apply the operation to the local data, mutating it in place
637637
this.data = this.type.apply(this.data, op.op);
638638
// Emit an 'op' event once the local data includes the changes from the
639639
// op. For locally submitted ops, this will be synchronously with
640640
// submission and before the server or other clients have received the op.
641641
// For ops from other clients, this will be after the op has been
642642
// committed to the database and published
643-
this.emit('op', op.op, source, op.src);
644-
this.emit('op batch', op.op, source);
643+
this.emit('opComponent', op.op, source, op.src);
644+
this.emit('op', op.op, source);
645645
return;
646646
}
647647

lib/client/presence/local-doc-presence.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ LocalDocPresence.prototype.submit = function(value, callback) {
3939
};
4040

4141
LocalDocPresence.prototype.destroy = function(callback) {
42-
this._doc.removeListener('op', this._opHandler);
42+
this._doc.removeListener('opComponent', this._opHandler);
4343
this._doc.removeListener('create', this._createOrDelHandler);
4444
this._doc.removeListener('del', this._createOrDelHandler);
4545
this._doc.removeListener('load', this._loadHandler);
@@ -67,7 +67,7 @@ LocalDocPresence.prototype._sendPending = function() {
6767
};
6868

6969
LocalDocPresence.prototype._registerWithDoc = function() {
70-
this._doc.on('op', this._opHandler);
70+
this._doc.on('opComponent', this._opHandler);
7171
this._doc.on('create', this._createOrDelHandler);
7272
this._doc.on('del', this._createOrDelHandler);
7373
this._doc.on('load', this._loadHandler);

lib/client/presence/remote-doc-presence.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ RemoteDocPresence.prototype.receiveUpdate = function(message) {
3131
};
3232

3333
RemoteDocPresence.prototype.destroy = function(callback) {
34-
this._doc.removeListener('op', this._opHandler);
34+
this._doc.removeListener('opComponent', this._opHandler);
3535
this._doc.removeListener('create', this._createDelHandler);
3636
this._doc.removeListener('del', this._createDelHandler);
3737
this._doc.removeListener('load', this._loadHandler);
@@ -40,7 +40,7 @@ RemoteDocPresence.prototype.destroy = function(callback) {
4040
};
4141

4242
RemoteDocPresence.prototype._registerWithDoc = function() {
43-
this._doc.on('op', this._opHandler);
43+
this._doc.on('opComponent', this._opHandler);
4444
this._doc.on('create', this._createDelHandler);
4545
this._doc.on('del', this._createDelHandler);
4646
this._doc.on('load', this._loadHandler);

test/client/doc.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ describe('Doc', function() {
150150
expect(doc.data).eql({color: 'black'});
151151
}
152152
];
153-
doc.on('op', function(op, source) {
153+
doc.on('opComponent', function(op, source) {
154154
var handler = handlers.shift();
155155
handler(op, source);
156156
});
@@ -199,7 +199,7 @@ describe('Doc', function() {
199199
expect(doc.data).eql({color: 'black', weight: 40, age: 5, owner: 'sue'});
200200
}
201201
];
202-
doc.on('op', function(op, source) {
202+
doc.on('opComponent', function(op, source) {
203203
var handler = handlers.shift();
204204
handler(op, source);
205205
});
@@ -250,7 +250,7 @@ describe('Doc', function() {
250250
expect(doc.data).eql({tricks: ['shake', 'tug stick']});
251251
}
252252
];
253-
doc.on('op', function(op, source) {
253+
doc.on('opComponent', function(op, source) {
254254
var handler = handlers.shift();
255255
handler(op, source);
256256
});
@@ -277,13 +277,13 @@ describe('Doc', function() {
277277
{p: ['tricks', 0], li: 'stand'}
278278
];
279279

280-
doc.on('before op batch', function(op, source) {
280+
doc.on('beforeOp', function(op, source) {
281281
expect(op).to.eql(submittedOp);
282282
expect(source).to.be.true;
283283
beforeOpBatchCount++;
284284
});
285285

286-
doc.on('op batch', function(op, source) {
286+
doc.on('op', function(op, source) {
287287
expect(op).to.eql(submittedOp);
288288
expect(source).to.be.true;
289289
expect(beforeOpBatchCount).to.equal(1);
@@ -304,13 +304,13 @@ describe('Doc', function() {
304304
{p: ['tricks', 0], li: 'stand'}
305305
];
306306

307-
doc.on('before op batch', function(op, source) {
307+
doc.on('beforeOp', function(op, source) {
308308
expect(op).to.eql(submittedOp);
309309
expect(source).to.be.false;
310310
beforeOpBatchCount++;
311311
});
312312

313-
doc.on('op batch', function(op, source) {
313+
doc.on('op', function(op, source) {
314314
expect(op).to.eql(submittedOp);
315315
expect(source).to.be.false;
316316
expect(beforeOpBatchCount).to.equal(1);

test/client/presence/doc-presence.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ describe('DocPresence', function() {
284284
function(next) {
285285
doc1.submitOp({index: 5, value: 'ern'}, errorHandler(done));
286286

287-
doc2.once('op', function() {
287+
doc2.once('opComponent', function() {
288288
presencePauser.resume();
289289
});
290290

@@ -324,7 +324,7 @@ describe('DocPresence', function() {
324324
// doc2 has received this op, so we know that when we finally receive our
325325
// presence, it will be stale
326326
doc1.submitOp({index: 5, value: 'ern'}, errorHandler(done));
327-
doc2.once('op', function() {
327+
doc2.once('opComponent', function() {
328328
next();
329329
});
330330
},
@@ -350,7 +350,7 @@ describe('DocPresence', function() {
350350
doc1.submitOp({index: 0, value: 'The'}, function(error) {
351351
if (error) return done(error);
352352
doc1.submitOp({index: 3, value: ' '}, errorHandler(done));
353-
doc2.on('op', function() {
353+
doc2.on('opComponent', function() {
354354
// This will get fired for v3 and then v4, so check for the later one
355355
if (doc1.version === 4 && doc2.version === 4) {
356356
// Only once doc2 has received the ops, should we resume our
@@ -395,7 +395,7 @@ describe('DocPresence', function() {
395395
},
396396
function(next) {
397397
doc1.submitOp({index: 5, value: 'ern'}, errorHandler(done));
398-
doc2.once('op', function() {
398+
doc2.once('opComponent', function() {
399399
next();
400400
});
401401
},
@@ -417,7 +417,7 @@ describe('DocPresence', function() {
417417
], errorHandler(done));
418418
};
419419

420-
doc2.on('op', function() {
420+
doc2.on('opComponent', function() {
421421
if (doc2.version !== 5) return;
422422
presencePauser.resume();
423423
presence2.once('receive', function(id, presence) {

test/client/projections.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ module.exports = function(options) {
124124
var fido = connection2.get('dogs_summary', 'fido');
125125
fido.subscribe(function(err) {
126126
if (err) return done(err);
127-
fido.on('op', function() {
127+
fido.on('opComponent', function() {
128128
expect(fido.data).eql(expected);
129129
expect(fido.version).eql(2);
130130
done();
@@ -164,7 +164,7 @@ module.exports = function(options) {
164164
var fido = connection2.get('dogs_summary', 'fido');
165165
connection2.createSubscribeQuery('dogs_summary', matchAllQuery, null, function(err) {
166166
if (err) return done(err);
167-
fido.on('op', function() {
167+
fido.on('opComponent', function() {
168168
expect(fido.data).eql(expected);
169169
expect(fido.version).eql(2);
170170
done();

0 commit comments

Comments
 (0)