@milaboratories/pl-tree 1.7.4 → 1.7.6
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 +297 -0
- package/dist/accessors.cjs.map +1 -0
- package/dist/accessors.d.ts +0 -1
- package/dist/accessors.js +288 -0
- package/dist/accessors.js.map +1 -0
- package/dist/dump.cjs +86 -0
- package/dist/dump.cjs.map +1 -0
- package/dist/dump.d.ts +0 -1
- package/dist/dump.js +84 -0
- package/dist/dump.js.map +1 -0
- package/dist/index.cjs +36 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +0 -1
- package/dist/index.js +7 -10
- package/dist/index.js.map +1 -1
- package/dist/snapshot.cjs +88 -0
- package/dist/snapshot.cjs.map +1 -0
- package/dist/snapshot.d.ts +0 -1
- package/dist/snapshot.js +83 -0
- package/dist/snapshot.js.map +1 -0
- package/dist/state.cjs +631 -0
- package/dist/state.cjs.map +1 -0
- package/dist/state.d.ts +0 -1
- package/dist/state.js +627 -0
- package/dist/state.js.map +1 -0
- package/dist/sync.cjs +156 -0
- package/dist/sync.cjs.map +1 -0
- package/dist/sync.d.ts +0 -1
- package/dist/sync.js +151 -0
- package/dist/sync.js.map +1 -0
- package/dist/synchronized_tree.cjs +235 -0
- package/dist/synchronized_tree.cjs.map +1 -0
- package/dist/synchronized_tree.d.ts +0 -1
- package/dist/synchronized_tree.js +214 -0
- package/dist/synchronized_tree.js.map +1 -0
- package/dist/test_utils.d.ts +0 -1
- package/dist/traversal_ops.d.ts +0 -1
- package/dist/value_and_error.cjs +20 -0
- package/dist/value_and_error.cjs.map +1 -0
- package/dist/value_and_error.d.ts +0 -1
- package/dist/value_and_error.js +17 -0
- package/dist/value_and_error.js.map +1 -0
- package/dist/value_or_error.d.ts +0 -1
- package/package.json +16 -14
- package/src/snapshot.test.ts +4 -3
- package/src/state.test.ts +6 -4
- package/dist/accessors.d.ts.map +0 -1
- package/dist/dump.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.mjs +0 -904
- package/dist/index.mjs.map +0 -1
- package/dist/snapshot.d.ts.map +0 -1
- package/dist/state.d.ts.map +0 -1
- package/dist/sync.d.ts.map +0 -1
- package/dist/synchronized_tree.d.ts.map +0 -1
- package/dist/test_utils.d.ts.map +0 -1
- package/dist/traversal_ops.d.ts.map +0 -1
- package/dist/value_and_error.d.ts.map +0 -1
- package/dist/value_or_error.d.ts.map +0 -1
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var computable = require('@milaboratories/computable');
|
|
4
|
+
var accessors = require('./accessors.cjs');
|
|
5
|
+
var plClient = require('@milaboratories/pl-client');
|
|
6
|
+
var state = require('./state.cjs');
|
|
7
|
+
var sync = require('./sync.cjs');
|
|
8
|
+
var tp = require('node:timers/promises');
|
|
9
|
+
|
|
10
|
+
function _interopNamespaceDefault(e) {
|
|
11
|
+
var n = Object.create(null);
|
|
12
|
+
if (e) {
|
|
13
|
+
Object.keys(e).forEach(function (k) {
|
|
14
|
+
if (k !== 'default') {
|
|
15
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
16
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
17
|
+
enumerable: true,
|
|
18
|
+
get: function () { return e[k]; }
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
n.default = e;
|
|
24
|
+
return Object.freeze(n);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
var tp__namespace = /*#__PURE__*/_interopNamespaceDefault(tp);
|
|
28
|
+
|
|
29
|
+
class SynchronizedTreeState {
|
|
30
|
+
pl;
|
|
31
|
+
root;
|
|
32
|
+
logger;
|
|
33
|
+
finalPredicate;
|
|
34
|
+
state;
|
|
35
|
+
pollingInterval;
|
|
36
|
+
pruning;
|
|
37
|
+
logStat;
|
|
38
|
+
hooks;
|
|
39
|
+
abortController = new AbortController();
|
|
40
|
+
constructor(pl, root, ops, logger) {
|
|
41
|
+
this.pl = pl;
|
|
42
|
+
this.root = root;
|
|
43
|
+
this.logger = logger;
|
|
44
|
+
const { finalPredicateOverride, pruning, pollingInterval, stopPollingDelay, logStat } = ops;
|
|
45
|
+
this.pruning = pruning;
|
|
46
|
+
this.pollingInterval = pollingInterval;
|
|
47
|
+
this.finalPredicate = finalPredicateOverride ?? pl.finalPredicate;
|
|
48
|
+
this.logStat = logStat;
|
|
49
|
+
this.state = new state.PlTreeState(root, this.finalPredicate);
|
|
50
|
+
this.hooks = new computable.PollingComputableHooks(() => this.startUpdating(), () => this.stopUpdating(), { stopDebounce: stopPollingDelay }, (resolve, reject) => this.scheduleOnNextState(resolve, reject));
|
|
51
|
+
}
|
|
52
|
+
/** @deprecated use "entry" instead */
|
|
53
|
+
accessor(rid = this.root) {
|
|
54
|
+
if (this.terminated)
|
|
55
|
+
throw new Error('tree synchronization is terminated');
|
|
56
|
+
return this.entry(rid);
|
|
57
|
+
}
|
|
58
|
+
entry(rid = this.root) {
|
|
59
|
+
if (this.terminated)
|
|
60
|
+
throw new Error('tree synchronization is terminated');
|
|
61
|
+
return new accessors.PlTreeEntry({ treeProvider: () => this.state, hooks: this.hooks }, rid);
|
|
62
|
+
}
|
|
63
|
+
/** Can be used to externally kick off the synchronization polling loop, and
|
|
64
|
+
* await for the first synchronization to happen. */
|
|
65
|
+
async refreshState() {
|
|
66
|
+
if (this.terminated)
|
|
67
|
+
throw new Error('tree synchronization is terminated');
|
|
68
|
+
await this.hooks.refreshState();
|
|
69
|
+
}
|
|
70
|
+
currentLoopDelayInterrupt = undefined;
|
|
71
|
+
scheduledOnNextState = [];
|
|
72
|
+
/** Called from computable hooks when external observer asks for state refresh */
|
|
73
|
+
scheduleOnNextState(resolve, reject) {
|
|
74
|
+
if (this.terminated)
|
|
75
|
+
reject(new Error('tree synchronization is terminated'));
|
|
76
|
+
else {
|
|
77
|
+
this.scheduledOnNextState.push({ resolve, reject });
|
|
78
|
+
if (this.currentLoopDelayInterrupt) {
|
|
79
|
+
this.currentLoopDelayInterrupt.abort();
|
|
80
|
+
this.currentLoopDelayInterrupt = undefined;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/** Called from observer */
|
|
85
|
+
startUpdating() {
|
|
86
|
+
if (this.terminated)
|
|
87
|
+
return;
|
|
88
|
+
this.keepRunning = true;
|
|
89
|
+
if (this.currentLoop === undefined)
|
|
90
|
+
this.currentLoop = this.mainLoop();
|
|
91
|
+
}
|
|
92
|
+
/** Called from observer */
|
|
93
|
+
stopUpdating() {
|
|
94
|
+
this.keepRunning = false;
|
|
95
|
+
}
|
|
96
|
+
/** If true, main loop will continue polling pl state. */
|
|
97
|
+
keepRunning = false;
|
|
98
|
+
/** Actual state of main loop. */
|
|
99
|
+
currentLoop = undefined;
|
|
100
|
+
/** Executed from the main loop, and initialization procedure. */
|
|
101
|
+
async refresh(stats, txOps) {
|
|
102
|
+
if (this.terminated)
|
|
103
|
+
throw new Error('tree synchronization is terminated');
|
|
104
|
+
const request = sync.constructTreeLoadingRequest(this.state, this.pruning);
|
|
105
|
+
const data = await this.pl.withReadTx('ReadingTree', async (tx) => {
|
|
106
|
+
return await sync.loadTreeState(tx, request, stats);
|
|
107
|
+
}, txOps);
|
|
108
|
+
this.state.updateFromResourceData(data, true);
|
|
109
|
+
}
|
|
110
|
+
/** If true this tree state is permanently terminaed. */
|
|
111
|
+
terminated = false;
|
|
112
|
+
async mainLoop() {
|
|
113
|
+
// will hold request stats
|
|
114
|
+
let stat = this.logStat ? sync.initialTreeLoadingStat() : undefined;
|
|
115
|
+
let lastUpdate = Date.now();
|
|
116
|
+
while (true) {
|
|
117
|
+
if (!this.keepRunning || this.terminated)
|
|
118
|
+
break;
|
|
119
|
+
// saving those who want to be notified about new state here
|
|
120
|
+
// because those who will be added during the tree retrieval
|
|
121
|
+
// should be notified only on the next round
|
|
122
|
+
let toNotify = undefined;
|
|
123
|
+
if (this.scheduledOnNextState.length > 0) {
|
|
124
|
+
toNotify = this.scheduledOnNextState;
|
|
125
|
+
this.scheduledOnNextState = [];
|
|
126
|
+
}
|
|
127
|
+
try {
|
|
128
|
+
// resetting stats if we were asked to collect non-cumulative stats
|
|
129
|
+
if (this.logStat === 'per-request')
|
|
130
|
+
stat = sync.initialTreeLoadingStat();
|
|
131
|
+
// actual tree synchronization
|
|
132
|
+
await this.refresh(stat);
|
|
133
|
+
// logging stats if we were asked to
|
|
134
|
+
if (stat && this.logger)
|
|
135
|
+
this.logger.info(`Tree stat (success, after ${Date.now() - lastUpdate}ms): ${JSON.stringify(stat)}`);
|
|
136
|
+
lastUpdate = Date.now();
|
|
137
|
+
// notifying that we got new state
|
|
138
|
+
if (toNotify !== undefined)
|
|
139
|
+
for (const n of toNotify)
|
|
140
|
+
n.resolve();
|
|
141
|
+
}
|
|
142
|
+
catch (e) {
|
|
143
|
+
// logging stats if we were asked to (even if error occured)
|
|
144
|
+
if (stat && this.logger)
|
|
145
|
+
this.logger.info(`Tree stat (error, after ${Date.now() - lastUpdate}ms): ${JSON.stringify(stat)}`);
|
|
146
|
+
lastUpdate = Date.now();
|
|
147
|
+
// notifying that we failed to refresh the state
|
|
148
|
+
if (toNotify !== undefined)
|
|
149
|
+
for (const n of toNotify)
|
|
150
|
+
n.reject(e);
|
|
151
|
+
// catching tree update errors, as they may leave our tree in inconsistent state
|
|
152
|
+
if (e instanceof state.TreeStateUpdateError) {
|
|
153
|
+
// important error logging, this should never happen
|
|
154
|
+
this.logger?.error(e);
|
|
155
|
+
// marking everybody who used previous state as changed
|
|
156
|
+
this.state.invalidateTree('stat update error');
|
|
157
|
+
// creating new tree
|
|
158
|
+
this.state = new state.PlTreeState(this.root, this.finalPredicate);
|
|
159
|
+
// scheduling state update without delay
|
|
160
|
+
continue;
|
|
161
|
+
// unfortunately external observer may still see tree in its default
|
|
162
|
+
// empty state, though this is best we can do in this exceptional
|
|
163
|
+
// situation, and hope on caching layers inside computables to present
|
|
164
|
+
// some stale state until we reconstruct the tree again
|
|
165
|
+
}
|
|
166
|
+
else
|
|
167
|
+
this.logger?.warn(e);
|
|
168
|
+
}
|
|
169
|
+
if (!this.keepRunning || this.terminated)
|
|
170
|
+
break;
|
|
171
|
+
if (this.scheduledOnNextState.length === 0) {
|
|
172
|
+
try {
|
|
173
|
+
this.currentLoopDelayInterrupt = new AbortController();
|
|
174
|
+
await tp__namespace.setTimeout(this.pollingInterval, AbortSignal.any([this.abortController.signal, this.currentLoopDelayInterrupt.signal]));
|
|
175
|
+
}
|
|
176
|
+
catch (e) {
|
|
177
|
+
if (!plClient.isTimeoutOrCancelError(e))
|
|
178
|
+
throw new Error('Unexpected error', { cause: e });
|
|
179
|
+
break;
|
|
180
|
+
}
|
|
181
|
+
finally {
|
|
182
|
+
this.currentLoopDelayInterrupt = undefined;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
// reset only as a very last line
|
|
187
|
+
this.currentLoop = undefined;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Dumps the current state of the tree.
|
|
191
|
+
* @returns An array of ExtendedResourceData objects representing the current state of the tree.
|
|
192
|
+
*/
|
|
193
|
+
dumpState() {
|
|
194
|
+
return this.state.dumpState();
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Terminates the internal loop, and permanently destoys all internal state, so
|
|
198
|
+
* all computables using this state will resolve to errors.
|
|
199
|
+
* */
|
|
200
|
+
async terminate() {
|
|
201
|
+
this.keepRunning = false;
|
|
202
|
+
this.terminated = true;
|
|
203
|
+
this.abortController.abort();
|
|
204
|
+
if (this.currentLoop === undefined)
|
|
205
|
+
return;
|
|
206
|
+
await this.currentLoop;
|
|
207
|
+
this.state.invalidateTree('synchronization terminated for the tree');
|
|
208
|
+
}
|
|
209
|
+
/** @deprecated */
|
|
210
|
+
async awaitSyncLoopTermination() {
|
|
211
|
+
if (this.currentLoop === undefined)
|
|
212
|
+
return;
|
|
213
|
+
await this.currentLoop;
|
|
214
|
+
}
|
|
215
|
+
static async init(pl, root, ops, logger) {
|
|
216
|
+
const tree = new SynchronizedTreeState(pl, root, ops, logger);
|
|
217
|
+
const stat = ops.logStat ? sync.initialTreeLoadingStat() : undefined;
|
|
218
|
+
let ok = false;
|
|
219
|
+
try {
|
|
220
|
+
await tree.refresh(stat, {
|
|
221
|
+
timeout: ops.initialTreeLoadingTimeout,
|
|
222
|
+
});
|
|
223
|
+
ok = true;
|
|
224
|
+
}
|
|
225
|
+
finally {
|
|
226
|
+
// logging stats if we were asked to (even if error occured)
|
|
227
|
+
if (stat && logger)
|
|
228
|
+
logger.info(`Tree stat (initial load, ${ok ? 'success' : 'failure'}): ${JSON.stringify(stat)}`);
|
|
229
|
+
}
|
|
230
|
+
return tree;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
exports.SynchronizedTreeState = SynchronizedTreeState;
|
|
235
|
+
//# sourceMappingURL=synchronized_tree.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"synchronized_tree.cjs","sources":["../src/synchronized_tree.ts"],"sourcesContent":["import { PollingComputableHooks } from '@milaboratories/computable';\nimport { PlTreeEntry } from './accessors';\nimport type {\n FinalResourceDataPredicate,\n PlClient,\n ResourceId,\n TxOps,\n} from '@milaboratories/pl-client';\nimport {\n isTimeoutOrCancelError,\n} from '@milaboratories/pl-client';\nimport type { ExtendedResourceData } from './state';\nimport { PlTreeState, TreeStateUpdateError } from './state';\nimport type {\n PruningFunction,\n TreeLoadingStat,\n} from './sync';\nimport {\n constructTreeLoadingRequest,\n initialTreeLoadingStat,\n loadTreeState,\n} from './sync';\nimport * as tp from 'node:timers/promises';\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 to limit set of fields through which tree will\n * traverse during state synchronization */\n pruning?: PruningFunction;\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\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 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: ResourceId,\n ops: SynchronizedTreeOps,\n private readonly logger?: MiLogger,\n ) {\n const { finalPredicateOverride, pruning, pollingInterval, stopPollingDelay, logStat } = ops;\n this.pruning = pruning;\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: ResourceId = 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: ResourceId = 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, this.pruning);\n const data = await this.pl.withReadTx('ReadingTree', async (tx) => {\n return await loadTreeState(tx, request, stats);\n }, txOps);\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) this.logger.info(`Tree stat (success, after ${Date.now() - lastUpdate}ms): ${JSON.stringify(stat)}`);\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) this.logger.info(`Tree stat (error, after ${Date.now() - lastUpdate}ms): ${JSON.stringify(stat)}`);\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 if (this.scheduledOnNextState.length === 0) {\n try {\n this.currentLoopDelayInterrupt = new AbortController();\n await tp.setTimeout(this.pollingInterval,\n AbortSignal.any([this.abortController.signal, this.currentLoopDelayInterrupt.signal]));\n } catch (e: unknown) {\n if (!isTimeoutOrCancelError(e)) throw new Error('Unexpected error', { cause: e });\n break;\n } finally {\n this.currentLoopDelayInterrupt = undefined;\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: ResourceId,\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"],"names":["PlTreeState","PollingComputableHooks","PlTreeEntry","constructTreeLoadingRequest","loadTreeState","initialTreeLoadingStat","TreeStateUpdateError","tp","isTimeoutOrCancelError"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;MAoDa,qBAAqB,CAAA;AAUb,IAAA,EAAA;AACA,IAAA,IAAA;AAEA,IAAA,MAAA;AAZF,IAAA,cAAc;AACvB,IAAA,KAAK;AACI,IAAA,eAAe;AACf,IAAA,OAAO;AACP,IAAA,OAAO;AACP,IAAA,KAAK;AACL,IAAA,eAAe,GAAG,IAAI,eAAe,EAAE;AAExD,IAAA,WAAA,CACmB,EAAY,EACZ,IAAgB,EACjC,GAAwB,EACP,MAAiB,EAAA;QAHjB,IAAA,CAAA,EAAE,GAAF,EAAE;QACF,IAAA,CAAA,IAAI,GAAJ,IAAI;QAEJ,IAAA,CAAA,MAAM,GAAN,MAAM;AAEvB,QAAA,MAAM,EAAE,sBAAsB,EAAE,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE,OAAO,EAAE,GAAG,GAAG;AAC3F,QAAA,IAAI,CAAC,OAAO,GAAG,OAAO;AACtB,QAAA,IAAI,CAAC,eAAe,GAAG,eAAe;QACtC,IAAI,CAAC,cAAc,GAAG,sBAAsB,IAAI,EAAE,CAAC,cAAc;AACjE,Q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Hooks } from '@milaboratories/computable';
|
|
2
|
+
import { PlTreeEntry } from './accessors.js';
|
|
3
|
+
import { isTimeoutOrCancelError } from '@milaboratories/pl-client';
|
|
4
|
+
import { PlTreeState, TreeStateUpdateError } from './state.js';
|
|
5
|
+
import { constructTreeLoadingRequest, loadTreeState, initialTreeLoadingStat } from './sync.js';
|
|
6
|
+
import * as tp from 'node:timers/promises';
|
|
7
|
+
|
|
8
|
+
class SynchronizedTreeState {
|
|
9
|
+
pl;
|
|
10
|
+
root;
|
|
11
|
+
logger;
|
|
12
|
+
finalPredicate;
|
|
13
|
+
state;
|
|
14
|
+
pollingInterval;
|
|
15
|
+
pruning;
|
|
16
|
+
logStat;
|
|
17
|
+
hooks;
|
|
18
|
+
abortController = new AbortController();
|
|
19
|
+
constructor(pl, root, ops, logger) {
|
|
20
|
+
this.pl = pl;
|
|
21
|
+
this.root = root;
|
|
22
|
+
this.logger = logger;
|
|
23
|
+
const { finalPredicateOverride, pruning, pollingInterval, stopPollingDelay, logStat } = ops;
|
|
24
|
+
this.pruning = pruning;
|
|
25
|
+
this.pollingInterval = pollingInterval;
|
|
26
|
+
this.finalPredicate = finalPredicateOverride ?? pl.finalPredicate;
|
|
27
|
+
this.logStat = logStat;
|
|
28
|
+
this.state = new PlTreeState(root, this.finalPredicate);
|
|
29
|
+
this.hooks = new PollingComputableHooks(() => this.startUpdating(), () => this.stopUpdating(), { stopDebounce: stopPollingDelay }, (resolve, reject) => this.scheduleOnNextState(resolve, reject));
|
|
30
|
+
}
|
|
31
|
+
/** @deprecated use "entry" instead */
|
|
32
|
+
accessor(rid = this.root) {
|
|
33
|
+
if (this.terminated)
|
|
34
|
+
throw new Error('tree synchronization is terminated');
|
|
35
|
+
return this.entry(rid);
|
|
36
|
+
}
|
|
37
|
+
entry(rid = this.root) {
|
|
38
|
+
if (this.terminated)
|
|
39
|
+
throw new Error('tree synchronization is terminated');
|
|
40
|
+
return new PlTreeEntry({ treeProvider: () => this.state, hooks: this.hooks }, rid);
|
|
41
|
+
}
|
|
42
|
+
/** Can be used to externally kick off the synchronization polling loop, and
|
|
43
|
+
* await for the first synchronization to happen. */
|
|
44
|
+
async refreshState() {
|
|
45
|
+
if (this.terminated)
|
|
46
|
+
throw new Error('tree synchronization is terminated');
|
|
47
|
+
await this.hooks.refreshState();
|
|
48
|
+
}
|
|
49
|
+
currentLoopDelayInterrupt = undefined;
|
|
50
|
+
scheduledOnNextState = [];
|
|
51
|
+
/** Called from computable hooks when external observer asks for state refresh */
|
|
52
|
+
scheduleOnNextState(resolve, reject) {
|
|
53
|
+
if (this.terminated)
|
|
54
|
+
reject(new Error('tree synchronization is terminated'));
|
|
55
|
+
else {
|
|
56
|
+
this.scheduledOnNextState.push({ resolve, reject });
|
|
57
|
+
if (this.currentLoopDelayInterrupt) {
|
|
58
|
+
this.currentLoopDelayInterrupt.abort();
|
|
59
|
+
this.currentLoopDelayInterrupt = undefined;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/** Called from observer */
|
|
64
|
+
startUpdating() {
|
|
65
|
+
if (this.terminated)
|
|
66
|
+
return;
|
|
67
|
+
this.keepRunning = true;
|
|
68
|
+
if (this.currentLoop === undefined)
|
|
69
|
+
this.currentLoop = this.mainLoop();
|
|
70
|
+
}
|
|
71
|
+
/** Called from observer */
|
|
72
|
+
stopUpdating() {
|
|
73
|
+
this.keepRunning = false;
|
|
74
|
+
}
|
|
75
|
+
/** If true, main loop will continue polling pl state. */
|
|
76
|
+
keepRunning = false;
|
|
77
|
+
/** Actual state of main loop. */
|
|
78
|
+
currentLoop = undefined;
|
|
79
|
+
/** Executed from the main loop, and initialization procedure. */
|
|
80
|
+
async refresh(stats, txOps) {
|
|
81
|
+
if (this.terminated)
|
|
82
|
+
throw new Error('tree synchronization is terminated');
|
|
83
|
+
const request = constructTreeLoadingRequest(this.state, this.pruning);
|
|
84
|
+
const data = await this.pl.withReadTx('ReadingTree', async (tx) => {
|
|
85
|
+
return await loadTreeState(tx, request, stats);
|
|
86
|
+
}, txOps);
|
|
87
|
+
this.state.updateFromResourceData(data, true);
|
|
88
|
+
}
|
|
89
|
+
/** If true this tree state is permanently terminaed. */
|
|
90
|
+
terminated = false;
|
|
91
|
+
async mainLoop() {
|
|
92
|
+
// will hold request stats
|
|
93
|
+
let stat = this.logStat ? initialTreeLoadingStat() : undefined;
|
|
94
|
+
let lastUpdate = Date.now();
|
|
95
|
+
while (true) {
|
|
96
|
+
if (!this.keepRunning || this.terminated)
|
|
97
|
+
break;
|
|
98
|
+
// saving those who want to be notified about new state here
|
|
99
|
+
// because those who will be added during the tree retrieval
|
|
100
|
+
// should be notified only on the next round
|
|
101
|
+
let toNotify = undefined;
|
|
102
|
+
if (this.scheduledOnNextState.length > 0) {
|
|
103
|
+
toNotify = this.scheduledOnNextState;
|
|
104
|
+
this.scheduledOnNextState = [];
|
|
105
|
+
}
|
|
106
|
+
try {
|
|
107
|
+
// resetting stats if we were asked to collect non-cumulative stats
|
|
108
|
+
if (this.logStat === 'per-request')
|
|
109
|
+
stat = initialTreeLoadingStat();
|
|
110
|
+
// actual tree synchronization
|
|
111
|
+
await this.refresh(stat);
|
|
112
|
+
// logging stats if we were asked to
|
|
113
|
+
if (stat && this.logger)
|
|
114
|
+
this.logger.info(`Tree stat (success, after ${Date.now() - lastUpdate}ms): ${JSON.stringify(stat)}`);
|
|
115
|
+
lastUpdate = Date.now();
|
|
116
|
+
// notifying that we got new state
|
|
117
|
+
if (toNotify !== undefined)
|
|
118
|
+
for (const n of toNotify)
|
|
119
|
+
n.resolve();
|
|
120
|
+
}
|
|
121
|
+
catch (e) {
|
|
122
|
+
// logging stats if we were asked to (even if error occured)
|
|
123
|
+
if (stat && this.logger)
|
|
124
|
+
this.logger.info(`Tree stat (error, after ${Date.now() - lastUpdate}ms): ${JSON.stringify(stat)}`);
|
|
125
|
+
lastUpdate = Date.now();
|
|
126
|
+
// notifying that we failed to refresh the state
|
|
127
|
+
if (toNotify !== undefined)
|
|
128
|
+
for (const n of toNotify)
|
|
129
|
+
n.reject(e);
|
|
130
|
+
// catching tree update errors, as they may leave our tree in inconsistent state
|
|
131
|
+
if (e instanceof TreeStateUpdateError) {
|
|
132
|
+
// important error logging, this should never happen
|
|
133
|
+
this.logger?.error(e);
|
|
134
|
+
// marking everybody who used previous state as changed
|
|
135
|
+
this.state.invalidateTree('stat update error');
|
|
136
|
+
// creating new tree
|
|
137
|
+
this.state = new PlTreeState(this.root, this.finalPredicate);
|
|
138
|
+
// scheduling state update without delay
|
|
139
|
+
continue;
|
|
140
|
+
// unfortunately external observer may still see tree in its default
|
|
141
|
+
// empty state, though this is best we can do in this exceptional
|
|
142
|
+
// situation, and hope on caching layers inside computables to present
|
|
143
|
+
// some stale state until we reconstruct the tree again
|
|
144
|
+
}
|
|
145
|
+
else
|
|
146
|
+
this.logger?.warn(e);
|
|
147
|
+
}
|
|
148
|
+
if (!this.keepRunning || this.terminated)
|
|
149
|
+
break;
|
|
150
|
+
if (this.scheduledOnNextState.length === 0) {
|
|
151
|
+
try {
|
|
152
|
+
this.currentLoopDelayInterrupt = new AbortController();
|
|
153
|
+
await tp.setTimeout(this.pollingInterval, AbortSignal.any([this.abortController.signal, this.currentLoopDelayInterrupt.signal]));
|
|
154
|
+
}
|
|
155
|
+
catch (e) {
|
|
156
|
+
if (!isTimeoutOrCancelError(e))
|
|
157
|
+
throw new Error('Unexpected error', { cause: e });
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
finally {
|
|
161
|
+
this.currentLoopDelayInterrupt = undefined;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
// reset only as a very last line
|
|
166
|
+
this.currentLoop = undefined;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Dumps the current state of the tree.
|
|
170
|
+
* @returns An array of ExtendedResourceData objects representing the current state of the tree.
|
|
171
|
+
*/
|
|
172
|
+
dumpState() {
|
|
173
|
+
return this.state.dumpState();
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Terminates the internal loop, and permanently destoys all internal state, so
|
|
177
|
+
* all computables using this state will resolve to errors.
|
|
178
|
+
* */
|
|
179
|
+
async terminate() {
|
|
180
|
+
this.keepRunning = false;
|
|
181
|
+
this.terminated = true;
|
|
182
|
+
this.abortController.abort();
|
|
183
|
+
if (this.currentLoop === undefined)
|
|
184
|
+
return;
|
|
185
|
+
await this.currentLoop;
|
|
186
|
+
this.state.invalidateTree('synchronization terminated for the tree');
|
|
187
|
+
}
|
|
188
|
+
/** @deprecated */
|
|
189
|
+
async awaitSyncLoopTermination() {
|
|
190
|
+
if (this.currentLoop === undefined)
|
|
191
|
+
return;
|
|
192
|
+
await this.currentLoop;
|
|
193
|
+
}
|
|
194
|
+
static async init(pl, root, ops, logger) {
|
|
195
|
+
const tree = new SynchronizedTreeState(pl, root, ops, logger);
|
|
196
|
+
const stat = ops.logStat ? initialTreeLoadingStat() : undefined;
|
|
197
|
+
let ok = false;
|
|
198
|
+
try {
|
|
199
|
+
await tree.refresh(stat, {
|
|
200
|
+
timeout: ops.initialTreeLoadingTimeout,
|
|
201
|
+
});
|
|
202
|
+
ok = true;
|
|
203
|
+
}
|
|
204
|
+
finally {
|
|
205
|
+
// logging stats if we were asked to (even if error occured)
|
|
206
|
+
if (stat && logger)
|
|
207
|
+
logger.info(`Tree stat (initial load, ${ok ? 'success' : 'failure'}): ${JSON.stringify(stat)}`);
|
|
208
|
+
}
|
|
209
|
+
return tree;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export { SynchronizedTreeState };
|
|
214
|
+
//# sourceMappingURL=synchronized_tree.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"synchronized_tree.js","sources":["../src/synchronized_tree.ts"],"sourcesContent":["import { PollingComputableHooks } from '@milaboratories/computable';\nimport { PlTreeEntry } from './accessors';\nimport type {\n FinalResourceDataPredicate,\n PlClient,\n ResourceId,\n TxOps,\n} from '@milaboratories/pl-client';\nimport {\n isTimeoutOrCancelError,\n} from '@milaboratories/pl-client';\nimport type { ExtendedResourceData } from './state';\nimport { PlTreeState, TreeStateUpdateError } from './state';\nimport type {\n PruningFunction,\n TreeLoadingStat,\n} from './sync';\nimport {\n constructTreeLoadingRequest,\n initialTreeLoadingStat,\n loadTreeState,\n} from './sync';\nimport * as tp from 'node:timers/promises';\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 to limit set of fields through which tree will\n * traverse during state synchronization */\n pruning?: PruningFunction;\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\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 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: ResourceId,\n ops: SynchronizedTreeOps,\n private readonly logger?: MiLogger,\n ) {\n const { finalPredicateOverride, pruning, pollingInterval, stopPollingDelay, logStat } = ops;\n this.pruning = pruning;\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: ResourceId = 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: ResourceId = 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, this.pruning);\n const data = await this.pl.withReadTx('ReadingTree', async (tx) => {\n return await loadTreeState(tx, request, stats);\n }, txOps);\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) this.logger.info(`Tree stat (success, after ${Date.now() - lastUpdate}ms): ${JSON.stringify(stat)}`);\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) this.logger.info(`Tree stat (error, after ${Date.now() - lastUpdate}ms): ${JSON.stringify(stat)}`);\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 if (this.scheduledOnNextState.length === 0) {\n try {\n this.currentLoopDelayInterrupt = new AbortController();\n await tp.setTimeout(this.pollingInterval,\n AbortSignal.any([this.abortController.signal, this.currentLoopDelayInterrupt.signal]));\n } catch (e: unknown) {\n if (!isTimeoutOrCancelError(e)) throw new Error('Unexpected error', { cause: e });\n break;\n } finally {\n this.currentLoopDelayInterrupt = undefined;\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: ResourceId,\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"],"names":[],"mappings":";;;;;;;MAoDa,qBAAqB,CAAA;AAUb,IAAA,EAAA;AACA,IAAA,IAAA;AAEA,IAAA,MAAA;AAZF,IAAA,cAAc;AACvB,IAAA,KAAK;AACI,IAAA,eAAe;AACf,IAAA,OAAO;AACP,IAAA,OAAO;AACP,IAAA,KAAK;AACL,IAAA,eAAe,GAAG,IAAI,eAAe,EAAE;AAExD,IAAA,WAAA,CACmB,EAAY,EACZ,IAAgB,EACjC,GAAwB,EACP,MAAiB,EAAA;QAHjB,IAAA,CAAA,EAAE,GAAF,EAAE;QACF,IAAA,CAAA,IAAI,GAAJ,IAAI;QAEJ,I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AI,IAAI,IAAI,MAAM;gBAChB,MAAM,CAAC,IAAI,CACT,CAAA,yBAAA,EAA4B,EAAE,GAAG,SAAS,GAAG,SAAS,CAAA,GAAA,EAAM,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAA,CAAE,CACnF;QACL;AAEA,QAAA,OAAO,IAAI;IACb;AACD;;;;"}
|
package/dist/test_utils.d.ts
CHANGED
|
@@ -22,4 +22,3 @@ export declare const TestDynamicRootState2: Omit<ExtendedResourceData, 'fields'>
|
|
|
22
22
|
export declare function field(type: FieldType, name: string, value?: OptionalResourceId, error?: OptionalResourceId, valueIsFinal?: boolean): FieldData;
|
|
23
23
|
export declare function dField(name: string, value?: OptionalResourceId, error?: OptionalResourceId): FieldData;
|
|
24
24
|
export declare function iField(name: string, value?: OptionalResourceId, error?: OptionalResourceId): FieldData;
|
|
25
|
-
//# sourceMappingURL=test_utils.d.ts.map
|
package/dist/traversal_ops.d.ts
CHANGED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
function mapValueAndErrorIfDefined(input, mapping) {
|
|
4
|
+
if (input === undefined)
|
|
5
|
+
return undefined;
|
|
6
|
+
else
|
|
7
|
+
return mapValueAndError(input, mapping);
|
|
8
|
+
}
|
|
9
|
+
function mapValueAndError(input, mapping) {
|
|
10
|
+
const ret = {};
|
|
11
|
+
if (input.value !== undefined)
|
|
12
|
+
ret.value = mapping(input.value);
|
|
13
|
+
if (input.error !== undefined)
|
|
14
|
+
ret.error = mapping(input.error);
|
|
15
|
+
return ret;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
exports.mapValueAndError = mapValueAndError;
|
|
19
|
+
exports.mapValueAndErrorIfDefined = mapValueAndErrorIfDefined;
|
|
20
|
+
//# sourceMappingURL=value_and_error.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"value_and_error.cjs","sources":["../src/value_and_error.ts"],"sourcesContent":["export interface ValueAndError<T> {\n value?: T;\n error?: T;\n}\n\nexport function mapValueAndErrorIfDefined<T1, T2>(\n input: ValueAndError<T1> | undefined,\n mapping: (v: T1) => T2,\n): ValueAndError<T2> | undefined {\n if (input === undefined) return undefined;\n else return mapValueAndError(input, mapping);\n}\n\nexport function mapValueAndError<T1, T2>(input: ValueAndError<T1>, mapping: (v: T1) => T2) {\n const ret = {} as ValueAndError<T2>;\n if (input.value !== undefined) ret.value = mapping(input.value);\n if (input.error !== undefined) ret.error = mapping(input.error);\n return ret;\n}\n"],"names":[],"mappings":";;AAKM,SAAU,yBAAyB,CACvC,KAAoC,EACpC,OAAsB,EAAA;IAEtB,IAAI,KAAK,KAAK,SAAS;AAAE,QAAA,OAAO,SAAS;;AACpC,QAAA,OAAO,gBAAgB,CAAC,KAAK,EAAE,OAAO,CAAC;AAC9C;AAEM,SAAU,gBAAgB,CAAS,KAAwB,EAAE,OAAsB,EAAA;IACvF,MAAM,GAAG,GAAG,EAAuB;AACnC,IAAA,IAAI,KAAK,CAAC,KAAK,KAAK,SAAS;QAAE,GAAG,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC;AAC/D,IAAA,IAAI,KAAK,CAAC,KAAK,KAAK,SAAS;QAAE,GAAG,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC;AAC/D,IAAA,OAAO,GAAG;AACZ;;;;;"}
|
|
@@ -4,4 +4,3 @@ export interface ValueAndError<T> {
|
|
|
4
4
|
}
|
|
5
5
|
export declare function mapValueAndErrorIfDefined<T1, T2>(input: ValueAndError<T1> | undefined, mapping: (v: T1) => T2): ValueAndError<T2> | undefined;
|
|
6
6
|
export declare function mapValueAndError<T1, T2>(input: ValueAndError<T1>, mapping: (v: T1) => T2): ValueAndError<T2>;
|
|
7
|
-
//# sourceMappingURL=value_and_error.d.ts.map
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
function mapValueAndErrorIfDefined(input, mapping) {
|
|
2
|
+
if (input === undefined)
|
|
3
|
+
return undefined;
|
|
4
|
+
else
|
|
5
|
+
return mapValueAndError(input, mapping);
|
|
6
|
+
}
|
|
7
|
+
function mapValueAndError(input, mapping) {
|
|
8
|
+
const ret = {};
|
|
9
|
+
if (input.value !== undefined)
|
|
10
|
+
ret.value = mapping(input.value);
|
|
11
|
+
if (input.error !== undefined)
|
|
12
|
+
ret.error = mapping(input.error);
|
|
13
|
+
return ret;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export { mapValueAndError, mapValueAndErrorIfDefined };
|
|
17
|
+
//# sourceMappingURL=value_and_error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"value_and_error.js","sources":["../src/value_and_error.ts"],"sourcesContent":["export interface ValueAndError<T> {\n value?: T;\n error?: T;\n}\n\nexport function mapValueAndErrorIfDefined<T1, T2>(\n input: ValueAndError<T1> | undefined,\n mapping: (v: T1) => T2,\n): ValueAndError<T2> | undefined {\n if (input === undefined) return undefined;\n else return mapValueAndError(input, mapping);\n}\n\nexport function mapValueAndError<T1, T2>(input: ValueAndError<T1>, mapping: (v: T1) => T2) {\n const ret = {} as ValueAndError<T2>;\n if (input.value !== undefined) ret.value = mapping(input.value);\n if (input.error !== undefined) ret.error = mapping(input.error);\n return ret;\n}\n"],"names":[],"mappings":"AAKM,SAAU,yBAAyB,CACvC,KAAoC,EACpC,OAAsB,EAAA;IAEtB,IAAI,KAAK,KAAK,SAAS;AAAE,QAAA,OAAO,SAAS;;AACpC,QAAA,OAAO,gBAAgB,CAAC,KAAK,EAAE,OAAO,CAAC;AAC9C;AAEM,SAAU,gBAAgB,CAAS,KAAwB,EAAE,OAAsB,EAAA;IACvF,MAAM,GAAG,GAAG,EAAuB;AACnC,IAAA,IAAI,KAAK,CAAC,KAAK,KAAK,SAAS;QAAE,GAAG,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC;AAC/D,IAAA,IAAI,KAAK,CAAC,KAAK,KAAK,SAAS;QAAE,GAAG,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC;AAC/D,IAAA,OAAO,GAAG;AACZ;;;;"}
|
package/dist/value_or_error.d.ts
CHANGED