|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#25467 cut 27] The automation service's flow reads, from |
| 5 | + * `ObjectStackProtocolImplementation`, moved out of `protocol.ts` whole: the |
| 6 | + * three members through which this protocol asks the `automation` service — |
| 7 | + * the engine of THIS process — about flows. |
| 8 | + * |
| 9 | + * - `resolveFlowCanonicalizer` — the service's `canonicalizeStoredFlow`, bound, |
| 10 | + * for the doors that canonicalize a stored flow body; nothing when the |
| 11 | + * service offers none. |
| 12 | + * - `resolveFlowRegistrationReader` — the service's `getFlowRegistration`, |
| 13 | + * bound, for a `flow` type only (#23775). |
| 14 | + * - `flowRegistrationsOf` — the list's `registrations`: that reader's verdict |
| 15 | + * per listed, non-draft item (#23316). |
| 16 | + * |
| 17 | + * A MOVE, not a redesign. Each function below is the body of the |
| 18 | + * `ObjectStackProtocolImplementation` method of the same name as it stood, |
| 19 | + * re-indented one level and otherwise unedited. |
| 20 | + * |
| 21 | + * ## How the moved bodies reach the class |
| 22 | + * |
| 23 | + * The shape #23050 set for `rest-server.ts` (its `data-action-routes.ts` |
| 24 | + * header carries the full reasoning), as `protocol-batch-data.ts` applies it. |
| 25 | + * The class keeps each method — the published `dist/*.d.ts` lists its name, |
| 26 | + * docblock and declared signature — and its body is one literal of exactly |
| 27 | + * the {@link AutomationFlowReadsHost} members it reads, plus a `.call`. The |
| 28 | + * constructor-supplied `getServicesRegistry` field is handed over as a |
| 29 | + * getter, so each read finds the registry as it stands at call time; |
| 30 | + * `flowRegistrationsOf`'s call to `resolveFlowRegistrationReader` is handed |
| 31 | + * over as a closure, so it still reaches the class method. Every caller keeps |
| 32 | + * calling the class methods. |
| 33 | + * |
| 34 | + * What the bodies read from elsewhere, they import from where `protocol.ts` |
| 35 | + * imports it. Nothing re-exports this module, so the published surface does |
| 36 | + * not move. |
| 37 | + */ |
| 38 | + |
| 39 | +import type { FlowRegistration } from '@objectstack/spec/api'; |
| 40 | +import type { ObjectStackProtocolImplementation } from './protocol.js'; |
| 41 | +import { canonicalMetaType } from './protocol.js'; |
| 42 | +import type { StoredFlowCanonicalization } from './stored-migration.js'; |
| 43 | + |
| 44 | +/** |
| 45 | + * The `ObjectStackProtocolImplementation` members the functions below read — |
| 46 | + * every one of them, and no other; each function's `this` is the `Pick` of |
| 47 | + * the members it reads. Each is typed AS the class member (an indexed access |
| 48 | + * on the class), so a change to a member's signature on the class is a change |
| 49 | + * here too, and naming a member the class does not have is a compile error. |
| 50 | + */ |
| 51 | +export interface AutomationFlowReadsHost { |
| 52 | + readonly getServicesRegistry: ObjectStackProtocolImplementation['getServicesRegistry']; |
| 53 | + readonly resolveFlowRegistrationReader: ObjectStackProtocolImplementation['resolveFlowRegistrationReader']; |
| 54 | +} |
| 55 | + |
| 56 | +/** The body of `ObjectStackProtocolImplementation#resolveFlowCanonicalizer`; the method's docblock there is its contract. */ |
| 57 | +export function resolveFlowCanonicalizer( |
| 58 | + this: Pick<AutomationFlowReadsHost, 'getServicesRegistry'>, |
| 59 | +): |
| 60 | + ((name: string, body: unknown, options?: { includeRetired?: boolean }) => StoredFlowCanonicalization) | undefined { |
| 61 | + const automation = this.getServicesRegistry?.().get('automation') as |
| 62 | + | { |
| 63 | + canonicalizeStoredFlow?: ( |
| 64 | + name: string, definition: unknown, options?: { includeRetired?: boolean }, |
| 65 | + ) => StoredFlowCanonicalization; |
| 66 | + } |
| 67 | + | undefined; |
| 68 | + const canonicalize = automation?.canonicalizeStoredFlow; |
| 69 | + if (typeof canonicalize !== 'function') return undefined; |
| 70 | + return (name, body, options) => (options === undefined |
| 71 | + ? canonicalize.call(automation, name, body) |
| 72 | + : canonicalize.call(automation, name, body, options)); |
| 73 | +} |
| 74 | + |
| 75 | +/** The body of `ObjectStackProtocolImplementation#resolveFlowRegistrationReader`; the method's docblock there is its contract. */ |
| 76 | +export function resolveFlowRegistrationReader( |
| 77 | + this: Pick<AutomationFlowReadsHost, 'getServicesRegistry'>, |
| 78 | + type: string, |
| 79 | +): ((name: string) => FlowRegistration) | undefined { |
| 80 | + if (canonicalMetaType(type) !== 'flow') return undefined; |
| 81 | + const automation = this.getServicesRegistry?.().get('automation') as |
| 82 | + | { getFlowRegistration?: (name: string) => FlowRegistration } |
| 83 | + | undefined; |
| 84 | + const read = automation?.getFlowRegistration; |
| 85 | + if (typeof read !== 'function') return undefined; |
| 86 | + return (name) => read.call(automation, name); |
| 87 | +} |
| 88 | + |
| 89 | +/** The body of `ObjectStackProtocolImplementation#flowRegistrationsOf`; the method's docblock there is its contract. */ |
| 90 | +export function flowRegistrationsOf( |
| 91 | + this: Pick<AutomationFlowReadsHost, 'resolveFlowRegistrationReader'>, |
| 92 | + type: string, |
| 93 | + items: readonly unknown[], |
| 94 | +): { registrations?: Record<string, FlowRegistration> } { |
| 95 | + const read = this.resolveFlowRegistrationReader(type); |
| 96 | + if (!read) return {}; |
| 97 | + const entries: Array<[string, FlowRegistration]> = []; |
| 98 | + for (const it of items) { |
| 99 | + const name = (it as { name?: unknown } | null | undefined)?.name; |
| 100 | + if (typeof name !== 'string' || (it as { _draft?: unknown })._draft === true) continue; |
| 101 | + entries.push([name, read(name)]); |
| 102 | + } |
| 103 | + return { registrations: Object.fromEntries(entries) }; |
| 104 | +} |
0 commit comments