From 8a4f42003c8702841ebc15a28884ec39b77abd04 Mon Sep 17 00:00:00 2001 From: Peter Clement Date: Fri, 3 Jan 2025 11:31:48 +0000 Subject: [PATCH] flatten actions --- packages/builder/src/stores/builder/users.ts | 21 ++++--------------- .../builder/src/stores/builder/websocket.ts | 8 +++---- 2 files changed, 8 insertions(+), 21 deletions(-) diff --git a/packages/builder/src/stores/builder/users.ts b/packages/builder/src/stores/builder/users.ts index 5f0c4a3701..5350b2af90 100644 --- a/packages/builder/src/stores/builder/users.ts +++ b/packages/builder/src/stores/builder/users.ts @@ -3,31 +3,18 @@ import { BudiStore } from "../BudiStore" import { UIUser } from "@budibase/types" export class UserStore extends BudiStore { - actions: { - init: (users: UIUser[]) => void - updateUser: (user: UIUser) => void - removeUser: (sessionId: string) => void - reset: () => void - } - constructor() { super([]) - this.actions = { - init: this.init.bind(this), - updateUser: this.updateUser.bind(this), - removeUser: this.removeUser.bind(this), - reset: this.reset.bind(this), - } } init(users: UIUser[]) { - this.store.set(users) + this.set(users) } updateUser(user: UIUser) { - const $users = get(this.store) + const $users = get(this) if (!$users.some(x => x.sessionId === user.sessionId)) { - this.store.set([...$users, user]) + this.set([...$users, user]) } else { this.update(state => { const index = state.findIndex(x => x.sessionId === user.sessionId) @@ -44,7 +31,7 @@ export class UserStore extends BudiStore { } reset() { - this.store.set([]) + this.set([]) } } diff --git a/packages/builder/src/stores/builder/websocket.ts b/packages/builder/src/stores/builder/websocket.ts index 49b8067dd8..bd9e2c8d4d 100644 --- a/packages/builder/src/stores/builder/websocket.ts +++ b/packages/builder/src/stores/builder/websocket.ts @@ -27,7 +27,7 @@ export const createBuilderWebsocket = (appId: string) => { BuilderSocketEvent.SelectApp, { appId }, ({ users }: { users: UIUser[] }) => { - userStore.actions.init(users) + userStore.init(users) } ) }) @@ -35,17 +35,17 @@ export const createBuilderWebsocket = (appId: string) => { console.error("Failed to connect to builder websocket:", err.message) }) socket.on("disconnect", () => { - userStore.actions.reset() + userStore.reset() }) // User events socket.onOther(SocketEvent.UserUpdate, ({ user }: { user: UIUser }) => { - userStore.actions.updateUser(user) + userStore.updateUser(user) }) socket.onOther( SocketEvent.UserDisconnect, ({ sessionId }: { sessionId: string }) => { - userStore.actions.removeUser(sessionId) + userStore.removeUser(sessionId) } ) socket.onOther(