Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Models): support dependencies for ListQueryModels #189

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions packages/models/src/base/ListQueryModel.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { hash } from "object-code";
import { joinedId } from "../lib/joinedId.js";

interface Options {
dependencies?: string[];
}

export abstract class ListQueryModel<TQuery> {
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);
}
}
1 change: 1 addition & 0 deletions packages/models/src/lib/joinedId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const joinedId = (...parts: Array<number | string>) => parts.join(" | ");
13 changes: 4 additions & 9 deletions packages/models/src/react/provideReact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<unknown>;

export const provideReact = <T extends AsyncFn>(
loader: T,
dependencies?: string[],
dependencies: string[] = [],
) => {
type P = Parameters<T>;
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(
{
Expand Down