1 | ;
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.SettlementSignal = void 0;
|
4 | /**
|
5 | * SettlementSignal is used to signal the resolution of a provider/instance.
|
6 | * Calling `complete` or `error` will resolve the promise returned by `asPromise`.
|
7 | * Can be used to detect circular dependencies.
|
8 | */
|
9 | class SettlementSignal {
|
10 | constructor() {
|
11 | this._refs = new Set();
|
12 | this.completed = false;
|
13 | this.settledPromise = new Promise(resolve => {
|
14 | this.settleFn = resolve;
|
15 | });
|
16 | }
|
17 | /**
|
18 | * Resolves the promise returned by `asPromise`.
|
19 | */
|
20 | complete() {
|
21 | this.completed = true;
|
22 | this.settleFn();
|
23 | }
|
24 | /**
|
25 | * Rejects the promise returned by `asPromise` with the given error.
|
26 | * @param err Error to reject the promise returned by `asPromise` with.
|
27 | */
|
28 | error(err) {
|
29 | this.completed = true;
|
30 | this.settleFn(err);
|
31 | }
|
32 | /**
|
33 | * Returns a promise that will be resolved when `complete` or `error` is called.
|
34 | * @returns Promise that will be resolved when `complete` or `error` is called.
|
35 | */
|
36 | asPromise() {
|
37 | return this.settledPromise;
|
38 | }
|
39 | /**
|
40 | * Inserts a wrapper id that the host of this signal depends on.
|
41 | * @param wrapperId Wrapper id to insert.
|
42 | */
|
43 | insertRef(wrapperId) {
|
44 | this._refs.add(wrapperId);
|
45 | }
|
46 | /**
|
47 | * Check if relationship is circular.
|
48 | * @param wrapperId Wrapper id to check.
|
49 | * @returns True if relationship is circular, false otherwise.
|
50 | */
|
51 | isCycle(wrapperId) {
|
52 | return !this.completed && this._refs.has(wrapperId);
|
53 | }
|
54 | }
|
55 | exports.SettlementSignal = SettlementSignal;
|