@milaboratories/pl-tree 1.8.47 → 1.8.49
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/_virtual/_rolldown/runtime.cjs +29 -0
- package/dist/accessors.cjs +236 -273
- package/dist/accessors.cjs.map +1 -1
- package/dist/accessors.d.ts +93 -93
- package/dist/accessors.js +235 -271
- package/dist/accessors.js.map +1 -1
- package/dist/dump.cjs +79 -80
- package/dist/dump.cjs.map +1 -1
- package/dist/dump.d.ts +16 -18
- package/dist/dump.js +79 -79
- package/dist/dump.js.map +1 -1
- package/dist/index.cjs +31 -35
- package/dist/index.d.ts +10 -10
- package/dist/index.js +9 -8
- package/dist/snapshot.cjs +50 -71
- package/dist/snapshot.cjs.map +1 -1
- package/dist/snapshot.d.ts +36 -38
- package/dist/snapshot.js +49 -69
- package/dist/snapshot.js.map +1 -1
- package/dist/state.cjs +487 -624
- package/dist/state.cjs.map +1 -1
- package/dist/state.d.ts +117 -116
- package/dist/state.js +486 -622
- package/dist/state.js.map +1 -1
- package/dist/sync.cjs +113 -133
- package/dist/sync.cjs.map +1 -1
- package/dist/sync.d.ts +37 -33
- package/dist/sync.js +111 -131
- package/dist/sync.js.map +1 -1
- package/dist/synchronized_tree.cjs +167 -232
- package/dist/synchronized_tree.cjs.map +1 -1
- package/dist/synchronized_tree.d.ts +66 -68
- package/dist/synchronized_tree.js +165 -211
- package/dist/synchronized_tree.js.map +1 -1
- package/dist/traversal_ops.d.ts +42 -42
- package/dist/value_and_error.cjs +9 -12
- package/dist/value_and_error.cjs.map +1 -1
- package/dist/value_and_error.d.ts +8 -5
- package/dist/value_and_error.js +9 -11
- package/dist/value_and_error.js.map +1 -1
- package/dist/value_or_error.d.ts +8 -5
- package/package.json +9 -9
- package/src/synchronized_tree.ts +10 -5
- package/dist/accessors.d.ts.map +0 -1
- package/dist/dump.d.ts.map +0 -1
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.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 +0 -25
- 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
|
@@ -1,214 +1,168 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import * as tp from
|
|
1
|
+
import { PlTreeEntry } from "./accessors.js";
|
|
2
|
+
import { PlTreeState, TreeStateUpdateError } from "./state.js";
|
|
3
|
+
import { constructTreeLoadingRequest, initialTreeLoadingStat, loadTreeState } from "./sync.js";
|
|
4
|
+
import { isTimeoutOrCancelError } from "@milaboratories/pl-client";
|
|
5
|
+
import { PollingComputableHooks } from "@milaboratories/computable";
|
|
6
|
+
import * as tp from "node:timers/promises";
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
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
|
-
}
|
|
8
|
+
//#region src/synchronized_tree.ts
|
|
9
|
+
var SynchronizedTreeState = class SynchronizedTreeState {
|
|
10
|
+
finalPredicate;
|
|
11
|
+
state;
|
|
12
|
+
pollingInterval;
|
|
13
|
+
pruning;
|
|
14
|
+
logStat;
|
|
15
|
+
hooks;
|
|
16
|
+
abortController = new AbortController();
|
|
17
|
+
constructor(pl, root, ops, logger) {
|
|
18
|
+
this.pl = pl;
|
|
19
|
+
this.root = root;
|
|
20
|
+
this.logger = logger;
|
|
21
|
+
const { finalPredicateOverride, pruning, pollingInterval, stopPollingDelay, logStat } = ops;
|
|
22
|
+
this.pruning = pruning;
|
|
23
|
+
this.pollingInterval = pollingInterval;
|
|
24
|
+
this.finalPredicate = finalPredicateOverride ?? pl.finalPredicate;
|
|
25
|
+
this.logStat = logStat;
|
|
26
|
+
this.state = new PlTreeState(root, this.finalPredicate);
|
|
27
|
+
this.hooks = new PollingComputableHooks(() => this.startUpdating(), () => this.stopUpdating(), { stopDebounce: stopPollingDelay }, (resolve, reject) => this.scheduleOnNextState(resolve, reject));
|
|
28
|
+
}
|
|
29
|
+
/** @deprecated use "entry" instead */
|
|
30
|
+
accessor(rid = this.root) {
|
|
31
|
+
if (this.terminated) throw new Error("tree synchronization is terminated");
|
|
32
|
+
return this.entry(rid);
|
|
33
|
+
}
|
|
34
|
+
entry(rid = this.root) {
|
|
35
|
+
if (this.terminated) throw new Error("tree synchronization is terminated");
|
|
36
|
+
return new PlTreeEntry({
|
|
37
|
+
treeProvider: () => this.state,
|
|
38
|
+
hooks: this.hooks
|
|
39
|
+
}, rid);
|
|
40
|
+
}
|
|
41
|
+
/** Can be used to externally kick off the synchronization polling loop, and
|
|
42
|
+
* await for the first synchronization to happen. */
|
|
43
|
+
async refreshState() {
|
|
44
|
+
if (this.terminated) throw new Error("tree synchronization is terminated");
|
|
45
|
+
await this.hooks.refreshState();
|
|
46
|
+
}
|
|
47
|
+
currentLoopDelayInterrupt = void 0;
|
|
48
|
+
scheduledOnNextState = [];
|
|
49
|
+
/** Called from computable hooks when external observer asks for state refresh */
|
|
50
|
+
scheduleOnNextState(resolve, reject) {
|
|
51
|
+
if (this.terminated) reject(/* @__PURE__ */ new Error("tree synchronization is terminated"));
|
|
52
|
+
else {
|
|
53
|
+
this.scheduledOnNextState.push({
|
|
54
|
+
resolve,
|
|
55
|
+
reject
|
|
56
|
+
});
|
|
57
|
+
if (this.currentLoopDelayInterrupt) {
|
|
58
|
+
this.currentLoopDelayInterrupt.abort();
|
|
59
|
+
this.currentLoopDelayInterrupt = void 0;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/** Called from observer */
|
|
64
|
+
startUpdating() {
|
|
65
|
+
if (this.terminated) return;
|
|
66
|
+
this.keepRunning = true;
|
|
67
|
+
if (this.currentLoop === void 0) this.currentLoop = this.mainLoop();
|
|
68
|
+
}
|
|
69
|
+
/** Called from observer */
|
|
70
|
+
stopUpdating() {
|
|
71
|
+
this.keepRunning = false;
|
|
72
|
+
}
|
|
73
|
+
/** If true, main loop will continue polling pl state. */
|
|
74
|
+
keepRunning = false;
|
|
75
|
+
/** Actual state of main loop. */
|
|
76
|
+
currentLoop = void 0;
|
|
77
|
+
/** Executed from the main loop, and initialization procedure. */
|
|
78
|
+
async refresh(stats, txOps) {
|
|
79
|
+
if (this.terminated) throw new Error("tree synchronization is terminated");
|
|
80
|
+
const request = constructTreeLoadingRequest(this.state, this.pruning);
|
|
81
|
+
const data = await this.pl.withReadTx("ReadingTree", async (tx) => {
|
|
82
|
+
return await loadTreeState(tx, request, stats);
|
|
83
|
+
}, txOps);
|
|
84
|
+
this.state.updateFromResourceData(data, true);
|
|
85
|
+
}
|
|
86
|
+
/** If true this tree state is permanently terminaed. */
|
|
87
|
+
terminated = false;
|
|
88
|
+
async mainLoop() {
|
|
89
|
+
let stat = this.logStat ? initialTreeLoadingStat() : void 0;
|
|
90
|
+
let lastUpdate = Date.now();
|
|
91
|
+
while (true) {
|
|
92
|
+
if (!this.keepRunning || this.terminated) break;
|
|
93
|
+
let toNotify = void 0;
|
|
94
|
+
if (this.scheduledOnNextState.length > 0) {
|
|
95
|
+
toNotify = this.scheduledOnNextState;
|
|
96
|
+
this.scheduledOnNextState = [];
|
|
97
|
+
}
|
|
98
|
+
try {
|
|
99
|
+
if (this.logStat === "per-request") stat = initialTreeLoadingStat();
|
|
100
|
+
await this.refresh(stat);
|
|
101
|
+
if (stat && this.logger) this.logger.info(`Tree stat (success, after ${Date.now() - lastUpdate}ms): ${JSON.stringify(stat)}`);
|
|
102
|
+
lastUpdate = Date.now();
|
|
103
|
+
if (toNotify !== void 0) for (const n of toNotify) n.resolve();
|
|
104
|
+
} catch (e) {
|
|
105
|
+
if (stat && this.logger) this.logger.info(`Tree stat (error, after ${Date.now() - lastUpdate}ms): ${JSON.stringify(stat)}`);
|
|
106
|
+
lastUpdate = Date.now();
|
|
107
|
+
if (toNotify !== void 0) for (const n of toNotify) n.reject(e);
|
|
108
|
+
if (e instanceof TreeStateUpdateError) {
|
|
109
|
+
this.logger?.error(e);
|
|
110
|
+
this.state.invalidateTree("stat update error");
|
|
111
|
+
this.state = new PlTreeState(this.root, this.finalPredicate);
|
|
112
|
+
continue;
|
|
113
|
+
} else this.logger?.warn(e);
|
|
114
|
+
}
|
|
115
|
+
if (!this.keepRunning || this.terminated) break;
|
|
116
|
+
if (this.scheduledOnNextState.length === 0) try {
|
|
117
|
+
this.currentLoopDelayInterrupt = new AbortController();
|
|
118
|
+
await tp.setTimeout(this.pollingInterval, void 0, { signal: AbortSignal.any([this.abortController.signal, this.currentLoopDelayInterrupt.signal]) });
|
|
119
|
+
} catch (e) {
|
|
120
|
+
if (!isTimeoutOrCancelError(e)) throw new Error("Unexpected error", { cause: e });
|
|
121
|
+
if (this.abortController.signal.aborted) break;
|
|
122
|
+
} finally {
|
|
123
|
+
this.currentLoopDelayInterrupt = void 0;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
this.currentLoop = void 0;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Dumps the current state of the tree.
|
|
130
|
+
* @returns An array of ExtendedResourceData objects representing the current state of the tree.
|
|
131
|
+
*/
|
|
132
|
+
dumpState() {
|
|
133
|
+
return this.state.dumpState();
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Terminates the internal loop, and permanently destoys all internal state, so
|
|
137
|
+
* all computables using this state will resolve to errors.
|
|
138
|
+
* */
|
|
139
|
+
async terminate() {
|
|
140
|
+
this.keepRunning = false;
|
|
141
|
+
this.terminated = true;
|
|
142
|
+
this.abortController.abort();
|
|
143
|
+
if (this.currentLoop === void 0) return;
|
|
144
|
+
await this.currentLoop;
|
|
145
|
+
this.state.invalidateTree("synchronization terminated for the tree");
|
|
146
|
+
}
|
|
147
|
+
/** @deprecated */
|
|
148
|
+
async awaitSyncLoopTermination() {
|
|
149
|
+
if (this.currentLoop === void 0) return;
|
|
150
|
+
await this.currentLoop;
|
|
151
|
+
}
|
|
152
|
+
static async init(pl, root, ops, logger) {
|
|
153
|
+
const tree = new SynchronizedTreeState(pl, root, ops, logger);
|
|
154
|
+
const stat = ops.logStat ? initialTreeLoadingStat() : void 0;
|
|
155
|
+
let ok = false;
|
|
156
|
+
try {
|
|
157
|
+
await tree.refresh(stat, { timeout: ops.initialTreeLoadingTimeout });
|
|
158
|
+
ok = true;
|
|
159
|
+
} finally {
|
|
160
|
+
if (stat && logger) logger.info(`Tree stat (initial load, ${ok ? "success" : "failure"}): ${JSON.stringify(stat)}`);
|
|
161
|
+
}
|
|
162
|
+
return tree;
|
|
163
|
+
}
|
|
164
|
+
};
|
|
212
165
|
|
|
166
|
+
//#endregion
|
|
213
167
|
export { SynchronizedTreeState };
|
|
214
|
-
//# sourceMappingURL=synchronized_tree.js.map
|
|
168
|
+
//# sourceMappingURL=synchronized_tree.js.map
|
|
@@ -1 +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 { isTimeoutOrCancelError } from \"@milaboratories/pl-client\";\nimport type { ExtendedResourceData } from \"./state\";\nimport { PlTreeState, TreeStateUpdateError } from \"./state\";\nimport type { PruningFunction, TreeLoadingStat } from \"./sync\";\nimport { constructTreeLoadingRequest, initialTreeLoadingStat, loadTreeState } 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(\n \"ReadingTree\",\n async (tx) => {\n return await loadTreeState(tx, request, stats);\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 if (this.scheduledOnNextState.length === 0) {\n try {\n this.currentLoopDelayInterrupt = new AbortController();\n await tp.setTimeout(\n this.pollingInterval,\n AbortSignal.any([this.abortController.signal, this.currentLoopDelayInterrupt.signal]),\n );\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":";;;;;;;MA2Ca,qBAAqB,CAAA;AAUb,IAAA,EAAA;AACA,IAAA,IAAA;AAEA,IAAA,MAAA;AAZF,IAAA,cAAc;AACvB,IAAA,KAAK;AACI,IAAA,eAAe;AACf,IAAA,O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EAAE,GAAG,SAAS;QAE/D,IAAI,EAAE,GAAG,KAAK;AAEd,QAAA,IAAI;AACF,YAAA,MAAM,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE;gBACvB,OAAO,EAAE,GAAG,CAAC,yBAAyB;AACvC,aAAA,CAAC;YACF,EAAE,GAAG,IAAI;QACX;gBAAU;;YAER,IAAI,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;;;;"}
|
|
1
|
+
{"version":3,"file":"synchronized_tree.js","names":[],"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 { isTimeoutOrCancelError } from \"@milaboratories/pl-client\";\nimport type { ExtendedResourceData } from \"./state\";\nimport { PlTreeState, TreeStateUpdateError } from \"./state\";\nimport type { PruningFunction, TreeLoadingStat } from \"./sync\";\nimport { constructTreeLoadingRequest, initialTreeLoadingStat, loadTreeState } 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(\n \"ReadingTree\",\n async (tx) => {\n return await loadTreeState(tx, request, stats);\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 if (this.scheduledOnNextState.length === 0) {\n try {\n this.currentLoopDelayInterrupt = new AbortController();\n await tp.setTimeout(this.pollingInterval, 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 the main abort controller fired, this is a permanent termination\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 // 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"],"mappings":";;;;;;;;AA2CA,IAAa,wBAAb,MAAa,sBAAsB;CACjC,AAAiB;CACjB,AAAQ;CACR,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CACjB,AAAiB,kBAAkB,IAAI,iBAAiB;CAExD,AAAQ,YACN,AAAiB,IACjB,AAAiB,MACjB,KACA,AAAiB,QACjB;EAJiB;EACA;EAEA;EAEjB,MAAM,EAAE,wBAAwB,SAAS,iBAAiB,kBAAkB,YAAY;AACxF,OAAK,UAAU;AACf,OAAK,kBAAkB;AACvB,OAAK,iBAAiB,0BAA0B,GAAG;AACnD,OAAK,UAAU;AACf,OAAK,QAAQ,IAAI,YAAY,MAAM,KAAK,eAAe;AACvD,OAAK,QAAQ,IAAI,6BACT,KAAK,eAAe,QACpB,KAAK,cAAc,EACzB,EAAE,cAAc,kBAAkB,GACjC,SAAS,WAAW,KAAK,oBAAoB,SAAS,OAAO,CAC/D;;;CAIH,AAAO,SAAS,MAAkB,KAAK,MAAmB;AACxD,MAAI,KAAK,WAAY,OAAM,IAAI,MAAM,qCAAqC;AAC1E,SAAO,KAAK,MAAM,IAAI;;CAGxB,AAAO,MAAM,MAAkB,KAAK,MAAmB;AACrD,MAAI,KAAK,WAAY,OAAM,IAAI,MAAM,qCAAqC;AAC1E,SAAO,IAAI,Y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K,cAAc;AACnB,OAAK,aAAa;AAClB,OAAK,gBAAgB,OAAO;AAE5B,MAAI,KAAK,gBAAgB,OAAW;AACpC,QAAM,KAAK;AAEX,OAAK,MAAM,eAAe,0CAA0C;;;CAItE,MAAa,2BAA0C;AACrD,MAAI,KAAK,gBAAgB,OAAW;AACpC,QAAM,KAAK;;CAGb,aAAoB,KAClB,IACA,MACA,KACA,QACA;EACA,MAAM,OAAO,IAAI,sBAAsB,IAAI,MAAM,KAAK,OAAO;EAE7D,MAAM,OAAO,IAAI,UAAU,wBAAwB,GAAG;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"}
|
package/dist/traversal_ops.d.ts
CHANGED
|
@@ -1,47 +1,47 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
import { FieldType, ResourceType } from "@milaboratories/pl-client";
|
|
2
|
+
|
|
3
|
+
//#region src/traversal_ops.d.ts
|
|
4
|
+
type CommonTraversalOps = {
|
|
5
|
+
/**
|
|
6
|
+
* Don't terminate chain if current resource or field has an error associated
|
|
7
|
+
* with, by default resource or field error will be thrown. If field has error
|
|
8
|
+
* and no value, error will be thrown anyway, because this is the reason
|
|
9
|
+
* traversal is terminated.
|
|
10
|
+
* */
|
|
11
|
+
ignoreError?: boolean;
|
|
10
12
|
};
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
pureFieldErrorToUndefined?: boolean;
|
|
13
|
+
type CommonFieldTraverseOps = {
|
|
14
|
+
/**
|
|
15
|
+
* Valid only if {@link assertFieldType} is defined and equal to 'Input',
|
|
16
|
+
* 'Service' or 'Output'. By default, if field is not found, and corresponding
|
|
17
|
+
* field list is locked, call will fail with exception.
|
|
18
|
+
* */
|
|
19
|
+
allowPermanentAbsence?: boolean; /** Will not mark current context as unstable, if field is not found. */
|
|
20
|
+
stableIfNotFound?: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* If encounter field with error and no value, will silently terminate the
|
|
23
|
+
* traversal and return undefined.
|
|
24
|
+
* */
|
|
25
|
+
pureFieldErrorToUndefined?: boolean;
|
|
25
26
|
};
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
27
|
+
type ResourceTraversalOps = CommonTraversalOps & {
|
|
28
|
+
/**
|
|
29
|
+
* Assert resource type of the resource the fields points to. Call will fail
|
|
30
|
+
* with exception if this assertion is not fulfilled.
|
|
31
|
+
* */
|
|
32
|
+
assertResourceType?: ResourceType | ResourceType[];
|
|
32
33
|
};
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
* fulfilled
|
|
43
|
-
* */
|
|
44
|
-
assertFieldType?: FieldType;
|
|
34
|
+
type GetFieldStep = CommonFieldTraverseOps & {
|
|
35
|
+
/** Field name */field: string; /** Field must exist, if this option is set, instead error will be thrown */
|
|
36
|
+
errorIfFieldNotFound?: boolean; /** Field must be resolved into resource if this option is set, instead error will be thrown */
|
|
37
|
+
errorIfFieldNotSet?: boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Assert field type. Call will fail with exception if this assertion is not
|
|
40
|
+
* fulfilled
|
|
41
|
+
* */
|
|
42
|
+
assertFieldType?: FieldType;
|
|
45
43
|
};
|
|
46
|
-
|
|
44
|
+
type FieldTraversalStep = GetFieldStep & ResourceTraversalOps;
|
|
45
|
+
//#endregion
|
|
46
|
+
export { CommonFieldTraverseOps, CommonTraversalOps, FieldTraversalStep, GetFieldStep, ResourceTraversalOps };
|
|
47
47
|
//# sourceMappingURL=traversal_ops.d.ts.map
|
package/dist/value_and_error.cjs
CHANGED
|
@@ -1,20 +1,17 @@
|
|
|
1
|
-
'use strict';
|
|
2
1
|
|
|
2
|
+
//#region src/value_and_error.ts
|
|
3
3
|
function mapValueAndErrorIfDefined(input, mapping) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
else
|
|
7
|
-
return mapValueAndError(input, mapping);
|
|
4
|
+
if (input === void 0) return void 0;
|
|
5
|
+
else return mapValueAndError(input, mapping);
|
|
8
6
|
}
|
|
9
7
|
function mapValueAndError(input, mapping) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
ret.error = mapping(input.error);
|
|
15
|
-
return ret;
|
|
8
|
+
const ret = {};
|
|
9
|
+
if (input.value !== void 0) ret.value = mapping(input.value);
|
|
10
|
+
if (input.error !== void 0) ret.error = mapping(input.error);
|
|
11
|
+
return ret;
|
|
16
12
|
}
|
|
17
13
|
|
|
14
|
+
//#endregion
|
|
18
15
|
exports.mapValueAndError = mapValueAndError;
|
|
19
16
|
exports.mapValueAndErrorIfDefined = mapValueAndErrorIfDefined;
|
|
20
|
-
//# sourceMappingURL=value_and_error.cjs.map
|
|
17
|
+
//# sourceMappingURL=value_and_error.cjs.map
|
|
@@ -1 +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"],"
|
|
1
|
+
{"version":3,"file":"value_and_error.cjs","names":[],"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"],"mappings":";;AAKA,SAAgB,0BACd,OACA,SAC+B;AAC/B,KAAI,UAAU,OAAW,QAAO;KAC3B,QAAO,iBAAiB,OAAO,QAAQ;;AAG9C,SAAgB,iBAAyB,OAA0B,SAAwB;CACzF,MAAM,MAAM,EAAE;AACd,KAAI,MAAM,UAAU,OAAW,KAAI,QAAQ,QAAQ,MAAM,MAAM;AAC/D,KAAI,MAAM,UAAU,OAAW,KAAI,QAAQ,QAAQ,MAAM,MAAM;AAC/D,QAAO"}
|
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
//#region src/value_and_error.d.ts
|
|
2
|
+
interface ValueAndError<T> {
|
|
3
|
+
value?: T;
|
|
4
|
+
error?: T;
|
|
4
5
|
}
|
|
5
|
-
|
|
6
|
-
|
|
6
|
+
declare function mapValueAndErrorIfDefined<T1, T2>(input: ValueAndError<T1> | undefined, mapping: (v: T1) => T2): ValueAndError<T2> | undefined;
|
|
7
|
+
declare function mapValueAndError<T1, T2>(input: ValueAndError<T1>, mapping: (v: T1) => T2): ValueAndError<T2>;
|
|
8
|
+
//#endregion
|
|
9
|
+
export { ValueAndError, mapValueAndError, mapValueAndErrorIfDefined };
|
|
7
10
|
//# sourceMappingURL=value_and_error.d.ts.map
|