|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#29986 codemod] The host of each facet of `ObjectQL`, built ONCE per engine (#30051): |
| 5 | + * what every stub of a facet hands that facet's bodies as `this`, in place of |
| 6 | + * a host literal built on every call. Each `FacetHost` below is filled by the |
| 7 | + * factory `ObjectQL`'s static block installs, the one place that can read |
| 8 | + * the class's private members; the host is built on the first call that needs |
| 9 | + * it and kept for the engine's life. A method goes as a closure that calls it |
| 10 | + * on the engine at call time, a field or accessor as a getter (and a setter |
| 11 | + * when a body assigns it), so a body reads and writes the engine exactly as |
| 12 | + * the per-call literal let it. |
| 13 | + * |
| 14 | + * Written by `scripts/codemod-split-engine.mjs`; it imports types only, so it forms no |
| 15 | + * evaluation cycle and is evaluated before the class whichever module loads |
| 16 | + * first. `index.ts` re-exports nothing from this module. |
| 17 | + */ |
| 18 | + |
| 19 | +import type { ObjectQL } from './engine.js'; |
| 20 | +import type { ActionsHost } from './engine-actions.js'; |
| 21 | +import type { CascadePlanningHost } from './engine-cascade-planning.js'; |
| 22 | +import type { CompatibilitySectionHost } from './engine-compatibility-section.js'; |
| 23 | +import type { DefaultsAndAutonumbersHost } from './engine-defaults-and-autonumbers.js'; |
| 24 | +import type { DeleteAndAggregateHost } from './engine-delete-and-aggregate.js'; |
| 25 | +import type { FindHost } from './engine-find.js'; |
| 26 | +import type { HooksAndFunctionsHost } from './engine-hooks-and-functions.js'; |
| 27 | +import type { InsertHost } from './engine-insert.js'; |
| 28 | +import type { MiddlewareAndContextHost } from './engine-middleware-and-context.js'; |
| 29 | +import type { MigrationStateHost } from './engine-migration-state.js'; |
| 30 | +import type { ReferencesAndMasterDetailHost } from './engine-references-and-master-detail.js'; |
| 31 | +import type { RegistrationHost } from './engine-registration.js'; |
| 32 | +import type { SecretsAndRoutingHost } from './engine-secrets-and-routing.js'; |
| 33 | +import type { SecurityResolversHost } from './engine-security-resolvers.js'; |
| 34 | +import type { SummariesAndExpansionHost } from './engine-summaries-and-expansion.js'; |
| 35 | +import type { TransactionsSectionHost } from './engine-transactions-section.js'; |
| 36 | + |
| 37 | +/** One facet's host for each engine: built on first use, kept for the engine's life. */ |
| 38 | +export class FacetHost<H> { |
| 39 | + private readonly hosts = new WeakMap<ObjectQL, H>(); |
| 40 | + private build: ((engine: ObjectQL) => H) | undefined; |
| 41 | + |
| 42 | + constructor(private readonly facet: string) {} |
| 43 | + |
| 44 | + /** Called once, from `ObjectQL`'s static block. */ |
| 45 | + install(build: (engine: ObjectQL) => H): void { |
| 46 | + this.build = build; |
| 47 | + } |
| 48 | + |
| 49 | + /** The host of `engine`, built on first use. */ |
| 50 | + of(engine: ObjectQL): H { |
| 51 | + let host = this.hosts.get(engine); |
| 52 | + if (host === undefined) { |
| 53 | + if (this.build === undefined) { |
| 54 | + throw new Error(`${this.facet}: ObjectQL has not installed its host factory`); |
| 55 | + } |
| 56 | + host = this.build(engine); |
| 57 | + this.hosts.set(engine, host); |
| 58 | + } |
| 59 | + return host; |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +export const actionsHost = new FacetHost<ActionsHost>('ActionsFacet'); |
| 64 | +export const cascadePlanningHost = new FacetHost<CascadePlanningHost>('CascadePlanningFacet'); |
| 65 | +export const compatibilitySectionHost = new FacetHost<CompatibilitySectionHost>('CompatibilitySectionFacet'); |
| 66 | +export const defaultsAndAutonumbersHost = new FacetHost<DefaultsAndAutonumbersHost>('DefaultsAndAutonumbersFacet'); |
| 67 | +export const deleteAndAggregateHost = new FacetHost<DeleteAndAggregateHost>('DeleteAndAggregateFacet'); |
| 68 | +export const findHost = new FacetHost<FindHost>('FindFacet'); |
| 69 | +export const hooksAndFunctionsHost = new FacetHost<HooksAndFunctionsHost>('HooksAndFunctionsFacet'); |
| 70 | +export const insertHost = new FacetHost<InsertHost>('InsertFacet'); |
| 71 | +export const middlewareAndContextHost = new FacetHost<MiddlewareAndContextHost>('MiddlewareAndContextFacet'); |
| 72 | +export const migrationStateHost = new FacetHost<MigrationStateHost>('MigrationStateFacet'); |
| 73 | +export const referencesAndMasterDetailHost = new FacetHost<ReferencesAndMasterDetailHost>('ReferencesAndMasterDetailFacet'); |
| 74 | +export const registrationHost = new FacetHost<RegistrationHost>('RegistrationFacet'); |
| 75 | +export const secretsAndRoutingHost = new FacetHost<SecretsAndRoutingHost>('SecretsAndRoutingFacet'); |
| 76 | +export const securityResolversHost = new FacetHost<SecurityResolversHost>('SecurityResolversFacet'); |
| 77 | +export const summariesAndExpansionHost = new FacetHost<SummariesAndExpansionHost>('SummariesAndExpansionFacet'); |
| 78 | +export const transactionsSectionHost = new FacetHost<TransactionsSectionHost>('TransactionsSectionFacet'); |
0 commit comments