Skip to content

Commit

Permalink
feat(main-loader): optimize memory usage for createQueriesMap in class
Browse files Browse the repository at this point in the history
  • Loading branch information
alemagio committed Jul 10, 2023
1 parent a8a1550 commit f07b955
Showing 1 changed file with 36 additions and 15 deletions.
51 changes: 36 additions & 15 deletions packages/main-loader/lib/main-loader.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,44 @@
import DataLoader, { BatchLoadFn } from 'dataloader';
import DataLoader, { BatchLoadFn } from "dataloader";

import { hash } from './hash';
import type { Key } from './key';
import { createQueriesMapFactory } from './create-queries-map-factory';
import { hash } from "./hash";
import type { Key } from "./key";
import { createQueriesMapFactory } from "./create-queries-map-factory";

export abstract class MainLoader<TReturn, TQuery extends object, TLoadKey extends Pick<Key<TQuery>, 'query' | 'skip' | 'limit'> = Key<TQuery>> {
const createQueriesMap = createQueriesMapFactory();

export abstract class MainLoader<
TReturn,
TQuery extends object,
TLoadKey extends Pick<Key<TQuery>, "query" | "skip" | "limit"> = Key<TQuery>
> {
protected loader: DataLoader<Key<TQuery>, TReturn>;
protected createQueriesMap;

constructor() {
this.loader = new DataLoader<Key<TQuery>, TReturn, string>(
this.batchLoadFn.bind(this) as BatchLoadFn<Key<TQuery>, TReturn>,
{
cacheKeyFn: this.cacheKeyFn,
},
}
);
this.createQueriesMap = createQueriesMapFactory()
}

public async load<U extends TReturn = TReturn>(key: TLoadKey): Promise<U | null> {
public async load<U extends TReturn = TReturn>(
key: TLoadKey
): Promise<U | null> {
const internalKey = this.preLoad(key);
return this.loader.load(internalKey) as Promise<U | null>;
}

public async loadMany<U extends TReturn = TReturn>(key: TLoadKey): Promise<U[]> {
public async loadMany<U extends TReturn = TReturn>(
key: TLoadKey
): Promise<U[]> {
const internalKey = this.preLoad(key);
const { skip, limit } = key
return this.loader.load({ skip: skip ?? 0, limit: limit ?? 0, ...internalKey }) as Promise<U[]>;
const { skip, limit } = key;
return this.loader.load({
skip: skip ?? 0,
limit: limit ?? 0,
...internalKey,
}) as Promise<U[]>;
}

protected async batchLoadFn(keys: readonly Key<TQuery>[]) {
Expand All @@ -48,9 +60,18 @@ export abstract class MainLoader<TReturn, TQuery extends object, TLoadKey extend
return hash(key);
}

protected preLoad(key: TLoadKey): Key<TQuery> { return key as unknown as Key<TQuery> }
private createQueriesMap(keys: readonly Key<TQuery>[]) {
return createQueriesMap(keys);
}

protected preLoad(key: TLoadKey): Key<TQuery> {
return key as unknown as Key<TQuery>;
}

protected onError(error: Error, key: Key<TQuery>) {}
protected onError(error: Error, key: Key<TQuery>) { }

protected abstract execute(key: Key<TQuery>, options?: unknown): Promise<TReturn | TReturn[] | null>;
protected abstract execute(
key: Key<TQuery>,
options?: unknown
): Promise<TReturn | TReturn[] | null>;
}

0 comments on commit f07b955

Please sign in to comment.