2022-04-17 20:50:04 +03:00
|
|
|
import { writable } from "svelte/store";
|
2022-04-17 04:08:40 +03:00
|
|
|
import gateway, { GatewayEventType } from "./gateway";
|
|
|
|
|
2022-04-17 20:23:20 +03:00
|
|
|
class Store {
|
|
|
|
constructor() {
|
|
|
|
this._handlers = [];
|
|
|
|
}
|
|
|
|
|
2022-04-17 04:08:40 +03:00
|
|
|
subscribe(handler) {
|
|
|
|
const newLength = this._handlers.push(handler);
|
|
|
|
const handlerIndex = newLength - 1;
|
2022-04-19 02:17:24 +03:00
|
|
|
handler(this.value);
|
2022-04-17 04:08:40 +03:00
|
|
|
return () => {
|
|
|
|
this._handlers.splice(handlerIndex, 1);
|
|
|
|
};
|
2022-04-17 20:23:20 +03:00
|
|
|
}
|
|
|
|
|
2022-04-19 02:17:24 +03:00
|
|
|
updated() {
|
2022-04-17 04:08:40 +03:00
|
|
|
this._handlers.forEach(e => {
|
2022-04-19 02:17:24 +03:00
|
|
|
e(this.value);
|
2022-04-17 04:08:40 +03:00
|
|
|
});
|
2022-04-17 20:23:20 +03:00
|
|
|
}
|
|
|
|
}
|
2022-04-17 04:08:40 +03:00
|
|
|
|
2022-04-17 20:23:20 +03:00
|
|
|
class ChannelsStore extends Store {
|
|
|
|
constructor() {
|
|
|
|
super();
|
2022-04-17 04:08:40 +03:00
|
|
|
|
2022-04-19 02:17:24 +03:00
|
|
|
this.value = gateway.channels || [];
|
2022-04-17 04:08:40 +03:00
|
|
|
|
|
|
|
gateway.subscribe(GatewayEventType.Ready, ({ channels }) => {
|
2022-04-19 02:17:24 +03:00
|
|
|
this.value = channels;
|
|
|
|
this.updated();
|
2022-04-17 04:08:40 +03:00
|
|
|
});
|
|
|
|
gateway.subscribe(GatewayEventType.ChannelCreate, (channel) => {
|
2022-04-19 02:17:24 +03:00
|
|
|
this.value.push(channel);
|
|
|
|
this.updated();
|
2022-04-17 04:08:40 +03:00
|
|
|
});
|
|
|
|
gateway.subscribe(GatewayEventType.ChannelDelete, ({ id }) => {
|
2022-04-19 02:17:24 +03:00
|
|
|
const index = this.value.findIndex(e => e.id === id);
|
2022-04-19 02:19:24 +03:00
|
|
|
if (index === -1)
|
2022-04-17 04:08:40 +03:00
|
|
|
return;
|
|
|
|
|
2022-04-19 02:17:24 +03:00
|
|
|
this.value.splice(index, 1);
|
|
|
|
this.updated();
|
2022-04-17 04:08:40 +03:00
|
|
|
});
|
|
|
|
gateway.subscribe(GatewayEventType.ChannelUpdate, (data) => {
|
2022-04-19 02:17:24 +03:00
|
|
|
const index = this.value.findIndex(e => e.id === data.id);
|
2022-04-19 02:19:24 +03:00
|
|
|
if (index === -1)
|
2022-04-17 04:08:40 +03:00
|
|
|
return;
|
2022-04-19 02:17:24 +03:00
|
|
|
if (!this.value[index])
|
2022-04-17 04:08:40 +03:00
|
|
|
return;
|
|
|
|
|
2022-04-19 02:17:24 +03:00
|
|
|
this.value[index] = data;
|
|
|
|
this.updated();
|
2022-04-17 04:08:40 +03:00
|
|
|
});
|
|
|
|
}
|
2022-04-17 20:23:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
class GatewayStatusStore extends Store {
|
|
|
|
constructor() {
|
|
|
|
super();
|
2022-04-19 02:17:24 +03:00
|
|
|
this.value = { open: gateway.open, ready: gateway.authenticated };
|
2022-04-17 20:23:20 +03:00
|
|
|
|
|
|
|
gateway.subscribe(GatewayEventType.Open, () => {
|
2022-04-19 02:17:24 +03:00
|
|
|
this.value.open = true;
|
|
|
|
this.updated();
|
2022-04-17 20:23:20 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
gateway.subscribe(GatewayEventType.Close, () => {
|
2022-04-19 02:17:24 +03:00
|
|
|
this.value.open = false;
|
|
|
|
this.value.ready = false;
|
|
|
|
this.updated();
|
2022-04-17 20:23:20 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
gateway.subscribe(GatewayEventType.Ready, () => {
|
2022-04-19 02:17:24 +03:00
|
|
|
this.value.ready = true;
|
|
|
|
this.updated();
|
2022-04-17 20:23:20 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const channels = new ChannelsStore();
|
|
|
|
export const gatewayStatus = new GatewayStatusStore();
|
2022-04-17 20:50:04 +03:00
|
|
|
export const selectedChannel = writable({ id: -1, name: "none", creator_id: -1 });
|