flatten actions

This commit is contained in:
Peter Clement 2025-01-03 11:31:48 +00:00
parent 9c7beeeeaf
commit 8a4f42003c
2 changed files with 8 additions and 21 deletions

View File

@ -3,31 +3,18 @@ import { BudiStore } from "../BudiStore"
import { UIUser } from "@budibase/types"
export class UserStore extends BudiStore<UIUser[]> {
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<UIUser[]> {
}
reset() {
this.store.set([])
this.set([])
}
}

View File

@ -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(