slightly optimize stores

This commit is contained in:
hippoz 2022-08-04 04:59:28 +03:00
parent e1fccd2a66
commit dcef776d44
Signed by: hippoz
GPG key ID: 7C52899193467641

View file

@ -15,7 +15,6 @@ class Store {
// like subscribe, but without initially calling the handler // like subscribe, but without initially calling the handler
watch(handler) { watch(handler) {
const handlerIndex = this._handlers.push(handler) - 1; const handlerIndex = this._handlers.push(handler) - 1;
storeLog(`(${this.name}) (watch/handler) New handler`, this.value);
return () => { return () => {
this._handlers.splice(handlerIndex, 1); this._handlers.splice(handlerIndex, 1);
}; };
@ -23,7 +22,7 @@ class Store {
subscribe(handler) { subscribe(handler) {
const handlerIndex = this._handlers.push(handler) - 1; const handlerIndex = this._handlers.push(handler) - 1;
storeLog(`(${this.name}) (subscribe/initial) Calling handler`, this.value); storeLog(`(${this.name}) (subscribe/initial)`, this.value);
handler(this.value); handler(this.value);
return () => { return () => {
this._handlers.splice(handlerIndex, 1); this._handlers.splice(handlerIndex, 1);
@ -60,7 +59,7 @@ class ChannelsStore extends Store {
gateway.subscribe(GatewayEventType.Ready, ({ channels }) => { gateway.subscribe(GatewayEventType.Ready, ({ channels }) => {
this.value = channels; this.value = channels;
if (getItem("ui:stateful:presistSelectedChannel")) { if (getItem("ui:stateful:presistSelectedChannel")) {
selectedChannel.set({ id: getItem("state:openChannelId"), name: "none", creator_id: -1 }); selectedChannel.value.id = getItem("state:openChannelId");
} }
if (channels.length >= 1) { if (channels.length >= 1) {
if (!selectedChannel.value || selectedChannel.value.id === -1) { if (!selectedChannel.value || selectedChannel.value.id === -1) {
@ -119,15 +118,9 @@ class ChannelsStore extends Store {
class GatewayStatusStore extends Store { class GatewayStatusStore extends Store {
constructor() { constructor() {
super({ open: gateway.open, ready: gateway.authenticated }, "GatewayStatusStore"); super({ ready: gateway.authenticated }, "GatewayStatusStore");
gateway.subscribe(GatewayEventType.Open, () => {
this.value.open = true;
this.updated();
});
gateway.subscribe(GatewayEventType.Close, () => { gateway.subscribe(GatewayEventType.Close, () => {
this.value.open = false;
this.value.ready = false; this.value.ready = false;
this.updated(); this.updated();
}); });