Skip to content

Commit

Permalink
add skipAutoUpdate flag
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseditson committed Sep 13, 2024
1 parent 5b7c43b commit ffe1f3b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
18 changes: 13 additions & 5 deletions src/upd8.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ export type ImperativeUpd8Fn<State, Event> = <
upd8: (view: ViewType) => R
) => R | undefined;
export type Upd8<State, Event> = {
(state: State, eventHandler: (evt: Event) => void): (
state: State
) => Promise<void>;
(
state: State,
eventHandler: (evt: Event) => void,
skipAutoUpdate?: boolean
): (state: State) => Promise<void>;
imperative: ImperativeUpd8Fn<State, Event>;
teardown: () => void;
};
Expand Down Expand Up @@ -73,7 +75,11 @@ export const cre8 = <State, Event>(
}
config.didUpdate(state);
};
const initUpd8 = (state: State, eventHandler: (evt: Event) => void) => {
const initUpd8 = (
state: State,
eventHandler: (evt: Event) => void,
skipAutoUpdate = false
) => {
if (initialized) {
throw new Error("upd8 may only be initialized once.");
}
Expand All @@ -92,7 +98,9 @@ export const cre8 = <State, Event>(
}
views.set(view.id, view);
view.listen(eventHandler);
upd8(state);
if (!skipAutoUpdate) {
upd8(state);
}
}
return upd8;
};
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/tests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,10 @@ describe("mount", () => {
initUI.teardown();
dom.window.close();
});
test("it emits a helpful error message when a listener fails to mount", async () => {
test("it emits a helpful error message when a listener fails to mount", () => {
rejects(
async () => {
upd8 = initUI({}, (event) => {});
const upd8 = initUI({}, (event) => {}, true);
await upd8({});
},
{
Expand Down

0 comments on commit ffe1f3b

Please sign in to comment.