13
0

WS: improve Observable implementation

This commit is contained in:
Luciano Iam 2020-06-14 12:18:21 +02:00 committed by Robin Gareus
parent 842b989e19
commit 1369076c14
Signed by: rgareus
GPG Key ID: A090BCE02CF57F04
5 changed files with 28 additions and 25 deletions

View File

@ -30,8 +30,12 @@ export class Component extends Observable {
return this._channel; return this._channel;
} }
on (property, callback) { on (event, callback) {
this.addObserver(property, (self) => callback(self[property])); this.addObserver(event, callback);
}
notify (property) {
this.notifyObservers(property, this['_' + property]);
} }
send (node, addr, val) { send (node, addr, val) {
@ -48,7 +52,7 @@ export class Component extends Observable {
updateLocal (property, value) { updateLocal (property, value) {
this['_' + property] = value; this['_' + property] = value;
this.notifyObservers(property); this.notify(property);
} }
updateRemote (property, value, node, addr) { updateRemote (property, value, node, addr) {

View File

@ -22,41 +22,40 @@ export default class Observable {
this._observers = {}; this._observers = {};
} }
addObserver (property, observer) { addObserver (event, observer) {
// property=undefined means the caller is interested in observing all properties // event=undefined means the caller is interested in observing all events
if (!(property in this._observers)) { if (!(event in this._observers)) {
this._observers[property] = []; this._observers[event] = [];
} }
this._observers[property].push(observer); this._observers[event].push(observer);
} }
removeObserver (property, observer) { removeObserver (event, observer) {
// property=undefined means the caller is not interested in any property anymore // event=undefined means the caller is not interested in any event anymore
if (typeof(property) == 'undefined') { if (typeof(event) == 'undefined') {
for (const property in this._observers) { for (const event in this._observers) {
this.removeObserver(property, observer); this.removeObserver(event, observer);
} }
} else { } else {
const index = this._observers[property].indexOf(observer); const index = this._observers[event].indexOf(observer);
if (index > -1) { if (index > -1) {
this._observers[property].splice(index, 1); this._observers[event].splice(index, 1);
} }
} }
} }
notifyObservers (property) { notifyObservers (event, ...args) {
// always notify observers that observe all properties // always notify observers that observe all events
if (undefined in this._observers) { if (undefined in this._observers) {
for (const observer of this._observers[undefined]) { for (const observer of this._observers[undefined]) {
observer(this, property); observer(event, ...args);
} }
} }
if (property in this._observers) { if (event in this._observers) {
for (const observer of this._observers[property]) { for (const observer of this._observers[event]) {
observer(this); observer(...args);
} }
} }
} }

View File

@ -45,7 +45,7 @@ export default class Mixer extends Component {
if (node.startsWith('strip')) { if (node.startsWith('strip')) {
if (node == StateNode.STRIP_DESCRIPTION) { if (node == StateNode.STRIP_DESCRIPTION) {
this._strips[addr] = new Strip(this, addr, val); this._strips[addr] = new Strip(this, addr, val);
this.notifyObservers('strips'); this.notify('strips');
return true; return true;
} else { } else {
const stripAddr = [addr[0]]; const stripAddr = [addr[0]];

View File

@ -53,7 +53,7 @@ export default class Plugin extends AddressableComponent {
if (node.startsWith('strip_plugin_param')) { if (node.startsWith('strip_plugin_param')) {
if (node == StateNode.STRIP_PLUGIN_PARAM_DESCRIPTION) { if (node == StateNode.STRIP_PLUGIN_PARAM_DESCRIPTION) {
this._parameters[addr] = new Parameter(this, addr, val); this._parameters[addr] = new Parameter(this, addr, val);
this.notifyObservers('parameters'); this.notify('parameters');
return true; return true;
} else { } else {
if (addr in this._parameters) { if (addr in this._parameters) {

View File

@ -84,7 +84,7 @@ export default class Strip extends AddressableComponent {
if (node.startsWith('strip_plugin')) { if (node.startsWith('strip_plugin')) {
if (node == StateNode.STRIP_PLUGIN_DESCRIPTION) { if (node == StateNode.STRIP_PLUGIN_DESCRIPTION) {
this._plugins[addr] = new Plugin(this, addr, val); this._plugins[addr] = new Plugin(this, addr, val);
this.notifyObservers('plugins'); this.notify('plugins');
return true; return true;
} else { } else {
const pluginAddr = [addr[0], addr[1]]; const pluginAddr = [addr[0], addr[1]];