import type { AbstractStore } from "./abstract-store.ts"; import { applyParams, combineParams, evalParamsOrFunc, type Params, type ParamsOrFunc, } from "./params.ts"; export type ShapeFor = F extends Factory ? S : never; interface FactoryClass { new (state: SharedState, params?: Params): Factory; } // This is a bit cursed but works and helps to keep "createContext" protected, // while allowing consumers to extend the build context... // Alternative is to make "createContext" public, but than it gets suggested and // has to throw when unintentionally called outside the factory. export type Context> = ReturnType< // @ts-expect-error Protected member 'createContext' cannot be accessed on a type parameter. F["createContext"] >; export interface FactoryContext { params?: Params | undefined; sequence: number; } export abstract class Factory { #state: SharedState; #params?: Params | undefined; #context: Context | null; protected abstract construct(): Shape; /** * **Do not call the constructor directly! Use {@link Factory.create} instead.** * * The constructor requires arguments that should not concern you. * It is only used internally for factory creation and refinement. It only has * to remain public in TypeScript to be able to extend a factory. * * @private */ constructor(state: SharedState, params?: Params) { this.#state = state; this.#params = params; this.#context = null; } static create(this: FactoryClass>): This { return new this(new SharedState()); } get identifier(): symbol { return this.#state.identifier; } protected get ctx(): Context { if (this.#context === null) { throw new TypeError( "FactoryContext can only be accessed in the `construct` method.", ); } return this.#context; } protected createContext(params?: Params): FactoryContext { return { sequence: this.#state.next(), params, }; } async #insertData(store: AbstractStore, data: Shape): Promise { const inserted = await Promise.resolve( store["insert"](this.identifier, data), ); if (!inserted) { throw new TypeError( `Failed to insert data into store for factory "${this.constructor.name}". Does the store know this factory?`, ); } } reset(): void { this.#state.reset(); } refine(params: Params): this { const Constructor = this.constructor as FactoryClass; return new Constructor(this.#state, combineParams(this.#params, params)); } build(params?: Params): Shape { params = combineParams(this.#params, params); this.#context = this.createContext(params) as Context; const data = this.construct(); this.#context = null; return applyParams(data, params); } buildMany(amount: number, params?: ParamsOrFunc): Shape[] { const list: Shape[] = []; for (let i = 0; i < amount; i++) { const p = evalParamsOrFunc(i, list, params); list.push(this.build(p)); } return list; } async seed(store: AbstractStore, params?: Params): Promise { const data = this.build(params); await this.#insertData(store, data); return data; } async seedMany( store: AbstractStore, amount: number, params?: ParamsOrFunc, ): Promise { const list = this.buildMany(amount, params); const insertions = list.map(this.#insertData.bind(this, store)); await Promise.all(insertions); return list; } } class SharedState { static initialSequence = 0; readonly identifier: symbol; #sequence: number; constructor() { this.identifier = Symbol("factory"); this.#sequence = SharedState.initialSequence; } next(): number { return this.#sequence++; } reset(): void { this.#sequence = SharedState.initialSequence; } }