mirror of
https://github.com/hay-kot/homebox.git
synced 2025-08-03 00:00:27 +00:00
refactor event system to use composables
This commit is contained in:
parent
884e2c700e
commit
c5c9da6a7e
2 changed files with 80 additions and 24 deletions
67
frontend/composables/use-server-events.ts
Normal file
67
frontend/composables/use-server-events.ts
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
export enum ServerEvent {
|
||||||
|
LocationMutation = "location.mutation",
|
||||||
|
ItemMutation = "item.mutation",
|
||||||
|
LabelMutation = "label.mutation",
|
||||||
|
}
|
||||||
|
|
||||||
|
export type EventMessage = {
|
||||||
|
event: ServerEvent;
|
||||||
|
};
|
||||||
|
|
||||||
|
let socket: WebSocket | null = null;
|
||||||
|
|
||||||
|
const listeners = new Map<ServerEvent, (() => void)[]>();
|
||||||
|
|
||||||
|
function connect(onmessage: (m: EventMessage) => void) {
|
||||||
|
const ws = new WebSocket(`ws://${window.location.host}/api/v1/ws/events`);
|
||||||
|
|
||||||
|
ws.onopen = () => {
|
||||||
|
console.debug("connected to server");
|
||||||
|
};
|
||||||
|
|
||||||
|
ws.onclose = () => {
|
||||||
|
console.debug("disconnected from server");
|
||||||
|
setTimeout(() => {
|
||||||
|
connect(onmessage);
|
||||||
|
}, 3000);
|
||||||
|
};
|
||||||
|
|
||||||
|
ws.onerror = err => {
|
||||||
|
console.error("websocket error", err);
|
||||||
|
};
|
||||||
|
|
||||||
|
ws.onmessage = msg => {
|
||||||
|
onmessage(JSON.parse(msg.data));
|
||||||
|
};
|
||||||
|
|
||||||
|
socket = ws;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function onServerEvent(event: ServerEvent, callback: () => void) {
|
||||||
|
if (socket === null) {
|
||||||
|
connect(e => {
|
||||||
|
listeners.get(e.event)?.forEach(c => c());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
if (!listeners.has(event)) {
|
||||||
|
listeners.set(event, []);
|
||||||
|
}
|
||||||
|
listeners.get(event)?.push(callback);
|
||||||
|
});
|
||||||
|
|
||||||
|
onUnmounted(() => {
|
||||||
|
const got = listeners.get(event);
|
||||||
|
if (got) {
|
||||||
|
listeners.set(
|
||||||
|
event,
|
||||||
|
got.filter(c => c !== callback)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (listeners.get(event)?.length === 0) {
|
||||||
|
listeners.delete(event);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
|
@ -178,31 +178,20 @@
|
||||||
|
|
||||||
const locationStore = useLocationStore();
|
const locationStore = useLocationStore();
|
||||||
|
|
||||||
type EventMessage = {
|
onServerEvent(ServerEvent.LabelMutation, () => {
|
||||||
event: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
onMounted(() => {
|
|
||||||
const ws = new WebSocket(`ws://${window.location.host}/api/v1/ws/events`);
|
|
||||||
ws.onmessage = event => {
|
|
||||||
const msg: EventMessage = JSON.parse(event.data);
|
|
||||||
console.debug("recieved event", msg);
|
|
||||||
switch (msg.event) {
|
|
||||||
case "label.mutation":
|
|
||||||
labelStore.refresh();
|
labelStore.refresh();
|
||||||
break;
|
});
|
||||||
case "location.mutation":
|
|
||||||
|
onServerEvent(ServerEvent.LocationMutation, () => {
|
||||||
locationStore.refreshChildren();
|
locationStore.refreshChildren();
|
||||||
locationStore.refreshParents();
|
locationStore.refreshParents();
|
||||||
break;
|
});
|
||||||
case "item.mutation":
|
|
||||||
|
onServerEvent(ServerEvent.ItemMutation, () => {
|
||||||
// item mutations can affect locations counts
|
// item mutations can affect locations counts
|
||||||
// so we need to refresh those as well
|
// so we need to refresh those as well
|
||||||
locationStore.refreshChildren();
|
locationStore.refreshChildren();
|
||||||
locationStore.refreshParents();
|
locationStore.refreshParents();
|
||||||
break;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const authCtx = useAuthContext();
|
const authCtx = useAuthContext();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue