Skip to content

Commit

Permalink
object_chain.concatSteps
Browse files Browse the repository at this point in the history
  • Loading branch information
mccraigmccraig committed Dec 12, 2023
1 parent 3ed3876 commit da76418
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/object_chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,24 @@ export function makePureStep
return addPureStep(chain, step)
}

// return a new ObjectChain with the addSteps concatenated
export function concatSteps
<Input extends ChainTagged,
const ChainSteps extends cons.NRConsList<UPObjectStepSpec>,
const NewSteps extends cons.NRConsList<UPObjectStepSpec>>
(chain: ObjectChain<Input, ChainSteps>,
addSteps: NewSteps) {

const newSteps =
// deno-lint-ignore no-explicit-any
cons.concat<UPObjectStepSpec>()(chain.steps as any, addSteps as any)

// deno-lint-ignore no-explicit-any
return objectChain<Input>()(chain.tag, newSteps as any) as
ObjectChain<Input, cons.Concat<UPObjectStepSpec, ChainSteps, NewSteps>>

}

////////////////////////////////// recursion support ////////////////////

// idea is that a chain will have an associated service, and we use the
Expand Down
33 changes: 33 additions & 0 deletions src/object_chain_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as cons from "./cons_list.ts"
import {
objectChain, addFxStep, makeFxStep,
addPureStep, makePureStep,
concatSteps,
objectChainFxFn, provideObjectChainServiceImpl
} from "./object_chain.ts"
import {
Expand Down Expand Up @@ -172,6 +173,38 @@ Deno.test("makeFxStep and makePureStep add steps", () => {
})
})

Deno.test("concatSteps concatenates steps", () => {
const firstSteps =
[getOrgObjectStepSpec,
[getUserObjectStepSpec,
cons.None]] as const

const moreSteps =
[pureFormatPushNotificationStepSpec,
[sendPusnNotificationStepSpec, cons.None]] as const

const shortChain = objectChain<SendPushNotification>()(SendPushNotificationTag,
firstSteps)

const chain = concatSteps(shortChain, moreSteps)

const input: SendPushNotification = {
_tag: "sendPushNotification" as const,
data: { org_nick: "foo", user_id: "bar" }
}
const effect = chain.program(input)
const runnable = Effect.provide(effect, testServiceContext)
const r = Effect.runSync(runnable)

assertEquals(r, {
...input,
org: { id: "foo", name: "Foo" },
user: { id: "bar", name: "Bar" },
formatPushNotification: "Welcome Bar of Foo",
sendPush: "push sent OK: Welcome Bar of Foo"
})
})

// make a getOrg chain which can be run as a step
type GetOrg = {
readonly _tag: "getOrg",
Expand Down

0 comments on commit da76418

Please sign in to comment.