Minor simplification of buffer flushing.
This commit is contained in:
parent
0ea662d8fe
commit
eeb898179e
@ -1,4 +1,4 @@
|
|||||||
import events from 'events'
|
import {EventEmitter} from 'events'
|
||||||
import makeError from 'make-error'
|
import makeError from 'make-error'
|
||||||
|
|
||||||
export const BufferAlreadyFlushed = makeError('BufferAlreadyFlushed')
|
export const BufferAlreadyFlushed = makeError('BufferAlreadyFlushed')
|
||||||
@ -7,19 +7,27 @@ export const IllegalAdd = makeError('IllegalAdd')
|
|||||||
export const IllegalTouch = makeError('IllegalTouch')
|
export const IllegalTouch = makeError('IllegalTouch')
|
||||||
export const NoSuchEntry = makeError('NoSuchEntry')
|
export const NoSuchEntry = makeError('NoSuchEntry')
|
||||||
|
|
||||||
export default class Collection extends events.EventEmitter {
|
function isNotEmpty (map) {
|
||||||
|
/* eslint no-unused-vars: 0*/
|
||||||
|
|
||||||
|
for (let key in map) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
export default class Collection extends EventEmitter {
|
||||||
constructor () {
|
constructor () {
|
||||||
super()
|
super()
|
||||||
|
|
||||||
|
this._buffer = Object.create(null)
|
||||||
this._buffering = 0
|
this._buffering = 0
|
||||||
this._map = Object.create(null)
|
this._map = Object.create(null)
|
||||||
this._size = 0
|
this._size = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
bufferChanges () {
|
bufferChanges () {
|
||||||
if (this._buffering++ === 0) {
|
++this._buffering
|
||||||
this._buffer = Object.create(null)
|
|
||||||
}
|
|
||||||
|
|
||||||
let called = false
|
let called = false
|
||||||
return () => {
|
return () => {
|
||||||
@ -28,30 +36,29 @@ export default class Collection extends events.EventEmitter {
|
|||||||
}
|
}
|
||||||
called = true
|
called = true
|
||||||
|
|
||||||
if (--this._buffering > 0) {
|
if (--this._buffering) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = {
|
const data = {
|
||||||
add: {data: {}},
|
add: Object.create(null),
|
||||||
remove: {data: {}},
|
remove: Object.create(null),
|
||||||
update: {data: {}}
|
update: Object.create(null)
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let key in this._buffer) {
|
for (let key in this._buffer) {
|
||||||
data[this._buffer[key]].data[key] = this.has(key) ?
|
data[this._buffer[key]][key] = this._map[key]
|
||||||
this.get(key) :
|
|
||||||
null // "remove" case
|
|
||||||
data[this._buffer[key]].has = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
['add', 'update', 'remove'].forEach(action => {
|
['add', 'update', 'remove'].forEach(action => {
|
||||||
if (data[action].has) {
|
const entries = data[action]
|
||||||
this.emit(action, data[action].data)
|
|
||||||
|
if (isNotEmpty(entries)) {
|
||||||
|
this.emit(action, entries)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
delete this._buffer
|
this._buffer = Object.create(null)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -81,7 +81,7 @@ describe('Collection', function () {
|
|||||||
// Async event.
|
// Async event.
|
||||||
return eventToPromise(this.col, 'remove').then(function (removed) {
|
return eventToPromise(this.col, 'remove').then(function (removed) {
|
||||||
expect(removed).to.have.all.keys('bar')
|
expect(removed).to.have.all.keys('bar')
|
||||||
expect(removed.bar).to.equal(null)
|
expect(removed.bar).to.not.exist()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user