/** * 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 ['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 ['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