import { BasicFun, Fun } from "../../state"; export type BasicUpdater = BasicFun; export type Updater = BasicUpdater & { fun: Fun; then(other: BasicUpdater): Updater; thenMany(others: Array>): Updater; }; export const Updater = (u: BasicUpdater): Updater => { return Object.assign(u, { fun: Fun(u), then: function (this: Updater, other: BasicUpdater): Updater { return Updater((_) => other(this(_))); }, thenMany: function ( this: Updater, others: Array>, ): Updater { return Updater( others.map((_) => Updater(_)).reduce((f, g) => f.then(g), this), ); }, }); }; /*export const Updater = Object.assign((_: BasicUpdater): Updater => { const u = _ as Updater; u.fun = Fun(u); u.thenMany = function (this: Updater, others: Array>): Updater { return Updater(others.map(_ => Updater(_)).reduce((f, g) => f.then(g), this)); }; u.then = function (this: Updater, other: BasicUpdater): Updater { return Updater(_ => other(this(_))); }; return u; }, { withState:(_: BasicFun>) : Updater => Updater(e => _(e)(e)) }); */ /* // little testing playground and microsample: please do not remove import { simpleUpdater, simpleUpdaterWithChildren } from "./domains/simpleUpdater/state"; type City = { name: string, population: number } const City = { Default: (): City => ({ name: "Mirano", population: 25000 }), Updaters: { Core: { ...simpleUpdater()("name"), ...simpleUpdater()("population"), } } } type Address = { street: string, number: number, city: City} const Address = { Default: (): Address => ({ street: "via Don Minzoni", number: 20, city: City.Default() }), Updaters: { Core: { ...simpleUpdater
()("street"), ...simpleUpdater
()("number"), ...simpleUpdaterWithChildren
()(City.Updaters)("city"), } } } type Person = { name: string, surname: string, address: Address } const Person = { Default: (): Person => ({ name: "John", surname: "Doe", address: Address.Default() }), Updaters: { Core: { ...simpleUpdater()("name"), ...simpleUpdater()("surname"), ...simpleUpdaterWithChildren()(Address.Updaters)("address"), } } } const personAndAddressUpdater:Updater = Person.Updaters.Core.name(_ => `Dr. ${_}`).then( Person.Updaters.Core.surname(_ => `${_}, von`) ).then( Person.Updaters.Core.address.children.Core.city.Core.name(_ => `${_} the Great`) ) */