| 585 | type NodeState = number |
| 586 | |
| 587 | class NodeImpl<A> { |
| 588 | constructor( |
| 589 | registry: RegistryImpl, |
| 590 | atom: Atom.Atom<A> |
| 591 | ) { |
| 592 | this.registry = registry |
| 593 | this.atom = atom |
| 594 | this.writeContext = new WriteContextImpl(registry, this) |
| 595 | } |
| 596 | |
| 597 | readonly registry: RegistryImpl |
| 598 | readonly atom: Atom.Atom<A> |
| 599 | state: NodeState = NodeState.uninitialized |
| 600 | lifetime: Lifetime<A> | undefined |
| 601 | writeContext: WriteContextImpl<A> |
| 602 | preserveInitialValueOnBuild = false |
| 603 | |
| 604 | parents = new Set<NodeImpl<any>>() |
| 605 | previousParents: Set<NodeImpl<any>> | undefined |
| 606 | children = new Set<NodeImpl<any>>() |
| 607 | listeners = new Set<() => void>() |
| 608 | skipInvalidation = false |
| 609 | building = false |
| 610 | invalidatedDuringBuild = false |
| 611 | |
| 612 | currentState() { |
| 613 | switch (this.state) { |
| 614 | case NodeState.uninitialized: |
| 615 | return "uninitialized" |
| 616 | case NodeState.stale: |
| 617 | return "stale" |
| 618 | case NodeState.valid: |
| 619 | return "valid" |
| 620 | default: |
| 621 | return "removed" |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | get canBeRemoved(): boolean { |
| 626 | return !this.atom.keepAlive && this.listeners.size === 0 && this.children.size === 0 && this.state !== 0 |
| 627 | } |
| 628 | |
| 629 | _value: A = undefined as any |
| 630 | value(): A { |
| 631 | if ((this.state & NodeFlags.waitingForValue) !== 0) { |
| 632 | this.lifetime = makeLifetime(this) |
| 633 | this.building = true |
| 634 | const value = this.atom.read(this.lifetime) |
| 635 | this.building = false |
| 636 | if ((this.state & NodeFlags.waitingForValue) !== 0) { |
| 637 | if (this.preserveInitialValueOnBuild) { |
| 638 | this.preserveInitialValueOnBuild = false |
| 639 | this.state = NodeState.valid |
| 640 | } else { |
| 641 | this.setValue(value) |
| 642 | } |
| 643 | } |
| 644 |
nothing calls this directly
no outgoing calls
no test coverage detected