From 594a8b1241649714f5a8a83f1e98d8d964dab154 Mon Sep 17 00:00:00 2001 From: "Emanuel F. Oliveira" Date: Fri, 28 Apr 2017 07:35:00 -0300 Subject: [PATCH] Adding onInsert interface to TypeSafeCollection class --- .../client/lib/classes/TypeSafeCollection.js | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/Packages/ohif-viewerbase/client/lib/classes/TypeSafeCollection.js b/Packages/ohif-viewerbase/client/lib/classes/TypeSafeCollection.js index 64ec0f217..556be08f8 100644 --- a/Packages/ohif-viewerbase/client/lib/classes/TypeSafeCollection.js +++ b/Packages/ohif-viewerbase/client/lib/classes/TypeSafeCollection.js @@ -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; }