* The used value calculation algorithm. * * @param {IContextPropUsed } used * @param {boolean} refreshParent * @return {T|undefined} The used value. * @private * @template T
(used, refreshParent)
| 554 | * @template T |
| 555 | */ |
| 556 | calc_(used, refreshParent) { |
| 557 | devAssert(this.isConnected_()); |
| 558 | |
| 559 | const {depValues, prop} = used; |
| 560 | const {compute, defaultValue, key} = prop; |
| 561 | |
| 562 | const inputValues = this.inputsByKey_?.get(key)?.values; |
| 563 | |
| 564 | // Calculate parent value. |
| 565 | const recursive = calcRecursive(prop, inputValues); |
| 566 | |
| 567 | // Refresh parent if requested. |
| 568 | if (refreshParent || recursive != Boolean(used.parentContextNode)) { |
| 569 | const newParentContextNode = recursive |
| 570 | ? findParent(this.contextNode_, hasInput, prop, /* includeSelf */ false) |
| 571 | : null; |
| 572 | this.updateParentContextNode_(used, newParentContextNode); |
| 573 | } |
| 574 | |
| 575 | // If no parent node is found, use the default value. |
| 576 | const parentValue = isDefined(used.parentValue) |
| 577 | ? used.parentValue |
| 578 | : recursive && !used.parentContextNode |
| 579 | ? defaultValue |
| 580 | : undefined; |
| 581 | |
| 582 | // Calculate the "used" value. |
| 583 | let newValue = undefined; |
| 584 | const ready = |
| 585 | depValues.every(isDefined) && (!recursive || isDefined(parentValue)); |
| 586 | if (ready) { |
| 587 | const {node} = this.contextNode_; |
| 588 | if (inputValues && !compute) { |
| 589 | newValue = inputValues[0]; |
| 590 | } else if (isRecursive(prop)) { |
| 591 | if (inputValues || depValues.length > 0) { |
| 592 | // The node specifies its own input values and they need to be |
| 593 | // recomputed with parent and dep values. |
| 594 | newValue = callRecursiveCompute( |
| 595 | compute, |
| 596 | node, |
| 597 | inputValues || EMPTY_ARRAY, |
| 598 | parentValue, |
| 599 | depValues |
| 600 | ); |
| 601 | } else if (isDefined(parentValue)) { |
| 602 | // The node doesn't specify its own value, but parent is available. |
| 603 | // Since parent is available, it means that the node is recursive. |
| 604 | newValue = parentValue; |
| 605 | } |
| 606 | } else if (compute) { |
| 607 | newValue = callCompute( |
| 608 | compute, |
| 609 | node, |
| 610 | inputValues || EMPTY_ARRAY, |
| 611 | depValues |
| 612 | ); |
| 613 | } |
no test coverage detected