From 1369076c1403316a405c92ee8c3b7d882834c23c Mon Sep 17 00:00:00 2001 From: Luciano Iam Date: Sun, 14 Jun 2020 12:18:21 +0200 Subject: [PATCH] WS: improve Observable implementation --- share/web_surfaces/shared/base/component.js | 10 +++-- share/web_surfaces/shared/base/observable.js | 37 +++++++++---------- share/web_surfaces/shared/components/mixer.js | 2 +- .../web_surfaces/shared/components/plugin.js | 2 +- share/web_surfaces/shared/components/strip.js | 2 +- 5 files changed, 28 insertions(+), 25 deletions(-) diff --git a/share/web_surfaces/shared/base/component.js b/share/web_surfaces/shared/base/component.js index 6e228caf46..1273338dec 100644 --- a/share/web_surfaces/shared/base/component.js +++ b/share/web_surfaces/shared/base/component.js @@ -30,8 +30,12 @@ export class Component extends Observable { return this._channel; } - on (property, callback) { - this.addObserver(property, (self) => callback(self[property])); + on (event, callback) { + this.addObserver(event, callback); + } + + notify (property) { + this.notifyObservers(property, this['_' + property]); } send (node, addr, val) { @@ -48,7 +52,7 @@ export class Component extends Observable { updateLocal (property, value) { this['_' + property] = value; - this.notifyObservers(property); + this.notify(property); } updateRemote (property, value, node, addr) { diff --git a/share/web_surfaces/shared/base/observable.js b/share/web_surfaces/shared/base/observable.js index 1c1849e5a8..1707c973f9 100644 --- a/share/web_surfaces/shared/base/observable.js +++ b/share/web_surfaces/shared/base/observable.js @@ -22,41 +22,40 @@ export default class Observable { this._observers = {}; } - addObserver (property, observer) { - // property=undefined means the caller is interested in observing all properties - if (!(property in this._observers)) { - this._observers[property] = []; + addObserver (event, observer) { + // event=undefined means the caller is interested in observing all events + if (!(event in this._observers)) { + this._observers[event] = []; } - this._observers[property].push(observer); + this._observers[event].push(observer); } - removeObserver (property, observer) { - // property=undefined means the caller is not interested in any property anymore - if (typeof(property) == 'undefined') { - for (const property in this._observers) { - this.removeObserver(property, observer); + removeObserver (event, observer) { + // event=undefined means the caller is not interested in any event anymore + if (typeof(event) == 'undefined') { + for (const event in this._observers) { + this.removeObserver(event, observer); } } else { - const index = this._observers[property].indexOf(observer); - + const index = this._observers[event].indexOf(observer); if (index > -1) { - this._observers[property].splice(index, 1); + this._observers[event].splice(index, 1); } } } - notifyObservers (property) { - // always notify observers that observe all properties + notifyObservers (event, ...args) { + // always notify observers that observe all events if (undefined in this._observers) { for (const observer of this._observers[undefined]) { - observer(this, property); + observer(event, ...args); } } - if (property in this._observers) { - for (const observer of this._observers[property]) { - observer(this); + if (event in this._observers) { + for (const observer of this._observers[event]) { + observer(...args); } } } diff --git a/share/web_surfaces/shared/components/mixer.js b/share/web_surfaces/shared/components/mixer.js index 55bd4b2d5e..bcbaa82ca9 100644 --- a/share/web_surfaces/shared/components/mixer.js +++ b/share/web_surfaces/shared/components/mixer.js @@ -45,7 +45,7 @@ export default class Mixer extends Component { if (node.startsWith('strip')) { if (node == StateNode.STRIP_DESCRIPTION) { this._strips[addr] = new Strip(this, addr, val); - this.notifyObservers('strips'); + this.notify('strips'); return true; } else { const stripAddr = [addr[0]]; diff --git a/share/web_surfaces/shared/components/plugin.js b/share/web_surfaces/shared/components/plugin.js index 4240293d9b..2245d4006a 100644 --- a/share/web_surfaces/shared/components/plugin.js +++ b/share/web_surfaces/shared/components/plugin.js @@ -53,7 +53,7 @@ export default class Plugin extends AddressableComponent { if (node.startsWith('strip_plugin_param')) { if (node == StateNode.STRIP_PLUGIN_PARAM_DESCRIPTION) { this._parameters[addr] = new Parameter(this, addr, val); - this.notifyObservers('parameters'); + this.notify('parameters'); return true; } else { if (addr in this._parameters) { diff --git a/share/web_surfaces/shared/components/strip.js b/share/web_surfaces/shared/components/strip.js index c27076ff1e..4cc5eef3b9 100644 --- a/share/web_surfaces/shared/components/strip.js +++ b/share/web_surfaces/shared/components/strip.js @@ -84,7 +84,7 @@ export default class Strip extends AddressableComponent { if (node.startsWith('strip_plugin')) { if (node == StateNode.STRIP_PLUGIN_DESCRIPTION) { this._plugins[addr] = new Plugin(this, addr, val); - this.notifyObservers('plugins'); + this.notify('plugins'); return true; } else { const pluginAddr = [addr[0], addr[1]];