( scope: Scope, sourceRef: StateRef, isGetState?: boolean, isKernelCall?: boolean, softRead?: boolean, )
| 465 | const noopParser = (x: any) => x |
| 466 | |
| 467 | export const initRefInScope = ( |
| 468 | scope: Scope, |
| 469 | sourceRef: StateRef, |
| 470 | isGetState?: boolean, |
| 471 | isKernelCall?: boolean, |
| 472 | softRead?: boolean, |
| 473 | ) => { |
| 474 | const refsMap = scope.reg |
| 475 | if (refsMap[sourceRef.id]) return |
| 476 | const sid = sourceRef.sid |
| 477 | const ref: StateRef = { |
| 478 | id: sourceRef.id, |
| 479 | current: sourceRef.initial!, |
| 480 | meta: sourceRef.meta, |
| 481 | } |
| 482 | |
| 483 | if (ref.id in scope.values.idMap) { |
| 484 | ref.current = scope.values.idMap[ref.id] |
| 485 | } else if (sid && sid in scope.values.sidMap && !(sid in scope.sidIdMap)) { |
| 486 | const serialize = sourceRef?.meta?.serialize |
| 487 | const parser = |
| 488 | scope.fromSerialize && serialize !== 'ignore' |
| 489 | ? serialize?.read || noopParser |
| 490 | : noopParser |
| 491 | ref.current = parser(scope.values.sidMap[sid]) |
| 492 | } else { |
| 493 | if (sourceRef.before && !softRead) { |
| 494 | let isFresh = false |
| 495 | const needToAssign = isGetState || !sourceRef.noInit || isKernelCall |
| 496 | forEach(sourceRef.before, cmd => { |
| 497 | switch (cmd.type) { |
| 498 | case 'map': { |
| 499 | const from = cmd.from |
| 500 | if (from || cmd.fn) { |
| 501 | if (from) initRefInScope(scope, from, isGetState, isKernelCall) |
| 502 | if (needToAssign) { |
| 503 | const value = from && refsMap[from.id].current |
| 504 | ref.current = cmd.fn ? cmd.fn(value) : value |
| 505 | } |
| 506 | } |
| 507 | break |
| 508 | } |
| 509 | case 'field': { |
| 510 | initRefInScope(scope, cmd.from, isGetState, isKernelCall) |
| 511 | if (!isFresh) { |
| 512 | isFresh = true |
| 513 | if (Array.isArray(ref.current)) { |
| 514 | ref.current = [...ref.current] |
| 515 | } else { |
| 516 | ref.current = {...ref.current} |
| 517 | } |
| 518 | } |
| 519 | if (needToAssign) { |
| 520 | const from = refsMap[cmd.from.id] |
| 521 | ref.current[cmd.field] = refsMap[from.id].current |
| 522 | } |
| 523 | break |
| 524 | } |
no test coverage detected
searching dependent graphs…