Skip to content

Commit

Permalink
ObjectStepSpecList type
Browse files Browse the repository at this point in the history
  • Loading branch information
mccraigmccraig committed Dec 16, 2023
1 parent fbe037d commit 72cbbf1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 30 deletions.
32 changes: 16 additions & 16 deletions src/object_chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import * as ctag from "./chain_tag.ts"
import {
CastUCObjectStepSpec,
UCFxObjectStepSpec, UCPureObjectStepSpec,
ObjectChainSteps, UPObjectStepSpec,
ObjectChainSteps, UPObjectStepSpec, ObjectStepSpecList,
ObjectStepsTupleReqsU,
ObjectStepsErrorsU, ObjectChainStepsReturn, objectChainStepsProg
} from "./object_chain_steps.ts"

// an effectful function of Input which build an Object
export type ObjectChainProgram<Input extends ChainTagged,
Steps extends cons.NRConsList<UPObjectStepSpec>> =
Steps extends ObjectStepSpecList> =
(i: Input) => Effect.Effect<
ObjectStepsTupleReqsU<Steps>,
ObjectStepsErrorsU<Steps>,
Expand All @@ -22,18 +22,18 @@ export type ObjectChainProgram<Input extends ChainTagged,

// a type for a service which can run an ObjectChain
export type ObjectChainService<Input extends ChainTagged,
Steps extends cons.NRConsList<UPObjectStepSpec>> = {
Steps extends ObjectStepSpecList> = {
readonly buildObject: ObjectChainProgram<Input, Steps>
}

export type ObjectChainServiceContextTag<Input extends ChainTagged,
Steps extends cons.NRConsList<UPObjectStepSpec>> =
Steps extends ObjectStepSpecList> =
Context.Tag<ChainTag<Input>, ObjectChainService<Input, Steps>>

// get a Context.Tag for an ObjectChainService
export function objectChainServiceContextTag
<Input extends ChainTagged,
Steps extends cons.NRConsList<UPObjectStepSpec>>
Steps extends ObjectStepSpecList>
() {
return Context.Tag<ChainTag<Input>, ObjectChainService<Input, Steps>>()
}
Expand All @@ -42,7 +42,7 @@ export function objectChainServiceContextTag
// Object. it can be built in a single step with objectChain, or iteratively
// with addSteps
export type ObjectChain<Input extends ChainTagged,
Steps extends cons.NRConsList<UPObjectStepSpec>,
Steps extends ObjectStepSpecList,
// this param is only here so that a chain value's IntelliSense
// shows the chain's return type
_Return = ObjectChainStepsReturn<Steps, Input>> = {
Expand All @@ -63,7 +63,7 @@ export type UPObjectChain = {
// deno-lint-ignore no-explicit-any
readonly tag: any
readonly tagStr: string
readonly steps: cons.NRConsList<UPObjectStepSpec>
readonly steps: ObjectStepSpecList
// deno-lint-ignore no-explicit-any
readonly program: (i: any) => Effect.Effect<any, any, any>
// deno-lint-ignore no-explicit-any
Expand Down Expand Up @@ -102,7 +102,7 @@ export type UPObjectChainProgramValue<T extends UPObjectChain> =

// build an ObjectChain from Steps
export function objectChain<Input extends ChainTagged>() {
return function <const Steps extends cons.NRConsList<UPObjectStepSpec>>
return function <const Steps extends ObjectStepSpecList>
(tag: ChainTag<Input>,

steps: Steps extends ObjectChainSteps<Steps, Input>
Expand All @@ -126,7 +126,7 @@ export function objectChain<Input extends ChainTagged>() {
// add a FxStep to an ObjectChain, returning a new ObjectChain
export function addStep
<Input extends ChainTagged,
const Steps extends cons.NRConsList<UPObjectStepSpec>,
const Steps extends ObjectStepSpecList,
NewStep extends UPObjectStepSpec>

(chain: ObjectChain<Input, Steps>,
Expand All @@ -146,7 +146,7 @@ export function addStep
// make an FxStep at the end of an ObjectChain, returning a new ObjectChain
export function makeFxStep
<Input extends ChainTagged,
const Steps extends cons.NRConsList<UPObjectStepSpec>,
const Steps extends ObjectStepSpecList,
K extends string,
A extends ObjectChainStepsReturn<Steps, Input>,
D1 extends D2,
Expand All @@ -168,7 +168,7 @@ export function makeFxStep
// make a PureStep at the end of an ObjectChain, returning a new ObjectChain
export function makePureStep
<Input extends ChainTagged,
const Steps extends cons.NRConsList<UPObjectStepSpec>,
const Steps extends ObjectStepSpecList,
K extends string,
A extends ObjectChainStepsReturn<Steps, Input>,
V>(chain: ObjectChain<Input, Steps>,
Expand All @@ -185,8 +185,8 @@ export function makePureStep
// return a new ObjectChain with the addSteps concatenated
export function concatSteps
<Input extends ChainTagged,
const ChainSteps extends cons.NRConsList<UPObjectStepSpec>,
const AddSteps extends cons.NRConsList<UPObjectStepSpec>>
const ChainSteps extends ObjectStepSpecList,
const AddSteps extends ObjectStepSpecList>
(chain: ObjectChain<Input, ChainSteps>,
addSteps: AddSteps extends cons.ConsList<UPObjectStepSpec, AddSteps>
? AddSteps
Expand Down Expand Up @@ -214,7 +214,7 @@ export function concatSteps
export function objectChainServiceImpl

<Input extends ChainTagged,
const Steps extends cons.NRConsList<UPObjectStepSpec>>
const Steps extends ObjectStepSpecList>

(chain: ObjectChain<Input, Steps>) {

Expand All @@ -231,7 +231,7 @@ export function objectChainServiceImpl
// to an Effect
export function provideObjectChainServiceImpl
<Input extends ChainTagged,
const Steps extends cons.NRConsList<UPObjectStepSpec>,
const Steps extends ObjectStepSpecList,
InR, InE, InV>

(effect: Effect.Effect<InR, InE, InV>,
Expand All @@ -248,7 +248,7 @@ export function provideObjectChainServiceImpl
// calls it's buildObject function
export function objectChainFxFn
<Input extends ChainTagged,
const Steps extends cons.NRConsList<UPObjectStepSpec>>
const Steps extends ObjectStepSpecList>

(chain: ObjectChain<Input, Steps>) {

Expand Down
30 changes: 16 additions & 14 deletions src/object_chain_steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ export function objectStepFn<Obj>() {
}
}

export type ObjectStepSpecList = NRConsList<UPObjectStepSpec>

// utility type to get a Union from a Tuple of types
// deno-lint-ignore no-explicit-any
export type UnionFromTuple<Tuple extends readonly any[]> = Tuple[number]
Expand All @@ -163,7 +165,7 @@ export type ExpandTuple<Tuple extends readonly [...any[]]> = {
} & { length: Tuple['length'] }

// convert a cons list of UPObjectStepSpecs type to a Tuple type
export type ObjectStepsTuple<Steps extends NRConsList<UPObjectStepSpec>> =
export type ObjectStepsTuple<Steps extends ObjectStepSpecList> =
ToTuple<UPObjectStepSpec, Steps>

// get a union of all the Requirements from a list of steps...
Expand All @@ -174,12 +176,12 @@ export type ObjectStepReqs<T extends UPObjectStepSpec> =
? R
: never
export type ObjectStepsReqsU<
List extends NRConsList<UPObjectStepSpec>,
List extends ObjectStepSpecList,
Acc = never> =
List extends None
? Acc
: List extends readonly [infer F extends UPObjectStepSpec,
infer R extends NRConsList<UPObjectStepSpec>]
infer R extends ObjectStepSpecList]
? ObjectStepsReqsU<R, Acc | ObjectStepReqs<F>>
: never
type ObjectStepsTupleReqsUImpl<
Expand All @@ -192,7 +194,7 @@ type ObjectStepsTupleReqsUImpl<
// setting up the recursion Service in object_chain.ts ...converting
// the list to a tuple before extracting the Requirements union seems
// to avoid that
export type ObjectStepsTupleReqsU<Steps extends NRConsList<UPObjectStepSpec>> =
export type ObjectStepsTupleReqsU<Steps extends ObjectStepSpecList> =
ObjectStepsTupleReqsUImpl<ObjectStepsTuple<Steps>>

// get a union of all the Errors from a list of steps
Expand All @@ -202,12 +204,12 @@ export type ObjectStepErrors<T extends UPObjectStepSpec> =
? E
: never
export type ObjectStepsErrorsU<
List extends NRConsList<UPObjectStepSpec>,
List extends ObjectStepSpecList,
Acc = never> =
List extends None
? Acc
: List extends readonly [infer F extends UPObjectStepSpec,
infer R extends NRConsList<UPObjectStepSpec>]
infer R extends ObjectStepSpecList]
? ObjectStepsErrorsU<R, Acc | ObjectStepErrors<F>>
: never

Expand All @@ -218,13 +220,13 @@ export type ObjectStepInput<T extends UPObjectStepSpec> =
? A
: never
export type ObjectStepsInputTuple<
List extends NRConsList<UPObjectStepSpec>,
List extends ObjectStepSpecList,
// deno-lint-ignore no-explicit-any
Acc extends readonly any[] = []> =
List extends None
? Acc
: List extends readonly [infer F extends UPObjectStepSpec,
infer R extends NRConsList<UPObjectStepSpec>]
infer R extends ObjectStepSpecList]
? ObjectStepsInputTuple<R, readonly [...Acc, ObjectStepInput<F>]>
: never

Expand All @@ -235,13 +237,13 @@ export type ObjectStepValue<T extends UPObjectStepSpec> =
? V
: never
export type ObjectStepsValueTuple<
List extends NRConsList<UPObjectStepSpec>,
List extends ObjectStepSpecList,
// deno-lint-ignore no-explicit-any
Acc extends readonly any[] = []> =
List extends None
? Acc
: List extends readonly [infer F extends UPObjectStepSpec,
infer R extends NRConsList<UPObjectStepSpec>]
infer R extends ObjectStepSpecList]
? ObjectStepsValueTuple<R, readonly [...Acc, ObjectStepValue<F>]>
: never

Expand All @@ -261,7 +263,7 @@ export type ObjectStepsValueTuple<
// 2. it's safe to use never in the else branches. they will not be hit

export type ObjectChainSteps<
Specs extends NRConsList<UPObjectStepSpec>,
Specs extends ObjectStepSpecList,
ObjAcc> =

// case: no more specs
Expand All @@ -270,7 +272,7 @@ export type ObjectChainSteps<

// case: there are more specs - add a property to ObjAcc and recurse
: Specs extends readonly [infer First,
infer Rest extends NRConsList<UPObjectStepSpec>]
infer Rest extends ObjectStepSpecList]
? First extends UCFxObjectStepSpec<
infer FK, infer _FA, infer _FD1, infer FD2,
infer FR, infer FE, infer FV>
Expand All @@ -285,7 +287,7 @@ export type ObjectChainSteps<

// get the final Object result type from a list of ObjectStepSpecs
export type ObjectChainStepsReturn<
Specs extends NRConsList<UPObjectStepSpec>,
Specs extends ObjectStepSpecList,
ObjAcc> =
Specs extends None
? ObjAcc // empty specs returns the input
Expand All @@ -304,7 +306,7 @@ export type ObjectChainStepsReturn<
// other type params
export function objectChainStepsProg<Obj>() {

return function <const Specs extends NRConsList<UPObjectStepSpec>>
return function <const Specs extends ObjectStepSpecList>
(objectStepSpecs:
Specs extends ObjectChainSteps<Specs, Obj>
? Specs
Expand Down

0 comments on commit 72cbbf1

Please sign in to comment.