/** * Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author David Sehnal */ import { ParamDefinition as PD } from './param-definition'; import { produce } from 'immer'; import { Mutable } from './type-helpers'; export interface ParamMapping { params(ctx: Ctx): PD.For, getTarget(ctx: Ctx): T, getValues(t: T, ctx: Ctx): S, update(s: S, ctx: Ctx): T, apply(t: T, ctx: Ctx): void | Promise } export function ParamMapping(def: { params: ((ctx: Ctx) => PD.For) | PD.For, target(ctx: Ctx): T }): (options: { values(t: T, ctx: Ctx): S, update(s: S, t: Mutable, ctx: Ctx): void, apply?(t: T, ctx: Ctx): void | Promise }) => ParamMapping { return ({ values, update, apply }) => ({ params: typeof def.params === 'function' ? def.params as any : ctx => def.params, getTarget: def.target, getValues: values, update(s, ctx) { const t = def.target(ctx); return produce(t, (t1: Mutable) => update(s, t1, ctx)); }, apply: apply ? apply : () => { } }); }