First time asking a question here so please be gentle.
I m attempting to set a specific object within a store with a reference to a callback, but rather than only setting that specific object s callback, it s setting the callback for all objects along a similar path in the store.
Relevant function:
const registerActionCallback = (
on: Use | Combo ,
actionName: string,
cbKey: string,
cb: (fns: ControllerActionFns) => void
) => {
setState(
actionPool , // Record<string, Action>
actionName, // string
`on${on}Callbacks`, // Record<string, fn>, property of Action object
cbKey,
(_: (fns: ControllerActionFns) => void) => cb
);
};
Example State:
const [state, setState] = createStore({
actionPool: {
A: {...},
...,
M: {
...,
onUseCallbacks: {} // initially empty
onComboCallbacks: {} // initially empty
...,
},
...,
Z: {...},
},
...,
});
The idea is I have a series of actions (buttons) that, when pressed, should run a series of callbacks that are located in either onUseCallbacks or onComboCallbacks , which are objects to make deregistering convenient.
So, let s say I have actions A through Z, but I want to register callback testCB , which logs hello , with action M on Use.
The function will then run: setState( actionPool , M , onUseCallbacks , testCB , () => () => console.log( hello ))
The obvious expectation is that only clicking the button tied to action M would log hello , except, clicking any button tied to any action in the actionPool A through Z will log hello .
I can t for the life of me figure out what would be causing this so any insight would be greatly appreciated. Never experienced this issue despite using setStore in similar ways constantly, although this may be the first time I ve tried setting callbacks using setStore.
Edit:
The way the callbacks are being called:
A reference to the pressed action is passed into a fireAction
function which runs:
for (const key in action.onUseCallbacks)
action.onUseCallbacks[key]();