@pumped-fn/core-next 0.5.78 → 0.5.80
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/CHANGELOG.md +52 -0
- package/dist/{chunk-CTAAG5j7.js → chunk-Bp6m_JJh.js} +1 -1
- package/dist/index.cjs +406 -365
- package/dist/index.d.cts +96 -135
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts +98 -135
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +405 -360
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import { __export } from "./chunk-
|
|
1
|
+
import { t as __export } from "./chunk-Bp6m_JJh.js";
|
|
2
2
|
|
|
3
|
+
//#region src/tag-types.ts
|
|
4
|
+
const tagSymbol = Symbol.for("@pumped-fn/core/tag");
|
|
5
|
+
|
|
6
|
+
//#endregion
|
|
3
7
|
//#region src/types.ts
|
|
4
8
|
const executorSymbol = Symbol.for("@pumped-fn/core/executor");
|
|
5
|
-
const metaSymbol = Symbol.for("@pumped-fn/core/meta");
|
|
6
9
|
var SchemaError = class extends Error {
|
|
7
10
|
issues;
|
|
8
11
|
constructor(issues) {
|
|
@@ -86,71 +89,165 @@ function custom() {
|
|
|
86
89
|
}
|
|
87
90
|
|
|
88
91
|
//#endregion
|
|
89
|
-
//#region src/
|
|
90
|
-
|
|
92
|
+
//#region src/tag.ts
|
|
93
|
+
const tagCacheMap = /* @__PURE__ */ new WeakMap();
|
|
94
|
+
function buildTagCache(tags) {
|
|
95
|
+
const map = /* @__PURE__ */ new Map();
|
|
96
|
+
for (const tagged of tags) {
|
|
97
|
+
const existing = map.get(tagged.key);
|
|
98
|
+
if (existing) existing.push(tagged.value);
|
|
99
|
+
else map.set(tagged.key, [tagged.value]);
|
|
100
|
+
}
|
|
101
|
+
return map;
|
|
102
|
+
}
|
|
103
|
+
function isStore(source) {
|
|
104
|
+
return typeof source === "object" && source !== null && "get" in source && "set" in source && typeof source.get === "function" && typeof source.set === "function";
|
|
105
|
+
}
|
|
106
|
+
function extract(source, key, schema) {
|
|
107
|
+
if (source === null || source === void 0) return;
|
|
108
|
+
if (isStore(source)) {
|
|
109
|
+
const value = source.get(key);
|
|
110
|
+
return value === void 0 ? void 0 : validate(schema, value);
|
|
111
|
+
}
|
|
112
|
+
let cache = tagCacheMap.get(source);
|
|
113
|
+
if (!cache) {
|
|
114
|
+
cache = buildTagCache(Array.isArray(source) ? source : source.tags ?? []);
|
|
115
|
+
tagCacheMap.set(source, cache);
|
|
116
|
+
}
|
|
117
|
+
const values = cache.get(key);
|
|
118
|
+
return values && values.length > 0 ? validate(schema, values[0]) : void 0;
|
|
119
|
+
}
|
|
120
|
+
function collect(source, key, schema) {
|
|
121
|
+
if (isStore(source)) {
|
|
122
|
+
const value = source.get(key);
|
|
123
|
+
return value === void 0 ? [] : [validate(schema, value)];
|
|
124
|
+
}
|
|
125
|
+
let cache = tagCacheMap.get(source);
|
|
126
|
+
if (!cache) {
|
|
127
|
+
cache = buildTagCache(Array.isArray(source) ? source : source.tags ?? []);
|
|
128
|
+
tagCacheMap.set(source, cache);
|
|
129
|
+
}
|
|
130
|
+
const values = cache.get(key);
|
|
131
|
+
return values ? values.map((v) => validate(schema, v)) : [];
|
|
132
|
+
}
|
|
133
|
+
function write(store, key, schema, value) {
|
|
134
|
+
const validated = validate(schema, value);
|
|
135
|
+
store.set(key, validated);
|
|
136
|
+
}
|
|
137
|
+
function createTagged(key, schema, value, label) {
|
|
138
|
+
const tagged = {
|
|
139
|
+
[tagSymbol]: true,
|
|
140
|
+
key,
|
|
141
|
+
schema,
|
|
142
|
+
value,
|
|
143
|
+
toString() {
|
|
144
|
+
return `${label || key.toString()}=${JSON.stringify(value)}`;
|
|
145
|
+
},
|
|
146
|
+
get [Symbol.toStringTag]() {
|
|
147
|
+
return "Tagged";
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
Object.defineProperty(tagged, Symbol.for("nodejs.util.inspect.custom"), { value: function(depth, opts) {
|
|
151
|
+
return `Tagged { ${label || "anonymous"}: ${opts.stylize ? opts.stylize(JSON.stringify(value), "string") : JSON.stringify(value)} }`;
|
|
152
|
+
} });
|
|
153
|
+
return tagged;
|
|
154
|
+
}
|
|
155
|
+
var TagImpl = class {
|
|
91
156
|
key;
|
|
92
157
|
schema;
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
158
|
+
label;
|
|
159
|
+
default;
|
|
160
|
+
constructor(schema, options) {
|
|
161
|
+
this.label = options?.label;
|
|
162
|
+
this.key = options?.label ? Symbol.for(options.label) : Symbol();
|
|
96
163
|
this.schema = schema;
|
|
164
|
+
this.default = options?.default ?? void 0;
|
|
97
165
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
166
|
+
get(source) {
|
|
167
|
+
const value = extract(source, this.key, this.schema);
|
|
168
|
+
if (value === void 0) {
|
|
169
|
+
if (this.default !== void 0) return this.default;
|
|
170
|
+
throw new Error(`Value not found for key: ${this.key.toString()}`);
|
|
171
|
+
}
|
|
172
|
+
return value;
|
|
105
173
|
}
|
|
106
|
-
|
|
107
|
-
|
|
174
|
+
find(source) {
|
|
175
|
+
const value = extract(source, this.key, this.schema);
|
|
176
|
+
return value !== void 0 ? value : this.default;
|
|
108
177
|
}
|
|
109
178
|
some(source) {
|
|
110
|
-
return
|
|
179
|
+
return collect(source, this.key, this.schema);
|
|
111
180
|
}
|
|
112
|
-
|
|
113
|
-
|
|
181
|
+
set(target, value) {
|
|
182
|
+
if (isStore(target)) {
|
|
183
|
+
write(target, this.key, this.schema, value);
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
const validated = validate(this.schema, value);
|
|
187
|
+
return createTagged(this.key, this.schema, validated, this.label);
|
|
114
188
|
}
|
|
115
|
-
|
|
116
|
-
const
|
|
117
|
-
if (
|
|
118
|
-
|
|
189
|
+
entry(value) {
|
|
190
|
+
const val = value !== void 0 ? value : this.default;
|
|
191
|
+
if (val === void 0) throw new Error("Value required for entry without default");
|
|
192
|
+
const validated = validate(this.schema, val);
|
|
193
|
+
return [this.key, validated];
|
|
194
|
+
}
|
|
195
|
+
toString() {
|
|
196
|
+
return this.label ? `Tag(${this.label})` : `Tag(${this.key.toString()})`;
|
|
197
|
+
}
|
|
198
|
+
get [Symbol.toStringTag]() {
|
|
199
|
+
return this.label ? `Tag<${this.label}>` : "Tag<anonymous>";
|
|
200
|
+
}
|
|
201
|
+
[Symbol.for("nodejs.util.inspect.custom")]() {
|
|
202
|
+
return this.label ? `Tag { ${this.label} }` : "Tag { anonymous }";
|
|
119
203
|
}
|
|
120
204
|
};
|
|
121
|
-
|
|
122
|
-
const
|
|
123
|
-
const fn = ((value) =>
|
|
205
|
+
function tag(schema, options) {
|
|
206
|
+
const impl = new TagImpl(schema, options);
|
|
207
|
+
const fn = ((value) => {
|
|
208
|
+
const val = value !== void 0 ? value : impl.default;
|
|
209
|
+
if (val === void 0) throw new Error("Value required for tag without default");
|
|
210
|
+
const validated = validate(schema, val);
|
|
211
|
+
return createTagged(impl.key, impl.schema, validated, impl.label);
|
|
212
|
+
});
|
|
124
213
|
Object.defineProperty(fn, "key", {
|
|
125
|
-
value:
|
|
214
|
+
value: impl.key,
|
|
126
215
|
writable: false,
|
|
127
216
|
configurable: false
|
|
128
217
|
});
|
|
129
|
-
Object.defineProperty(fn,
|
|
130
|
-
value:
|
|
218
|
+
Object.defineProperty(fn, "schema", {
|
|
219
|
+
value: impl.schema,
|
|
131
220
|
writable: false,
|
|
132
221
|
configurable: false
|
|
133
222
|
});
|
|
134
|
-
fn
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
223
|
+
Object.defineProperty(fn, "label", {
|
|
224
|
+
value: impl.label,
|
|
225
|
+
writable: false,
|
|
226
|
+
configurable: false
|
|
227
|
+
});
|
|
228
|
+
Object.defineProperty(fn, "default", {
|
|
229
|
+
value: impl.default,
|
|
230
|
+
writable: false,
|
|
231
|
+
configurable: false
|
|
232
|
+
});
|
|
233
|
+
fn.get = impl.get.bind(impl);
|
|
234
|
+
fn.find = impl.find.bind(impl);
|
|
235
|
+
fn.some = impl.some.bind(impl);
|
|
236
|
+
fn.set = impl.set.bind(impl);
|
|
237
|
+
fn.entry = impl.entry.bind(impl);
|
|
238
|
+
fn.toString = impl.toString.bind(impl);
|
|
239
|
+
fn.partial = (d) => {
|
|
240
|
+
return Object.assign({}, createTagged(impl.key, impl.schema, {}, impl.label), d);
|
|
241
|
+
};
|
|
242
|
+
Object.defineProperty(fn, Symbol.toStringTag, { get: () => impl[Symbol.toStringTag] });
|
|
243
|
+
const inspectSymbol = Symbol.for("nodejs.util.inspect.custom");
|
|
244
|
+
Object.defineProperty(fn, inspectSymbol, { value: impl[inspectSymbol].bind(impl) });
|
|
138
245
|
return fn;
|
|
139
|
-
};
|
|
140
|
-
function getValue(meta$1) {
|
|
141
|
-
return validate(meta$1.schema, meta$1.value);
|
|
142
|
-
}
|
|
143
|
-
function findValues(executor, meta$1) {
|
|
144
|
-
if (!executor) return [];
|
|
145
|
-
return (Array.isArray(executor) ? executor : executor.metas ?? []).filter((m) => m.key === meta$1.key).map((m) => getValue(m));
|
|
146
|
-
}
|
|
147
|
-
function findValue(executor, meta$1) {
|
|
148
|
-
return findValues(executor, meta$1).at(0);
|
|
149
246
|
}
|
|
150
247
|
|
|
151
248
|
//#endregion
|
|
152
249
|
//#region src/executor.ts
|
|
153
|
-
function createExecutor(factory, dependencies,
|
|
250
|
+
function createExecutor(factory, dependencies, tags) {
|
|
154
251
|
const executor = {
|
|
155
252
|
[executorSymbol]: "main",
|
|
156
253
|
factory: (_, controller) => {
|
|
@@ -158,27 +255,27 @@ function createExecutor(factory, dependencies, metas) {
|
|
|
158
255
|
return factory(_, controller);
|
|
159
256
|
},
|
|
160
257
|
dependencies,
|
|
161
|
-
|
|
258
|
+
tags
|
|
162
259
|
};
|
|
163
260
|
const lazyExecutor = {
|
|
164
261
|
[executorSymbol]: "lazy",
|
|
165
262
|
dependencies: void 0,
|
|
166
263
|
executor,
|
|
167
264
|
factory: void 0,
|
|
168
|
-
|
|
265
|
+
tags
|
|
169
266
|
};
|
|
170
267
|
const reactiveExecutor = {
|
|
171
268
|
[executorSymbol]: "reactive",
|
|
172
269
|
executor,
|
|
173
270
|
factory: void 0,
|
|
174
271
|
dependencies: void 0,
|
|
175
|
-
|
|
272
|
+
tags
|
|
176
273
|
};
|
|
177
274
|
const staticExecutor = {
|
|
178
275
|
[executorSymbol]: "static",
|
|
179
276
|
dependencies: void 0,
|
|
180
277
|
factory: void 0,
|
|
181
|
-
|
|
278
|
+
tags,
|
|
182
279
|
executor
|
|
183
280
|
};
|
|
184
281
|
Object.defineProperties(executor, {
|
|
@@ -221,12 +318,12 @@ function isExecutor(input) {
|
|
|
221
318
|
function isPreset(input) {
|
|
222
319
|
return typeof input === "object" && input !== null && executorSymbol in input && input[executorSymbol] === "preset";
|
|
223
320
|
}
|
|
224
|
-
function provide(factory, ...
|
|
225
|
-
return createExecutor(factory, void 0,
|
|
321
|
+
function provide(factory, ...tags) {
|
|
322
|
+
return createExecutor(factory, void 0, tags);
|
|
226
323
|
}
|
|
227
|
-
function derive(pdependencies, pfactory, ...
|
|
324
|
+
function derive(pdependencies, pfactory, ...tags) {
|
|
228
325
|
const factory = (deps, ctl) => pfactory(deps, ctl);
|
|
229
|
-
return createExecutor(factory, pdependencies,
|
|
326
|
+
return createExecutor(factory, pdependencies, tags);
|
|
230
327
|
}
|
|
231
328
|
function preset(e, v) {
|
|
232
329
|
const executor = isExecutor(e) ? e : e.escape();
|
|
@@ -536,89 +633,12 @@ function buildDependencyChain(executorStack) {
|
|
|
536
633
|
return executorStack.map(getExecutorName);
|
|
537
634
|
}
|
|
538
635
|
|
|
539
|
-
//#endregion
|
|
540
|
-
//#region src/accessor.ts
|
|
541
|
-
function isDataStore(source) {
|
|
542
|
-
return "get" in source && "set" in source && typeof source.get === "function" && typeof source.set === "function";
|
|
543
|
-
}
|
|
544
|
-
function isMetaArray(source) {
|
|
545
|
-
return Array.isArray(source);
|
|
546
|
-
}
|
|
547
|
-
function extractFromSource(source, key, schema) {
|
|
548
|
-
if (isDataStore(source)) {
|
|
549
|
-
const value = source.get(key);
|
|
550
|
-
return value === void 0 ? void 0 : validate(schema, value);
|
|
551
|
-
}
|
|
552
|
-
if (isMetaArray(source)) {
|
|
553
|
-
const meta$2 = source.find((m) => m.key === key);
|
|
554
|
-
return meta$2 ? validate(schema, meta$2.value) : void 0;
|
|
555
|
-
}
|
|
556
|
-
const meta$1 = (source.metas ?? []).find((m) => m.key === key);
|
|
557
|
-
return meta$1 ? validate(schema, meta$1.value) : void 0;
|
|
558
|
-
}
|
|
559
|
-
function validateAndSet(source, key, schema, value) {
|
|
560
|
-
if (!isDataStore(source)) throw new Error("set() can only be used with DataStore");
|
|
561
|
-
const validated = validate(schema, value);
|
|
562
|
-
source.set(key, validated);
|
|
563
|
-
}
|
|
564
|
-
function validateAndPreset(key, schema, value) {
|
|
565
|
-
return [key, validate(schema, value)];
|
|
566
|
-
}
|
|
567
|
-
var AccessorImpl$1 = class {
|
|
568
|
-
key;
|
|
569
|
-
schema;
|
|
570
|
-
constructor(key, schema) {
|
|
571
|
-
this.key = typeof key === "string" ? Symbol(key) : key;
|
|
572
|
-
this.schema = schema;
|
|
573
|
-
}
|
|
574
|
-
get(source) {
|
|
575
|
-
const value = extractFromSource(source, this.key, this.schema);
|
|
576
|
-
if (value === void 0) throw new Error(`Value not found for key: ${this.key.toString()}`);
|
|
577
|
-
return value;
|
|
578
|
-
}
|
|
579
|
-
find(source) {
|
|
580
|
-
return extractFromSource(source, this.key, this.schema);
|
|
581
|
-
}
|
|
582
|
-
set(source, value) {
|
|
583
|
-
validateAndSet(source, this.key, this.schema, value);
|
|
584
|
-
}
|
|
585
|
-
preset(value) {
|
|
586
|
-
return validateAndPreset(this.key, this.schema, value);
|
|
587
|
-
}
|
|
588
|
-
};
|
|
589
|
-
var AccessorWithDefaultImpl = class {
|
|
590
|
-
key;
|
|
591
|
-
schema;
|
|
592
|
-
defaultValue;
|
|
593
|
-
constructor(key, schema, defaultValue) {
|
|
594
|
-
this.key = typeof key === "string" ? Symbol(key) : key;
|
|
595
|
-
this.schema = schema;
|
|
596
|
-
this.defaultValue = validate(schema, defaultValue);
|
|
597
|
-
}
|
|
598
|
-
get(source) {
|
|
599
|
-
return extractFromSource(source, this.key, this.schema) ?? this.defaultValue;
|
|
600
|
-
}
|
|
601
|
-
find(source) {
|
|
602
|
-
return extractFromSource(source, this.key, this.schema) ?? this.defaultValue;
|
|
603
|
-
}
|
|
604
|
-
set(source, value) {
|
|
605
|
-
validateAndSet(source, this.key, this.schema, value);
|
|
606
|
-
}
|
|
607
|
-
preset(value) {
|
|
608
|
-
return validateAndPreset(this.key, this.schema, value);
|
|
609
|
-
}
|
|
610
|
-
};
|
|
611
|
-
function accessor(key, schema, defaultValue) {
|
|
612
|
-
if (defaultValue !== void 0) return new AccessorWithDefaultImpl(key, schema, defaultValue);
|
|
613
|
-
return new AccessorImpl$1(key, schema);
|
|
614
|
-
}
|
|
615
|
-
|
|
616
636
|
//#endregion
|
|
617
637
|
//#region src/flow.ts
|
|
618
638
|
function isErrorEntry(entry) {
|
|
619
639
|
return typeof entry === "object" && entry !== null && "__error" in entry;
|
|
620
640
|
}
|
|
621
|
-
function wrapWithExtensions(extensions, baseExecutor,
|
|
641
|
+
function wrapWithExtensions(extensions, baseExecutor, scope, operation) {
|
|
622
642
|
if (!extensions || extensions.length === 0) return baseExecutor;
|
|
623
643
|
let executor = baseExecutor;
|
|
624
644
|
for (let i = extensions.length - 1; i >= 0; i--) {
|
|
@@ -626,28 +646,34 @@ function wrapWithExtensions(extensions, baseExecutor, dataStore, operation) {
|
|
|
626
646
|
if (extension$1.wrap) {
|
|
627
647
|
const current = executor;
|
|
628
648
|
executor = () => {
|
|
629
|
-
const result = extension$1.wrap(
|
|
649
|
+
const result = extension$1.wrap(scope, current, operation);
|
|
630
650
|
return result instanceof Promised ? result : Promised.create(result);
|
|
631
651
|
};
|
|
632
652
|
}
|
|
633
653
|
}
|
|
634
654
|
return executor;
|
|
635
655
|
}
|
|
636
|
-
const flowDefinitionMeta =
|
|
656
|
+
const flowDefinitionMeta = tag(custom(), { label: "flow.definition" });
|
|
637
657
|
const flowMeta = {
|
|
638
|
-
depth:
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
658
|
+
depth: tag(custom(), {
|
|
659
|
+
label: "flow.depth",
|
|
660
|
+
default: 0
|
|
661
|
+
}),
|
|
662
|
+
flowName: tag(custom(), { label: "flow.name" }),
|
|
663
|
+
parentFlowName: tag(custom(), { label: "flow.parentName" }),
|
|
664
|
+
isParallel: tag(custom(), {
|
|
665
|
+
label: "flow.isParallel",
|
|
666
|
+
default: false
|
|
667
|
+
}),
|
|
668
|
+
journal: tag(custom(), { label: "flow.journal" })
|
|
643
669
|
};
|
|
644
670
|
var FlowDefinition = class {
|
|
645
|
-
constructor(name$1, version, input, output,
|
|
671
|
+
constructor(name$1, version, input, output, tags = []) {
|
|
646
672
|
this.name = name$1;
|
|
647
673
|
this.version = version;
|
|
648
674
|
this.input = input;
|
|
649
675
|
this.output = output;
|
|
650
|
-
this.
|
|
676
|
+
this.tags = tags;
|
|
651
677
|
}
|
|
652
678
|
handler(dependenciesOrHandler, handlerFn) {
|
|
653
679
|
if (typeof dependenciesOrHandler === "function") {
|
|
@@ -657,7 +683,7 @@ var FlowDefinition = class {
|
|
|
657
683
|
return noDepsHandler(ctx, input);
|
|
658
684
|
};
|
|
659
685
|
return flowHandler;
|
|
660
|
-
}, void 0, [...this.
|
|
686
|
+
}, void 0, [...this.tags, flowDefinitionMeta(this)]);
|
|
661
687
|
executor$1.definition = this;
|
|
662
688
|
return executor$1;
|
|
663
689
|
}
|
|
@@ -668,26 +694,26 @@ var FlowDefinition = class {
|
|
|
668
694
|
return dependentHandler(deps, ctx, input);
|
|
669
695
|
};
|
|
670
696
|
return flowHandler;
|
|
671
|
-
}, dependencies, [...this.
|
|
697
|
+
}, dependencies, [...this.tags, flowDefinitionMeta(this)]);
|
|
672
698
|
executor.definition = this;
|
|
673
699
|
return executor;
|
|
674
700
|
}
|
|
675
701
|
};
|
|
676
702
|
function define(config) {
|
|
677
|
-
return new FlowDefinition(config.name, config.version || "1.0.0", config.input, config.output, config.
|
|
703
|
+
return new FlowDefinition(config.name || "anonymous", config.version || "1.0.0", config.input, config.output, config.tags || []);
|
|
678
704
|
}
|
|
679
705
|
var FlowContext = class FlowContext {
|
|
680
706
|
contextData = /* @__PURE__ */ new Map();
|
|
681
707
|
journal = null;
|
|
682
708
|
scope;
|
|
683
709
|
reversedExtensions;
|
|
684
|
-
|
|
685
|
-
constructor(scope, extensions,
|
|
710
|
+
tags;
|
|
711
|
+
constructor(scope, extensions, tags, parent) {
|
|
686
712
|
this.extensions = extensions;
|
|
687
713
|
this.parent = parent;
|
|
688
714
|
this.scope = scope;
|
|
689
715
|
this.reversedExtensions = [...extensions].reverse();
|
|
690
|
-
this.
|
|
716
|
+
this.tags = tags;
|
|
691
717
|
}
|
|
692
718
|
resolve(executor) {
|
|
693
719
|
return this.scope.resolve(executor);
|
|
@@ -700,7 +726,7 @@ var FlowContext = class FlowContext {
|
|
|
700
726
|
for (const extension$1 of this.reversedExtensions) if (extension$1.wrap) {
|
|
701
727
|
const current = executor;
|
|
702
728
|
executor = () => {
|
|
703
|
-
const result = extension$1.wrap(this, current, operation);
|
|
729
|
+
const result = extension$1.wrap(this.scope, current, operation);
|
|
704
730
|
return result instanceof Promised ? result : Promised.create(result);
|
|
705
731
|
};
|
|
706
732
|
}
|
|
@@ -718,13 +744,17 @@ var FlowContext = class FlowContext {
|
|
|
718
744
|
if (typeof accessorOrKey === "object" && accessorOrKey !== null && "get" in accessorOrKey) return accessorOrKey.get(this);
|
|
719
745
|
const key = accessorOrKey;
|
|
720
746
|
if (this.contextData.has(key)) return this.contextData.get(key);
|
|
747
|
+
if (this.tags && typeof key === "symbol") {
|
|
748
|
+
const tagged = this.tags.find((m) => m.key === key);
|
|
749
|
+
if (tagged) return tagged.value;
|
|
750
|
+
}
|
|
721
751
|
if (this.parent) return this.parent.get(key);
|
|
722
752
|
}
|
|
723
|
-
find(accessor
|
|
724
|
-
return accessor
|
|
753
|
+
find(accessor) {
|
|
754
|
+
return accessor.find(this);
|
|
725
755
|
}
|
|
726
756
|
set(accessorOrKey, value) {
|
|
727
|
-
if (typeof accessorOrKey === "object"
|
|
757
|
+
if (accessorOrKey !== null && accessorOrKey !== void 0 && (typeof accessorOrKey === "object" || typeof accessorOrKey === "function") && "set" in accessorOrKey) {
|
|
728
758
|
accessorOrKey.set(this, value);
|
|
729
759
|
return;
|
|
730
760
|
}
|
|
@@ -924,29 +954,30 @@ var FlowContext = class FlowContext {
|
|
|
924
954
|
get: (key) => contextDataSnapshot.get(key),
|
|
925
955
|
set: (_key, _value) => {
|
|
926
956
|
throw new Error("Cannot set values on execution snapshot");
|
|
927
|
-
}
|
|
957
|
+
},
|
|
958
|
+
tags: this.tags
|
|
928
959
|
};
|
|
929
960
|
return { context: {
|
|
930
|
-
get(accessor
|
|
931
|
-
return accessor
|
|
961
|
+
get(accessor) {
|
|
962
|
+
return accessor.get(dataStore);
|
|
932
963
|
},
|
|
933
|
-
find(accessor
|
|
934
|
-
return accessor
|
|
964
|
+
find(accessor) {
|
|
965
|
+
return accessor.find(dataStore);
|
|
935
966
|
}
|
|
936
967
|
} };
|
|
937
968
|
}
|
|
938
969
|
};
|
|
939
970
|
function execute(flow$1, input, options) {
|
|
940
|
-
const scope = options?.scope || createScope({
|
|
971
|
+
const scope = options?.scope || createScope({ tags: options?.scopeTags });
|
|
941
972
|
const shouldDisposeScope = !options?.scope;
|
|
942
973
|
let resolveSnapshot;
|
|
943
974
|
const snapshotPromise = new Promise((resolve) => {
|
|
944
975
|
resolveSnapshot = resolve;
|
|
945
976
|
});
|
|
946
977
|
const promise = (async () => {
|
|
947
|
-
const context = new FlowContext(scope, options?.extensions || [], options?.
|
|
978
|
+
const context = new FlowContext(scope, options?.extensions || [], options?.tags);
|
|
948
979
|
try {
|
|
949
|
-
if (options?.initialContext) for (const [accessor
|
|
980
|
+
if (options?.initialContext) for (const [accessor, value] of options.initialContext) accessor.set(context, value);
|
|
950
981
|
const executeCore = () => {
|
|
951
982
|
return scope.resolve(flow$1).map(async (handler) => {
|
|
952
983
|
const definition$1 = flowDefinitionMeta.find(flow$1);
|
|
@@ -960,7 +991,7 @@ function execute(flow$1, input, options) {
|
|
|
960
991
|
};
|
|
961
992
|
const definition = flowDefinitionMeta.find(flow$1);
|
|
962
993
|
if (!definition) throw new Error("Flow definition not found in executor metadata");
|
|
963
|
-
const result = await wrapWithExtensions(options?.extensions, executeCore, context, {
|
|
994
|
+
const result = await wrapWithExtensions(options?.extensions, executeCore, context.scope, {
|
|
964
995
|
kind: "execute",
|
|
965
996
|
flow: flow$1,
|
|
966
997
|
definition,
|
|
@@ -1001,129 +1032,100 @@ function execute(flow$1, input, options) {
|
|
|
1001
1032
|
}
|
|
1002
1033
|
return Promised.create(promise, snapshotPromise);
|
|
1003
1034
|
}
|
|
1004
|
-
function flowImpl(
|
|
1005
|
-
if (typeof
|
|
1006
|
-
const handler =
|
|
1035
|
+
function flowImpl(first, second, third) {
|
|
1036
|
+
if (typeof first === "function") {
|
|
1037
|
+
const handler = first;
|
|
1007
1038
|
return define({
|
|
1008
|
-
name: "anonymous",
|
|
1009
|
-
version: "1.0.0",
|
|
1010
1039
|
input: custom(),
|
|
1011
1040
|
output: custom()
|
|
1012
1041
|
}).handler(handler);
|
|
1013
1042
|
}
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
const dependencies =
|
|
1017
|
-
const
|
|
1018
|
-
const hasInput$1 = "input" in definition$1 && definition$1.input !== void 0;
|
|
1019
|
-
const hasOutput$1 = "output" in definition$1 && definition$1.output !== void 0;
|
|
1020
|
-
return define({
|
|
1021
|
-
name: definition$1.name,
|
|
1022
|
-
version: definition$1.version,
|
|
1023
|
-
input: hasInput$1 ? definition$1.input : custom(),
|
|
1024
|
-
output: hasOutput$1 ? definition$1.output : custom(),
|
|
1025
|
-
meta: definition$1.meta
|
|
1026
|
-
}).handler(dependencies, handlerFn);
|
|
1027
|
-
}
|
|
1028
|
-
if (typeof dependenciesOrHandler === "function" && !("name" in firstArg)) {
|
|
1029
|
-
const dependencies = firstArg;
|
|
1030
|
-
const handler = dependenciesOrHandler;
|
|
1043
|
+
if (isExecutor(first)) {
|
|
1044
|
+
if (typeof second !== "function") throw new Error("flow(deps, handler) requires handler as second argument");
|
|
1045
|
+
const dependencies = first;
|
|
1046
|
+
const handler = second;
|
|
1031
1047
|
return define({
|
|
1032
|
-
name: "anonymous",
|
|
1033
|
-
version: "1.0.0",
|
|
1034
1048
|
input: custom(),
|
|
1035
1049
|
output: custom()
|
|
1036
1050
|
}).handler(dependencies, handler);
|
|
1037
1051
|
}
|
|
1038
|
-
if ("
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
return
|
|
1052
|
+
if (typeof first === "object" && first !== null) {
|
|
1053
|
+
if ("input" in first && "output" in first) {
|
|
1054
|
+
const config = first;
|
|
1055
|
+
if ("handler" in config || "dependencies" in config) throw new Error("Config object cannot contain 'handler' or 'dependencies' properties. Use flow(config, handler) or flow(config, deps, handler) instead.");
|
|
1056
|
+
const def = define(config);
|
|
1057
|
+
if (!second) return def;
|
|
1058
|
+
if (typeof second === "function") return def.handler(second);
|
|
1059
|
+
if (isExecutor(second)) {
|
|
1060
|
+
if (!third) throw new Error("flow(config, deps, handler) requires handler as third argument");
|
|
1061
|
+
return def.handler(second, third);
|
|
1062
|
+
}
|
|
1063
|
+
throw new Error("Invalid flow() call: second argument must be handler function or dependencies");
|
|
1064
|
+
}
|
|
1065
|
+
const isValidDependencyObject = (obj) => {
|
|
1066
|
+
const values = Object.values(obj);
|
|
1067
|
+
if (values.length === 0) return true;
|
|
1068
|
+
return values.every((value) => typeof value === "function" || isExecutor(value));
|
|
1069
|
+
};
|
|
1070
|
+
if (!isValidDependencyObject(first)) throw new Error("Invalid flow() call: first argument must be either a config object with 'input' and 'output' properties, or a valid dependency object containing executors/functions");
|
|
1071
|
+
if (typeof second === "function") {
|
|
1072
|
+
const dependencies = first;
|
|
1073
|
+
const handler = second;
|
|
1074
|
+
return define({
|
|
1075
|
+
input: custom(),
|
|
1076
|
+
output: custom()
|
|
1077
|
+
}).handler(dependencies, handler);
|
|
1055
1078
|
}
|
|
1079
|
+
throw new Error("Invalid flow() call: object dependencies require handler function as second argument");
|
|
1056
1080
|
}
|
|
1057
|
-
|
|
1058
|
-
const hasInput = "input" in definition && definition.input !== void 0;
|
|
1059
|
-
const hasOutput = "output" in definition && definition.output !== void 0;
|
|
1060
|
-
const def = define({
|
|
1061
|
-
name: definition.name || "anonymous",
|
|
1062
|
-
version: definition.version,
|
|
1063
|
-
input: hasInput ? definition.input : custom(),
|
|
1064
|
-
output: hasOutput ? definition.output : custom(),
|
|
1065
|
-
meta: definition.meta
|
|
1066
|
-
});
|
|
1067
|
-
if (!dependenciesOrHandler) return def;
|
|
1068
|
-
if (typeof dependenciesOrHandler === "function") return def.handler(dependenciesOrHandler);
|
|
1069
|
-
else return def.handler(dependenciesOrHandler, handlerFn);
|
|
1081
|
+
throw new Error("Invalid flow() call: first argument must be handler, dependencies, or config object");
|
|
1070
1082
|
}
|
|
1071
|
-
const flow = Object.assign(flowImpl, {
|
|
1072
|
-
define,
|
|
1073
|
-
execute
|
|
1074
|
-
});
|
|
1083
|
+
const flow = Object.assign(flowImpl, { execute });
|
|
1075
1084
|
|
|
1076
1085
|
//#endregion
|
|
1077
1086
|
//#region src/scope.ts
|
|
1078
1087
|
var AccessorImpl = class {
|
|
1079
|
-
|
|
1088
|
+
tags;
|
|
1080
1089
|
scope;
|
|
1081
1090
|
requestor;
|
|
1082
1091
|
currentPromise = null;
|
|
1083
1092
|
currentPromised = null;
|
|
1084
|
-
cachedResolvedPromised = null;
|
|
1085
1093
|
resolve;
|
|
1086
|
-
constructor(scope, requestor,
|
|
1094
|
+
constructor(scope, requestor, tags) {
|
|
1087
1095
|
this.scope = scope;
|
|
1088
1096
|
this.requestor = requestor;
|
|
1089
|
-
this.
|
|
1097
|
+
this.tags = tags;
|
|
1090
1098
|
this.resolve = this.createResolveFunction();
|
|
1091
|
-
const
|
|
1092
|
-
if (!
|
|
1093
|
-
accessor: this,
|
|
1094
|
-
value: existing?.value
|
|
1095
|
-
});
|
|
1099
|
+
const state = this.scope["getOrCreateState"](requestor);
|
|
1100
|
+
if (!state.accessor) state.accessor = this;
|
|
1096
1101
|
}
|
|
1097
1102
|
async resolveCore() {
|
|
1098
1103
|
const { factory, dependencies, immediateValue } = this.processReplacer();
|
|
1099
1104
|
if (immediateValue !== void 0) {
|
|
1100
1105
|
await new Promise((resolve) => queueMicrotask(resolve));
|
|
1101
|
-
this.scope["
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
});
|
|
1106
|
+
const state$1 = this.scope["getOrCreateState"](this.requestor);
|
|
1107
|
+
state$1.accessor = this;
|
|
1108
|
+
state$1.value = {
|
|
1109
|
+
kind: "resolved",
|
|
1110
|
+
value: immediateValue,
|
|
1111
|
+
promised: Promised.create(Promise.resolve(immediateValue))
|
|
1112
|
+
};
|
|
1109
1113
|
return immediateValue;
|
|
1110
1114
|
}
|
|
1111
1115
|
const controller = this.createController();
|
|
1112
1116
|
const resolvedDependencies = await this.scope["~resolveDependencies"](dependencies, this.requestor);
|
|
1113
1117
|
const result = await this.executeFactory(factory, resolvedDependencies, controller);
|
|
1114
1118
|
const processedResult = await this.processChangeEvents(result);
|
|
1115
|
-
this.scope["
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
});
|
|
1119
|
+
const state = this.scope["getOrCreateState"](this.requestor);
|
|
1120
|
+
state.accessor = this;
|
|
1121
|
+
state.value = {
|
|
1122
|
+
kind: "resolved",
|
|
1123
|
+
value: processedResult,
|
|
1124
|
+
promised: Promised.create(Promise.resolve(processedResult))
|
|
1125
|
+
};
|
|
1123
1126
|
this.scope["~removeFromResolutionChain"](this.requestor);
|
|
1124
1127
|
this.currentPromise = null;
|
|
1125
1128
|
this.currentPromised = null;
|
|
1126
|
-
this.cachedResolvedPromised = null;
|
|
1127
1129
|
return processedResult;
|
|
1128
1130
|
}
|
|
1129
1131
|
async resolveWithErrorHandling() {
|
|
@@ -1131,15 +1133,14 @@ var AccessorImpl = class {
|
|
|
1131
1133
|
return await this.resolveCore();
|
|
1132
1134
|
} catch (error) {
|
|
1133
1135
|
const { enhancedError, errorContext, originalError } = this.enhanceResolutionError(error);
|
|
1134
|
-
this.scope["
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
});
|
|
1136
|
+
const state = this.scope["getOrCreateState"](this.requestor);
|
|
1137
|
+
state.accessor = this;
|
|
1138
|
+
state.value = {
|
|
1139
|
+
kind: "rejected",
|
|
1140
|
+
error: originalError,
|
|
1141
|
+
context: errorContext,
|
|
1142
|
+
enhancedError
|
|
1143
|
+
};
|
|
1143
1144
|
this.scope["~removeFromResolutionChain"](this.requestor);
|
|
1144
1145
|
this.scope["~triggerError"](enhancedError, this.requestor);
|
|
1145
1146
|
this.currentPromise = null;
|
|
@@ -1158,23 +1159,18 @@ var AccessorImpl = class {
|
|
|
1158
1159
|
}
|
|
1159
1160
|
this.scope["~addToResolutionChain"](this.requestor, this.requestor);
|
|
1160
1161
|
this.currentPromise = this.resolveWithErrorHandling();
|
|
1161
|
-
this.scope["
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
});
|
|
1162
|
+
const state = this.scope["getOrCreateState"](this.requestor);
|
|
1163
|
+
state.accessor = this;
|
|
1164
|
+
state.value = {
|
|
1165
|
+
kind: "pending",
|
|
1166
|
+
promise: this.currentPromise
|
|
1167
|
+
};
|
|
1168
1168
|
this.currentPromised = Promised.create(this.currentPromise);
|
|
1169
1169
|
return this.currentPromised;
|
|
1170
1170
|
};
|
|
1171
1171
|
}
|
|
1172
1172
|
handleCachedState(cached) {
|
|
1173
|
-
if (cached.kind === "resolved")
|
|
1174
|
-
if (cached.promised) return cached.promised;
|
|
1175
|
-
if (!this.cachedResolvedPromised) this.cachedResolvedPromised = Promised.create(Promise.resolve(cached.value));
|
|
1176
|
-
return this.cachedResolvedPromised;
|
|
1177
|
-
}
|
|
1173
|
+
if (cached.kind === "resolved") return cached.promised;
|
|
1178
1174
|
if (cached.kind === "rejected") throw cached.error;
|
|
1179
1175
|
if (!this.currentPromised) this.currentPromised = Promised.create(cached.promise);
|
|
1180
1176
|
return this.currentPromised;
|
|
@@ -1278,9 +1274,8 @@ var AccessorImpl = class {
|
|
|
1278
1274
|
createController() {
|
|
1279
1275
|
return {
|
|
1280
1276
|
cleanup: (cleanup) => {
|
|
1281
|
-
const
|
|
1282
|
-
this.scope["
|
|
1283
|
-
currentSet.add(cleanup);
|
|
1277
|
+
const state = this.scope["getOrCreateState"](this.requestor);
|
|
1278
|
+
this.scope["ensureCleanups"](state).add(cleanup);
|
|
1284
1279
|
},
|
|
1285
1280
|
release: () => this.scope.release(this.requestor),
|
|
1286
1281
|
reload: () => this.scope.resolve(this.requestor, true).map(() => void 0),
|
|
@@ -1292,25 +1287,21 @@ function getExecutor(e) {
|
|
|
1292
1287
|
if (isLazyExecutor(e) || isReactiveExecutor(e) || isStaticExecutor(e)) return e.executor;
|
|
1293
1288
|
return e;
|
|
1294
1289
|
}
|
|
1295
|
-
var BaseScope = class
|
|
1290
|
+
var BaseScope = class {
|
|
1296
1291
|
disposed = false;
|
|
1297
1292
|
cache = /* @__PURE__ */ new Map();
|
|
1298
|
-
cleanups = /* @__PURE__ */ new Map();
|
|
1299
|
-
onUpdateCallbacks = /* @__PURE__ */ new Map();
|
|
1300
|
-
onUpdateExecutors = /* @__PURE__ */ new Map();
|
|
1301
1293
|
onEvents = {
|
|
1302
1294
|
change: /* @__PURE__ */ new Set(),
|
|
1303
1295
|
release: /* @__PURE__ */ new Set(),
|
|
1304
1296
|
error: /* @__PURE__ */ new Set()
|
|
1305
1297
|
};
|
|
1306
|
-
onErrors = /* @__PURE__ */ new Map();
|
|
1307
1298
|
isDisposing = false;
|
|
1308
|
-
|
|
1299
|
+
CIRCULAR_CHECK_THRESHOLD = 15;
|
|
1309
1300
|
extensions = [];
|
|
1310
1301
|
reversedExtensions = [];
|
|
1311
1302
|
registry = [];
|
|
1312
1303
|
initialValues = [];
|
|
1313
|
-
|
|
1304
|
+
tags;
|
|
1314
1305
|
static emptyDataStore = {
|
|
1315
1306
|
get: () => void 0,
|
|
1316
1307
|
set: () => void 0
|
|
@@ -1318,11 +1309,39 @@ var BaseScope = class BaseScope {
|
|
|
1318
1309
|
constructor(options) {
|
|
1319
1310
|
if (options?.registry) this.registry = [...options.registry];
|
|
1320
1311
|
if (options?.initialValues) this.initialValues = options.initialValues;
|
|
1321
|
-
if (options?.
|
|
1312
|
+
if (options?.tags) this.tags = options.tags;
|
|
1322
1313
|
if (options?.extensions) for (const extension$1 of options.extensions) this.useExtension(extension$1);
|
|
1323
1314
|
}
|
|
1315
|
+
getOrCreateState(executor) {
|
|
1316
|
+
let state = this.cache.get(executor);
|
|
1317
|
+
if (!state) {
|
|
1318
|
+
state = { accessor: null };
|
|
1319
|
+
this.cache.set(executor, state);
|
|
1320
|
+
}
|
|
1321
|
+
return state;
|
|
1322
|
+
}
|
|
1323
|
+
ensureCleanups(state) {
|
|
1324
|
+
if (!state.cleanups) state.cleanups = /* @__PURE__ */ new Set();
|
|
1325
|
+
return state.cleanups;
|
|
1326
|
+
}
|
|
1327
|
+
ensureCallbacks(state) {
|
|
1328
|
+
if (!state.onUpdateCallbacks) state.onUpdateCallbacks = /* @__PURE__ */ new Set();
|
|
1329
|
+
return state.onUpdateCallbacks;
|
|
1330
|
+
}
|
|
1331
|
+
ensureExecutors(state) {
|
|
1332
|
+
if (!state.onUpdateExecutors) state.onUpdateExecutors = /* @__PURE__ */ new Set();
|
|
1333
|
+
return state.onUpdateExecutors;
|
|
1334
|
+
}
|
|
1335
|
+
ensureErrors(state) {
|
|
1336
|
+
if (!state.onErrors) state.onErrors = /* @__PURE__ */ new Set();
|
|
1337
|
+
return state.onErrors;
|
|
1338
|
+
}
|
|
1339
|
+
ensureResolutionChain(state) {
|
|
1340
|
+
if (!state.resolutionChain) state.resolutionChain = /* @__PURE__ */ new Set();
|
|
1341
|
+
return state.resolutionChain;
|
|
1342
|
+
}
|
|
1324
1343
|
"~checkCircularDependency"(executor, resolvingExecutor) {
|
|
1325
|
-
const currentChain = this.
|
|
1344
|
+
const currentChain = this.cache.get(resolvingExecutor)?.resolutionChain;
|
|
1326
1345
|
if (currentChain && currentChain.has(executor)) {
|
|
1327
1346
|
const chainArray = Array.from(currentChain);
|
|
1328
1347
|
const dependencyChain = buildDependencyChain(chainArray);
|
|
@@ -1333,40 +1352,46 @@ var BaseScope = class BaseScope {
|
|
|
1333
1352
|
}
|
|
1334
1353
|
}
|
|
1335
1354
|
"~addToResolutionChain"(executor, resolvingExecutor) {
|
|
1336
|
-
const
|
|
1337
|
-
|
|
1338
|
-
this.resolutionChain.set(resolvingExecutor, currentChain);
|
|
1355
|
+
const state = this.getOrCreateState(resolvingExecutor);
|
|
1356
|
+
this.ensureResolutionChain(state).add(executor);
|
|
1339
1357
|
}
|
|
1340
1358
|
"~removeFromResolutionChain"(executor) {
|
|
1341
|
-
this.
|
|
1359
|
+
const state = this.cache.get(executor);
|
|
1360
|
+
if (state) {
|
|
1361
|
+
delete state.resolutionDepth;
|
|
1362
|
+
delete state.resolutionChain;
|
|
1363
|
+
}
|
|
1342
1364
|
}
|
|
1343
1365
|
"~propagateResolutionChain"(fromExecutor, toExecutor) {
|
|
1344
|
-
const
|
|
1345
|
-
if (
|
|
1346
|
-
const
|
|
1366
|
+
const fromState = this.cache.get(fromExecutor);
|
|
1367
|
+
if (fromState?.resolutionChain) {
|
|
1368
|
+
const toState = this.getOrCreateState(toExecutor);
|
|
1369
|
+
const newChain = new Set(fromState.resolutionChain);
|
|
1347
1370
|
newChain.add(fromExecutor);
|
|
1348
|
-
|
|
1371
|
+
toState.resolutionChain = newChain;
|
|
1349
1372
|
}
|
|
1350
1373
|
}
|
|
1351
1374
|
async "~triggerCleanup"(e) {
|
|
1352
|
-
const
|
|
1353
|
-
if (
|
|
1375
|
+
const state = this.cache.get(e);
|
|
1376
|
+
if (state?.cleanups) {
|
|
1377
|
+
for (const c of Array.from(state.cleanups.values()).reverse()) await c();
|
|
1378
|
+
delete state.cleanups;
|
|
1379
|
+
}
|
|
1354
1380
|
}
|
|
1355
1381
|
async "~triggerUpdate"(e) {
|
|
1356
|
-
const
|
|
1357
|
-
if (!
|
|
1358
|
-
const
|
|
1359
|
-
|
|
1360
|
-
if (
|
|
1361
|
-
await
|
|
1362
|
-
if (
|
|
1382
|
+
const state = this.cache.get(e);
|
|
1383
|
+
if (!state) throw new Error("Executor is not yet resolved");
|
|
1384
|
+
if (state.onUpdateExecutors) for (const t of Array.from(state.onUpdateExecutors.values())) {
|
|
1385
|
+
const depState = this.cache.get(t);
|
|
1386
|
+
if (depState?.cleanups) this["~triggerCleanup"](t);
|
|
1387
|
+
await depState.accessor.resolve(true);
|
|
1388
|
+
if (depState.onUpdateExecutors || depState.onUpdateCallbacks) await this["~triggerUpdate"](t);
|
|
1363
1389
|
}
|
|
1364
|
-
const
|
|
1365
|
-
if (callbacks) for (const cb of Array.from(callbacks.values())) await cb(ce.accessor);
|
|
1390
|
+
if (state.onUpdateCallbacks) for (const cb of Array.from(state.onUpdateCallbacks.values())) await cb(state.accessor);
|
|
1366
1391
|
}
|
|
1367
1392
|
async "~triggerError"(error, executor) {
|
|
1368
|
-
const
|
|
1369
|
-
if (
|
|
1393
|
+
const state = this.cache.get(executor);
|
|
1394
|
+
if (state?.onErrors) for (const callback of Array.from(state.onErrors.values())) try {
|
|
1370
1395
|
await callback(error, executor, this);
|
|
1371
1396
|
} catch (callbackError) {
|
|
1372
1397
|
console.error("Error in error callback:", callbackError);
|
|
@@ -1384,14 +1409,25 @@ var BaseScope = class BaseScope {
|
|
|
1384
1409
|
}
|
|
1385
1410
|
async "~resolveExecutor"(ie, ref) {
|
|
1386
1411
|
const e = getExecutor(ie);
|
|
1387
|
-
|
|
1388
|
-
|
|
1412
|
+
if (e === ref) {
|
|
1413
|
+
const executorName = getExecutorName(e);
|
|
1414
|
+
throw createDependencyError(codes.CIRCULAR_DEPENDENCY, executorName, [executorName], executorName, void 0, {
|
|
1415
|
+
circularPath: `${executorName} -> ${executorName}`,
|
|
1416
|
+
detectedAt: executorName
|
|
1417
|
+
});
|
|
1418
|
+
}
|
|
1419
|
+
const currentDepth = (this.cache.get(ref)?.resolutionDepth ?? 0) + 1;
|
|
1420
|
+
const state = this.getOrCreateState(e);
|
|
1421
|
+
state.resolutionDepth = currentDepth;
|
|
1422
|
+
if (currentDepth > this.CIRCULAR_CHECK_THRESHOLD) {
|
|
1423
|
+
this["~checkCircularDependency"](e, ref);
|
|
1424
|
+
this["~propagateResolutionChain"](ref, e);
|
|
1425
|
+
}
|
|
1389
1426
|
const a = this["~makeAccessor"](e);
|
|
1390
1427
|
if (isLazyExecutor(ie)) return a;
|
|
1391
1428
|
if (isReactiveExecutor(ie)) {
|
|
1392
|
-
const
|
|
1393
|
-
this.
|
|
1394
|
-
c.add(ref);
|
|
1429
|
+
const parentState = this.getOrCreateState(ie.executor);
|
|
1430
|
+
this.ensureExecutors(parentState).add(ref);
|
|
1395
1431
|
}
|
|
1396
1432
|
await a.resolve(false);
|
|
1397
1433
|
if (isStaticExecutor(ie)) return a;
|
|
@@ -1413,12 +1449,12 @@ var BaseScope = class BaseScope {
|
|
|
1413
1449
|
"~ensureNotDisposed"() {
|
|
1414
1450
|
if (this.disposed) throw new Error("Scope is disposed");
|
|
1415
1451
|
}
|
|
1416
|
-
wrapWithExtensions(baseExecutor,
|
|
1452
|
+
wrapWithExtensions(baseExecutor, operation) {
|
|
1417
1453
|
let executor = baseExecutor;
|
|
1418
1454
|
for (const extension$1 of this.reversedExtensions) if (extension$1.wrap) {
|
|
1419
1455
|
const current = executor;
|
|
1420
1456
|
executor = () => {
|
|
1421
|
-
const result = extension$1.wrap(
|
|
1457
|
+
const result = extension$1.wrap(this, current, operation);
|
|
1422
1458
|
return result instanceof Promised ? result : Promised.create(result);
|
|
1423
1459
|
};
|
|
1424
1460
|
}
|
|
@@ -1428,7 +1464,7 @@ var BaseScope = class BaseScope {
|
|
|
1428
1464
|
let requestor = isLazyExecutor(e) || isReactiveExecutor(e) || isStaticExecutor(e) ? e.executor : e;
|
|
1429
1465
|
const cachedAccessor = this.cache.get(requestor);
|
|
1430
1466
|
if (cachedAccessor && cachedAccessor.accessor) return cachedAccessor.accessor;
|
|
1431
|
-
return new AccessorImpl(this, requestor, e.
|
|
1467
|
+
return new AccessorImpl(this, requestor, e.tags);
|
|
1432
1468
|
}
|
|
1433
1469
|
accessor(executor) {
|
|
1434
1470
|
this["~ensureNotDisposed"]();
|
|
@@ -1446,10 +1482,10 @@ var BaseScope = class BaseScope {
|
|
|
1446
1482
|
resolve(executor, force = false) {
|
|
1447
1483
|
this["~ensureNotDisposed"]();
|
|
1448
1484
|
const coreResolve = () => {
|
|
1449
|
-
const accessor
|
|
1450
|
-
return accessor
|
|
1485
|
+
const accessor = this["~makeAccessor"](executor);
|
|
1486
|
+
return accessor.resolve(force).map(() => accessor.get());
|
|
1451
1487
|
};
|
|
1452
|
-
return this.wrapWithExtensions(coreResolve,
|
|
1488
|
+
return this.wrapWithExtensions(coreResolve, {
|
|
1453
1489
|
kind: "resolve",
|
|
1454
1490
|
executor,
|
|
1455
1491
|
scope: this,
|
|
@@ -1458,8 +1494,23 @@ var BaseScope = class BaseScope {
|
|
|
1458
1494
|
}
|
|
1459
1495
|
resolveAccessor(executor, force = false) {
|
|
1460
1496
|
this["~ensureNotDisposed"]();
|
|
1461
|
-
const accessor
|
|
1462
|
-
return accessor
|
|
1497
|
+
const accessor = this["~makeAccessor"](executor);
|
|
1498
|
+
return accessor.resolve(force).map(() => accessor);
|
|
1499
|
+
}
|
|
1500
|
+
run(dependencies, callback, args) {
|
|
1501
|
+
this["~ensureNotDisposed"]();
|
|
1502
|
+
const dummyExecutor = {
|
|
1503
|
+
[executorSymbol]: "main",
|
|
1504
|
+
factory: void 0,
|
|
1505
|
+
dependencies,
|
|
1506
|
+
tags: {}
|
|
1507
|
+
};
|
|
1508
|
+
return Promised.create((async () => {
|
|
1509
|
+
const deps = dependencies;
|
|
1510
|
+
const resolvedDeps = await this["~resolveDependencies"](deps, dummyExecutor);
|
|
1511
|
+
if (args !== void 0 && args.length > 0) return await callback(resolvedDeps, ...args);
|
|
1512
|
+
return await callback(resolvedDeps, ...[]);
|
|
1513
|
+
})());
|
|
1463
1514
|
}
|
|
1464
1515
|
update(e, u) {
|
|
1465
1516
|
if (this.isDisposing) return Promised.create(Promise.resolve());
|
|
@@ -1467,30 +1518,29 @@ var BaseScope = class BaseScope {
|
|
|
1467
1518
|
const coreUpdate = () => {
|
|
1468
1519
|
return Promised.create((async () => {
|
|
1469
1520
|
this["~triggerCleanup"](e);
|
|
1470
|
-
const accessor
|
|
1521
|
+
const accessor = this["~makeAccessor"](e);
|
|
1471
1522
|
let value;
|
|
1472
|
-
if (typeof u === "function") value = u(accessor
|
|
1523
|
+
if (typeof u === "function") value = u(accessor.get());
|
|
1473
1524
|
else value = u;
|
|
1474
1525
|
const events = this.onEvents.change;
|
|
1475
1526
|
for (const event of events) {
|
|
1476
1527
|
const updated = await event("update", e, value, this);
|
|
1477
1528
|
if (updated !== void 0 && e === updated.executor) value = updated.value;
|
|
1478
1529
|
}
|
|
1479
|
-
this.
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
});
|
|
1530
|
+
const state = this.getOrCreateState(e);
|
|
1531
|
+
state.accessor = accessor;
|
|
1532
|
+
state.value = {
|
|
1533
|
+
kind: "resolved",
|
|
1534
|
+
value,
|
|
1535
|
+
promised: Promised.create(Promise.resolve(value))
|
|
1536
|
+
};
|
|
1487
1537
|
await this["~triggerUpdate"](e);
|
|
1488
1538
|
})());
|
|
1489
1539
|
};
|
|
1490
1540
|
const baseUpdater = () => {
|
|
1491
1541
|
return coreUpdate().map(() => this.accessor(e).get());
|
|
1492
1542
|
};
|
|
1493
|
-
return this.wrapWithExtensions(baseUpdater,
|
|
1543
|
+
return this.wrapWithExtensions(baseUpdater, {
|
|
1494
1544
|
kind: "resolve",
|
|
1495
1545
|
operation: "update",
|
|
1496
1546
|
executor: e,
|
|
@@ -1503,16 +1553,12 @@ var BaseScope = class BaseScope {
|
|
|
1503
1553
|
release(e, s = false) {
|
|
1504
1554
|
this["~ensureNotDisposed"]();
|
|
1505
1555
|
const coreRelease = async () => {
|
|
1506
|
-
|
|
1556
|
+
const state = this.cache.get(e);
|
|
1557
|
+
if (!state && !s) throw new Error("Executor is not yet resolved");
|
|
1507
1558
|
await this["~triggerCleanup"](e);
|
|
1508
1559
|
const events = this.onEvents.release;
|
|
1509
1560
|
for (const event of events) await event("release", e, this);
|
|
1510
|
-
const
|
|
1511
|
-
if (executors) {
|
|
1512
|
-
for (const t of Array.from(executors.values())) await this.release(t, true);
|
|
1513
|
-
this.onUpdateExecutors.delete(e);
|
|
1514
|
-
}
|
|
1515
|
-
this.onUpdateCallbacks.delete(e);
|
|
1561
|
+
if (state?.onUpdateExecutors) for (const t of Array.from(state.onUpdateExecutors.values())) await this.release(t, true);
|
|
1516
1562
|
this.cache.delete(e);
|
|
1517
1563
|
};
|
|
1518
1564
|
return Promised.create(coreRelease());
|
|
@@ -1527,28 +1573,22 @@ var BaseScope = class BaseScope {
|
|
|
1527
1573
|
for (const current of currents) await this.release(current, true);
|
|
1528
1574
|
this.disposed = true;
|
|
1529
1575
|
this.cache.clear();
|
|
1530
|
-
this.cleanups.clear();
|
|
1531
|
-
this.onUpdateCallbacks.clear();
|
|
1532
|
-
this.onUpdateExecutors.clear();
|
|
1533
1576
|
this.onEvents.change.clear();
|
|
1534
1577
|
this.onEvents.release.clear();
|
|
1535
1578
|
this.onEvents.error.clear();
|
|
1536
|
-
this.onErrors.clear();
|
|
1537
|
-
this.resolutionChain.clear();
|
|
1538
1579
|
})());
|
|
1539
1580
|
}
|
|
1540
1581
|
onUpdate(e, cb) {
|
|
1541
1582
|
this["~ensureNotDisposed"]();
|
|
1542
1583
|
if (this.isDisposing) throw new Error("Cannot register update callback on a disposing scope");
|
|
1543
|
-
const
|
|
1544
|
-
this.
|
|
1545
|
-
ou.add(cb);
|
|
1584
|
+
const state = this.getOrCreateState(e);
|
|
1585
|
+
this.ensureCallbacks(state).add(cb);
|
|
1546
1586
|
return () => {
|
|
1547
1587
|
this["~ensureNotDisposed"]();
|
|
1548
|
-
const
|
|
1549
|
-
if (
|
|
1550
|
-
|
|
1551
|
-
if (
|
|
1588
|
+
const state$1 = this.cache.get(e);
|
|
1589
|
+
if (state$1?.onUpdateCallbacks) {
|
|
1590
|
+
state$1.onUpdateCallbacks.delete(cb);
|
|
1591
|
+
if (state$1.onUpdateCallbacks.size === 0) delete state$1.onUpdateCallbacks;
|
|
1552
1592
|
}
|
|
1553
1593
|
};
|
|
1554
1594
|
}
|
|
@@ -1582,15 +1622,14 @@ var BaseScope = class BaseScope {
|
|
|
1582
1622
|
}
|
|
1583
1623
|
if (callback) {
|
|
1584
1624
|
const executor = executorOrCallback;
|
|
1585
|
-
const
|
|
1586
|
-
this.
|
|
1587
|
-
errorCallbacks.add(callback);
|
|
1625
|
+
const state = this.getOrCreateState(executor);
|
|
1626
|
+
this.ensureErrors(state).add(callback);
|
|
1588
1627
|
return () => {
|
|
1589
1628
|
this["~ensureNotDisposed"]();
|
|
1590
|
-
const
|
|
1591
|
-
if (
|
|
1592
|
-
|
|
1593
|
-
if (
|
|
1629
|
+
const state$1 = this.cache.get(executor);
|
|
1630
|
+
if (state$1?.onErrors) {
|
|
1631
|
+
state$1.onErrors.delete(callback);
|
|
1632
|
+
if (state$1.onErrors.size === 0) delete state$1.onErrors;
|
|
1594
1633
|
}
|
|
1595
1634
|
};
|
|
1596
1635
|
}
|
|
@@ -1620,14 +1659,14 @@ var BaseScope = class BaseScope {
|
|
|
1620
1659
|
scope: this,
|
|
1621
1660
|
extensions: options.extensions,
|
|
1622
1661
|
initialContext: options.initialContext,
|
|
1623
|
-
|
|
1662
|
+
tags: options.tags,
|
|
1624
1663
|
details: true
|
|
1625
1664
|
});
|
|
1626
1665
|
return flow.execute(flow$1, input, {
|
|
1627
1666
|
scope: this,
|
|
1628
1667
|
extensions: options?.extensions,
|
|
1629
1668
|
initialContext: options?.initialContext,
|
|
1630
|
-
|
|
1669
|
+
tags: options?.tags,
|
|
1631
1670
|
details: false
|
|
1632
1671
|
});
|
|
1633
1672
|
}
|
|
@@ -1714,40 +1753,46 @@ var MultiExecutorImpl = class {
|
|
|
1714
1753
|
function createValidatedExecutor(option, key, createExecutorFn) {
|
|
1715
1754
|
return createExecutorFn(validate(option.keySchema, key));
|
|
1716
1755
|
}
|
|
1717
|
-
function createMultiExecutor(option, poolId, keyPool, createNewExecutor,
|
|
1756
|
+
function createMultiExecutor(option, poolId, keyPool, createNewExecutor, providerTags) {
|
|
1718
1757
|
const impl = new MultiExecutorImpl(option, poolId, keyPool, createNewExecutor);
|
|
1719
|
-
const provider = createExecutor((ctl) => impl.providerFactory(ctl), void 0,
|
|
1758
|
+
const provider = createExecutor((ctl) => impl.providerFactory(ctl), void 0, providerTags);
|
|
1720
1759
|
const callableFn = (key) => impl.__call(key);
|
|
1721
1760
|
return Object.assign(callableFn, provider, {
|
|
1722
1761
|
release: (scope) => impl.release(scope),
|
|
1723
1762
|
id: impl.id
|
|
1724
1763
|
});
|
|
1725
1764
|
}
|
|
1726
|
-
function provide$1(option, valueFn, ...
|
|
1727
|
-
const poolId =
|
|
1765
|
+
function provide$1(option, valueFn, ...tags) {
|
|
1766
|
+
const poolId = tag(custom(), {
|
|
1767
|
+
label: Symbol().toString(),
|
|
1768
|
+
default: null
|
|
1769
|
+
});
|
|
1728
1770
|
const keyPool = /* @__PURE__ */ new Map();
|
|
1729
1771
|
const createNewExecutor = (key) => {
|
|
1730
|
-
return createValidatedExecutor(option, key, (validatedKey) => createExecutor((ctl) => valueFn(validatedKey, ctl), void 0, [poolId(
|
|
1772
|
+
return createValidatedExecutor(option, key, (validatedKey) => createExecutor((ctl) => valueFn(validatedKey, ctl), void 0, [poolId(null), ...tags]));
|
|
1731
1773
|
};
|
|
1732
|
-
return createMultiExecutor(option, poolId, keyPool, createNewExecutor, [poolId(
|
|
1774
|
+
return createMultiExecutor(option, poolId, keyPool, createNewExecutor, [poolId(null), ...tags]);
|
|
1733
1775
|
}
|
|
1734
|
-
function derive$1(option, valueFn, ...
|
|
1735
|
-
const poolId =
|
|
1776
|
+
function derive$1(option, valueFn, ...tags) {
|
|
1777
|
+
const poolId = tag(custom(), {
|
|
1778
|
+
label: Symbol().toString(),
|
|
1779
|
+
default: null
|
|
1780
|
+
});
|
|
1736
1781
|
const keyPool = /* @__PURE__ */ new Map();
|
|
1737
1782
|
const createNewExecutor = (key) => {
|
|
1738
1783
|
return createValidatedExecutor(option, key, (validatedKey) => {
|
|
1739
1784
|
const factory = (dependencies, ctl) => valueFn(dependencies, validatedKey, ctl);
|
|
1740
1785
|
const deps = option.dependencies;
|
|
1741
|
-
return createExecutor(factory, deps,
|
|
1786
|
+
return createExecutor(factory, deps, tags);
|
|
1742
1787
|
});
|
|
1743
1788
|
};
|
|
1744
|
-
return createMultiExecutor(option, poolId, keyPool, createNewExecutor,
|
|
1789
|
+
return createMultiExecutor(option, poolId, keyPool, createNewExecutor, tags);
|
|
1745
1790
|
}
|
|
1746
1791
|
|
|
1747
1792
|
//#endregion
|
|
1748
1793
|
//#region src/index.ts
|
|
1749
|
-
const name =
|
|
1794
|
+
const name = tag(custom(), { label: "pumped-fn/name" });
|
|
1750
1795
|
|
|
1751
1796
|
//#endregion
|
|
1752
|
-
export { DependencyResolutionError, ExecutorResolutionError, FactoryExecutionError, FlowError, FlowValidationError, Promised, SchemaError,
|
|
1797
|
+
export { DependencyResolutionError, ExecutorResolutionError, FactoryExecutionError, FlowError, FlowValidationError, Promised, SchemaError, createScope, custom, derive, errors_exports as errors, executorSymbol, extension, flow, flowMeta, isExecutor, isLazyExecutor, isMainExecutor, isPreset, isReactiveExecutor, isStaticExecutor, multi_exports as multi, name, preset, provide, resolves, ssch_exports as standardSchema, tag, tagSymbol };
|
|
1753
1798
|
//# sourceMappingURL=index.js.map
|