import { Users } from './users.svelte.js'; import { Channels } from './channels.svelte.js'; import { Messages } from './messages.svelte.js'; export class State { currentUser = $state(); users = $state(); channels = $state(); messages = $state(); static boot({ currentUser, heartbeat, users, channels, messages, resumePoint }) { return new State({ currentUser, heartbeat, users: Users.boot(users), channels: Channels.boot(channels), messages: Messages.boot(messages), resumePoint }); } constructor({ currentUser, heartbeat, users, channels, messages, resumePoint }) { this.currentUser = currentUser; this.heartbeat = heartbeat; this.users = users; this.channels = channels; this.messages = messages; this.resumePoint = resumePoint; } onEvent(event) { // Heartbeats are actually completely ignored here. They're handled in `Session`, but not as a // special case; _any_ event is a heartbeat event. switch (event.type) { case 'channel': return this.onChannelEvent(event); case 'user': return this.onUserEvent(event); case 'message': return this.onMessageEvent(event); } } onChannelEvent(event) { switch (event.event) { case 'created': return this.onChannelCreated(event); case 'deleted': return this.onChannelDeleted(event); } } onChannelCreated(event) { const { id, name } = event; this.channels.add({ id, name }); } onChannelDeleted(event) { const { id } = event; this.channels.remove(id); } onUserEvent(event) { switch (event.event) { case 'created': return this.onUserCreated(event); } } onUserCreated(event) { const { id, name } = event; this.users.add({ id, name }); } onMessageEvent(event) { switch (event.event) { case 'sent': return this.onMessageSent(event); case 'deleted': return this.onMessageDeleted(event); } } onMessageSent(event) { const { id, at, channel, sender, body } = event; this.messages.add({ id, at, channel, sender, body }); } onMessageDeleted(event) { const { id } = event; this.messages.remove(id); } }