Adding onInsert interface to TypeSafeCollection class
This commit is contained in:
parent
74d8cba5be
commit
594a8b1241
@ -20,6 +20,7 @@ export class TypeSafeCollection {
|
||||
constructor() {
|
||||
this._operationCount = new ReactiveVar(MIN_COUNT);
|
||||
this._elementList = [];
|
||||
this._handlers = Object.create(null);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -44,10 +45,37 @@ export class TypeSafeCollection {
|
||||
return this._elements(silent).find(item => item.id === id);
|
||||
}
|
||||
|
||||
_trigger(event, data) {
|
||||
let handlers = this._handlers;
|
||||
if (event in handlers) {
|
||||
handlers = handlers[event];
|
||||
if (!(handlers instanceof Array)) {
|
||||
return;
|
||||
}
|
||||
for (let i = 0, limit = handlers.length; i < limit; ++i) {
|
||||
let handler = handlers[i];
|
||||
if (_isFunction(handler)) {
|
||||
handler.call(null, data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Public Methods
|
||||
*/
|
||||
|
||||
onInsert(callback) {
|
||||
if (_isFunction(callback)) {
|
||||
let handlers = this._handlers.insert;
|
||||
if (!(handlers instanceof Array)) {
|
||||
handlers = [];
|
||||
this._handlers.insert = handlers;
|
||||
}
|
||||
handlers.push(callback);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the payload associated with the given ID to be the new supplied payload.
|
||||
* @param {string} id The ID of the entry that will be updated.
|
||||
@ -105,6 +133,7 @@ export class TypeSafeCollection {
|
||||
id = Random.id();
|
||||
this._elements(true).push({ id, payload });
|
||||
this._invalidate();
|
||||
this._trigger('insert', { id, data: payload });
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user