Skip to content

Commit

Permalink
fix: fix event connections
Browse files Browse the repository at this point in the history
  • Loading branch information
paradoxuum committed Dec 24, 2023
1 parent be7eb5a commit 32cec62
Showing 1 changed file with 17 additions and 24 deletions.
41 changes: 17 additions & 24 deletions src/client/interface/providers/commanderProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Roact, { createContext, useEffect, useState } from "@rbxts/roact";
import { useEventListener, useMountEffect } from "@rbxts/pretty-react-hooks";
import Roact, { createContext, useState } from "@rbxts/roact";
import { copyDeep } from "@rbxts/sift/out/Dictionary";
import { CommandOptions, CommandPath, GroupOptions } from "../../../shared";
import { DEFAULT_OPTIONS } from "../../options";
Expand Down Expand Up @@ -54,7 +55,7 @@ export function CommanderProvider({ value, children }: CommanderProviderProps) {
);
const [groups, setGroups] = useState<Map<string, GroupOptions>>(new Map());

useEffect(() => {
useMountEffect(() => {
setStaticData({
addHistoryEntry: value.addHistoryEntry,
execute: value.execute,
Expand All @@ -64,31 +65,23 @@ export function CommanderProvider({ value, children }: CommanderProviderProps) {
setHistory(value.initialData.history);
setCommands(value.initialData.commands);
setGroups(value.initialData.groups);
});

const historyConnection = value.events.historyUpdated.Connect((entries) => {
setHistory(copyDeep(entries));
});

const commandConnection = value.events.commandAdded.Connect(
(key, command) => {
const newData = copyDeep(commands);
newData.set(key, command);
setCommands(commands);
},
);
useEventListener(value.events.historyUpdated, (entries) => {
setHistory(copyDeep(entries));
});

const groupConnection = value.events.groupAdded.Connect((key, group) => {
const newData = copyDeep(groups);
groups.set(key, group);
setGroups(newData);
});
useEventListener(value.events.commandAdded, (key, command) => {
const newData = copyDeep(commands);
newData.set(key, command);
setCommands(newData);
});

return () => {
historyConnection.Disconnect();
commandConnection.Disconnect();
groupConnection.Disconnect();
};
}, []);
useEventListener(value.events.groupAdded, (key, group) => {
const newData = copyDeep(groups);
groups.set(key, group);
setGroups(newData);
});

return (
<CommanderContext.Provider
Expand Down

0 comments on commit 32cec62

Please sign in to comment.