From d29d162920d6a5297304a5aa2cb945a691bd9f40 Mon Sep 17 00:00:00 2001 From: Marco Falkenberg Date: Thu, 26 Sep 2024 11:02:02 +0200 Subject: [PATCH] feat(Models): support dependencies for ListQueryModels --- packages/models/src/base/ListQueryModel.ts | 10 ++++++++-- packages/models/src/lib/joinedId.ts | 1 + packages/models/src/react/provideReact.ts | 13 ++++--------- 3 files changed, 13 insertions(+), 11 deletions(-) create mode 100644 packages/models/src/lib/joinedId.ts diff --git a/packages/models/src/base/ListQueryModel.ts b/packages/models/src/base/ListQueryModel.ts index 76c1fbb0..0a33ee22 100644 --- a/packages/models/src/base/ListQueryModel.ts +++ b/packages/models/src/base/ListQueryModel.ts @@ -1,11 +1,17 @@ import { hash } from "object-code"; +import { joinedId } from "../lib/joinedId.js"; + +interface Options { + dependencies?: string[]; +} export abstract class ListQueryModel { protected readonly query: TQuery; public readonly queryId: string; - public constructor(query: TQuery) { + public constructor(query: TQuery, opts: Options = {}) { + const { dependencies = [] } = opts; this.query = query; - this.queryId = hash(query).toString(); + this.queryId = joinedId(hash(query), ...dependencies); } } diff --git a/packages/models/src/lib/joinedId.ts b/packages/models/src/lib/joinedId.ts new file mode 100644 index 00000000..5c164dd6 --- /dev/null +++ b/packages/models/src/lib/joinedId.ts @@ -0,0 +1 @@ +export const joinedId = (...parts: Array) => parts.join(" | "); diff --git a/packages/models/src/react/provideReact.ts b/packages/models/src/react/provideReact.ts index 1bdda1dc..cb3f4d2a 100644 --- a/packages/models/src/react/provideReact.ts +++ b/packages/models/src/react/provideReact.ts @@ -2,24 +2,19 @@ import { AsyncResource, reactUsePromise } from "./reactUsePromise.js"; import { AsyncReturnType } from "type-fest"; import { hash } from "object-code"; import { reactProvisionContext } from "./reactProvisionContext.js"; +import { joinedId } from "../lib/joinedId.js"; type AsyncFn = (...args: any[]) => Promise; export const provideReact = ( loader: T, - dependencies?: string[], + dependencies: string[] = [], ) => { type P = Parameters; - const provisionId = String( - hash({ - loader, - dependencies, - }), - ); + const provisionId = joinedId(hash(loader), ...dependencies); const getAsyncResource = (params: P) => { - const paramsHash = params && params.length > 0 ? String(hash(params)) : ""; - const contextId = provisionId + paramsHash; + const contextId = joinedId(provisionId, hash(params)); const loaderWithContext = reactProvisionContext.bind( {