diff --git a/packages/xo-collection/README.md b/packages/xo-collection/README.md index ccabddecf..96a669f7c 100644 --- a/packages/xo-collection/README.md +++ b/packages/xo-collection/README.md @@ -163,6 +163,17 @@ col.on('remove', (removed) => { }) ``` +**End of update** + +> Emitted when all the update process is finished and all the update +> events has been emitted. + +```javascript +col.on('finish', () => { + console.log('the collection has been updated') +}) +``` + ### Iteration ```javascript diff --git a/packages/xo-collection/src/collection.js b/packages/xo-collection/src/collection.js index 401584532..632145629 100644 --- a/packages/xo-collection/src/collection.js +++ b/packages/xo-collection/src/collection.js @@ -248,6 +248,13 @@ export default class Collection extends EventEmitter { return } + const {_buffer: buffer} = this + + // Due to deduplication there could be nothing in the buffer. + if (isEmpty(buffer)) { + return + } + const data = { add: Object.create(null), remove: Object.create(null), @@ -255,7 +262,7 @@ export default class Collection extends EventEmitter { } for (let key in this._buffer) { - data[this._buffer[key]][key] = this._items[key] + data[buffer[key]][key] = this._items[key] } forEach(data, (items, action) => { @@ -264,6 +271,12 @@ export default class Collection extends EventEmitter { } }) + // Indicates the end of the update. + // + // This name has been chosen because it is used in Node writable + // streams when the data has been successfully committed. + this.emit('finish') + this._buffer = Object.create(null) } }