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;
}
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) {

View File

@ -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);
}
}
}

View File

@ -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]];

View File

@ -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) {

View File

@ -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]];