Use constants for code robustness & performance.
This commit is contained in:
parent
7d7e6e10b9
commit
43a362d0eb
@ -13,6 +13,10 @@ const {
|
||||
prototype: { hasOwnProperty }
|
||||
} = Object
|
||||
|
||||
export const ACTION_ADD = 'add'
|
||||
export const ACTION_UPDATE = 'update'
|
||||
export const ACTION_REMOVE = 'remove'
|
||||
|
||||
// ===================================================================
|
||||
|
||||
export class BufferAlreadyFlushed extends BaseError {
|
||||
@ -105,14 +109,14 @@ export default class Collection extends EventEmitter {
|
||||
|
||||
this._items[key] = value
|
||||
this._size++
|
||||
this._touch('add', key)
|
||||
this._touch(ACTION_ADD, key)
|
||||
}
|
||||
|
||||
clear () {
|
||||
forEach(this._items, (_, key) => {
|
||||
delete this._items[key]
|
||||
this._size--
|
||||
this._touch('remove', key)
|
||||
this._touch(ACTION_REMOVE, key)
|
||||
})
|
||||
}
|
||||
|
||||
@ -122,15 +126,15 @@ export default class Collection extends EventEmitter {
|
||||
|
||||
delete this._items[key]
|
||||
this._size--
|
||||
this._touch('remove', key)
|
||||
this._touch(ACTION_REMOVE, key)
|
||||
}
|
||||
|
||||
set (keyOrObjectWithId, valueIfKey = undefined) {
|
||||
const [key, value] = this._resolveItem(keyOrObjectWithId, valueIfKey)
|
||||
|
||||
const action = this.has(key) ? 'update' : 'add'
|
||||
const action = this.has(key) ? ACTION_UPDATE : ACTION_ADD
|
||||
this._items[key] = value
|
||||
if (action === 'add') {
|
||||
if (action === ACTION_ADD) {
|
||||
this._size++
|
||||
}
|
||||
this._touch(action, key)
|
||||
@ -144,7 +148,7 @@ export default class Collection extends EventEmitter {
|
||||
throw new IllegalTouch(value)
|
||||
}
|
||||
|
||||
this._touch('update', key)
|
||||
this._touch(ACTION_UPDATE, key)
|
||||
|
||||
return this.get(key)
|
||||
}
|
||||
@ -155,7 +159,7 @@ export default class Collection extends EventEmitter {
|
||||
if (this.has(key)) {
|
||||
delete this._items[key]
|
||||
this._size--
|
||||
this._touch('remove', key)
|
||||
this._touch(ACTION_REMOVE, key)
|
||||
}
|
||||
}
|
||||
|
||||
@ -164,7 +168,7 @@ export default class Collection extends EventEmitter {
|
||||
this._assertHas(key)
|
||||
|
||||
this._items[key] = value
|
||||
this._touch('update', key)
|
||||
this._touch(ACTION_UPDATE, key)
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------
|
||||
@ -344,17 +348,17 @@ export default class Collection extends EventEmitter {
|
||||
process.nextTick(flush)
|
||||
}
|
||||
|
||||
if (action === 'add') {
|
||||
this._buffer[key] = this._buffer[key] ? 'update' : 'add'
|
||||
} else if (action === 'remove') {
|
||||
if (this._buffer[key] === 'add') {
|
||||
if (action === ACTION_ADD) {
|
||||
this._buffer[key] = this._buffer[key] ? ACTION_UPDATE : ACTION_ADD
|
||||
} else if (action === ACTION_REMOVE) {
|
||||
if (this._buffer[key] === ACTION_ADD) {
|
||||
delete this._buffer[key]
|
||||
} else {
|
||||
this._buffer[key] = 'remove'
|
||||
this._buffer[key] = ACTION_REMOVE
|
||||
}
|
||||
} else { // update
|
||||
if (!this._buffer[key]) {
|
||||
this._buffer[key] = 'update'
|
||||
this._buffer[key] = ACTION_UPDATE
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,11 @@ import callback from 'lodash.callback'
|
||||
import clearObject from './clear-object'
|
||||
import isEmpty from './is-empty'
|
||||
import NotImplemented from './not-implemented'
|
||||
import {
|
||||
ACTION_ADD,
|
||||
ACTION_UPDATE,
|
||||
ACTION_REMOVE
|
||||
} from './collection'
|
||||
|
||||
// ===================================================================
|
||||
|
||||
@ -53,15 +58,15 @@ export default class Index {
|
||||
// has not been emitted yet.
|
||||
this._onAdd(collection.all)
|
||||
|
||||
collection.on('add', this._onAdd)
|
||||
collection.on('update', this._onUpdate)
|
||||
collection.on('remove', this._onRemove)
|
||||
collection.on(ACTION_ADD, this._onAdd)
|
||||
collection.on(ACTION_UPDATE, this._onUpdate)
|
||||
collection.on(ACTION_REMOVE, this._onRemove)
|
||||
}
|
||||
|
||||
_detachCollection (collection) {
|
||||
collection.removeListener('add', this._onAdd)
|
||||
collection.removeListener('update', this._onUpdate)
|
||||
collection.removeListener('remove', this._onRemove)
|
||||
collection.removeListener(ACTION_ADD, this._onAdd)
|
||||
collection.removeListener(ACTION_UPDATE, this._onUpdate)
|
||||
collection.removeListener(ACTION_REMOVE, this._onRemove)
|
||||
|
||||
clearObject(this._itemsByHash)
|
||||
clearObject(this._keysToHash)
|
||||
|
@ -3,6 +3,11 @@ import callback from 'lodash.callback'
|
||||
|
||||
import clearObject from './clear-object'
|
||||
import NotImplemented from './not-implemented'
|
||||
import {
|
||||
ACTION_ADD,
|
||||
ACTION_UPDATE,
|
||||
ACTION_REMOVE
|
||||
} from './collection'
|
||||
|
||||
// ===================================================================
|
||||
|
||||
@ -42,15 +47,15 @@ export default class UniqueIndex {
|
||||
// has not been emitted yet.
|
||||
this._onAdd(collection.all)
|
||||
|
||||
collection.on('add', this._onAdd)
|
||||
collection.on('update', this._onUpdate)
|
||||
collection.on('remove', this._onRemove)
|
||||
collection.on(ACTION_ADD, this._onAdd)
|
||||
collection.on(ACTION_UPDATE, this._onUpdate)
|
||||
collection.on(ACTION_REMOVE, this._onRemove)
|
||||
}
|
||||
|
||||
_detachCollection (collection) {
|
||||
collection.removeListener('add', this._onAdd)
|
||||
collection.removeListener('update', this._onUpdate)
|
||||
collection.removeListener('remove', this._onRemove)
|
||||
collection.removeListener(ACTION_ADD, this._onAdd)
|
||||
collection.removeListener(ACTION_UPDATE, this._onUpdate)
|
||||
collection.removeListener(ACTION_REMOVE, this._onRemove)
|
||||
|
||||
clearObject(this._itemByHash)
|
||||
clearObject(this._keysToHash)
|
||||
|
@ -2,7 +2,11 @@ import bind from 'lodash.bind'
|
||||
import createCallback from 'lodash.callback'
|
||||
import forEach from 'lodash.foreach'
|
||||
|
||||
import Collection from './collection'
|
||||
import Collection, {
|
||||
ACTION_ADD,
|
||||
ACTION_UPDATE,
|
||||
ACTION_REMOVE
|
||||
} from './collection'
|
||||
|
||||
// ===================================================================
|
||||
|
||||
@ -22,17 +26,17 @@ export default class View extends Collection {
|
||||
this._onRemove = bind(this._onRemove, this)
|
||||
|
||||
// Register listeners.
|
||||
this._collection.on('add', this._onAdd)
|
||||
this._collection.on('update', this._onUpdate)
|
||||
this._collection.on('remove', this._onRemove)
|
||||
this._collection.on(ACTION_ADD, this._onAdd)
|
||||
this._collection.on(ACTION_UPDATE, this._onUpdate)
|
||||
this._collection.on(ACTION_REMOVE, this._onRemove)
|
||||
}
|
||||
|
||||
// This method is necessary to free the memory of the view if its
|
||||
// life span is shorter than the collection.
|
||||
destroy () {
|
||||
this._collection.removeListener('add', this._onAdd)
|
||||
this._collection.removeListener('update', this._onUpdate)
|
||||
this._collection.removeListener('remove', this._onRemove)
|
||||
this._collection.removeListener(ACTION_ADD, this._onAdd)
|
||||
this._collection.removeListener(ACTION_UPDATE, this._onUpdate)
|
||||
this._collection.removeListener(ACTION_REMOVE, this._onRemove)
|
||||
}
|
||||
|
||||
add () {
|
||||
|
Loading…
Reference in New Issue
Block a user