This commit is contained in:
Maze Winther
2026-01-15 11:05:17 +01:00
parent c19f085e48
commit deef784b76
182 changed files with 7787 additions and 18046 deletions
+53
View File
@@ -0,0 +1,53 @@
import type {
TAction,
TActionFunc,
TActionWithArgs,
TActionWithOptionalArgs,
TActionArgsMap,
TArgOfAction,
TInvocationTrigger,
TBoundActionList,
} from "./types";
const boundActions: TBoundActionList = {};
export function bindAction<A extends TAction>(
action: A,
handler: TActionFunc<A>,
) {
if (boundActions[action]) {
boundActions[action]?.push(handler);
} else {
boundActions[action] = [handler] as any;
}
}
export function unbindAction<A extends TAction>(
action: A,
handler: TActionFunc<A>,
) {
boundActions[action] = boundActions[action]?.filter(
(x) => x !== handler,
) as any;
if (boundActions[action]?.length === 0) {
delete boundActions[action];
}
}
type InvokeActionFunc = {
(
action: TActionWithOptionalArgs,
args?: undefined,
trigger?: TInvocationTrigger,
): void;
<A extends TActionWithArgs>(action: A, args: TActionArgsMap[A]): void;
};
export const invokeAction: InvokeActionFunc = <A extends TAction>(
action: A,
args?: TArgOfAction<A>,
trigger?: TInvocationTrigger,
) => {
boundActions[action]?.forEach((handler) => (handler as any)(args, trigger));
};