-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the useBroadcastStore. It will give us the ability to easily broadcast actions to all connected clients. In this case, we requery the doc to everyone when a change relative to the doc rights is made.
- Loading branch information
Showing
9 changed files
with
120 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './useBroadcastStore'; | ||
export * from './useResponsiveStore'; |
54 changes: 54 additions & 0 deletions
54
src/frontend/apps/impress/src/stores/useBroadcastStore.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { HocuspocusProvider } from '@hocuspocus/provider'; | ||
import * as Y from 'yjs'; | ||
import { create } from 'zustand'; | ||
|
||
interface BroadcastState { | ||
addTask: (taskLabel: string, action: () => void) => void; | ||
broadcast: (taskLabel: string) => void; | ||
getBroadcastProvider: () => HocuspocusProvider | undefined; | ||
provider?: HocuspocusProvider; | ||
setBroadcastProvider: (provider: HocuspocusProvider) => void; | ||
tasks: { [taskLabel: string]: Y.Array<string> }; | ||
} | ||
|
||
export const useBroadcastStore = create<BroadcastState>((set, get) => ({ | ||
provider: undefined, | ||
tasks: {}, | ||
setBroadcastProvider: (provider) => set({ provider }), | ||
getBroadcastProvider: () => { | ||
const provider = get().provider; | ||
if (!provider) { | ||
console.warn('Provider is not defined'); | ||
return; | ||
} | ||
|
||
return provider; | ||
}, | ||
addTask: (taskLabel, action) => { | ||
const taskExistAlready = get().tasks[taskLabel]; | ||
const provider = get().getBroadcastProvider(); | ||
if (taskExistAlready || !provider) { | ||
return; | ||
} | ||
|
||
const task = provider.document.getArray<string>(taskLabel); | ||
task.observe(() => { | ||
action(); | ||
}); | ||
|
||
set((state) => ({ | ||
tasks: { | ||
...state.tasks, | ||
[taskLabel]: task, | ||
}, | ||
})); | ||
}, | ||
broadcast: (taskLabel) => { | ||
const task = get().tasks[taskLabel]; | ||
if (!task) { | ||
console.warn(`Task ${taskLabel} is not defined`); | ||
return; | ||
} | ||
task.push([`broadcast: ${taskLabel}`]); | ||
}, | ||
})); |