* Node in the tree that manages the collective state of shutdowns, so that * terminations and disconnections happen gracefully in-order.
| 612 | * terminations and disconnections happen gracefully in-order. |
| 613 | */ |
| 614 | class TreeNode { |
| 615 | public static targetNodes = new WeakMap<ITarget, TreeNode>(); |
| 616 | public readonly threadData = getDeferred<ThreadData>(); |
| 617 | |
| 618 | private _state = TargetState.Running; |
| 619 | private readonly _children = new Set<TreeNode>(); |
| 620 | private readonly _stateChangeEmitter = new EventEmitter<TargetState>(); |
| 621 | |
| 622 | public get state() { |
| 623 | return this._state; |
| 624 | } |
| 625 | |
| 626 | public set state(state: TargetState) { |
| 627 | if (state > this._state) { |
| 628 | this._state = state; |
| 629 | this._stateChangeEmitter.fire(state); |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | /** Value is only undefined for the root node */ |
| 634 | constructor(public readonly value: ITarget | undefined) { |
| 635 | if (value) { |
| 636 | TreeNode.targetNodes.set(value, this); |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | /** |
| 641 | * Adds a new child to the target tree. |
| 642 | */ |
| 643 | public add(child: TreeNode) { |
| 644 | this._children.add(child); |
| 645 | } |
| 646 | |
| 647 | /** |
| 648 | * Removes a child to the target tree. |
| 649 | */ |
| 650 | public remove(child: TreeNode) { |
| 651 | this._children.delete(child); |
| 652 | } |
| 653 | |
| 654 | /** |
| 655 | * Returns a promise that resolves when this node has reached at least the |
| 656 | * given state in the lifecycle. |
| 657 | */ |
| 658 | public async waitUntil(state: TargetState) { |
| 659 | if (this._state >= state) { |
| 660 | return Promise.resolve(); |
| 661 | } |
| 662 | |
| 663 | return new Promise<void>(resolve => { |
| 664 | const l = this._stateChangeEmitter.event(s => { |
| 665 | if (s >= state) { |
| 666 | l.dispose(); |
| 667 | resolve(); |
| 668 | } |
| 669 | }); |
| 670 | }); |
| 671 | } |
nothing calls this directly
no test coverage detected