@milaboratories/pl-tree 1.12.13 → 1.12.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/accessors.cjs +71 -1
- package/dist/accessors.cjs.map +1 -1
- package/dist/accessors.d.ts +29 -1
- package/dist/accessors.d.ts.map +1 -1
- package/dist/accessors.js +68 -1
- package/dist/accessors.js.map +1 -1
- package/dist/dump.cjs.map +1 -1
- package/dist/dump.d.ts.map +1 -1
- package/dist/dump.js.map +1 -1
- package/dist/index.cjs +4 -0
- package/dist/index.d.ts +3 -3
- package/dist/index.js +2 -2
- package/dist/snapshot.cjs +1 -2
- package/dist/snapshot.cjs.map +1 -1
- package/dist/snapshot.d.ts.map +1 -1
- package/dist/snapshot.js +1 -1
- package/dist/snapshot.js.map +1 -1
- package/dist/state.cjs +78 -9
- package/dist/state.cjs.map +1 -1
- package/dist/state.d.ts +31 -4
- package/dist/state.d.ts.map +1 -1
- package/dist/state.js +78 -8
- package/dist/state.js.map +1 -1
- package/dist/sync.cjs +4 -2
- package/dist/sync.cjs.map +1 -1
- package/dist/sync.d.ts.map +1 -1
- package/dist/sync.js +3 -1
- package/dist/sync.js.map +1 -1
- package/dist/synchronized_tree.cjs +106 -10
- package/dist/synchronized_tree.cjs.map +1 -1
- package/dist/synchronized_tree.d.ts +49 -5
- package/dist/synchronized_tree.d.ts.map +1 -1
- package/dist/synchronized_tree.js +107 -11
- package/dist/synchronized_tree.js.map +1 -1
- package/dist/traversal_ops.d.ts.map +1 -1
- package/dist/value_and_error.cjs.map +1 -1
- package/dist/value_and_error.d.ts.map +1 -1
- package/dist/value_and_error.js.map +1 -1
- package/dist/value_or_error.d.ts.map +1 -1
- package/package.json +6 -6
- package/src/accessors.ts +79 -0
- package/src/state.ts +101 -12
- package/src/sync.ts +3 -2
- package/src/synchronized_tree.ts +153 -12
|
@@ -5,13 +5,39 @@ const require_sync = require("./sync.cjs");
|
|
|
5
5
|
let _milaboratories_pl_client = require("@milaboratories/pl-client");
|
|
6
6
|
let _milaboratories_computable = require("@milaboratories/computable");
|
|
7
7
|
let node_timers_promises = require("node:timers/promises");
|
|
8
|
-
node_timers_promises = require_runtime.__toESM(node_timers_promises);
|
|
8
|
+
node_timers_promises = require_runtime.__toESM(node_timers_promises, 1);
|
|
9
9
|
//#region src/synchronized_tree.ts
|
|
10
10
|
/** Hard floor between consecutive tree-refresh calls.
|
|
11
11
|
* Applies even when {@link scheduleOnNextState} has woken the loop early,
|
|
12
12
|
* preventing tight polling loops during rapid state transitions. */
|
|
13
13
|
const MIN_POLLING_INTERVAL_MS = 100;
|
|
14
|
+
/** Normalizes the {@link SynchronizedTreeState.init} seed argument — a bare
|
|
15
|
+
* {@link SignedResourceId}, a single {@link TreeSeed}, or an array — to `TreeSeed[]`, so
|
|
16
|
+
* every existing single-root caller is unchanged. */
|
|
17
|
+
function normalizeSeeds(seeds) {
|
|
18
|
+
if (Array.isArray(seeds)) return seeds;
|
|
19
|
+
if (typeof seeds === "object" && seeds !== null && "kind" in seeds) return [seeds];
|
|
20
|
+
return [{
|
|
21
|
+
kind: "resource",
|
|
22
|
+
root: seeds
|
|
23
|
+
}];
|
|
24
|
+
}
|
|
25
|
+
/** How often, in main-loop iterations, a tree with shared-type seeds re-polls
|
|
26
|
+
* ListUserResources to reconcile its discovered roots. 1 would mean every iteration.
|
|
27
|
+
*
|
|
28
|
+
* Discovery (a full ListUserResources stream) is far heavier than an ordinary incremental
|
|
29
|
+
* refresh, so it must NOT run on every fast refresh tick. The refresh cadence is the tree's
|
|
30
|
+
* `pollingInterval` floored by {@link MIN_POLLING_INTERVAL_MS} (~200ms-1s in practice — the
|
|
31
|
+
* shared-seed discovery tree runs at the 200ms default). At N = 15 discovery fires roughly
|
|
32
|
+
* every 15 × 200ms ≈ 3s, decoupling it from the fast refresh loop while keeping the latency
|
|
33
|
+
* of noticing a new/removed share to a few seconds — acceptable for a human-driven share flow.
|
|
34
|
+
*
|
|
35
|
+
* Only trees with shared-type seeds gate on this; {@link discover} is a no-op for single-root
|
|
36
|
+
* and explicit-seed trees (empty `sharedSeeds`), so the value never affects them. */
|
|
37
|
+
const DISCOVERY_EVERY_N_REFRESHES = 15;
|
|
14
38
|
var SynchronizedTreeState = class SynchronizedTreeState {
|
|
39
|
+
pl;
|
|
40
|
+
logger;
|
|
15
41
|
finalPredicate;
|
|
16
42
|
state;
|
|
17
43
|
pollingInterval;
|
|
@@ -22,9 +48,14 @@ var SynchronizedTreeState = class SynchronizedTreeState {
|
|
|
22
48
|
logStat;
|
|
23
49
|
hooks;
|
|
24
50
|
abortController = new AbortController();
|
|
25
|
-
|
|
51
|
+
/** Explicit-resource seeds: fixed roots, present from construction. */
|
|
52
|
+
explicitRoots;
|
|
53
|
+
/** Shared-type seeds (discovered roots), if any. */
|
|
54
|
+
sharedSeeds;
|
|
55
|
+
/** Roots discovered for shared-type seeds on the last discovery poll. */
|
|
56
|
+
discoveredRoots = [];
|
|
57
|
+
constructor(pl, seeds, ops, logger) {
|
|
26
58
|
this.pl = pl;
|
|
27
|
-
this.root = root;
|
|
28
59
|
this.logger = logger;
|
|
29
60
|
const { finalPredicateOverride, pruning, fieldFilter, traverseStopRules, traversalMode, pollingInterval, stopPollingDelay, logStat } = ops;
|
|
30
61
|
this.pruning = pruning;
|
|
@@ -34,20 +65,45 @@ var SynchronizedTreeState = class SynchronizedTreeState {
|
|
|
34
65
|
this.pollingInterval = pollingInterval;
|
|
35
66
|
this.finalPredicate = finalPredicateOverride ?? pl.finalPredicate;
|
|
36
67
|
this.logStat = logStat;
|
|
37
|
-
this.
|
|
68
|
+
this.explicitRoots = seeds.filter((s) => s.kind === "resource").map((s) => s.root);
|
|
69
|
+
this.sharedSeeds = seeds.filter((s) => s.kind === "shared");
|
|
70
|
+
this.state = new require_state.PlTreeState(this.currentRootSet(), this.finalPredicate);
|
|
38
71
|
this.hooks = new _milaboratories_computable.PollingComputableHooks(() => this.startUpdating(), () => this.stopUpdating(), { stopDebounce: stopPollingDelay }, (resolve, reject) => this.scheduleOnNextState(resolve, reject));
|
|
39
72
|
}
|
|
73
|
+
/** The current protected root set: explicit roots plus the latest discovered roots. */
|
|
74
|
+
currentRootSet() {
|
|
75
|
+
return /* @__PURE__ */ new Set([...this.explicitRoots, ...this.discoveredRoots]);
|
|
76
|
+
}
|
|
77
|
+
/** Resolves the single root for the backward-compatible single-root accessors, throwing
|
|
78
|
+
* if the tree does not have exactly one root (guards legacy callers against multi-root). */
|
|
79
|
+
soleRoot() {
|
|
80
|
+
const roots = this.currentRootSet();
|
|
81
|
+
if (roots.size !== 1) throw new Error(`single-root accessor used on a tree with ${roots.size} roots; use rootsEntry() instead`);
|
|
82
|
+
return roots.values().next().value;
|
|
83
|
+
}
|
|
40
84
|
/** @deprecated use "entry" instead */
|
|
41
|
-
accessor(rid
|
|
85
|
+
accessor(rid) {
|
|
42
86
|
if (this.terminated) throw new Error("tree synchronization is terminated");
|
|
43
87
|
return this.entry(rid);
|
|
44
88
|
}
|
|
45
|
-
entry
|
|
89
|
+
/** Backward-compatible single-root entry. With no `rid` it returns the sole root's entry
|
|
90
|
+
* and THROWS if the tree has zero or more than one root. An explicit `rid` addresses any
|
|
91
|
+
* resource in the heap, as today. */
|
|
92
|
+
entry(rid) {
|
|
46
93
|
if (this.terminated) throw new Error("tree synchronization is terminated");
|
|
47
94
|
return new require_accessors.PlTreeEntry({
|
|
48
95
|
treeProvider: () => this.state,
|
|
49
96
|
hooks: this.hooks
|
|
50
|
-
}, rid);
|
|
97
|
+
}, rid ?? this.soleRoot());
|
|
98
|
+
}
|
|
99
|
+
/** Reactive provider for the current root SET. Reading it inside a Computable tracks the
|
|
100
|
+
* set as a dependency, so the Computable recomputes when discovered roots appear/disappear. */
|
|
101
|
+
rootsEntry() {
|
|
102
|
+
if (this.terminated) throw new Error("tree synchronization is terminated");
|
|
103
|
+
return new require_accessors.PlTreeRootsEntry({
|
|
104
|
+
treeProvider: () => this.state,
|
|
105
|
+
hooks: this.hooks
|
|
106
|
+
});
|
|
51
107
|
}
|
|
52
108
|
/** Can be used to externally kick off the synchronization polling loop, and
|
|
53
109
|
* await for the first synchronization to happen. */
|
|
@@ -88,21 +144,51 @@ var SynchronizedTreeState = class SynchronizedTreeState {
|
|
|
88
144
|
/** Executed from the main loop, and initialization procedure. */
|
|
89
145
|
async refresh(stats, txOps) {
|
|
90
146
|
if (this.terminated) throw new Error("tree synchronization is terminated");
|
|
147
|
+
try {
|
|
148
|
+
await this.loadAndApply(stats, txOps);
|
|
149
|
+
} catch (e) {
|
|
150
|
+
if (this.sharedSeeds.length > 0 && (0, _milaboratories_pl_client.isUnauthenticated)(e)) {
|
|
151
|
+
this.logger?.warn("discovery tree: Unauthenticated on ResourceTree (likely revoked/expired root); re-discovering and retrying");
|
|
152
|
+
await this.discover();
|
|
153
|
+
await this.loadAndApply(stats, txOps);
|
|
154
|
+
} else throw e;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
async loadAndApply(stats, txOps) {
|
|
91
158
|
const request = require_sync.constructTreeLoadingRequest(this.state, {
|
|
92
159
|
pruningFunction: this.pruning,
|
|
93
160
|
fieldFilter: this.fieldFilter,
|
|
94
161
|
traverseStopRules: this.traverseStopRules
|
|
95
162
|
});
|
|
163
|
+
if (request.seedResources.length === 0 && request.finalResources.size === 0) return;
|
|
96
164
|
const data = await this.pl.withReadTx("ReadingTree", async (tx) => {
|
|
97
165
|
return await require_sync.loadTreeState(tx, request, stats, this.pl.serverInfo.capabilities ?? [], this.traversalMode, this.logger);
|
|
98
166
|
}, txOps);
|
|
99
167
|
this.state.updateFromResourceData(data, true);
|
|
100
168
|
}
|
|
169
|
+
/** Discovery sync for shared-type seeds: re-polls `ListUserResources` (gRPC-only) and
|
|
170
|
+
* reconciles the discovered root set against the heap. A longer poll result adds roots; a
|
|
171
|
+
* shorter one removes them (grant revoked/expired) — the removed roots' subtrees cascade
|
|
172
|
+
* to collection via the ordinary refcount GC ({@link PlTreeState.setRoots}). No-op when the
|
|
173
|
+
* tree has no shared-type seeds, or silently no-op on a REST client where `ListUserResources`
|
|
174
|
+
* is unavailable. */
|
|
175
|
+
async discover() {
|
|
176
|
+
if (this.terminated) throw new Error("tree synchronization is terminated");
|
|
177
|
+
if (this.sharedSeeds.length === 0) return;
|
|
178
|
+
const discovered = /* @__PURE__ */ new Set();
|
|
179
|
+
for (const seed of this.sharedSeeds) {
|
|
180
|
+
const ids = await this.pl.userResources.listSharedResourcesByType(seed.resourceType.name);
|
|
181
|
+
for (const id of ids) discovered.add(id);
|
|
182
|
+
}
|
|
183
|
+
this.discoveredRoots = [...discovered];
|
|
184
|
+
this.state.setRoots(this.currentRootSet());
|
|
185
|
+
}
|
|
101
186
|
/** If true this tree state is permanently terminaed. */
|
|
102
187
|
terminated = false;
|
|
103
188
|
async mainLoop() {
|
|
104
189
|
let stat = this.logStat ? require_sync.initialTreeLoadingStat() : void 0;
|
|
105
190
|
let lastUpdate = Date.now();
|
|
191
|
+
let iteration = 0;
|
|
106
192
|
while (true) {
|
|
107
193
|
if (!this.keepRunning || this.terminated) break;
|
|
108
194
|
let toNotify = void 0;
|
|
@@ -112,6 +198,8 @@ var SynchronizedTreeState = class SynchronizedTreeState {
|
|
|
112
198
|
}
|
|
113
199
|
try {
|
|
114
200
|
if (this.logStat === "per-request") stat = require_sync.initialTreeLoadingStat();
|
|
201
|
+
if (this.sharedSeeds.length > 0 && iteration % DISCOVERY_EVERY_N_REFRESHES === 0) await this.discover();
|
|
202
|
+
iteration++;
|
|
115
203
|
await this.refresh(stat);
|
|
116
204
|
if (stat && this.logger) this.logger.info(`Tree stat (success, after ${Date.now() - lastUpdate}ms): ${JSON.stringify(stat)}`);
|
|
117
205
|
lastUpdate = Date.now();
|
|
@@ -123,7 +211,7 @@ var SynchronizedTreeState = class SynchronizedTreeState {
|
|
|
123
211
|
if (e instanceof require_state.TreeStateUpdateError) {
|
|
124
212
|
this.logger?.error(e);
|
|
125
213
|
this.state.invalidateTree("stat update error");
|
|
126
|
-
this.state = new require_state.PlTreeState(this.
|
|
214
|
+
this.state = new require_state.PlTreeState(this.currentRootSet(), this.finalPredicate);
|
|
127
215
|
continue;
|
|
128
216
|
} else this.logger?.warn(e);
|
|
129
217
|
}
|
|
@@ -174,11 +262,19 @@ var SynchronizedTreeState = class SynchronizedTreeState {
|
|
|
174
262
|
if (this.currentLoop === void 0) return;
|
|
175
263
|
await this.currentLoop;
|
|
176
264
|
}
|
|
177
|
-
|
|
178
|
-
|
|
265
|
+
/**
|
|
266
|
+
* Initializes a synchronized tree from one or more seeds.
|
|
267
|
+
*
|
|
268
|
+
* @param seeds a bare {@link SignedResourceId} (the original single-root contract), a
|
|
269
|
+
* single {@link TreeSeed}, or an array of seeds. Bare ids and explicit-resource seeds
|
|
270
|
+
* become roots immediately; shared-type seeds discover their roots via `ListUserResources`.
|
|
271
|
+
*/
|
|
272
|
+
static async init(pl, seeds, ops, logger) {
|
|
273
|
+
const tree = new SynchronizedTreeState(pl, normalizeSeeds(seeds), ops, logger);
|
|
179
274
|
const stat = ops.logStat ? require_sync.initialTreeLoadingStat() : void 0;
|
|
180
275
|
let ok = false;
|
|
181
276
|
try {
|
|
277
|
+
await tree.discover();
|
|
182
278
|
await tree.refresh(stat, { timeout: ops.initialTreeLoadingTimeout });
|
|
183
279
|
ok = true;
|
|
184
280
|
} finally {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"synchronized_tree.cjs","names":["PlTreeState","PollingComputableHooks","PlTreeEntry","constructTreeLoadingRequest","loadTreeState","initialTreeLoadingStat","TreeStateUpdateError","tp"],"sources":["../src/synchronized_tree.ts"],"sourcesContent":["import { PollingComputableHooks } from \"@milaboratories/computable\";\nimport { PlTreeEntry } from \"./accessors\";\nimport type {\n FinalResourceDataPredicate,\n PlClient,\n SignedResourceId,\n TxOps,\n} from \"@milaboratories/pl-client\";\nimport type { Filter } from \"@milaboratories/pl-client\";\nimport { isTimeoutOrCancelError } from \"@milaboratories/pl-client\";\nimport type { ExtendedResourceData } from \"./state\";\nimport { PlTreeState, TreeStateUpdateError } from \"./state\";\nimport type { PruningFunction, TraversalMode, TreeLoadingStat } from \"./sync\";\nimport { constructTreeLoadingRequest, initialTreeLoadingStat, loadTreeState } from \"./sync\";\nimport * as tp from \"node:timers/promises\";\n\n/** Hard floor between consecutive tree-refresh calls.\n * Applies even when {@link scheduleOnNextState} has woken the loop early,\n * preventing tight polling loops during rapid state transitions. */\nconst MIN_POLLING_INTERVAL_MS = 100;\nimport type { MiLogger } from \"@milaboratories/ts-helpers\";\n\ntype StatLoggingMode = \"cumulative\" | \"per-request\";\n\nexport type SynchronizedTreeOps = {\n /** Override final predicate from the PlClient */\n finalPredicateOverride?: FinalResourceDataPredicate;\n\n /** Pruning function for legacy fallback path. */\n pruning?: PruningFunction;\n\n /** ResourceTree field filter for modern backend path. */\n fieldFilter?: Filter;\n\n /** ResourceTree traversal stop rules for modern backend path. */\n traverseStopRules?: Filter;\n\n /** Interval after last sync to sleep before the next one */\n pollingInterval: number;\n /** For how long to continue polling after the last derived value access */\n stopPollingDelay: number;\n\n /** If one of the values, tree will log stats of each polling request */\n logStat?: StatLoggingMode;\n\n /** Timeout for initial tree loading. If not specified, will use default for RO tx from pl-client. */\n initialTreeLoadingTimeout?: number;\n\n /** Controls which tree-loading path to use. Default `\"auto\"`. */\n traversalMode?: TraversalMode;\n};\n\ntype ScheduledRefresh = {\n resolve: () => void;\n reject: (err: any) => void;\n};\n\nexport class SynchronizedTreeState {\n private readonly finalPredicate: FinalResourceDataPredicate;\n private state: PlTreeState;\n private readonly pollingInterval: number;\n private readonly pruning?: PruningFunction;\n private readonly fieldFilter?: Filter;\n private readonly traverseStopRules?: Filter;\n private readonly traversalMode: TraversalMode;\n private readonly logStat?: StatLoggingMode;\n private readonly hooks: PollingComputableHooks;\n private readonly abortController = new AbortController();\n\n private constructor(\n private readonly pl: PlClient,\n private readonly root: SignedResourceId,\n ops: SynchronizedTreeOps,\n private readonly logger?: MiLogger,\n ) {\n const {\n finalPredicateOverride,\n pruning,\n fieldFilter,\n traverseStopRules,\n traversalMode,\n pollingInterval,\n stopPollingDelay,\n logStat,\n } = ops;\n this.pruning = pruning;\n this.fieldFilter = fieldFilter;\n this.traverseStopRules = traverseStopRules;\n this.traversalMode = traversalMode ?? \"auto\";\n this.pollingInterval = pollingInterval;\n this.finalPredicate = finalPredicateOverride ?? pl.finalPredicate;\n this.logStat = logStat;\n this.state = new PlTreeState(root, this.finalPredicate);\n this.hooks = new PollingComputableHooks(\n () => this.startUpdating(),\n () => this.stopUpdating(),\n { stopDebounce: stopPollingDelay },\n (resolve, reject) => this.scheduleOnNextState(resolve, reject),\n );\n }\n\n /** @deprecated use \"entry\" instead */\n public accessor(rid: SignedResourceId = this.root): PlTreeEntry {\n if (this.terminated) throw new Error(\"tree synchronization is terminated\");\n return this.entry(rid);\n }\n\n public entry(rid: SignedResourceId = this.root): PlTreeEntry {\n if (this.terminated) throw new Error(\"tree synchronization is terminated\");\n return new PlTreeEntry({ treeProvider: () => this.state, hooks: this.hooks }, rid);\n }\n\n /** Can be used to externally kick off the synchronization polling loop, and\n * await for the first synchronization to happen. */\n public async refreshState(): Promise<void> {\n if (this.terminated) throw new Error(\"tree synchronization is terminated\");\n await this.hooks.refreshState();\n }\n\n private currentLoopDelayInterrupt: AbortController | undefined = undefined;\n private scheduledOnNextState: ScheduledRefresh[] = [];\n\n /** Called from computable hooks when external observer asks for state refresh */\n private scheduleOnNextState(resolve: () => void, reject: (err: any) => void): void {\n if (this.terminated) reject(new Error(\"tree synchronization is terminated\"));\n else {\n this.scheduledOnNextState.push({ resolve, reject });\n if (this.currentLoopDelayInterrupt) {\n this.currentLoopDelayInterrupt.abort();\n this.currentLoopDelayInterrupt = undefined;\n }\n }\n }\n\n /** Called from observer */\n private startUpdating(): void {\n if (this.terminated) return;\n this.keepRunning = true;\n if (this.currentLoop === undefined) this.currentLoop = this.mainLoop();\n }\n\n /** Called from observer */\n private stopUpdating(): void {\n this.keepRunning = false;\n }\n\n /** If true, main loop will continue polling pl state. */\n private keepRunning = false;\n /** Actual state of main loop. */\n private currentLoop: Promise<void> | undefined = undefined;\n\n /** Executed from the main loop, and initialization procedure. */\n private async refresh(stats?: TreeLoadingStat, txOps?: TxOps): Promise<void> {\n if (this.terminated) throw new Error(\"tree synchronization is terminated\");\n const request = constructTreeLoadingRequest(this.state, {\n pruningFunction: this.pruning,\n fieldFilter: this.fieldFilter,\n traverseStopRules: this.traverseStopRules,\n });\n const data = await this.pl.withReadTx(\n \"ReadingTree\",\n async (tx) => {\n return await loadTreeState(\n tx,\n request,\n stats,\n this.pl.serverInfo.capabilities ?? [],\n this.traversalMode,\n this.logger,\n );\n },\n txOps,\n );\n this.state.updateFromResourceData(data, true);\n }\n\n /** If true this tree state is permanently terminaed. */\n private terminated = false;\n\n private async mainLoop() {\n // will hold request stats\n let stat = this.logStat ? initialTreeLoadingStat() : undefined;\n\n let lastUpdate = Date.now();\n\n while (true) {\n if (!this.keepRunning || this.terminated) break;\n\n // saving those who want to be notified about new state here\n // because those who will be added during the tree retrieval\n // should be notified only on the next round\n let toNotify: ScheduledRefresh[] | undefined = undefined;\n if (this.scheduledOnNextState.length > 0) {\n toNotify = this.scheduledOnNextState;\n this.scheduledOnNextState = [];\n }\n\n try {\n // resetting stats if we were asked to collect non-cumulative stats\n if (this.logStat === \"per-request\") stat = initialTreeLoadingStat();\n\n // actual tree synchronization\n await this.refresh(stat);\n\n // logging stats if we were asked to\n if (stat && this.logger)\n this.logger.info(\n `Tree stat (success, after ${Date.now() - lastUpdate}ms): ${JSON.stringify(stat)}`,\n );\n lastUpdate = Date.now();\n\n // notifying that we got new state\n if (toNotify !== undefined) for (const n of toNotify) n.resolve();\n } catch (e: any) {\n // logging stats if we were asked to (even if error occured)\n if (stat && this.logger)\n this.logger.info(\n `Tree stat (error, after ${Date.now() - lastUpdate}ms): ${JSON.stringify(stat)}`,\n );\n lastUpdate = Date.now();\n\n // notifying that we failed to refresh the state\n if (toNotify !== undefined) for (const n of toNotify) n.reject(e);\n\n // catching tree update errors, as they may leave our tree in inconsistent state\n if (e instanceof TreeStateUpdateError) {\n // important error logging, this should never happen\n this.logger?.error(e);\n\n // marking everybody who used previous state as changed\n this.state.invalidateTree(\"stat update error\");\n // creating new tree\n this.state = new PlTreeState(this.root, this.finalPredicate);\n\n // scheduling state update without delay\n continue;\n\n // unfortunately external observer may still see tree in its default\n // empty state, though this is best we can do in this exceptional\n // situation, and hope on caching layers inside computables to present\n // some stale state until we reconstruct the tree again\n } else this.logger?.warn(e);\n }\n\n if (!this.keepRunning || this.terminated) break;\n\n // Phase 1: mandatory floor — always wait at least MIN_POLLING_INTERVAL_MS.\n // Not interruptible by scheduleOnNextState; only termination aborts it.\n try {\n await tp.setTimeout(MIN_POLLING_INTERVAL_MS, undefined, {\n signal: this.abortController.signal,\n });\n } catch (e: unknown) {\n if (!isTimeoutOrCancelError(e)) throw new Error(\"Unexpected error\", { cause: e });\n if (this.abortController.signal.aborted) break;\n }\n\n if (!this.keepRunning || this.terminated) break;\n\n // Phase 2: optional remainder up to pollingInterval — interruptible by\n // scheduleOnNextState so that an external nudge wakes the loop promptly.\n if (this.scheduledOnNextState.length === 0) {\n const remaining = Math.max(0, this.pollingInterval - MIN_POLLING_INTERVAL_MS);\n if (remaining > 0) {\n try {\n this.currentLoopDelayInterrupt = new AbortController();\n await tp.setTimeout(remaining, undefined, {\n signal: AbortSignal.any([\n this.abortController.signal,\n this.currentLoopDelayInterrupt.signal,\n ]),\n });\n } catch (e: unknown) {\n if (!isTimeoutOrCancelError(e)) throw new Error(\"Unexpected error\", { cause: e });\n if (this.abortController.signal.aborted) break;\n // Otherwise it was just the loop delay interrupt (scheduleOnNextState),\n // continue to the next iteration\n } finally {\n this.currentLoopDelayInterrupt = undefined;\n }\n }\n }\n }\n\n // reset only as a very last line\n this.currentLoop = undefined;\n }\n\n /**\n * Dumps the current state of the tree.\n * @returns An array of ExtendedResourceData objects representing the current state of the tree.\n */\n public dumpState(): ExtendedResourceData[] {\n return this.state.dumpState();\n }\n\n /**\n * Terminates the internal loop, and permanently destoys all internal state, so\n * all computables using this state will resolve to errors.\n * */\n public async terminate(): Promise<void> {\n this.keepRunning = false;\n this.terminated = true;\n this.abortController.abort();\n\n if (this.currentLoop === undefined) return;\n await this.currentLoop;\n\n this.state.invalidateTree(\"synchronization terminated for the tree\");\n }\n\n /** @deprecated */\n public async awaitSyncLoopTermination(): Promise<void> {\n if (this.currentLoop === undefined) return;\n await this.currentLoop;\n }\n\n public static async init(\n pl: PlClient,\n root: SignedResourceId,\n ops: SynchronizedTreeOps,\n logger?: MiLogger,\n ) {\n const tree = new SynchronizedTreeState(pl, root, ops, logger);\n\n const stat = ops.logStat ? initialTreeLoadingStat() : undefined;\n\n let ok = false;\n\n try {\n await tree.refresh(stat, {\n timeout: ops.initialTreeLoadingTimeout,\n });\n ok = true;\n } finally {\n // logging stats if we were asked to (even if error occured)\n if (stat && logger)\n logger.info(\n `Tree stat (initial load, ${ok ? \"success\" : \"failure\"}): ${JSON.stringify(stat)}`,\n );\n }\n\n return tree;\n }\n}\n"],"mappings":";;;;;;;;;;;;AAmBA,MAAM,0BAA0B;AAsChC,IAAa,wBAAb,MAAa,sBAAsB;CACjC;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA,kBAAmC,IAAI,iBAAiB;CAExD,YACE,IACA,MACA,KACA,QACA;AAJiB,OAAA,KAAA;AACA,OAAA,OAAA;AAEA,OAAA,SAAA;EAEjB,MAAM,EACJ,wBACA,SACA,aACA,mBACA,eACA,iBACA,kBACA,YACE;AACJ,OAAK,UAAU;AACf,OAAK,cAAc;AACnB,OAAK,oBAAoB;AACzB,OAAK,gBAAgB,iBAAiB;AACtC,OAAK,kBAAkB;AACvB,OAAK,iBAAiB,0BAA0B,GAAG;AACnD,OAAK,UAAU;AACf,OAAK,QAAQ,IAAIA,cAAAA,YAAY,MAAM,KAAK,eAAe;AACvD,OAAK,QAAQ,IAAIC,2BAAAA,6BACT,KAAK,eA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UAAUF,aAAAA,wBAAwB,GAAG,KAAA;EAEtD,IAAI,KAAK;AAET,MAAI;AACF,SAAM,KAAK,QAAQ,MAAM,EACvB,SAAS,IAAI,2BACd,CAAC;AACF,QAAK;YACG;AAER,OAAI,QAAQ,OACV,QAAO,KACL,4BAA4B,KAAK,YAAY,UAAU,KAAK,KAAK,UAAU,KAAK,GACjF;;AAGL,SAAO"}
|
|
1
|
+
{"version":3,"file":"synchronized_tree.cjs","names":["PlTreeState","PollingComputableHooks","PlTreeEntry","PlTreeRootsEntry","constructTreeLoadingRequest","loadTreeState","initialTreeLoadingStat","TreeStateUpdateError","tp"],"sources":["../src/synchronized_tree.ts"],"sourcesContent":["import { PollingComputableHooks } from \"@milaboratories/computable\";\nimport { PlTreeEntry, PlTreeRootsEntry } from \"./accessors\";\nimport type {\n FinalResourceDataPredicate,\n PlClient,\n ResourceType,\n SignedResourceId,\n TxOps,\n} from \"@milaboratories/pl-client\";\nimport type { Filter } from \"@milaboratories/pl-client\";\nimport { isUnauthenticated, isTimeoutOrCancelError } from \"@milaboratories/pl-client\";\nimport type { ExtendedResourceData } from \"./state\";\nimport { PlTreeState, TreeStateUpdateError } from \"./state\";\nimport type { PruningFunction, TraversalMode, TreeLoadingStat } from \"./sync\";\nimport { constructTreeLoadingRequest, initialTreeLoadingStat, loadTreeState } from \"./sync\";\nimport * as tp from \"node:timers/promises\";\nimport type { MiLogger } from \"@milaboratories/ts-helpers\";\n\n/** Hard floor between consecutive tree-refresh calls.\n * Applies even when {@link scheduleOnNextState} has woken the loop early,\n * preventing tight polling loops during rapid state transitions. */\nconst MIN_POLLING_INTERVAL_MS = 100;\n\ntype StatLoggingMode = \"cumulative\" | \"per-request\";\n\nexport type SynchronizedTreeOps = {\n /** Override final predicate from the PlClient */\n finalPredicateOverride?: FinalResourceDataPredicate;\n\n /** Pruning function for legacy fallback path. */\n pruning?: PruningFunction;\n\n /** ResourceTree field filter for modern backend path. */\n fieldFilter?: Filter;\n\n /** ResourceTree traversal stop rules for modern backend path. */\n traverseStopRules?: Filter;\n\n /** Interval after last sync to sleep before the next one */\n pollingInterval: number;\n /** For how long to continue polling after the last derived value access */\n stopPollingDelay: number;\n\n /** If one of the values, tree will log stats of each polling request */\n logStat?: StatLoggingMode;\n\n /** Timeout for initial tree loading. If not specified, will use default for RO tx from pl-client. */\n initialTreeLoadingTimeout?: number;\n\n /** Controls which tree-loading path to use. Default `\"auto\"`. */\n traversalMode?: TraversalMode;\n};\n\n/** An explicit resource to serve as a tree root. Several explicit seeds may be passed. */\nexport type ExplicitRootSeed = { kind: \"resource\"; root: SignedResourceId };\n\n/** Discovers, as roots, every resource of this type shared with the current user.\n * Matched against SharedResource.resourceType by NAME (version optional/ignored). The\n * discovered set is DYNAMIC — roots appear/disappear as grants are added/revoked/expire. */\nexport type SharedTypeSeed = { kind: \"shared\"; resourceType: ResourceType };\n\nexport type TreeSeed = ExplicitRootSeed | SharedTypeSeed;\n\n/** Normalizes the {@link SynchronizedTreeState.init} seed argument — a bare\n * {@link SignedResourceId}, a single {@link TreeSeed}, or an array — to `TreeSeed[]`, so\n * every existing single-root caller is unchanged. */\nfunction normalizeSeeds(seeds: SignedResourceId | TreeSeed | TreeSeed[]): TreeSeed[] {\n if (Array.isArray(seeds)) return seeds;\n if (typeof seeds === \"object\" && seeds !== null && \"kind\" in seeds) return [seeds];\n // bare SignedResourceId\n return [{ kind: \"resource\", root: seeds }];\n}\n\n/** How often, in main-loop iterations, a tree with shared-type seeds re-polls\n * ListUserResources to reconcile its discovered roots. 1 would mean every iteration.\n *\n * Discovery (a full ListUserResources stream) is far heavier than an ordinary incremental\n * refresh, so it must NOT run on every fast refresh tick. The refresh cadence is the tree's\n * `pollingInterval` floored by {@link MIN_POLLING_INTERVAL_MS} (~200ms-1s in practice — the\n * shared-seed discovery tree runs at the 200ms default). At N = 15 discovery fires roughly\n * every 15 × 200ms ≈ 3s, decoupling it from the fast refresh loop while keeping the latency\n * of noticing a new/removed share to a few seconds — acceptable for a human-driven share flow.\n *\n * Only trees with shared-type seeds gate on this; {@link discover} is a no-op for single-root\n * and explicit-seed trees (empty `sharedSeeds`), so the value never affects them. */\nconst DISCOVERY_EVERY_N_REFRESHES = 15;\n\ntype ScheduledRefresh = {\n resolve: () => void;\n reject: (err: any) => void;\n};\n\nexport class SynchronizedTreeState {\n private readonly finalPredicate: FinalResourceDataPredicate;\n private state: PlTreeState;\n private readonly pollingInterval: number;\n private readonly pruning?: PruningFunction;\n private readonly fieldFilter?: Filter;\n private readonly traverseStopRules?: Filter;\n private readonly traversalMode: TraversalMode;\n private readonly logStat?: StatLoggingMode;\n private readonly hooks: PollingComputableHooks;\n private readonly abortController = new AbortController();\n\n /** Explicit-resource seeds: fixed roots, present from construction. */\n private readonly explicitRoots: SignedResourceId[];\n /** Shared-type seeds (discovered roots), if any. */\n private readonly sharedSeeds: SharedTypeSeed[];\n /** Roots discovered for shared-type seeds on the last discovery poll. */\n private discoveredRoots: SignedResourceId[] = [];\n\n private constructor(\n private readonly pl: PlClient,\n seeds: TreeSeed[],\n ops: SynchronizedTreeOps,\n private readonly logger?: MiLogger,\n ) {\n const {\n finalPredicateOverride,\n pruning,\n fieldFilter,\n traverseStopRules,\n traversalMode,\n pollingInterval,\n stopPollingDelay,\n logStat,\n } = ops;\n this.pruning = pruning;\n this.fieldFilter = fieldFilter;\n this.traverseStopRules = traverseStopRules;\n this.traversalMode = traversalMode ?? \"auto\";\n this.pollingInterval = pollingInterval;\n this.finalPredicate = finalPredicateOverride ?? pl.finalPredicate;\n this.logStat = logStat;\n\n this.explicitRoots = seeds\n .filter((s): s is ExplicitRootSeed => s.kind === \"resource\")\n .map((s) => s.root);\n this.sharedSeeds = seeds.filter((s): s is SharedTypeSeed => s.kind === \"shared\");\n\n this.state = new PlTreeState(this.currentRootSet(), this.finalPredicate);\n this.hooks = new PollingComputableHooks(\n () => this.startUpdating(),\n () => this.stopUpdating(),\n { stopDebounce: stopPollingDelay },\n (resolve, reject) => this.scheduleOnNextState(resolve, reject),\n );\n }\n\n /** The current protected root set: explicit roots plus the latest discovered roots. */\n private currentRootSet(): Set<SignedResourceId> {\n return new Set([...this.explicitRoots, ...this.discoveredRoots]);\n }\n\n /** Resolves the single root for the backward-compatible single-root accessors, throwing\n * if the tree does not have exactly one root (guards legacy callers against multi-root). */\n private soleRoot(): SignedResourceId {\n const roots = this.currentRootSet();\n if (roots.size !== 1)\n throw new Error(\n `single-root accessor used on a tree with ${roots.size} roots; use rootsEntry() instead`,\n );\n return roots.values().next().value!;\n }\n\n /** @deprecated use \"entry\" instead */\n public accessor(rid?: SignedResourceId): PlTreeEntry {\n if (this.terminated) throw new Error(\"tree synchronization is terminated\");\n return this.entry(rid);\n }\n\n /** Backward-compatible single-root entry. With no `rid` it returns the sole root's entry\n * and THROWS if the tree has zero or more than one root. An explicit `rid` addresses any\n * resource in the heap, as today. */\n public entry(rid?: SignedResourceId): PlTreeEntry {\n if (this.terminated) throw new Error(\"tree synchronization is terminated\");\n return new PlTreeEntry(\n { treeProvider: () => this.state, hooks: this.hooks },\n rid ?? this.soleRoot(),\n );\n }\n\n /** Reactive provider for the current root SET. Reading it inside a Computable tracks the\n * set as a dependency, so the Computable recomputes when discovered roots appear/disappear. */\n public rootsEntry(): PlTreeRootsEntry {\n if (this.terminated) throw new Error(\"tree synchronization is terminated\");\n return new PlTreeRootsEntry({ treeProvider: () => this.state, hooks: this.hooks });\n }\n\n /** Can be used to externally kick off the synchronization polling loop, and\n * await for the first synchronization to happen. */\n public async refreshState(): Promise<void> {\n if (this.terminated) throw new Error(\"tree synchronization is terminated\");\n await this.hooks.refreshState();\n }\n\n private currentLoopDelayInterrupt: AbortController | undefined = undefined;\n private scheduledOnNextState: ScheduledRefresh[] = [];\n\n /** Called from computable hooks when external observer asks for state refresh */\n private scheduleOnNextState(resolve: () => void, reject: (err: any) => void): void {\n if (this.terminated) reject(new Error(\"tree synchronization is terminated\"));\n else {\n this.scheduledOnNextState.push({ resolve, reject });\n if (this.currentLoopDelayInterrupt) {\n this.currentLoopDelayInterrupt.abort();\n this.currentLoopDelayInterrupt = undefined;\n }\n }\n }\n\n /** Called from observer */\n private startUpdating(): void {\n if (this.terminated) return;\n this.keepRunning = true;\n if (this.currentLoop === undefined) this.currentLoop = this.mainLoop();\n }\n\n /** Called from observer */\n private stopUpdating(): void {\n this.keepRunning = false;\n }\n\n /** If true, main loop will continue polling pl state. */\n private keepRunning = false;\n /** Actual state of main loop. */\n private currentLoop: Promise<void> | undefined = undefined;\n\n /** Executed from the main loop, and initialization procedure. */\n private async refresh(stats?: TreeLoadingStat, txOps?: TxOps): Promise<void> {\n if (this.terminated) throw new Error(\"tree synchronization is terminated\");\n try {\n await this.loadAndApply(stats, txOps);\n } catch (e) {\n // Discovery-tree self-heal: a discovered root whose grant was revoked/expired fails the whole\n // ResourceTree poll with Unauthenticated. Re-discover (drops dead roots) and retry once. This is\n // self-discriminating — a genuinely dead session also fails discover()'s own call, so real auth\n // loss still propagates. Only for discovery trees; explicit-root trees propagate as-is.\n if (this.sharedSeeds.length > 0 && isUnauthenticated(e)) {\n this.logger?.warn(\n \"discovery tree: Unauthenticated on ResourceTree (likely revoked/expired root); re-discovering and retrying\",\n );\n await this.discover();\n await this.loadAndApply(stats, txOps);\n } else throw e;\n }\n }\n\n private async loadAndApply(stats?: TreeLoadingStat, txOps?: TxOps): Promise<void> {\n const request = constructTreeLoadingRequest(this.state, {\n pruningFunction: this.pruning,\n fieldFilter: this.fieldFilter,\n traverseStopRules: this.traverseStopRules,\n });\n // A shared-type-seed tree with no currently-discovered roots is legitimately empty:\n // there is nothing to traverse, and tx.resourceTree([]) would throw \"at least one seed\n // must be provided\". Skip the backend load and leave the (empty) state as-is — discovery\n // adds roots later via setRoots(), which schedules the next refresh. Explicit-root trees\n // never hit this (their root set is non-empty by construction).\n if (request.seedResources.length === 0 && request.finalResources.size === 0) return;\n const data = await this.pl.withReadTx(\n \"ReadingTree\",\n async (tx) => {\n return await loadTreeState(\n tx,\n request,\n stats,\n this.pl.serverInfo.capabilities ?? [],\n this.traversalMode,\n this.logger,\n );\n },\n txOps,\n );\n this.state.updateFromResourceData(data, true);\n }\n\n /** Discovery sync for shared-type seeds: re-polls `ListUserResources` (gRPC-only) and\n * reconciles the discovered root set against the heap. A longer poll result adds roots; a\n * shorter one removes them (grant revoked/expired) — the removed roots' subtrees cascade\n * to collection via the ordinary refcount GC ({@link PlTreeState.setRoots}). No-op when the\n * tree has no shared-type seeds, or silently no-op on a REST client where `ListUserResources`\n * is unavailable. */\n private async discover(): Promise<void> {\n if (this.terminated) throw new Error(\"tree synchronization is terminated\");\n if (this.sharedSeeds.length === 0) return;\n\n const discovered = new Set<SignedResourceId>();\n for (const seed of this.sharedSeeds) {\n // match by name only (permissive; ignores version, so it survives schema bumps)\n const ids = await this.pl.userResources.listSharedResourcesByType(seed.resourceType.name);\n for (const id of ids) discovered.add(id);\n }\n\n this.discoveredRoots = [...discovered];\n this.state.setRoots(this.currentRootSet());\n }\n\n /** If true this tree state is permanently terminaed. */\n private terminated = false;\n\n private async mainLoop() {\n // will hold request stats\n let stat = this.logStat ? initialTreeLoadingStat() : undefined;\n\n let lastUpdate = Date.now();\n\n // counts refresh iterations to pace the discovery poll for shared-type seeds.\n let iteration = 0;\n\n while (true) {\n if (!this.keepRunning || this.terminated) break;\n\n // saving those who want to be notified about new state here\n // because those who will be added during the tree retrieval\n // should be notified only on the next round\n let toNotify: ScheduledRefresh[] | undefined = undefined;\n if (this.scheduledOnNextState.length > 0) {\n toNotify = this.scheduledOnNextState;\n this.scheduledOnNextState = [];\n }\n\n try {\n // resetting stats if we were asked to collect non-cumulative stats\n if (this.logStat === \"per-request\") stat = initialTreeLoadingStat();\n\n // discovery sync for shared-type seeds: reconcile the discovered root set before\n // refreshing, so newly discovered roots are materialized in this same iteration.\n if (this.sharedSeeds.length > 0 && iteration % DISCOVERY_EVERY_N_REFRESHES === 0) {\n await this.discover();\n }\n iteration++;\n\n // actual tree synchronization\n await this.refresh(stat);\n\n // logging stats if we were asked to\n if (stat && this.logger)\n this.logger.info(\n `Tree stat (success, after ${Date.now() - lastUpdate}ms): ${JSON.stringify(stat)}`,\n );\n lastUpdate = Date.now();\n\n // notifying that we got new state\n if (toNotify !== undefined) for (const n of toNotify) n.resolve();\n } catch (e: any) {\n // logging stats if we were asked to (even if error occured)\n if (stat && this.logger)\n this.logger.info(\n `Tree stat (error, after ${Date.now() - lastUpdate}ms): ${JSON.stringify(stat)}`,\n );\n lastUpdate = Date.now();\n\n // notifying that we failed to refresh the state\n if (toNotify !== undefined) for (const n of toNotify) n.reject(e);\n\n // catching tree update errors, as they may leave our tree in inconsistent state\n if (e instanceof TreeStateUpdateError) {\n // important error logging, this should never happen\n this.logger?.error(e);\n\n // marking everybody who used previous state as changed\n this.state.invalidateTree(\"stat update error\");\n // creating new tree with the full current root set (re-discovered on next iteration)\n this.state = new PlTreeState(this.currentRootSet(), this.finalPredicate);\n\n // scheduling state update without delay\n continue;\n\n // unfortunately external observer may still see tree in its default\n // empty state, though this is best we can do in this exceptional\n // situation, and hope on caching layers inside computables to present\n // some stale state until we reconstruct the tree again\n } else this.logger?.warn(e);\n }\n\n if (!this.keepRunning || this.terminated) break;\n\n // Phase 1: mandatory floor — always wait at least MIN_POLLING_INTERVAL_MS.\n // Not interruptible by scheduleOnNextState; only termination aborts it.\n try {\n await tp.setTimeout(MIN_POLLING_INTERVAL_MS, undefined, {\n signal: this.abortController.signal,\n });\n } catch (e: unknown) {\n if (!isTimeoutOrCancelError(e)) throw new Error(\"Unexpected error\", { cause: e });\n if (this.abortController.signal.aborted) break;\n }\n\n if (!this.keepRunning || this.terminated) break;\n\n // Phase 2: optional remainder up to pollingInterval — interruptible by\n // scheduleOnNextState so that an external nudge wakes the loop promptly.\n if (this.scheduledOnNextState.length === 0) {\n const remaining = Math.max(0, this.pollingInterval - MIN_POLLING_INTERVAL_MS);\n if (remaining > 0) {\n try {\n this.currentLoopDelayInterrupt = new AbortController();\n await tp.setTimeout(remaining, undefined, {\n signal: AbortSignal.any([\n this.abortController.signal,\n this.currentLoopDelayInterrupt.signal,\n ]),\n });\n } catch (e: unknown) {\n if (!isTimeoutOrCancelError(e)) throw new Error(\"Unexpected error\", { cause: e });\n if (this.abortController.signal.aborted) break;\n // Otherwise it was just the loop delay interrupt (scheduleOnNextState),\n // continue to the next iteration\n } finally {\n this.currentLoopDelayInterrupt = undefined;\n }\n }\n }\n }\n\n // reset only as a very last line\n this.currentLoop = undefined;\n }\n\n /**\n * Dumps the current state of the tree.\n * @returns An array of ExtendedResourceData objects representing the current state of the tree.\n */\n public dumpState(): ExtendedResourceData[] {\n return this.state.dumpState();\n }\n\n /**\n * Terminates the internal loop, and permanently destoys all internal state, so\n * all computables using this state will resolve to errors.\n * */\n public async terminate(): Promise<void> {\n this.keepRunning = false;\n this.terminated = true;\n this.abortController.abort();\n\n if (this.currentLoop === undefined) return;\n await this.currentLoop;\n\n this.state.invalidateTree(\"synchronization terminated for the tree\");\n }\n\n /** @deprecated */\n public async awaitSyncLoopTermination(): Promise<void> {\n if (this.currentLoop === undefined) return;\n await this.currentLoop;\n }\n\n /**\n * Initializes a synchronized tree from one or more seeds.\n *\n * @param seeds a bare {@link SignedResourceId} (the original single-root contract), a\n * single {@link TreeSeed}, or an array of seeds. Bare ids and explicit-resource seeds\n * become roots immediately; shared-type seeds discover their roots via `ListUserResources`.\n */\n public static async init(\n pl: PlClient,\n seeds: SignedResourceId | TreeSeed | TreeSeed[],\n ops: SynchronizedTreeOps,\n logger?: MiLogger,\n ) {\n const tree = new SynchronizedTreeState(pl, normalizeSeeds(seeds), ops, logger);\n\n const stat = ops.logStat ? initialTreeLoadingStat() : undefined;\n\n let ok = false;\n\n try {\n // resolve shared-type seeds before the first refresh so discovered roots load now\n await tree.discover();\n await tree.refresh(stat, {\n timeout: ops.initialTreeLoadingTimeout,\n });\n ok = true;\n } finally {\n // logging stats if we were asked to (even if error occured)\n if (stat && logger)\n logger.info(\n `Tree stat (initial load, ${ok ? \"success\" : \"failure\"}):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E7E,MAAM,OAAO,IAAI,UAAUF,aAAAA,uBAAuB,IAAI,KAAA;EAEtD,IAAI,KAAK;EAET,IAAI;GAEF,MAAM,KAAK,SAAS;GACpB,MAAM,KAAK,QAAQ,MAAM,EACvB,SAAS,IAAI,0BACf,CAAC;GACD,KAAK;EACP,UAAU;GAER,IAAI,QAAQ,QACV,OAAO,KACL,4BAA4B,KAAK,YAAY,UAAU,KAAK,KAAK,UAAU,IAAI,GACjF;EACJ;EAEA,OAAO;CACT;AACF"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { PlTreeEntry } from "./accessors.js";
|
|
1
|
+
import { PlTreeEntry, PlTreeRootsEntry } from "./accessors.js";
|
|
2
2
|
import { ExtendedResourceData } from "./state.js";
|
|
3
3
|
import { PruningFunction, TraversalMode } from "./sync.js";
|
|
4
|
-
import { Filter, FinalResourceDataPredicate, PlClient, SignedResourceId } from "@milaboratories/pl-client";
|
|
4
|
+
import { Filter, FinalResourceDataPredicate, PlClient, ResourceType, SignedResourceId } from "@milaboratories/pl-client";
|
|
5
5
|
import { MiLogger } from "@milaboratories/ts-helpers";
|
|
6
6
|
|
|
7
7
|
//#region src/synchronized_tree.d.ts
|
|
@@ -17,9 +17,21 @@ type SynchronizedTreeOps = {
|
|
|
17
17
|
initialTreeLoadingTimeout?: number; /** Controls which tree-loading path to use. Default `"auto"`. */
|
|
18
18
|
traversalMode?: TraversalMode;
|
|
19
19
|
};
|
|
20
|
+
/** An explicit resource to serve as a tree root. Several explicit seeds may be passed. */
|
|
21
|
+
type ExplicitRootSeed = {
|
|
22
|
+
kind: "resource";
|
|
23
|
+
root: SignedResourceId;
|
|
24
|
+
};
|
|
25
|
+
/** Discovers, as roots, every resource of this type shared with the current user.
|
|
26
|
+
* Matched against SharedResource.resourceType by NAME (version optional/ignored). The
|
|
27
|
+
* discovered set is DYNAMIC — roots appear/disappear as grants are added/revoked/expire. */
|
|
28
|
+
type SharedTypeSeed = {
|
|
29
|
+
kind: "shared";
|
|
30
|
+
resourceType: ResourceType;
|
|
31
|
+
};
|
|
32
|
+
type TreeSeed = ExplicitRootSeed | SharedTypeSeed;
|
|
20
33
|
declare class SynchronizedTreeState {
|
|
21
34
|
private readonly pl;
|
|
22
|
-
private readonly root;
|
|
23
35
|
private readonly logger?;
|
|
24
36
|
private readonly finalPredicate;
|
|
25
37
|
private state;
|
|
@@ -31,10 +43,27 @@ declare class SynchronizedTreeState {
|
|
|
31
43
|
private readonly logStat?;
|
|
32
44
|
private readonly hooks;
|
|
33
45
|
private readonly abortController;
|
|
46
|
+
/** Explicit-resource seeds: fixed roots, present from construction. */
|
|
47
|
+
private readonly explicitRoots;
|
|
48
|
+
/** Shared-type seeds (discovered roots), if any. */
|
|
49
|
+
private readonly sharedSeeds;
|
|
50
|
+
/** Roots discovered for shared-type seeds on the last discovery poll. */
|
|
51
|
+
private discoveredRoots;
|
|
34
52
|
private constructor();
|
|
53
|
+
/** The current protected root set: explicit roots plus the latest discovered roots. */
|
|
54
|
+
private currentRootSet;
|
|
55
|
+
/** Resolves the single root for the backward-compatible single-root accessors, throwing
|
|
56
|
+
* if the tree does not have exactly one root (guards legacy callers against multi-root). */
|
|
57
|
+
private soleRoot;
|
|
35
58
|
/** @deprecated use "entry" instead */
|
|
36
59
|
accessor(rid?: SignedResourceId): PlTreeEntry;
|
|
60
|
+
/** Backward-compatible single-root entry. With no `rid` it returns the sole root's entry
|
|
61
|
+
* and THROWS if the tree has zero or more than one root. An explicit `rid` addresses any
|
|
62
|
+
* resource in the heap, as today. */
|
|
37
63
|
entry(rid?: SignedResourceId): PlTreeEntry;
|
|
64
|
+
/** Reactive provider for the current root SET. Reading it inside a Computable tracks the
|
|
65
|
+
* set as a dependency, so the Computable recomputes when discovered roots appear/disappear. */
|
|
66
|
+
rootsEntry(): PlTreeRootsEntry;
|
|
38
67
|
/** Can be used to externally kick off the synchronization polling loop, and
|
|
39
68
|
* await for the first synchronization to happen. */
|
|
40
69
|
refreshState(): Promise<void>;
|
|
@@ -52,6 +81,14 @@ declare class SynchronizedTreeState {
|
|
|
52
81
|
private currentLoop;
|
|
53
82
|
/** Executed from the main loop, and initialization procedure. */
|
|
54
83
|
private refresh;
|
|
84
|
+
private loadAndApply;
|
|
85
|
+
/** Discovery sync for shared-type seeds: re-polls `ListUserResources` (gRPC-only) and
|
|
86
|
+
* reconciles the discovered root set against the heap. A longer poll result adds roots; a
|
|
87
|
+
* shorter one removes them (grant revoked/expired) — the removed roots' subtrees cascade
|
|
88
|
+
* to collection via the ordinary refcount GC ({@link PlTreeState.setRoots}). No-op when the
|
|
89
|
+
* tree has no shared-type seeds, or silently no-op on a REST client where `ListUserResources`
|
|
90
|
+
* is unavailable. */
|
|
91
|
+
private discover;
|
|
55
92
|
/** If true this tree state is permanently terminaed. */
|
|
56
93
|
private terminated;
|
|
57
94
|
private mainLoop;
|
|
@@ -67,8 +104,15 @@ declare class SynchronizedTreeState {
|
|
|
67
104
|
terminate(): Promise<void>;
|
|
68
105
|
/** @deprecated */
|
|
69
106
|
awaitSyncLoopTermination(): Promise<void>;
|
|
70
|
-
|
|
107
|
+
/**
|
|
108
|
+
* Initializes a synchronized tree from one or more seeds.
|
|
109
|
+
*
|
|
110
|
+
* @param seeds a bare {@link SignedResourceId} (the original single-root contract), a
|
|
111
|
+
* single {@link TreeSeed}, or an array of seeds. Bare ids and explicit-resource seeds
|
|
112
|
+
* become roots immediately; shared-type seeds discover their roots via `ListUserResources`.
|
|
113
|
+
*/
|
|
114
|
+
static init(pl: PlClient, seeds: SignedResourceId | TreeSeed | TreeSeed[], ops: SynchronizedTreeOps, logger?: MiLogger): Promise<SynchronizedTreeState>;
|
|
71
115
|
}
|
|
72
116
|
//#endregion
|
|
73
|
-
export { SynchronizedTreeOps, SynchronizedTreeState };
|
|
117
|
+
export { ExplicitRootSeed, SharedTypeSeed, SynchronizedTreeOps, SynchronizedTreeState, TreeSeed };
|
|
74
118
|
//# sourceMappingURL=synchronized_tree.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"synchronized_tree.d.ts","names":[],"sources":["../src/synchronized_tree.ts"],"mappings":";;;;;;;
|
|
1
|
+
{"version":3,"file":"synchronized_tree.d.ts","names":[],"sources":["../src/synchronized_tree.ts"],"mappings":";;;;;;;KAuBK,eAAA;AAAA,KAEO,mBAAA;EAFQ,iDAIlB,sBAAA,GAAyB,0BAAA,EAJP;EAOlB,OAAA,GAAU,eAAA,EALA;EAQV,WAAA,GAAc,MAAA;EAGd,iBAAA,GAAoB,MAAA,EANV;EASV,eAAA,UAHoB;EAKpB,gBAAA,UASgB;EANhB,OAAA,GAAU,eAAA,EAMmB;EAH7B,yBAAA,WApByB;EAuBzB,aAAA,GAAgB,aAAA;AAAA;;KAIN,gBAAA;EAAqB,IAAA;EAAkB,IAAA,EAAM,gBAAgB;AAAA;;;;KAK7D,cAAA;EAAmB,IAAA;EAAgB,YAAA,EAAc,YAAY;AAAA;AAAA,KAE7D,QAAA,GAAW,gBAAA,GAAmB,cAAc;AAAA,cA+B3C,qBAAA;EAAA,iBAoBQ,EAAA;EAAA,iBAGA,MAAA;EAAA,iBAtBF,cAAA;EAAA,QACT,KAAA;EAAA,iBACS,eAAA;EAAA,iBACA,OAAA;EAAA,iBACA,WAAA;EAAA,iBACA,iBAAA;EAAA,iBACA,aAAA;EAAA,iBACA,OAAA;EAAA,iBACA,KAAA;EAAA,iBACA,eAAA;EA3CY;EAAA,iBA8CZ,aAAA;EA9C0C;EAAA,iBAgD1C,WAAA;EAhDsD;EAAA,QAkD/D,eAAA;EAAA,QAED,WAAA;;UAuCC,cAAA;EAzF8C;AA+BxD;EA/BwD,QA+F9C,QAAA;;EAUD,QAAA,CAAS,GAAA,GAAM,gBAAA,GAAmB,WAAA;EAAA;;;EAQlC,KAAA,CAAM,GAAA,GAAM,gBAAA,GAAmB,WAAA;EAiBT;;EAPtB,UAAA,IAAc,gBAAA;EAoQoB;;EA7P5B,YAAA,IAAgB,OAAA;EAAA,QAKrB,yBAAA;EAAA,QACA,oBAAA;EAsQD;EAAA,QAnQC,mBAAA;EAoQW;EAAA,QAxPX,aAAA;EAwPW;EAAA,QAjPX,YAAA;EA3GW;EAAA,QAgHX,WAAA;EAnIS;EAAA,QAqIT,WAAA;EAnIS;EAAA,QAsIH,OAAA;EAAA,QAmBA,YAAA;EAtJG;;;;;;EAAA,QAyLH,QAAA;EA9KN;EAAA,QA8LA,UAAA;EAAA,QAEM,QAAA;EAjJN;;;;EA4QD,SAAA,IAAa,oBAAA;EA1Pb;;;;EAkQM,SAAA,IAAa,OAAA;EAxPL;EAoQR,wBAAA,IAA4B,OAAA;EA7PZ;;;;;;;EAAA,OAyQT,IAAA,CAClB,EAAA,EAAI,QAAA,EACJ,KAAA,EAAO,gBAAA,GAAmB,QAAA,GAAW,QAAA,IACrC,GAAA,EAAK,mBAAA,EACL,MAAA,GAAS,QAAA,GAAQ,OAAA,CAAA,qBAAA;AAAA"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { PlTreeEntry } from "./accessors.js";
|
|
1
|
+
import { PlTreeEntry, PlTreeRootsEntry } from "./accessors.js";
|
|
2
2
|
import { PlTreeState, TreeStateUpdateError } from "./state.js";
|
|
3
3
|
import { constructTreeLoadingRequest, initialTreeLoadingStat, loadTreeState } from "./sync.js";
|
|
4
|
-
import { isTimeoutOrCancelError } from "@milaboratories/pl-client";
|
|
4
|
+
import { isTimeoutOrCancelError, isUnauthenticated } from "@milaboratories/pl-client";
|
|
5
5
|
import { PollingComputableHooks } from "@milaboratories/computable";
|
|
6
6
|
import * as tp from "node:timers/promises";
|
|
7
7
|
//#region src/synchronized_tree.ts
|
|
@@ -9,7 +9,33 @@ import * as tp from "node:timers/promises";
|
|
|
9
9
|
* Applies even when {@link scheduleOnNextState} has woken the loop early,
|
|
10
10
|
* preventing tight polling loops during rapid state transitions. */
|
|
11
11
|
const MIN_POLLING_INTERVAL_MS = 100;
|
|
12
|
+
/** Normalizes the {@link SynchronizedTreeState.init} seed argument — a bare
|
|
13
|
+
* {@link SignedResourceId}, a single {@link TreeSeed}, or an array — to `TreeSeed[]`, so
|
|
14
|
+
* every existing single-root caller is unchanged. */
|
|
15
|
+
function normalizeSeeds(seeds) {
|
|
16
|
+
if (Array.isArray(seeds)) return seeds;
|
|
17
|
+
if (typeof seeds === "object" && seeds !== null && "kind" in seeds) return [seeds];
|
|
18
|
+
return [{
|
|
19
|
+
kind: "resource",
|
|
20
|
+
root: seeds
|
|
21
|
+
}];
|
|
22
|
+
}
|
|
23
|
+
/** How often, in main-loop iterations, a tree with shared-type seeds re-polls
|
|
24
|
+
* ListUserResources to reconcile its discovered roots. 1 would mean every iteration.
|
|
25
|
+
*
|
|
26
|
+
* Discovery (a full ListUserResources stream) is far heavier than an ordinary incremental
|
|
27
|
+
* refresh, so it must NOT run on every fast refresh tick. The refresh cadence is the tree's
|
|
28
|
+
* `pollingInterval` floored by {@link MIN_POLLING_INTERVAL_MS} (~200ms-1s in practice — the
|
|
29
|
+
* shared-seed discovery tree runs at the 200ms default). At N = 15 discovery fires roughly
|
|
30
|
+
* every 15 × 200ms ≈ 3s, decoupling it from the fast refresh loop while keeping the latency
|
|
31
|
+
* of noticing a new/removed share to a few seconds — acceptable for a human-driven share flow.
|
|
32
|
+
*
|
|
33
|
+
* Only trees with shared-type seeds gate on this; {@link discover} is a no-op for single-root
|
|
34
|
+
* and explicit-seed trees (empty `sharedSeeds`), so the value never affects them. */
|
|
35
|
+
const DISCOVERY_EVERY_N_REFRESHES = 15;
|
|
12
36
|
var SynchronizedTreeState = class SynchronizedTreeState {
|
|
37
|
+
pl;
|
|
38
|
+
logger;
|
|
13
39
|
finalPredicate;
|
|
14
40
|
state;
|
|
15
41
|
pollingInterval;
|
|
@@ -20,9 +46,14 @@ var SynchronizedTreeState = class SynchronizedTreeState {
|
|
|
20
46
|
logStat;
|
|
21
47
|
hooks;
|
|
22
48
|
abortController = new AbortController();
|
|
23
|
-
|
|
49
|
+
/** Explicit-resource seeds: fixed roots, present from construction. */
|
|
50
|
+
explicitRoots;
|
|
51
|
+
/** Shared-type seeds (discovered roots), if any. */
|
|
52
|
+
sharedSeeds;
|
|
53
|
+
/** Roots discovered for shared-type seeds on the last discovery poll. */
|
|
54
|
+
discoveredRoots = [];
|
|
55
|
+
constructor(pl, seeds, ops, logger) {
|
|
24
56
|
this.pl = pl;
|
|
25
|
-
this.root = root;
|
|
26
57
|
this.logger = logger;
|
|
27
58
|
const { finalPredicateOverride, pruning, fieldFilter, traverseStopRules, traversalMode, pollingInterval, stopPollingDelay, logStat } = ops;
|
|
28
59
|
this.pruning = pruning;
|
|
@@ -32,20 +63,45 @@ var SynchronizedTreeState = class SynchronizedTreeState {
|
|
|
32
63
|
this.pollingInterval = pollingInterval;
|
|
33
64
|
this.finalPredicate = finalPredicateOverride ?? pl.finalPredicate;
|
|
34
65
|
this.logStat = logStat;
|
|
35
|
-
this.
|
|
66
|
+
this.explicitRoots = seeds.filter((s) => s.kind === "resource").map((s) => s.root);
|
|
67
|
+
this.sharedSeeds = seeds.filter((s) => s.kind === "shared");
|
|
68
|
+
this.state = new PlTreeState(this.currentRootSet(), this.finalPredicate);
|
|
36
69
|
this.hooks = new PollingComputableHooks(() => this.startUpdating(), () => this.stopUpdating(), { stopDebounce: stopPollingDelay }, (resolve, reject) => this.scheduleOnNextState(resolve, reject));
|
|
37
70
|
}
|
|
71
|
+
/** The current protected root set: explicit roots plus the latest discovered roots. */
|
|
72
|
+
currentRootSet() {
|
|
73
|
+
return /* @__PURE__ */ new Set([...this.explicitRoots, ...this.discoveredRoots]);
|
|
74
|
+
}
|
|
75
|
+
/** Resolves the single root for the backward-compatible single-root accessors, throwing
|
|
76
|
+
* if the tree does not have exactly one root (guards legacy callers against multi-root). */
|
|
77
|
+
soleRoot() {
|
|
78
|
+
const roots = this.currentRootSet();
|
|
79
|
+
if (roots.size !== 1) throw new Error(`single-root accessor used on a tree with ${roots.size} roots; use rootsEntry() instead`);
|
|
80
|
+
return roots.values().next().value;
|
|
81
|
+
}
|
|
38
82
|
/** @deprecated use "entry" instead */
|
|
39
|
-
accessor(rid
|
|
83
|
+
accessor(rid) {
|
|
40
84
|
if (this.terminated) throw new Error("tree synchronization is terminated");
|
|
41
85
|
return this.entry(rid);
|
|
42
86
|
}
|
|
43
|
-
entry
|
|
87
|
+
/** Backward-compatible single-root entry. With no `rid` it returns the sole root's entry
|
|
88
|
+
* and THROWS if the tree has zero or more than one root. An explicit `rid` addresses any
|
|
89
|
+
* resource in the heap, as today. */
|
|
90
|
+
entry(rid) {
|
|
44
91
|
if (this.terminated) throw new Error("tree synchronization is terminated");
|
|
45
92
|
return new PlTreeEntry({
|
|
46
93
|
treeProvider: () => this.state,
|
|
47
94
|
hooks: this.hooks
|
|
48
|
-
}, rid);
|
|
95
|
+
}, rid ?? this.soleRoot());
|
|
96
|
+
}
|
|
97
|
+
/** Reactive provider for the current root SET. Reading it inside a Computable tracks the
|
|
98
|
+
* set as a dependency, so the Computable recomputes when discovered roots appear/disappear. */
|
|
99
|
+
rootsEntry() {
|
|
100
|
+
if (this.terminated) throw new Error("tree synchronization is terminated");
|
|
101
|
+
return new PlTreeRootsEntry({
|
|
102
|
+
treeProvider: () => this.state,
|
|
103
|
+
hooks: this.hooks
|
|
104
|
+
});
|
|
49
105
|
}
|
|
50
106
|
/** Can be used to externally kick off the synchronization polling loop, and
|
|
51
107
|
* await for the first synchronization to happen. */
|
|
@@ -86,21 +142,51 @@ var SynchronizedTreeState = class SynchronizedTreeState {
|
|
|
86
142
|
/** Executed from the main loop, and initialization procedure. */
|
|
87
143
|
async refresh(stats, txOps) {
|
|
88
144
|
if (this.terminated) throw new Error("tree synchronization is terminated");
|
|
145
|
+
try {
|
|
146
|
+
await this.loadAndApply(stats, txOps);
|
|
147
|
+
} catch (e) {
|
|
148
|
+
if (this.sharedSeeds.length > 0 && isUnauthenticated(e)) {
|
|
149
|
+
this.logger?.warn("discovery tree: Unauthenticated on ResourceTree (likely revoked/expired root); re-discovering and retrying");
|
|
150
|
+
await this.discover();
|
|
151
|
+
await this.loadAndApply(stats, txOps);
|
|
152
|
+
} else throw e;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
async loadAndApply(stats, txOps) {
|
|
89
156
|
const request = constructTreeLoadingRequest(this.state, {
|
|
90
157
|
pruningFunction: this.pruning,
|
|
91
158
|
fieldFilter: this.fieldFilter,
|
|
92
159
|
traverseStopRules: this.traverseStopRules
|
|
93
160
|
});
|
|
161
|
+
if (request.seedResources.length === 0 && request.finalResources.size === 0) return;
|
|
94
162
|
const data = await this.pl.withReadTx("ReadingTree", async (tx) => {
|
|
95
163
|
return await loadTreeState(tx, request, stats, this.pl.serverInfo.capabilities ?? [], this.traversalMode, this.logger);
|
|
96
164
|
}, txOps);
|
|
97
165
|
this.state.updateFromResourceData(data, true);
|
|
98
166
|
}
|
|
167
|
+
/** Discovery sync for shared-type seeds: re-polls `ListUserResources` (gRPC-only) and
|
|
168
|
+
* reconciles the discovered root set against the heap. A longer poll result adds roots; a
|
|
169
|
+
* shorter one removes them (grant revoked/expired) — the removed roots' subtrees cascade
|
|
170
|
+
* to collection via the ordinary refcount GC ({@link PlTreeState.setRoots}). No-op when the
|
|
171
|
+
* tree has no shared-type seeds, or silently no-op on a REST client where `ListUserResources`
|
|
172
|
+
* is unavailable. */
|
|
173
|
+
async discover() {
|
|
174
|
+
if (this.terminated) throw new Error("tree synchronization is terminated");
|
|
175
|
+
if (this.sharedSeeds.length === 0) return;
|
|
176
|
+
const discovered = /* @__PURE__ */ new Set();
|
|
177
|
+
for (const seed of this.sharedSeeds) {
|
|
178
|
+
const ids = await this.pl.userResources.listSharedResourcesByType(seed.resourceType.name);
|
|
179
|
+
for (const id of ids) discovered.add(id);
|
|
180
|
+
}
|
|
181
|
+
this.discoveredRoots = [...discovered];
|
|
182
|
+
this.state.setRoots(this.currentRootSet());
|
|
183
|
+
}
|
|
99
184
|
/** If true this tree state is permanently terminaed. */
|
|
100
185
|
terminated = false;
|
|
101
186
|
async mainLoop() {
|
|
102
187
|
let stat = this.logStat ? initialTreeLoadingStat() : void 0;
|
|
103
188
|
let lastUpdate = Date.now();
|
|
189
|
+
let iteration = 0;
|
|
104
190
|
while (true) {
|
|
105
191
|
if (!this.keepRunning || this.terminated) break;
|
|
106
192
|
let toNotify = void 0;
|
|
@@ -110,6 +196,8 @@ var SynchronizedTreeState = class SynchronizedTreeState {
|
|
|
110
196
|
}
|
|
111
197
|
try {
|
|
112
198
|
if (this.logStat === "per-request") stat = initialTreeLoadingStat();
|
|
199
|
+
if (this.sharedSeeds.length > 0 && iteration % DISCOVERY_EVERY_N_REFRESHES === 0) await this.discover();
|
|
200
|
+
iteration++;
|
|
113
201
|
await this.refresh(stat);
|
|
114
202
|
if (stat && this.logger) this.logger.info(`Tree stat (success, after ${Date.now() - lastUpdate}ms): ${JSON.stringify(stat)}`);
|
|
115
203
|
lastUpdate = Date.now();
|
|
@@ -121,7 +209,7 @@ var SynchronizedTreeState = class SynchronizedTreeState {
|
|
|
121
209
|
if (e instanceof TreeStateUpdateError) {
|
|
122
210
|
this.logger?.error(e);
|
|
123
211
|
this.state.invalidateTree("stat update error");
|
|
124
|
-
this.state = new PlTreeState(this.
|
|
212
|
+
this.state = new PlTreeState(this.currentRootSet(), this.finalPredicate);
|
|
125
213
|
continue;
|
|
126
214
|
} else this.logger?.warn(e);
|
|
127
215
|
}
|
|
@@ -172,11 +260,19 @@ var SynchronizedTreeState = class SynchronizedTreeState {
|
|
|
172
260
|
if (this.currentLoop === void 0) return;
|
|
173
261
|
await this.currentLoop;
|
|
174
262
|
}
|
|
175
|
-
|
|
176
|
-
|
|
263
|
+
/**
|
|
264
|
+
* Initializes a synchronized tree from one or more seeds.
|
|
265
|
+
*
|
|
266
|
+
* @param seeds a bare {@link SignedResourceId} (the original single-root contract), a
|
|
267
|
+
* single {@link TreeSeed}, or an array of seeds. Bare ids and explicit-resource seeds
|
|
268
|
+
* become roots immediately; shared-type seeds discover their roots via `ListUserResources`.
|
|
269
|
+
*/
|
|
270
|
+
static async init(pl, seeds, ops, logger) {
|
|
271
|
+
const tree = new SynchronizedTreeState(pl, normalizeSeeds(seeds), ops, logger);
|
|
177
272
|
const stat = ops.logStat ? initialTreeLoadingStat() : void 0;
|
|
178
273
|
let ok = false;
|
|
179
274
|
try {
|
|
275
|
+
await tree.discover();
|
|
180
276
|
await tree.refresh(stat, { timeout: ops.initialTreeLoadingTimeout });
|
|
181
277
|
ok = true;
|
|
182
278
|
} finally {
|