From da76418fb0e57733297960e5faff6420ba0cdcc6 Mon Sep 17 00:00:00 2001 From: mccraig mccraig of the clan mccraig Date: Tue, 12 Dec 2023 19:12:04 +0000 Subject: [PATCH] object_chain.concatSteps --- src/object_chain.ts | 18 ++++++++++++++++++ src/object_chain_test.ts | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/src/object_chain.ts b/src/object_chain.ts index 5781301..26ed510 100644 --- a/src/object_chain.ts +++ b/src/object_chain.ts @@ -203,6 +203,24 @@ export function makePureStep return addPureStep(chain, step) } +// return a new ObjectChain with the addSteps concatenated +export function concatSteps + , + const NewSteps extends cons.NRConsList> + (chain: ObjectChain, + addSteps: NewSteps) { + + const newSteps = + // deno-lint-ignore no-explicit-any + cons.concat()(chain.steps as any, addSteps as any) + + // deno-lint-ignore no-explicit-any + return objectChain()(chain.tag, newSteps as any) as + ObjectChain> + +} + ////////////////////////////////// recursion support //////////////////// // idea is that a chain will have an associated service, and we use the diff --git a/src/object_chain_test.ts b/src/object_chain_test.ts index 6ff885f..0851a0f 100644 --- a/src/object_chain_test.ts +++ b/src/object_chain_test.ts @@ -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 { @@ -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()(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",