Skip to content

Commit

Permalink
chore: cleanup thunk registry fix
Browse files Browse the repository at this point in the history
  • Loading branch information
neurosnap committed Oct 2, 2024
1 parent f19888b commit addefe4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
4 changes: 2 additions & 2 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 8 additions & 12 deletions query/thunk.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ActionContext, API_ACTION_PREFIX, takeEvery } from "../action.ts";
import { compose } from "../compose.ts";
import { Callable, Ok, Operation, Signal, spawn } from "../deps.ts";
import { Callable, Ok, Operation, Signal } from "../deps.ts";
import { keepAlive, supervise } from "../fx/mod.ts";
import { createKey } from "./create-key.ts";
import { isFn, isObject } from "./util.ts";
Expand Down Expand Up @@ -131,11 +131,10 @@ export function createThunks<Ctx extends ThunkCtx = ThunkCtx<any>>(
const actionMap: {
[key: string]: CreateActionWithPayload<Ctx, any>;
} = {};

const thunkId = `${Date.now().toString(36)}-${
Math.random().toString(36).substring(2, 11)
}`;
const thunkRegistry: Record<string, boolean> = { [thunkId]: false };
let hasRegistered = false;

function* defaultMiddleware(_: Ctx, next: Next) {
yield* next();
Expand Down Expand Up @@ -254,24 +253,21 @@ export function createThunks<Ctx extends ThunkCtx = ThunkCtx<any>>(
}

function* register() {
if (thunkRegistry?.[thunkId] === true) {
if (hasRegistered) {
console.warn("This thunk instance is already registered.");
return;
}
thunkRegistry[thunkId] = true;
hasRegistered = true;
signal = yield* ActionContext;

// Register any thunks created after signal is available
yield* keepAlive(Object.values(visors));

// Spawn a watcher for further thunk matchingPairs
const task = yield* spawn(function* () {
yield* takeEvery(
`${API_ACTION_PREFIX}REGISTER_THUNK_${thunkId}`,
watcher as any,
);
});
yield* task;
yield* takeEvery(
`${API_ACTION_PREFIX}REGISTER_THUNK_${thunkId}`,
watcher as any,
);
}

function routes() {
Expand Down

0 comments on commit addefe4

Please sign in to comment.