File size: 1,790 Bytes
4e23b01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { IInstantiationService } from '#/_base/di/instantiation';
import type { InstantiationService } from '#/_base/di/instantiationService';
import { ScopeActivation, registerScopedService } from '#/_base/di/scope';
import { LifecycleScope } from '#/app/scopes';

import { IDebugLedgerService, type DebugLedgerNode, type DebugUnit } from './debugLedger';
import { scopeContainerLabel } from './scopeTree';

export class DebugLedgerService implements IDebugLedgerService {
  declare readonly _serviceBrand: undefined;

  private readonly root: InstantiationService;

  constructor(@IInstantiationService instantiation: IInstantiationService) {
    this.root = instantiation as InstantiationService;
  }

  tree(): DebugLedgerNode {
    return this._node(this.root, scopeContainerLabel(this.root));
  }

  private _node(container: InstantiationService, path: string): DebugLedgerNode {
    return {
      path,
      label: scopeContainerLabel(container),
      units: joinUnits(container),
      ledger: container.ledger.entries(),
      children: container.children.map((child) =>
        this._node(child, `${path}/${scopeContainerLabel(child)}`),
      ),
    };
  }
}

function joinUnits(container: InstantiationService): DebugUnit[] {
  const states = new Map(container.cascade.unitsSnapshot().map((unit) => [unit.token, unit]));
  return container.servicesSnapshot().map((registration) => {
    const unit = states.get(registration.token);
    return {
      token: registration.token,
      uid: registration.uid,
      state: unit?.state,
      error: unit?.error,
      everActive: unit?.everActive,
      inFlight: unit?.inFlight,
    };
  });
}

registerScopedService(
  LifecycleScope.App,
  IDebugLedgerService,
  DebugLedgerService,
  ScopeActivation.OnDemand,
  'debug',
);