/** * Extends a bundle without forking it. Each combinator returns a new bundle and * works data-first or in a pipe: `Counter.pipe(Bundle.rename('Clicks'))`. */ import { Function } from 'effect' import type * as Submodel from 'foldkit/submodel' import * as Subscription from 'foldkit/subscription' import type * as Update from 'foldkit/update' import { make, type AnyBundle, type Bundle, type BundleSpec, type HelperRecord, type HelperRequirements, } from './bundle.js' import type { BundleParts } from './declare.js' import { helpersFor, readsArgs } from './helpers.js' type P = BundleParts type Rebuilt< B, Changes extends Partial<{ Name: string R: unknown S: unknown Helpers: unknown ViewInputs: unknown }> = {}, > = Bundle< Changes extends { Name: infer N extends string } ? N : P['Name'], P['Args'], P['Model'], P['Message'], P['OutMessage'], Changes extends { R: infer R } ? R : P['R'], Changes extends { S: infer S } ? S : P['S'], Changes extends { ViewInputs: infer V } ? V : P['ViewInputs'], P['Resources'], Extract< Changes extends { Helpers: infer H } ? H : P['Helpers'], HelperRecord['Model'], P['Message'], P['OutMessage']> > > type UpdateOf = ( model: P['Model'], message: P['Message'], args: P['Args'], ) => Update.ReturnWithOutMessage['Model'], P['Message'], P['OutMessage'], P['R']> type InitOf = (args: P['Args']) => Update.Return['Model'], P['Message'], P['R']> const specOf = ( bundle: AnyBundle, ): BundleSpec => { const { with: _with, at: _at, each: _each, pipe: _pipe, ...spec } = bundle return spec } const rebuild = (bundle: AnyBundle, changes: object): any => make({ ...specOf(bundle), ...changes }) /** A new name, for the placement keys and the Module contract. */ export const rename: { ( name: Name, ): (self: B) => Rebuilt (self: B, name: Name): Rebuilt } = Function.dual(2, (self: AnyBundle, name: string) => rebuild(self, { name })) /** Wraps `update`, for logging, guards, or extra transitions. */ export const mapUpdate: { (f: (update: UpdateOf) => UpdateOf): (self: B) => Rebuilt (self: B, f: (update: UpdateOf) => UpdateOf): Rebuilt } = Function.dual(2, (self: AnyBundle, f: (update: AnyBundle['update']) => AnyBundle['update']) => rebuild(self, { update: f(self.update) }), ) /** Wraps `init`, for example to start an extra Command. */ export const mapInit: { (f: (init: InitOf) => InitOf): (self: B) => Rebuilt (self: B, f: (init: InitOf) => InitOf): Rebuilt } = Function.dual(2, (self: AnyBundle, f: (init: AnyBundle['init']) => AnyBundle['init']) => rebuild(self, { init: f(self.init) }), ) type ViewOf = Submodel.View['Model'], P['Message'], P['ViewInputs']> /** Replaces or wraps the view; the result must still come from `Submodel.defineView`. */ export const mapView: { (f: (view: ViewOf | undefined) => ViewOf): (self: B) => Rebuilt (self: B, f: (view: ViewOf | undefined) => ViewOf): Rebuilt } = Function.dual(2, (self: AnyBundle, f: (view: AnyBundle['view']) => AnyBundle['view']) => rebuild(self, { view: f(self.view) }), ) /** * Gives the bundle a view, with whatever inputs that view takes. Unlike * `mapView`, the inputs need not be the ones the bundle had, so a headless * bundle can be drawn by a package that knows nothing of how it updates. */ export const withView: { ( view: Submodel.View['Model'], P['Message'], ViewInputs>, ): (self: B) => Rebuilt ( self: B, view: Submodel.View['Model'], P['Message'], ViewInputs>, ): Rebuilt } = Function.dual(2, (self: AnyBundle, view: AnyBundle['view']) => rebuild(self, { view })) /** Adds programmatic entry points beside the bundle's own; the services their Commands need join the bundle's. */ export const withHelpers: { >( helpers: Helpers, ): (self: B) => Rebuilt> >( self: B, helpers: Helpers, ): Rebuilt> } = Function.dual(2, (self: AnyBundle, helpers: object) => rebuild(self, { helpers: readsArgs(self.helpers) ? (args: unknown) => ({ ...helpersFor(self.helpers, args), ...helpers }) : { ...self.helpers, ...helpers }, }), ) type HelperOf = HelperRecord['Model'], P['Message'], P['OutMessage']> type WithHelpers = { Helpers: P['Helpers'] & Helpers R: P['R'] | HelperRequirements } /** Adds Subscriptions beside the bundle's own. A duplicate key throws, as in `Subscription.aggregate`. */ export const withSubscriptions: { ( subscriptions: ( args: P['Args'], ) => Subscription.Subscriptions['Model'], P['Message'], S2>, ): (self: B) => Rebuilt['S'] | S2 }> ( self: B, subscriptions: ( args: P['Args'], ) => Subscription.Subscriptions['Model'], P['Message'], S2>, ): Rebuilt['S'] | S2 }> } = Function.dual( 2, (self: AnyBundle, extra: (args: unknown) => Subscription.Subscriptions) => rebuild(self, { subscriptions: (args: unknown) => Subscription.aggregate()( self.subscriptions?.(args) ?? {}, extra(args), ), }), )