Skip to content

Commit

Permalink
feat: Update code
Browse files Browse the repository at this point in the history
  • Loading branch information
nuintun committed Jun 12, 2024
1 parent 9da5a82 commit 3152484
Showing 1 changed file with 5 additions and 13 deletions.
18 changes: 5 additions & 13 deletions app/js/hooks/useStateMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ export interface StateOptions<C, S extends string, E extends string> {
on?: {
[key in E]?: Transition<C, S, E>;
};
effect?: (send: Send<E>, update: Update<C>) => Destructor;
effect?: (send: Send<E>, update: Update<C>) => EffectReturn;
}

type Destructor = void | (() => void) | Promise<void | (() => void)>;
type EffectReturn = void | (() => void) | Promise<void | (() => void)>;

type Action<C, E extends string> = UpdateAction<C> | TransitionAction<E>;

Expand Down Expand Up @@ -222,21 +222,13 @@ export default function useStateMachine<C = undefined, S extends string = string
const returned = (async () => {
const { effect } = options.states[state.value];

try {
return await effect?.(send, update);
} catch (error) {
console.error(error);
}
return await effect?.(send, update);
})();

return () => {
returned.then(exit => {
returned.then(async exit => {
if (isFunction(exit)) {
try {
exit();
} catch (error) {
console.error(error);
}
await exit();
}
});
};
Expand Down

0 comments on commit 3152484

Please sign in to comment.