Use constants for code robustness & performance.

This commit is contained in:
Julien Fontanet 2015-10-02 12:56:39 +02:00
parent 7d7e6e10b9
commit 43a362d0eb
4 changed files with 51 additions and 33 deletions

View File

@ -13,6 +13,10 @@ const {
prototype: { hasOwnProperty } prototype: { hasOwnProperty }
} = Object } = Object
export const ACTION_ADD = 'add'
export const ACTION_UPDATE = 'update'
export const ACTION_REMOVE = 'remove'
// =================================================================== // ===================================================================
export class BufferAlreadyFlushed extends BaseError { export class BufferAlreadyFlushed extends BaseError {
@ -105,14 +109,14 @@ export default class Collection extends EventEmitter {
this._items[key] = value this._items[key] = value
this._size++ this._size++
this._touch('add', key) this._touch(ACTION_ADD, key)
} }
clear () { clear () {
forEach(this._items, (_, key) => { forEach(this._items, (_, key) => {
delete this._items[key] delete this._items[key]
this._size-- 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] delete this._items[key]
this._size-- this._size--
this._touch('remove', key) this._touch(ACTION_REMOVE, key)
} }
set (keyOrObjectWithId, valueIfKey = undefined) { set (keyOrObjectWithId, valueIfKey = undefined) {
const [key, value] = this._resolveItem(keyOrObjectWithId, valueIfKey) 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 this._items[key] = value
if (action === 'add') { if (action === ACTION_ADD) {
this._size++ this._size++
} }
this._touch(action, key) this._touch(action, key)
@ -144,7 +148,7 @@ export default class Collection extends EventEmitter {
throw new IllegalTouch(value) throw new IllegalTouch(value)
} }
this._touch('update', key) this._touch(ACTION_UPDATE, key)
return this.get(key) return this.get(key)
} }
@ -155,7 +159,7 @@ export default class Collection extends EventEmitter {
if (this.has(key)) { if (this.has(key)) {
delete this._items[key] delete this._items[key]
this._size-- 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._assertHas(key)
this._items[key] = value 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) process.nextTick(flush)
} }
if (action === 'add') { if (action === ACTION_ADD) {
this._buffer[key] = this._buffer[key] ? 'update' : 'add' this._buffer[key] = this._buffer[key] ? ACTION_UPDATE : ACTION_ADD
} else if (action === 'remove') { } else if (action === ACTION_REMOVE) {
if (this._buffer[key] === 'add') { if (this._buffer[key] === ACTION_ADD) {
delete this._buffer[key] delete this._buffer[key]
} else { } else {
this._buffer[key] = 'remove' this._buffer[key] = ACTION_REMOVE
} }
} else { // update } else { // update
if (!this._buffer[key]) { if (!this._buffer[key]) {
this._buffer[key] = 'update' this._buffer[key] = ACTION_UPDATE
} }
} }
} }

View File

@ -4,6 +4,11 @@ import callback from 'lodash.callback'
import clearObject from './clear-object' import clearObject from './clear-object'
import isEmpty from './is-empty' import isEmpty from './is-empty'
import NotImplemented from './not-implemented' 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. // has not been emitted yet.
this._onAdd(collection.all) this._onAdd(collection.all)
collection.on('add', this._onAdd) collection.on(ACTION_ADD, this._onAdd)
collection.on('update', this._onUpdate) collection.on(ACTION_UPDATE, this._onUpdate)
collection.on('remove', this._onRemove) collection.on(ACTION_REMOVE, this._onRemove)
} }
_detachCollection (collection) { _detachCollection (collection) {
collection.removeListener('add', this._onAdd) collection.removeListener(ACTION_ADD, this._onAdd)
collection.removeListener('update', this._onUpdate) collection.removeListener(ACTION_UPDATE, this._onUpdate)
collection.removeListener('remove', this._onRemove) collection.removeListener(ACTION_REMOVE, this._onRemove)
clearObject(this._itemsByHash) clearObject(this._itemsByHash)
clearObject(this._keysToHash) clearObject(this._keysToHash)

View File

@ -3,6 +3,11 @@ import callback from 'lodash.callback'
import clearObject from './clear-object' import clearObject from './clear-object'
import NotImplemented from './not-implemented' 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. // has not been emitted yet.
this._onAdd(collection.all) this._onAdd(collection.all)
collection.on('add', this._onAdd) collection.on(ACTION_ADD, this._onAdd)
collection.on('update', this._onUpdate) collection.on(ACTION_UPDATE, this._onUpdate)
collection.on('remove', this._onRemove) collection.on(ACTION_REMOVE, this._onRemove)
} }
_detachCollection (collection) { _detachCollection (collection) {
collection.removeListener('add', this._onAdd) collection.removeListener(ACTION_ADD, this._onAdd)
collection.removeListener('update', this._onUpdate) collection.removeListener(ACTION_UPDATE, this._onUpdate)
collection.removeListener('remove', this._onRemove) collection.removeListener(ACTION_REMOVE, this._onRemove)
clearObject(this._itemByHash) clearObject(this._itemByHash)
clearObject(this._keysToHash) clearObject(this._keysToHash)

View File

@ -2,7 +2,11 @@ import bind from 'lodash.bind'
import createCallback from 'lodash.callback' import createCallback from 'lodash.callback'
import forEach from 'lodash.foreach' 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) this._onRemove = bind(this._onRemove, this)
// Register listeners. // Register listeners.
this._collection.on('add', this._onAdd) this._collection.on(ACTION_ADD, this._onAdd)
this._collection.on('update', this._onUpdate) this._collection.on(ACTION_UPDATE, this._onUpdate)
this._collection.on('remove', this._onRemove) this._collection.on(ACTION_REMOVE, this._onRemove)
} }
// This method is necessary to free the memory of the view if its // This method is necessary to free the memory of the view if its
// life span is shorter than the collection. // life span is shorter than the collection.
destroy () { destroy () {
this._collection.removeListener('add', this._onAdd) this._collection.removeListener(ACTION_ADD, this._onAdd)
this._collection.removeListener('update', this._onUpdate) this._collection.removeListener(ACTION_UPDATE, this._onUpdate)
this._collection.removeListener('remove', this._onRemove) this._collection.removeListener(ACTION_REMOVE, this._onRemove)
} }
add () { add () {