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" import { UIUser } from "@budibase/types"
export class UserStore extends BudiStore<UIUser[]> { export class UserStore extends BudiStore<UIUser[]> {
actions: {
init: (users: UIUser[]) => void
updateUser: (user: UIUser) => void
removeUser: (sessionId: string) => void
reset: () => void
}
constructor() { constructor() {
super([]) 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[]) { init(users: UIUser[]) {
this.store.set(users) this.set(users)
} }
updateUser(user: UIUser) { updateUser(user: UIUser) {
const $users = get(this.store) const $users = get(this)
if (!$users.some(x => x.sessionId === user.sessionId)) { if (!$users.some(x => x.sessionId === user.sessionId)) {
this.store.set([...$users, user]) this.set([...$users, user])
} else { } else {
this.update(state => { this.update(state => {
const index = state.findIndex(x => x.sessionId === user.sessionId) const index = state.findIndex(x => x.sessionId === user.sessionId)
@ -44,7 +31,7 @@ export class UserStore extends BudiStore<UIUser[]> {
} }
reset() { reset() {
this.store.set([]) this.set([])
} }
} }

View File

@ -27,7 +27,7 @@ export const createBuilderWebsocket = (appId: string) => {
BuilderSocketEvent.SelectApp, BuilderSocketEvent.SelectApp,
{ appId }, { appId },
({ users }: { users: UIUser[] }) => { ({ 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) console.error("Failed to connect to builder websocket:", err.message)
}) })
socket.on("disconnect", () => { socket.on("disconnect", () => {
userStore.actions.reset() userStore.reset()
}) })
// User events // User events
socket.onOther(SocketEvent.UserUpdate, ({ user }: { user: UIUser }) => { socket.onOther(SocketEvent.UserUpdate, ({ user }: { user: UIUser }) => {
userStore.actions.updateUser(user) userStore.updateUser(user)
}) })
socket.onOther( socket.onOther(
SocketEvent.UserDisconnect, SocketEvent.UserDisconnect,
({ sessionId }: { sessionId: string }) => { ({ sessionId }: { sessionId: string }) => {
userStore.actions.removeUser(sessionId) userStore.removeUser(sessionId)
} }
) )
socket.onOther( socket.onOther(