- commit
- eb28818
- parent
- 6fd92f8
- author
- Eric Bower
- date
- 2024-10-03 13:50:21 +0000 UTC
fix: unregister a thunk when the operation shuts down
2 files changed,
+31,
-2
+5,
-1
1@@ -1,6 +1,6 @@
2 import { ActionContext, API_ACTION_PREFIX, takeEvery } from "../action.ts";
3 import { compose } from "../compose.ts";
4-import { Callable, Ok, Operation, Signal } from "../deps.ts";
5+import { Callable, ensure, Ok, Operation, Signal } from "../deps.ts";
6 import { keepAlive, supervise } from "../fx/mod.ts";
7 import { createKey } from "./create-key.ts";
8 import { isFn, isObject } from "./util.ts";
9@@ -260,6 +260,10 @@ export function createThunks<Ctx extends ThunkCtx = ThunkCtx<any>>(
10 hasRegistered = true;
11 signal = yield* ActionContext;
12
13+ yield* ensure(function* () {
14+ hasRegistered = false;
15+ });
16+
17 // Register any thunks created after signal is available
18 yield* keepAlive(Object.values(visors));
19
+26,
-1
1@@ -8,7 +8,6 @@ import {
2 } from "../mod.ts";
3 import { createStore, updateStore } from "../store/mod.ts";
4 import { assertLike, asserts, describe, it } from "../test.ts";
5-
6 import type { Next, ThunkCtx } from "../mod.ts";
7
8 // deno-lint-ignore no-explicit-any
9@@ -600,3 +599,29 @@ it(
10 );
11 },
12 );
13+
14+it(
15+ tests,
16+ "should unregister the thunk when the registration function exits",
17+ async () => {
18+ const api1 = createThunks<RoboCtx>();
19+ api1.use(api1.routes());
20+
21+ const store = createStore({ initialState: {} });
22+ const task = store.run(api1.register);
23+ await task.halt();
24+ store.run(api1.register);
25+
26+ let acc = "";
27+ const action = api1.create("/users", function* () {
28+ acc += "b";
29+ });
30+ store.dispatch(action());
31+
32+ asserts.assertEquals(
33+ acc,
34+ "b",
35+ "Expected 'b' after first API call, but got: " + acc,
36+ );
37+ },
38+);