UNPKG

@bemedev/app-react

Version:
1 lines 2.22 kB
{"version":3,"file":"interpret.cjs","sources":["../src/interpret.ts"],"sourcesContent":["import {\n interpret as _interpret,\n getByKey,\n type AnyMachine,\n type ContextFrom,\n type Mode,\n type PrivateContextFrom,\n} from '@bemedev/app-ts';\nimport type { Decompose, State } from './types';\nimport { useSelector } from './useSelector';\nimport { defaultCompare, identity, type Compare_F } from './utils';\nimport { reFunction } from './utils/reFunction';\n\nexport const interpret = <const M extends AnyMachine = AnyMachine>(\n machine: M,\n config: {\n pContext: PrivateContextFrom<M>;\n context: ContextFrom<M>;\n mode?: Mode;\n },\n) => {\n type _State = State<ContextFrom<M>>;\n\n const service = _interpret(machine, config);\n const start = reFunction(service, 'start');\n const stop = reFunction(service, 'stop');\n const send = reFunction(service, 'send');\n const addOptions = reFunction(service, 'addOptions');\n const selector = identity<keyof Decompose<_State>>;\n\n const useState = <\n D extends Decompose<_State>,\n K extends Extract<keyof D, string>,\n R extends D[K],\n >(\n key?: K,\n compare: Compare_F = defaultCompare,\n ) => {\n const out = useSelector<M, keyof D extends K ? _State : R>(\n service,\n state => (!key ? state : getByKey(state, key)),\n compare,\n );\n\n return out;\n };\n\n return { start, stop, send, selector, useState, addOptions };\n};\n"],"names":["_interpret","reFunction","identity","defaultCompare","useSelector","getByKey"],"mappings":";;;;;;;;;MAaa,SAAS,GAAG,CACvB,OAAU,EACV,MAIC,KACC;IAGF,MAAM,OAAO,GAAGA,eAAU,CAAC,OAAO,EAAE,MAAM,CAAC;IAC3C,MAAM,KAAK,GAAGC,2BAAU,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1C,MAAM,IAAI,GAAGA,2BAAU,CAAC,OAAO,EAAE,MAAM,CAAC;IACxC,MAAM,IAAI,GAAGA,2BAAU,CAAC,OAAO,EAAE,MAAM,CAAC;IACxC,MAAM,UAAU,GAAGA,2BAAU,CAAC,OAAO,EAAE,YAAY,CAAC;AACpD,IAAA,MAAM,QAAQ,IAAGC,uBAAiC,CAAA;IAElD,MAAM,QAAQ,GAAG,CAKf,GAAO,EACP,OAAqB,GAAAC,4BAAc,KACjC;AACF,QAAA,MAAM,GAAG,GAAGC,uBAAW,CACrB,OAAO,EACP,KAAK,KAAK,CAAC,GAAG,GAAG,KAAK,GAAGC,cAAQ,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,EAC9C,OAAO,CACR;AAED,QAAA,OAAO,GAAG;AACZ,KAAC;AAED,IAAA,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,QAAQ,EAAE,QAAQ,EAAE,UAAU,EAAE;AAC9D;;;;"}