(config: any)
| 20 | import {Cmd, Node, Stack} from './index.h' |
| 21 | |
| 22 | export function attach(config: any) { |
| 23 | let injected |
| 24 | ;[config, injected] = processArgsToConfig(config, true) |
| 25 | const errorTitle = generateErrorTitle('attach', injected) |
| 26 | let {source, effect, mapParams, domain} = config |
| 27 | if (is.effect(effect)) { |
| 28 | assert( |
| 29 | isVoid(domain), |
| 30 | '`domain` can only be used with a plain function', |
| 31 | errorTitle, |
| 32 | ) |
| 33 | } |
| 34 | const attached = createEffect(config, injected) |
| 35 | setMeta(attached, 'attached', true) |
| 36 | setUnitTrace(attached, getUnitTrace(attach)) |
| 37 | const {runner} = getGraph(attached).scope as {runner: Node} |
| 38 | let runnerSteps: Array<Cmd> |
| 39 | const runnerFnStep = (upd: any, _: any, stack: Stack) => { |
| 40 | const {params, req, handler} = upd |
| 41 | const anyway = attached.finally |
| 42 | const rj = onSettled(params, req, false, anyway, stack) |
| 43 | const sourceData = stack.a |
| 44 | const isEffectHandler = is.effect(handler) |
| 45 | let ok = true |
| 46 | let computedParams |
| 47 | if (mapParams) { |
| 48 | ;[ok, computedParams] = runFn(mapParams, rj, [params, sourceData]) |
| 49 | } else { |
| 50 | computedParams = source && isEffectHandler ? sourceData : params |
| 51 | } |
| 52 | if (ok) { |
| 53 | if (isEffectHandler) { |
| 54 | launch({ |
| 55 | target: handler as any, |
| 56 | params: { |
| 57 | params: computedParams, |
| 58 | req: { |
| 59 | rs: onSettled(params, req, true, anyway, stack), |
| 60 | rj, |
| 61 | }, |
| 62 | }, |
| 63 | page: stack.page, |
| 64 | defer: true, |
| 65 | meta: stack.meta, |
| 66 | }) |
| 67 | } else { |
| 68 | upd.args = [sourceData, computedParams] |
| 69 | return true |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | if (source) { |
| 74 | runner.scope.runnerFn = runnerFnStep |
| 75 | let state |
| 76 | if (is.store(source)) { |
| 77 | state = source |
| 78 | own(state, [attached]) |
| 79 | } else { |
searching dependent graphs…