Skip to content

Commit

Permalink
♻️(frontend) update already existing tasks
Browse files Browse the repository at this point in the history
When a task was already existing, we were not
updating it. This commit fixes that.
  • Loading branch information
AntoLC committed Dec 23, 2024
1 parent 8ab0609 commit 375418d
Showing 1 changed file with 42 additions and 8 deletions.
50 changes: 42 additions & 8 deletions src/frontend/apps/impress/src/stores/useBroadcastStore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,20 @@ interface BroadcastState {
getBroadcastProvider: () => HocuspocusProvider | undefined;
provider?: HocuspocusProvider;
setBroadcastProvider: (provider: HocuspocusProvider) => void;
tasks: { [taskLabel: string]: Y.Array<string> };
setTask: (
taskLabel: string,
task: Y.Array<string>,
action: () => void,
) => void;
tasks: {
[taskLabel: string]: {
task: Y.Array<string>;
observer: (
event: Y.YArrayEvent<string>,
transaction: Y.Transaction,
) => void;
};
};
}

export const useBroadcastStore = create<BroadcastState>((set, get) => ({
Expand All @@ -25,26 +38,47 @@ export const useBroadcastStore = create<BroadcastState>((set, get) => ({
return provider;
},
addTask: (taskLabel, action) => {
const taskExistAlready = get().tasks[taskLabel];
const provider = get().getBroadcastProvider();
if (taskExistAlready || !provider) {
if (!provider) {
return;
}

const existingTask = get().tasks[taskLabel];
if (existingTask) {
existingTask.task.unobserve(existingTask.observer);
get().setTask(taskLabel, existingTask.task, action);
return;
}

const task = provider.document.getArray<string>(taskLabel);
task.observe(() => {
action();
});
get().setTask(taskLabel, task, action);
},
setTask: (taskLabel: string, task: Y.Array<string>, action: () => void) => {
let isInitializing = true;
const observer = () => {
if (!isInitializing) {
action();
}
};

task.observe(observer);

setTimeout(() => {
isInitializing = false;
}, 1000);

set((state) => ({
tasks: {
...state.tasks,
[taskLabel]: task,
[taskLabel]: {
task,
observer,
},
},
}));
},
broadcast: (taskLabel) => {
const task = get().tasks[taskLabel];
const { task } = get().tasks[taskLabel];
if (!task) {
console.warn(`Task ${taskLabel} is not defined`);
return;
Expand Down

0 comments on commit 375418d

Please sign in to comment.