frontend: simplify store code
This commit simplifies the store code by removing the _init method, and instead initializing everything on instantiation. This leads to not missing events and less complexity.
This commit is contained in:
parent
59145c3dc2
commit
abdaa80d1a
1 changed files with 23 additions and 47 deletions
|
@ -4,30 +4,20 @@ import gateway, { GatewayEventType } from "./gateway";
|
||||||
class Store {
|
class Store {
|
||||||
constructor() {
|
constructor() {
|
||||||
this._handlers = [];
|
this._handlers = [];
|
||||||
this._didInit = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
_tryInit() {
|
|
||||||
if (!this._didInit && this._init) {
|
|
||||||
this._init();
|
|
||||||
this._didInit = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
subscribe(handler) {
|
subscribe(handler) {
|
||||||
this._tryInit();
|
|
||||||
|
|
||||||
const newLength = this._handlers.push(handler);
|
const newLength = this._handlers.push(handler);
|
||||||
const handlerIndex = newLength - 1;
|
const handlerIndex = newLength - 1;
|
||||||
handler(this._value);
|
handler(this.value);
|
||||||
return () => {
|
return () => {
|
||||||
this._handlers.splice(handlerIndex, 1);
|
this._handlers.splice(handlerIndex, 1);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
_dispatchUpdate() {
|
updated() {
|
||||||
this._handlers.forEach(e => {
|
this._handlers.forEach(e => {
|
||||||
e(this._value);
|
e(this.value);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -36,47 +26,38 @@ class ChannelsStore extends Store {
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
this._value = [];
|
this.value = gateway.channels || [];
|
||||||
}
|
|
||||||
|
|
||||||
_init() {
|
|
||||||
const channels = gateway.channels;
|
|
||||||
if (channels) {
|
|
||||||
this._value = channels;
|
|
||||||
}
|
|
||||||
|
|
||||||
gateway.subscribe(GatewayEventType.Ready, ({ channels }) => {
|
gateway.subscribe(GatewayEventType.Ready, ({ channels }) => {
|
||||||
if (channels) {
|
this.value = channels;
|
||||||
this._value = channels;
|
|
||||||
}
|
|
||||||
|
|
||||||
this._dispatchUpdate();
|
this.updated();
|
||||||
});
|
});
|
||||||
gateway.subscribe(GatewayEventType.ChannelCreate, (channel) => {
|
gateway.subscribe(GatewayEventType.ChannelCreate, (channel) => {
|
||||||
this._value.push(channel);
|
this.value.push(channel);
|
||||||
|
|
||||||
this._dispatchUpdate();
|
this.updated();
|
||||||
});
|
});
|
||||||
gateway.subscribe(GatewayEventType.ChannelDelete, ({ id }) => {
|
gateway.subscribe(GatewayEventType.ChannelDelete, ({ id }) => {
|
||||||
const index = this._value.findIndex(e => e.id === id);
|
const index = this.value.findIndex(e => e.id === id);
|
||||||
if (!index)
|
if (!index)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
this._value.splice(index, 1);
|
this.value.splice(index, 1);
|
||||||
|
|
||||||
this._dispatchUpdate();
|
this.updated();
|
||||||
});
|
});
|
||||||
gateway.subscribe(GatewayEventType.ChannelUpdate, (data) => {
|
gateway.subscribe(GatewayEventType.ChannelUpdate, (data) => {
|
||||||
const index = this._value.findIndex(e => e.id === data.id);
|
const index = this.value.findIndex(e => e.id === data.id);
|
||||||
if (!index)
|
if (!index)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (!this._value[index])
|
if (!this.value[index])
|
||||||
return;
|
return;
|
||||||
|
|
||||||
this._value[index] = data;
|
this.value[index] = data;
|
||||||
|
|
||||||
this._dispatchUpdate();
|
this.updated();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -84,27 +65,22 @@ class ChannelsStore extends Store {
|
||||||
class GatewayStatusStore extends Store {
|
class GatewayStatusStore extends Store {
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
this._value = { open: false, ready: false };
|
this.value = { open: gateway.open, ready: gateway.authenticated };
|
||||||
}
|
|
||||||
|
|
||||||
_init() {
|
|
||||||
this._value.open = gateway.open;
|
|
||||||
this._value.ready = gateway.authenticated;
|
|
||||||
|
|
||||||
gateway.subscribe(GatewayEventType.Open, () => {
|
gateway.subscribe(GatewayEventType.Open, () => {
|
||||||
this._value.open = true;
|
this.value.open = true;
|
||||||
this._dispatchUpdate();
|
this.updated();
|
||||||
});
|
});
|
||||||
|
|
||||||
gateway.subscribe(GatewayEventType.Close, () => {
|
gateway.subscribe(GatewayEventType.Close, () => {
|
||||||
this._value.open = false;
|
this.value.open = false;
|
||||||
this._value.ready = false;
|
this.value.ready = false;
|
||||||
this._dispatchUpdate();
|
this.updated();
|
||||||
});
|
});
|
||||||
|
|
||||||
gateway.subscribe(GatewayEventType.Ready, () => {
|
gateway.subscribe(GatewayEventType.Ready, () => {
|
||||||
this._value.ready = true;
|
this.value.ready = true;
|
||||||
this._dispatchUpdate();
|
this.updated();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue