From 3ffea92142b05c95639f949cc99350c35382014b Mon Sep 17 00:00:00 2001 From: Jason Kuhrt Date: Mon, 4 Nov 2024 13:31:53 -0500 Subject: [PATCH] refactor: anyware pipeline namespace --- src/lib/anyware/Interceptor.ts | 2 +- src/lib/anyware/Pipeline.ts | 46 ------------------- src/lib/anyware/Pipeline/Pipeline.ts | 38 +++++++++++++++ src/lib/anyware/{ => Pipeline}/builder.ts | 12 ++--- src/lib/anyware/_.ts | 4 +- ...trypoint.test.ts => __.entrypoint.test.ts} | 0 .../anyware/{Pipeline.test.ts => __.test.ts} | 0 src/lib/anyware/run/runHook.ts | 2 +- src/lib/anyware/run/runPipeline.ts | 2 +- src/lib/anyware/run/runner.ts | 2 +- src/lib/builder/Definition.ts | 26 +++++------ src/lib/builder/__.test-d.ts | 6 +-- src/lib/private.ts | 4 +- src/requestPipeline/RequestPipeline.ts | 4 +- 14 files changed, 69 insertions(+), 79 deletions(-) delete mode 100644 src/lib/anyware/Pipeline.ts create mode 100644 src/lib/anyware/Pipeline/Pipeline.ts rename src/lib/anyware/{ => Pipeline}/builder.ts (89%) rename src/lib/anyware/{Pipeline.entrypoint.test.ts => __.entrypoint.test.ts} (100%) rename src/lib/anyware/{Pipeline.test.ts => __.test.ts} (100%) diff --git a/src/lib/anyware/Interceptor.ts b/src/lib/anyware/Interceptor.ts index 828a90ef3..33a84c0d8 100644 --- a/src/lib/anyware/Interceptor.ts +++ b/src/lib/anyware/Interceptor.ts @@ -1,7 +1,7 @@ import type { Deferred, MaybePromise } from '../prelude.js' import type { Private } from '../private.js' import type { InferPublicHooks, SomePublicHookEnvelope } from './hook/public.js' -import type { Pipeline } from './Pipeline.js' +import type { Pipeline } from './Pipeline/Pipeline.js' export type InterceptorOptions = { retrying: boolean diff --git a/src/lib/anyware/Pipeline.ts b/src/lib/anyware/Pipeline.ts deleted file mode 100644 index 3c32bd502..000000000 --- a/src/lib/anyware/Pipeline.ts +++ /dev/null @@ -1,46 +0,0 @@ -import type { HookDefinitionMap, HookSequence } from './hook/definition.js' - -import type { FindValueAfter, IsLastValue } from '../prelude.js' -import type { Private } from '../private.js' -import type { PipelineInput } from './builder.js' -import type { HookPrivateInput, PrivateHook } from './hook/private.js' - -export type Pipeline< - $HookSequence extends HookSequence = HookSequence, - $HookMap extends HookDefinitionMap<$HookSequence> = HookDefinitionMap<$HookSequence>, - $Result = unknown, -> = Private.Add<{ - hookNamesOrderedBySequence: $HookSequence - // dprint-ignore - hooks: { - [$HookName in $HookSequence[number]]: - PrivateHook< - $HookMap[$HookName]['slots'], - HookPrivateInput< - $HookMap[$HookName]['input'], - $HookMap[$HookName]['slots'] - >, - IsLastValue<$HookName, $HookSequence> extends true - ? $Result - : $HookMap[FindValueAfter<$HookName, $HookSequence>] - > - // [$HookName in $HookSequence[number]]: { - // slots: $HookMap[$HookName]['slots'] - // run: ( - // input: HookPrivateInput< - // $HookMap[$HookName]['input'], - // $HookMap[$HookName]['slots'] - // >, - // ) => MaybePromise< - // IsLastValue<$HookName, $HookSequence> extends true ? $Result - // : $HookMap[FindValueAfter<$HookName, $HookSequence>] - // > - // } - } - passthroughErrorInstanceOf?: PipelineInput['passthroughErrorInstanceOf'] - passthroughErrorWith?: PipelineInput['passthroughErrorWith'] -}, { - hookSequence: $HookSequence - hookMap: $HookMap - result: $Result -}> diff --git a/src/lib/anyware/Pipeline/Pipeline.ts b/src/lib/anyware/Pipeline/Pipeline.ts new file mode 100644 index 000000000..8a789748a --- /dev/null +++ b/src/lib/anyware/Pipeline/Pipeline.ts @@ -0,0 +1,38 @@ +import type { FindValueAfter, IsLastValue } from '../../prelude.js' +import type { Private } from '../../private.js' +import type { HookDefinitionMap, HookSequence } from '../hook/definition.js' +import type { HookPrivateInput, HookResultError, PrivateHook } from '../hook/private.js' +export * as Pipeline from './builder.js' + +// dprint-ignore +export type Pipeline< + $HookSequence extends HookSequence = HookSequence, + $HookMap extends HookDefinitionMap<$HookSequence> = HookDefinitionMap<$HookSequence>, + $Result = unknown, +> = + // todo what is the point of private here? Its usually for hiding fields on a user facing interface. But is this type actually user facing? + Private.Add< + { + hookSequence: $HookSequence + hookMap: $HookMap + result: $Result + }, + { + hookNamesOrderedBySequence: $HookSequence + hooks: { + [$HookName in $HookSequence[number]]: + PrivateHook< + $HookMap[$HookName]['slots'], + HookPrivateInput< + $HookMap[$HookName]['input'], + $HookMap[$HookName]['slots'] + >, + IsLastValue<$HookName, $HookSequence> extends true + ? $Result + : $HookMap[FindValueAfter<$HookName, $HookSequence>] + > + } + passthroughErrorInstanceOf?: Function[] + passthroughErrorWith?: (signal: HookResultError) => boolean + } + > diff --git a/src/lib/anyware/builder.ts b/src/lib/anyware/Pipeline/builder.ts similarity index 89% rename from src/lib/anyware/builder.ts rename to src/lib/anyware/Pipeline/builder.ts index 13f62c61f..94745c8ee 100644 --- a/src/lib/anyware/builder.ts +++ b/src/lib/anyware/Pipeline/builder.ts @@ -1,10 +1,10 @@ -import { type FindValueAfter, type IsLastValue, type MaybePromise } from '../prelude.js' -import type { HookDefinitionMap, HookSequence } from './hook/definition.js' -import type { HookResultError, InferPrivateHookInput } from './hook/private.js' +import { type FindValueAfter, type IsLastValue, type MaybePromise } from '../../prelude.js' +import type { HookDefinitionMap, HookSequence } from '../hook/definition.js' +import type { HookResultError, InferPrivateHookInput } from '../hook/private.js' +import { createRunner, type Runner } from '../run/runner.js' import type { Pipeline } from './Pipeline.js' -import { createRunner, type Runner } from './run/runner.js' -export { type HookDefinitionMap } from './hook/definition.js' +export { type HookDefinitionMap } from '../hook/definition.js' export type PipelineInput< $HookSequence extends HookSequence = HookSequence, @@ -79,7 +79,5 @@ export const create = < export type Builder<$Pipeline extends Pipeline> = { pipeline: $Pipeline - // todo - // use: run: Runner<$Pipeline> } diff --git a/src/lib/anyware/_.ts b/src/lib/anyware/_.ts index afb068342..917c6e340 100644 --- a/src/lib/anyware/_.ts +++ b/src/lib/anyware/_.ts @@ -1,4 +1,4 @@ -export * from './builder.js' export * from './Interceptor.js' -export * from './Pipeline.js' +export * from './Pipeline/builder.js' +export * from './Pipeline/Pipeline.js' export * from './run/runner.js' diff --git a/src/lib/anyware/Pipeline.entrypoint.test.ts b/src/lib/anyware/__.entrypoint.test.ts similarity index 100% rename from src/lib/anyware/Pipeline.entrypoint.test.ts rename to src/lib/anyware/__.entrypoint.test.ts diff --git a/src/lib/anyware/Pipeline.test.ts b/src/lib/anyware/__.test.ts similarity index 100% rename from src/lib/anyware/Pipeline.test.ts rename to src/lib/anyware/__.test.ts diff --git a/src/lib/anyware/run/runHook.ts b/src/lib/anyware/run/runHook.ts index 7fd2b0da2..deb93574b 100644 --- a/src/lib/anyware/run/runHook.ts +++ b/src/lib/anyware/run/runHook.ts @@ -3,7 +3,7 @@ import { casesExhausted, createDeferred, debugSub, errorFromMaybeError } from '. import type { HookResult, HookResultErrorAsync, Slots } from '../hook/private.js' import { createPublicHook, type SomePublicHookEnvelope } from '../hook/public.js' import type { InterceptorGeneric } from '../Interceptor.js' -import type { Pipeline } from '../Pipeline.js' +import type { Pipeline } from '../Pipeline/Pipeline.js' import type { ResultEnvelop } from './resultEnvelope.js' type HookDoneResolver = (input: HookResult) => void diff --git a/src/lib/anyware/run/runPipeline.ts b/src/lib/anyware/run/runPipeline.ts index b6d8cb8af..c923bdb25 100644 --- a/src/lib/anyware/run/runPipeline.ts +++ b/src/lib/anyware/run/runPipeline.ts @@ -3,7 +3,7 @@ import { ContextualError } from '../../errors/ContextualError.js' import { casesExhausted, createDeferred, debug } from '../../prelude.js' import type { HookResult, HookResultErrorAsync } from '../hook/private.js' import type { InterceptorGeneric } from '../Interceptor.js' -import type { Pipeline } from '../Pipeline.js' +import type { Pipeline } from '../Pipeline/Pipeline.js' import { createResultEnvelope } from './resultEnvelope.js' import type { ResultEnvelop } from './resultEnvelope.js' import { runHook } from './runHook.js' diff --git a/src/lib/anyware/run/runner.ts b/src/lib/anyware/run/runner.ts index 0f9750e45..80aa062e2 100644 --- a/src/lib/anyware/run/runner.ts +++ b/src/lib/anyware/run/runner.ts @@ -7,7 +7,7 @@ import type { HookName } from '../hook/definition.js' import type { HookResultErrorExtension } from '../hook/private.js' import type { SomePublicHookEnvelope } from '../hook/public.js' import { createRetryingInterceptor, type Interceptor, type InterceptorInput } from '../Interceptor.js' -import type { Pipeline } from '../Pipeline.js' +import type { Pipeline } from '../Pipeline/Pipeline.js' import { getEntrypoint } from './getEntrypoint.js' import { runPipeline } from './runPipeline.js' diff --git a/src/lib/builder/Definition.ts b/src/lib/builder/Definition.ts index a628802a9..00dacd7b5 100644 --- a/src/lib/builder/Definition.ts +++ b/src/lib/builder/Definition.ts @@ -53,15 +53,15 @@ export type AddExtension<$Chain_ extends Definition_, $Extension_ extends Extens export type MaterializeGeneric<$Chain_ extends Definition_> = Simplify< Private.Add< - mergeArrayOfObjects< - MaterializeExtensionsGeneric<$Chain_, $Chain_['extensions']> - >, { chain: $Chain_, context: mergeArrayOfObjects< MaterializeExtensionsGenericContext<$Chain_['extensions']> > - } + }, + mergeArrayOfObjects< + MaterializeExtensionsGeneric<$Chain_, $Chain_['extensions']> + > > > @@ -81,15 +81,15 @@ type MaterializeExtensionsGenericContext<$Extensions extends [...Extension[]]> = export type MaterializeSpecific<$Chain_ extends Definition_> = Simplify< Private.Add< - mergeArrayOfObjects< - MaterializeExtensionsInitial<$Chain_, $Chain_['extensions']> - >, { chain: $Chain_, context: mergeArrayOfObjects< MaterializeExtensionsInitialContext<$Chain_['extensions']> > - } + }, + mergeArrayOfObjects< + MaterializeExtensionsInitial<$Chain_, $Chain_['extensions']> + > > > @@ -109,17 +109,17 @@ type MaterializeExtensionsInitialContext<$Extensions extends [...Extension[]]> = export type MaterializeWithNewContext<$Chain_ extends Definition_, $Context extends Context> = // Simplify< Private.Add< + { + chain: $Chain_, + context: $Context + }, mergeArrayOfObjects< MaterializeExtensionsWithNewState< $Chain_, $Context, $Chain_['extensions'] > - >, - { - chain: $Chain_, - context: $Context - } + > > // > diff --git a/src/lib/builder/__.test-d.ts b/src/lib/builder/__.test-d.ts index 73c2f212d..6159d9527 100644 --- a/src/lib/builder/__.test-d.ts +++ b/src/lib/builder/__.test-d.ts @@ -12,7 +12,7 @@ import type { Builder } from './__.js' type B = Builder.Definition.AddExtension assertEqual() type b = Builder.Definition.MaterializeSpecific - assertEqual>() + assertEqual>() } // --------------------------------------------------------------------------------------------------------------------- { @@ -24,7 +24,7 @@ import type { Builder } from './__.js' type B = Builder.Definition.AddExtensions assertEqual() type b = Builder.Definition.MaterializeSpecific - assertEqual>() + assertEqual>() } // --------------------------------------------------------------------------------------------------------------------- { @@ -37,7 +37,7 @@ import type { Builder } from './__.js' } type B = Builder.Definition.AddExtension type b = Builder.Definition.MaterializeSpecific - assertEqual>() + assertEqual>() } // --------------------------------------------------------------------------------------------------------------------- { diff --git a/src/lib/private.ts b/src/lib/private.ts index c28f274be..a2d1b3704 100644 --- a/src/lib/private.ts +++ b/src/lib/private.ts @@ -25,11 +25,11 @@ export namespace Private { * * The given data will be added to the type under a symbol that is private to this library. */ - export type Add<$Type extends object, $PrivateData extends Data> = - & $Type + export type Add<$PrivateData extends Data, $Type extends object> = & { [privateSymbol]: $PrivateData } + & $Type /** * Get the private data from a type. diff --git a/src/requestPipeline/RequestPipeline.ts b/src/requestPipeline/RequestPipeline.ts index dd06e64b0..6a1fad08a 100644 --- a/src/requestPipeline/RequestPipeline.ts +++ b/src/requestPipeline/RequestPipeline.ts @@ -32,7 +32,7 @@ export type RequestPipeline<$Config extends Config = Config> = Anyware.Pipeline< GraffleExecutionResultVar<$Config> > -export const RequestPipeline = Anyware.create({ +export const RequestPipeline = Anyware.Pipeline.create({ // If core errors caused by an abort error then raise it as a direct error. // This is an expected possible error. Possible when user cancels a request. passthroughErrorWith: (signal) => { @@ -86,7 +86,7 @@ export const RequestPipeline = Anyware.create