@pumped-fn/core-next 0.5.56 → 0.5.58

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/index.cjs CHANGED
@@ -56,22 +56,12 @@ var DependencyResolutionError = class extends ExecutorResolutionError {
56
56
 
57
57
  //#endregion
58
58
  //#region src/ssch.ts
59
- function validate(schema, data$1) {
60
- const result = schema["~standard"].validate(data$1);
59
+ function validate(schema, data) {
60
+ const result = schema["~standard"].validate(data);
61
61
  if ("then" in result) throw new Error("validating async is not supported");
62
62
  if (result.issues) throw new SchemaError(result.issues);
63
63
  return result.value;
64
64
  }
65
- async function validateAsync(schema, data$1) {
66
- const result = schema["~standard"].validate(data$1);
67
- if ("then" in result) {
68
- const result_1 = await result;
69
- if (result_1.issues) throw new SchemaError(result_1.issues);
70
- return result_1.value;
71
- }
72
- if (result.issues) throw new SchemaError(result.issues);
73
- return Promise.resolve(result.value);
74
- }
75
65
  function custom() {
76
66
  return { "~standard": {
77
67
  vendor: "pumped-fn",
@@ -84,54 +74,52 @@ function custom() {
84
74
 
85
75
  //#endregion
86
76
  //#region src/meta.ts
77
+ var MetaFunction = class {
78
+ key;
79
+ schema;
80
+ [metaSymbol] = true;
81
+ constructor(key, schema) {
82
+ this.key = typeof key === "string" ? Symbol(key) : key;
83
+ this.schema = schema;
84
+ }
85
+ __call(value) {
86
+ return {
87
+ [metaSymbol]: true,
88
+ key: this.key,
89
+ schema: this.schema,
90
+ value
91
+ };
92
+ }
93
+ partial(d) {
94
+ return Object.assign({}, this.__call({}), d);
95
+ }
96
+ some(source) {
97
+ return findValues(source, this);
98
+ }
99
+ find(source) {
100
+ return findValue(source, this);
101
+ }
102
+ get(source) {
103
+ return getValue(findValue(source, this));
104
+ }
105
+ };
87
106
  const meta = (key, schema) => {
88
- const _key = typeof key === "string" ? Symbol(key) : key;
89
- const fn = (value) => ({
90
- [metaSymbol]: true,
91
- key: _key,
92
- schema,
93
- value
94
- });
107
+ const metaFunc = new MetaFunction(key, schema);
108
+ const fn = (value) => metaFunc.__call(value);
95
109
  Object.defineProperty(fn, "key", {
96
- value: _key,
97
- configurable: false,
98
- enumerable: false,
99
- writable: false
110
+ value: metaFunc.key,
111
+ writable: false,
112
+ configurable: false
100
113
  });
101
114
  Object.defineProperty(fn, metaSymbol, {
102
115
  value: true,
103
- configurable: false,
104
- enumerable: false,
105
- writable: false
106
- });
107
- Object.defineProperties(fn, {
108
- partial: {
109
- value: (d) => {
110
- return Object.assign({}, fn({}), d);
111
- },
112
- configurable: false,
113
- enumerable: false,
114
- writable: false
115
- },
116
- some: {
117
- value: (source) => findValues(source, fn),
118
- configurable: false,
119
- enumerable: false,
120
- writable: false
121
- },
122
- find: {
123
- value: (source) => findValue(source, fn),
124
- configurable: false,
125
- enumerable: false,
126
- writable: false
127
- },
128
- get: {
129
- value: (source) => getValue(findValue(source, fn)),
130
- configurable: false,
131
- enumerable: false,
132
- writable: false
133
- }
116
+ writable: false,
117
+ configurable: false
134
118
  });
119
+ fn.partial = metaFunc.partial.bind(metaFunc);
120
+ fn.some = metaFunc.some.bind(metaFunc);
121
+ fn.find = metaFunc.find.bind(metaFunc);
122
+ fn.get = metaFunc.get.bind(metaFunc);
135
123
  return fn;
136
124
  };
137
125
  function getValue(meta$1) {
@@ -149,214 +137,81 @@ function findValue(executor, meta$1) {
149
137
  }
150
138
 
151
139
  //#endregion
152
- //#region src/executor.ts
153
- function createExecutor(factory, dependencies, metas) {
154
- const executor = {
155
- [executorSymbol]: "main",
156
- factory: (_, controller) => {
157
- if (dependencies === void 0) {
158
- const f$1 = factory;
159
- return f$1(controller);
160
- }
161
- const f = factory;
162
- return f(_, controller);
163
- },
164
- dependencies,
165
- metas
166
- };
167
- const lazyExecutor = {
168
- [executorSymbol]: "lazy",
169
- dependencies: void 0,
170
- executor,
171
- factory: void 0,
172
- metas
173
- };
174
- const reactiveExecutor = {
175
- [executorSymbol]: "reactive",
176
- executor,
177
- factory: void 0,
178
- dependencies: void 0,
179
- metas
180
- };
181
- const staticExecutor = {
182
- [executorSymbol]: "static",
183
- dependencies: void 0,
184
- factory: void 0,
185
- metas,
186
- executor
187
- };
188
- Object.defineProperties(executor, {
189
- lazy: {
190
- value: lazyExecutor,
191
- writable: false,
192
- configurable: false,
193
- enumerable: false
194
- },
195
- reactive: {
196
- value: reactiveExecutor,
197
- writable: false,
198
- configurable: false,
199
- enumerable: false
200
- },
201
- static: {
202
- value: staticExecutor,
203
- writable: false,
204
- configurable: false,
205
- enumerable: false
206
- }
207
- });
208
- return executor;
140
+ //#region src/accessor.ts
141
+ function isDataStore(source) {
142
+ return "get" in source && "set" in source;
209
143
  }
210
- function isLazyExecutor(executor) {
211
- return executor[executorSymbol] === "lazy";
144
+ function isMetaArray(source) {
145
+ return Array.isArray(source);
212
146
  }
213
- function isReactiveExecutor(executor) {
214
- return executor[executorSymbol] === "reactive";
215
- }
216
- function isStaticExecutor(executor) {
217
- return executor[executorSymbol] === "static";
218
- }
219
- function isMainExecutor(executor) {
220
- return isExecutor(executor) && executor[executorSymbol] === "main";
221
- }
222
- function isExecutor(input) {
223
- return typeof input === "object" && input !== null && executorSymbol in input;
224
- }
225
- function isPreset(input) {
226
- return typeof input === "object" && input !== null && executorSymbol in input && input[executorSymbol] === "preset";
227
- }
228
- function provide(factory, ...metas) {
229
- return createExecutor(factory, void 0, metas);
230
- }
231
- function derive(pdependencies, pfactory, ...metas) {
232
- return createExecutor(pfactory, pdependencies, metas);
233
- }
234
- function preset(e, v) {
235
- const executor = isExecutor(e) ? e : e.escape();
236
- return {
237
- [executorSymbol]: "preset",
238
- value: v,
239
- executor
240
- };
241
- }
242
-
243
- //#endregion
244
- //#region src/multi.ts
245
- var multi_exports = {};
246
- __export(multi_exports, {
247
- derive: () => derive$2,
248
- provide: () => provide$2
249
- });
250
- function createMultiExecutor(option, poolId, keyPool, createNewExecutor, providerMetas) {
251
- const processKey = (key) => {
252
- const validatedKey = validate(option.keySchema, key);
253
- const transformedKey = option.keyTransform ? option.keyTransform(validatedKey) : validatedKey;
254
- return {
255
- validatedKey,
256
- transformedKey
257
- };
258
- };
259
- const newProvider = (key) => {
260
- const { transformedKey } = processKey(key);
261
- const executor = createNewExecutor(key);
262
- keyPool.set(transformedKey, executor);
263
- return executor;
264
- };
265
- const provider = createExecutor((ctl) => {
266
- return (key) => {
267
- const { transformedKey } = processKey(key);
268
- let executor = keyPool.get(transformedKey);
269
- if (!executor) executor = newProvider(key);
270
- return ctl.scope.accessor(executor);
271
- };
272
- }, void 0, providerMetas);
273
- const multiExecutor = (key) => {
274
- const { transformedKey } = processKey(key);
275
- return keyPool.get(transformedKey) || newProvider(key);
276
- };
277
- Object.assign(multiExecutor, provider);
278
- multiExecutor.release = async (scope) => {
279
- const entries = scope.entries();
280
- for (const [executor] of entries) {
281
- const check = poolId.some ? poolId.some(executor) : poolId.find(executor);
282
- if (check && (Array.isArray(check) ? check.length > 0 : check)) await scope.release(executor);
283
- }
284
- };
285
- multiExecutor.id = poolId;
286
- return multiExecutor;
287
- }
288
- function provide$2(option, valueFn, ...metas) {
289
- const poolId = meta(Symbol(), custom());
290
- const keyPool = new Map();
291
- const createNewExecutor = (key) => {
292
- const validatedKey = validate(option.keySchema, key);
293
- return createExecutor((ctl) => valueFn(validatedKey, ctl), void 0, [poolId(void 0), ...metas]);
294
- };
295
- return createMultiExecutor(option, poolId, keyPool, createNewExecutor, [poolId(void 0), ...metas]);
296
- }
297
- function derive$2(option, valueFn, ...metas) {
298
- const poolId = meta(Symbol(), custom());
299
- const keyPool = new Map();
300
- const createNewExecutor = (key) => {
301
- const validatedKey = validate(option.keySchema, key);
302
- return createExecutor((dependencies, ctl) => valueFn(dependencies, validatedKey, ctl), option.dependencies, metas);
303
- };
304
- return createMultiExecutor(option, poolId, keyPool, createNewExecutor, metas);
305
- }
306
-
307
- //#endregion
308
- //#region src/generator-utils.ts
309
- function isGenerator(value) {
310
- return value != null && typeof value === "object" && typeof value[Symbol.iterator] === "function" && typeof value.next === "function" && typeof value.return === "function" && typeof value.throw === "function";
311
- }
312
- function isAsyncGenerator(value) {
313
- return value != null && typeof value === "object" && typeof value[Symbol.asyncIterator] === "function" && typeof value.next === "function" && typeof value.return === "function" && typeof value.throw === "function";
314
- }
315
- function isGeneratorFunction(fn) {
316
- return typeof fn === "function" && fn.constructor && fn.constructor.name === "GeneratorFunction";
317
- }
318
- function isAsyncGeneratorFunction(fn) {
319
- return typeof fn === "function" && fn.constructor && fn.constructor.name === "AsyncGeneratorFunction";
320
- }
321
- function isIterableOrAsyncIterable(value) {
322
- return isGenerator(value) || isAsyncGenerator(value);
323
- }
324
- async function collectFromGenerator(gen) {
325
- const yielded = [];
326
- let returned;
327
- try {
328
- let result;
329
- do {
330
- result = await gen.next();
331
- if (!result.done) yielded.push(result.value);
332
- else returned = result.value;
333
- } while (!result.done);
334
- return {
335
- yielded,
336
- returned
337
- };
338
- } catch (error) {
339
- await gen.return?.(void 0);
340
- throw error;
147
+ function extractFromSource(source, key, schema) {
148
+ if (isDataStore(source)) {
149
+ const value = source.get(key);
150
+ return value === void 0 ? void 0 : validate(schema, value);
341
151
  }
152
+ if (isMetaArray(source)) {
153
+ const meta$2 = source.find((m) => m.key === key);
154
+ return meta$2 ? validate(schema, meta$2.value) : void 0;
155
+ }
156
+ const metas = source.metas ?? [];
157
+ const meta$1 = metas.find((m) => m.key === key);
158
+ return meta$1 ? validate(schema, meta$1.value) : void 0;
342
159
  }
343
- async function processGenerator(gen, handler) {
344
- let index = 0;
345
- try {
346
- let result;
347
- do {
348
- result = await gen.next();
349
- if (!result.done) await handler.onYield?.(result.value, index++);
350
- else {
351
- await handler.onReturn?.(result.value);
352
- return result.value;
353
- }
354
- } while (true);
355
- } catch (error) {
356
- await handler.onError?.(error);
357
- await gen.return?.(void 0);
358
- throw error;
160
+ var AccessorImpl$1 = class {
161
+ key;
162
+ schema;
163
+ constructor(key, schema) {
164
+ this.key = typeof key === "string" ? Symbol(key) : key;
165
+ this.schema = schema;
359
166
  }
167
+ get(source) {
168
+ const value = extractFromSource(source, this.key, this.schema);
169
+ if (value === void 0) throw new Error(`Value not found for key: ${this.key.toString()}`);
170
+ return value;
171
+ }
172
+ find(source) {
173
+ return extractFromSource(source, this.key, this.schema);
174
+ }
175
+ set(source, value) {
176
+ if (!isDataStore(source)) throw new Error("set() can only be used with DataStore");
177
+ const validated = validate(this.schema, value);
178
+ source.set(this.key, validated);
179
+ }
180
+ preset(value) {
181
+ const validated = validate(this.schema, value);
182
+ return [this.key, validated];
183
+ }
184
+ };
185
+ var AccessorWithDefaultImpl = class {
186
+ key;
187
+ schema;
188
+ defaultValue;
189
+ constructor(key, schema, defaultValue) {
190
+ this.key = typeof key === "string" ? Symbol(key) : key;
191
+ this.schema = schema;
192
+ this.defaultValue = validate(schema, defaultValue);
193
+ }
194
+ get(source) {
195
+ const value = extractFromSource(source, this.key, this.schema);
196
+ return value ?? this.defaultValue;
197
+ }
198
+ find(source) {
199
+ const value = extractFromSource(source, this.key, this.schema);
200
+ return value ?? this.defaultValue;
201
+ }
202
+ set(source, value) {
203
+ if (!isDataStore(source)) throw new Error("set() can only be used with DataStore");
204
+ const validated = validate(this.schema, value);
205
+ source.set(this.key, validated);
206
+ }
207
+ preset(value) {
208
+ const validated = validate(this.schema, value);
209
+ return [this.key, validated];
210
+ }
211
+ };
212
+ function accessor(key, schema, defaultValue) {
213
+ if (defaultValue !== void 0) return new AccessorWithDefaultImpl(key, schema, defaultValue);
214
+ return new AccessorImpl$1(key, schema);
360
215
  }
361
216
 
362
217
  //#endregion
@@ -528,142 +383,345 @@ function buildDependencyChain(executorStack) {
528
383
  }
529
384
 
530
385
  //#endregion
531
- //#region src/scope.ts
532
- function getExecutor(e) {
533
- if (isLazyExecutor(e) || isReactiveExecutor(e) || isStaticExecutor(e)) return e.executor;
534
- return e;
386
+ //#region src/executor.ts
387
+ function createExecutor(factory, dependencies, metas) {
388
+ const executor = {
389
+ [executorSymbol]: "main",
390
+ factory: (_, controller) => {
391
+ if (dependencies === void 0) {
392
+ const f$1 = factory;
393
+ return f$1(controller);
394
+ }
395
+ const f = factory;
396
+ return f(_, controller);
397
+ },
398
+ dependencies,
399
+ metas
400
+ };
401
+ const lazyExecutor = {
402
+ [executorSymbol]: "lazy",
403
+ dependencies: void 0,
404
+ executor,
405
+ factory: void 0,
406
+ metas
407
+ };
408
+ const reactiveExecutor = {
409
+ [executorSymbol]: "reactive",
410
+ executor,
411
+ factory: void 0,
412
+ dependencies: void 0,
413
+ metas
414
+ };
415
+ const staticExecutor = {
416
+ [executorSymbol]: "static",
417
+ dependencies: void 0,
418
+ factory: void 0,
419
+ metas,
420
+ executor
421
+ };
422
+ Object.defineProperties(executor, {
423
+ lazy: {
424
+ value: lazyExecutor,
425
+ writable: false,
426
+ configurable: false,
427
+ enumerable: false
428
+ },
429
+ reactive: {
430
+ value: reactiveExecutor,
431
+ writable: false,
432
+ configurable: false,
433
+ enumerable: false
434
+ },
435
+ static: {
436
+ value: staticExecutor,
437
+ writable: false,
438
+ configurable: false,
439
+ enumerable: false
440
+ }
441
+ });
442
+ return executor;
535
443
  }
536
- var BaseScope = class {
537
- disposed = false;
538
- cache = new Map();
539
- cleanups = new Map();
540
- onUpdates = new Map();
541
- onEvents = {
542
- change: new Set(),
543
- release: new Set(),
544
- error: new Set()
444
+ function isLazyExecutor(executor) {
445
+ return executor[executorSymbol] === "lazy";
446
+ }
447
+ function isReactiveExecutor(executor) {
448
+ return executor[executorSymbol] === "reactive";
449
+ }
450
+ function isStaticExecutor(executor) {
451
+ return executor[executorSymbol] === "static";
452
+ }
453
+ function isMainExecutor(executor) {
454
+ return isExecutor(executor) && executor[executorSymbol] === "main";
455
+ }
456
+ function isExecutor(input) {
457
+ return typeof input === "object" && input !== null && executorSymbol in input;
458
+ }
459
+ function isPreset(input) {
460
+ return typeof input === "object" && input !== null && executorSymbol in input && input[executorSymbol] === "preset";
461
+ }
462
+ function provide(factory, ...metas) {
463
+ return createExecutor(factory, void 0, metas);
464
+ }
465
+ function derive(pdependencies, pfactory, ...metas) {
466
+ return createExecutor(pfactory, pdependencies, metas);
467
+ }
468
+ function preset(e, v) {
469
+ const executor = isExecutor(e) ? e : e.escape();
470
+ return {
471
+ [executorSymbol]: "preset",
472
+ value: v,
473
+ executor
545
474
  };
546
- onErrors = new Map();
547
- isPod;
548
- isDisposing = false;
549
- plugins = [];
550
- registry = [];
551
- initialValues = [];
552
- constructor(options) {
553
- this.isPod = options?.pod || false;
554
- if (options?.registry) this.registry = [...options.registry];
555
- if (options?.initialValues) this.initialValues = options.initialValues;
556
- if (options?.plugins) for (const plugin$1 of options.plugins) this.use(plugin$1);
475
+ }
476
+
477
+ //#endregion
478
+ //#region src/generator-utils.ts
479
+ function isGenerator(value) {
480
+ return value != null && typeof value === "object" && typeof value[Symbol.iterator] === "function" && typeof value.next === "function" && typeof value.return === "function" && typeof value.throw === "function";
481
+ }
482
+ function isAsyncGenerator(value) {
483
+ return value != null && typeof value === "object" && typeof value[Symbol.asyncIterator] === "function" && typeof value.next === "function" && typeof value.return === "function" && typeof value.throw === "function";
484
+ }
485
+ function isGeneratorFunction(fn) {
486
+ return typeof fn === "function" && fn.constructor && fn.constructor.name === "GeneratorFunction";
487
+ }
488
+ function isAsyncGeneratorFunction(fn) {
489
+ return typeof fn === "function" && fn.constructor && fn.constructor.name === "AsyncGeneratorFunction";
490
+ }
491
+ function isIterableOrAsyncIterable(value) {
492
+ return isGenerator(value) || isAsyncGenerator(value);
493
+ }
494
+ async function collectFromGenerator(gen) {
495
+ const yielded = [];
496
+ let returned;
497
+ try {
498
+ let result;
499
+ do {
500
+ result = await gen.next();
501
+ if (!result.done) yielded.push(result.value);
502
+ else returned = result.value;
503
+ } while (!result.done);
504
+ return {
505
+ yielded,
506
+ returned
507
+ };
508
+ } catch (error) {
509
+ await gen.return?.(void 0);
510
+ throw error;
557
511
  }
558
- async "~triggerCleanup"(e) {
559
- const cs = this.cleanups.get(e);
560
- if (cs) for (const c of Array.from(cs.values()).reverse()) await c();
512
+ }
513
+ async function processGenerator(gen, handler) {
514
+ let index = 0;
515
+ try {
516
+ let result;
517
+ do {
518
+ result = await gen.next();
519
+ if (!result.done) await handler.onYield?.(result.value, index++);
520
+ else {
521
+ await handler.onReturn?.(result.value);
522
+ return result.value;
523
+ }
524
+ } while (true);
525
+ } catch (error) {
526
+ await handler.onError?.(error);
527
+ await gen.return?.(void 0);
528
+ throw error;
561
529
  }
562
- async "~triggerUpdate"(e) {
563
- const ce = this.cache.get(e);
564
- if (!ce) throw new Error("Executor is not yet resolved");
565
- const ou = this.onUpdates.get(e);
566
- if (ou) for (const t of Array.from(ou.values())) if (isMainExecutor(t)) {
567
- if (this.cleanups.has(t)) this["~triggerCleanup"](t);
568
- const a = this.cache.get(t);
569
- await a.accessor.resolve(true);
570
- if (this.onUpdates.has(t)) await this["~triggerUpdate"](t);
571
- } else await t(ce.accessor);
530
+ }
531
+
532
+ //#endregion
533
+ //#region src/helpers.ts
534
+ async function resolves(scope, executors) {
535
+ const objectOutput = {};
536
+ const arrayOutput = [];
537
+ const isArray = Array.isArray(executors);
538
+ for (const [index, executor] of Object.entries(executors)) {
539
+ const target = !isExecutor(executor) ? executor.escape() : isLazyExecutor(executor) || isReactiveExecutor(executor) || isStaticExecutor(executor) ? executor.executor : executor;
540
+ const result$1 = await scope.resolve(target);
541
+ if (isArray) arrayOutput.push(result$1);
542
+ else Object.assign(objectOutput, { [index]: result$1 });
572
543
  }
573
- async "~triggerError"(error, executor) {
574
- const executorCallbacks = this.onErrors.get(executor);
575
- if (executorCallbacks) for (const callback of Array.from(executorCallbacks.values())) try {
576
- await callback(error, executor, this);
577
- } catch (callbackError) {
578
- console.error("Error in error callback:", callbackError);
579
- }
580
- for (const callback of Array.from(this.onEvents.error.values())) try {
581
- await callback(error, executor, this);
582
- } catch (callbackError) {
583
- console.error("Error in global error callback:", callbackError);
584
- }
585
- for (const plugin$1 of this.plugins) if (plugin$1.onError) try {
586
- await plugin$1.onError(error, executor, this);
587
- } catch (pluginError) {
588
- console.error("Error in plugin error handler:", pluginError);
589
- }
544
+ const result = isArray ? arrayOutput : objectOutput;
545
+ return result;
546
+ }
547
+ var PreparedExecutorImpl = class {
548
+ scope;
549
+ executor;
550
+ constructor(scope, executor) {
551
+ this.scope = scope;
552
+ this.executor = executor;
590
553
  }
591
- async "~resolveExecutor"(ie, ref) {
592
- const e = getExecutor(ie);
593
- const a = this["~makeAccessor"](e);
594
- if (isLazyExecutor(ie)) return a;
595
- if (isReactiveExecutor(ie)) {
596
- const c = this.onUpdates.get(ie.executor) ?? new Set();
597
- this.onUpdates.set(ie.executor, c);
598
- c.add(ref);
599
- }
600
- await a.resolve(false);
601
- if (isStaticExecutor(ie)) return a;
602
- return a.get();
554
+ async __call() {
555
+ return await this.scope.resolve(this.executor);
556
+ }
557
+ escape() {
558
+ return this.executor;
559
+ }
560
+ };
561
+ function prepare(scope, executor) {
562
+ const impl = new PreparedExecutorImpl(scope, executor);
563
+ const fn = async () => impl.__call();
564
+ fn.escape = () => impl.escape();
565
+ return fn;
566
+ }
567
+ var AdaptedExecutorImpl = class {
568
+ scope;
569
+ executor;
570
+ constructor(scope, executor) {
571
+ this.scope = scope;
572
+ this.executor = executor;
573
+ }
574
+ async __call(...args) {
575
+ const fn = await this.scope.resolve(this.executor);
576
+ return await fn(...args);
577
+ }
578
+ escape() {
579
+ return this.executor;
580
+ }
581
+ };
582
+ function adapt(scope, executor) {
583
+ const impl = new AdaptedExecutorImpl(scope, executor);
584
+ const fn = async (...args) => impl.__call(...args);
585
+ fn.escape = () => impl.escape();
586
+ return fn;
587
+ }
588
+
589
+ //#endregion
590
+ //#region src/multi.ts
591
+ var multi_exports = {};
592
+ __export(multi_exports, {
593
+ derive: () => derive$1,
594
+ provide: () => provide$1
595
+ });
596
+ var MultiExecutorImpl = class {
597
+ option;
598
+ poolId;
599
+ keyPool;
600
+ createNewExecutor;
601
+ id;
602
+ constructor(option, poolId, keyPool, createNewExecutor) {
603
+ this.option = option;
604
+ this.poolId = poolId;
605
+ this.keyPool = keyPool;
606
+ this.createNewExecutor = createNewExecutor;
607
+ this.id = poolId;
608
+ }
609
+ processKey(key) {
610
+ const validatedKey = validate(this.option.keySchema, key);
611
+ const transformedKey = this.option.keyTransform ? this.option.keyTransform(validatedKey) : validatedKey;
612
+ return {
613
+ validatedKey,
614
+ transformedKey
615
+ };
616
+ }
617
+ newProvider(key) {
618
+ const { transformedKey } = this.processKey(key);
619
+ const executor = this.createNewExecutor(key);
620
+ this.keyPool.set(transformedKey, executor);
621
+ return executor;
622
+ }
623
+ __call(key) {
624
+ const { transformedKey } = this.processKey(key);
625
+ return this.keyPool.get(transformedKey) || this.newProvider(key);
626
+ }
627
+ providerFactory(ctl) {
628
+ return (key) => {
629
+ const { transformedKey } = this.processKey(key);
630
+ let executor = this.keyPool.get(transformedKey);
631
+ if (!executor) executor = this.newProvider(key);
632
+ return ctl.scope.accessor(executor);
633
+ };
603
634
  }
604
- async "~resolveDependencies"(ie, ref) {
605
- if (ie === void 0) return void 0;
606
- if (isExecutor(ie)) return this["~resolveExecutor"](ie, ref);
607
- if (Array.isArray(ie)) return await Promise.all(ie.map((item) => this["~resolveDependencies"](item, ref)));
608
- const r = {};
609
- for (const k of Object.keys(ie)) {
610
- const t = ie[k];
611
- const rd = await this["~resolveDependencies"](t, ref);
612
- r[k] = rd;
635
+ async release(scope) {
636
+ const entries = scope.entries();
637
+ for (const [executor] of entries) {
638
+ const check = this.poolId.some ? this.poolId.some(executor) : this.poolId.find(executor);
639
+ if (check && (Array.isArray(check) ? check.length > 0 : check)) await scope.release(executor);
613
640
  }
614
- return r;
615
641
  }
616
- "~ensureNotDisposed"() {
617
- if (this.disposed) throw new Error("Scope is disposed");
642
+ };
643
+ function createMultiExecutor(option, poolId, keyPool, createNewExecutor, providerMetas) {
644
+ const impl = new MultiExecutorImpl(option, poolId, keyPool, createNewExecutor);
645
+ const provider = createExecutor((ctl) => impl.providerFactory(ctl), void 0, providerMetas);
646
+ const multiExecutor = (key) => impl.__call(key);
647
+ Object.assign(multiExecutor, provider);
648
+ multiExecutor.release = (scope) => impl.release(scope);
649
+ multiExecutor.id = impl.id;
650
+ return multiExecutor;
651
+ }
652
+ function provide$1(option, valueFn, ...metas) {
653
+ const poolId = meta(Symbol(), custom());
654
+ const keyPool = new Map();
655
+ const createNewExecutor = (key) => {
656
+ const validatedKey = validate(option.keySchema, key);
657
+ return createExecutor((ctl) => valueFn(validatedKey, ctl), void 0, [poolId(void 0), ...metas]);
658
+ };
659
+ return createMultiExecutor(option, poolId, keyPool, createNewExecutor, [poolId(void 0), ...metas]);
660
+ }
661
+ function derive$1(option, valueFn, ...metas) {
662
+ const poolId = meta(Symbol(), custom());
663
+ const keyPool = new Map();
664
+ const createNewExecutor = (key) => {
665
+ const validatedKey = validate(option.keySchema, key);
666
+ return createExecutor((dependencies, ctl) => valueFn(dependencies, validatedKey, ctl), option.dependencies, metas);
667
+ };
668
+ return createMultiExecutor(option, poolId, keyPool, createNewExecutor, metas);
669
+ }
670
+
671
+ //#endregion
672
+ //#region src/scope.ts
673
+ var AccessorImpl = class {
674
+ metas;
675
+ scope;
676
+ requestor;
677
+ currentPromise = null;
678
+ resolve;
679
+ constructor(scope, requestor, metas) {
680
+ this.scope = scope;
681
+ this.requestor = requestor;
682
+ this.metas = metas;
683
+ this.resolve = this.createResolveFunction();
684
+ const existing = this.scope["cache"].get(requestor);
685
+ if (!existing || !existing.accessor) this.scope["cache"].set(requestor, {
686
+ accessor: this,
687
+ value: existing?.value || void 0
688
+ });
618
689
  }
619
- "~makeAccessor"(e) {
620
- let requestor = isLazyExecutor(e) || isReactiveExecutor(e) || isStaticExecutor(e) ? e.executor : e;
621
- const cachedAccessor = this.cache.get(requestor);
622
- if (cachedAccessor) return cachedAccessor.accessor;
623
- const accessor = {};
624
- const controller = {
625
- cleanup: (cleanup) => {
626
- const currentSet = this.cleanups.get(requestor) ?? new Set();
627
- this.cleanups.set(requestor, currentSet);
628
- currentSet.add(cleanup);
629
- },
630
- release: async () => this.release(requestor),
631
- reload: async () => {
632
- await this.resolve(requestor, true);
633
- },
634
- scope: this
635
- };
636
- const resolve = (force) => {
637
- this["~ensureNotDisposed"]();
638
- const entry = this.cache.get(requestor);
690
+ createResolveFunction() {
691
+ return (force = false) => {
692
+ this.scope["~ensureNotDisposed"]();
693
+ const entry = this.scope["cache"].get(this.requestor);
639
694
  const cached = entry?.value;
640
695
  if (cached && !force) if (cached.kind === "resolved") return Promise.resolve(cached.value);
641
696
  else if (cached.kind === "rejected") throw cached.error;
642
697
  else return cached.promise;
643
- const promise = new Promise((resolve$1, reject) => {
644
- const replacer = this.initialValues.find((item) => item.executor === requestor);
645
- let factory = requestor.factory;
646
- let dependencies = requestor.dependencies;
698
+ if (this.currentPromise && !force) return this.currentPromise;
699
+ this.scope["~addToResolutionChain"](this.requestor, this.requestor);
700
+ this.currentPromise = new Promise((resolve, reject) => {
701
+ const replacer = this.scope["initialValues"].find((item) => item.executor === this.requestor);
702
+ let factory = this.requestor.factory;
703
+ let dependencies = this.requestor.dependencies;
647
704
  if (replacer) {
648
705
  const value = replacer.value;
649
706
  if (!isExecutor(value)) return setTimeout(() => {
650
- this.cache.set(requestor, {
651
- accessor,
707
+ this.scope["cache"].set(this.requestor, {
708
+ accessor: this,
652
709
  value: {
653
710
  kind: "resolved",
654
711
  value: replacer.value
655
712
  }
656
713
  });
657
- resolve$1(replacer.value);
714
+ resolve(replacer.value);
658
715
  }, 0);
659
716
  factory = value.factory;
660
717
  dependencies = value.dependencies;
661
718
  }
662
- this["~resolveDependencies"](dependencies, requestor).then((dependencies$1) => {
719
+ const controller = this.createController();
720
+ this.scope["~resolveDependencies"](dependencies, this.requestor).then((dependencies$1) => {
663
721
  try {
664
722
  const factoryResult = factory(dependencies$1, controller);
665
723
  if (factoryResult instanceof Promise) return factoryResult.catch((asyncError) => {
666
- const executorName = getExecutorName(requestor);
724
+ const executorName = getExecutorName(this.requestor);
667
725
  const dependencyChain = [executorName];
668
726
  const factoryError = createFactoryError(ErrorCodes.FACTORY_ASYNC_ERROR, executorName, dependencyChain, asyncError, {
669
727
  dependenciesResolved: dependencies$1 !== void 0,
@@ -674,7 +732,7 @@ var BaseScope = class {
674
732
  });
675
733
  return factoryResult;
676
734
  } catch (error) {
677
- const executorName = getExecutorName(requestor);
735
+ const executorName = getExecutorName(this.requestor);
678
736
  const dependencyChain = [executorName];
679
737
  const factoryError = createFactoryError(ErrorCodes.FACTORY_THREW_ERROR, executorName, dependencyChain, error, {
680
738
  dependenciesResolved: dependencies$1 !== void 0,
@@ -689,7 +747,7 @@ var BaseScope = class {
689
747
  const { returned } = await collectFromGenerator(result);
690
748
  current = returned;
691
749
  } catch (generatorError) {
692
- const executorName = getExecutorName(requestor);
750
+ const executorName = getExecutorName(this.requestor);
693
751
  const dependencyChain = [executorName];
694
752
  const factoryError = createFactoryError(ErrorCodes.FACTORY_GENERATOR_ERROR, executorName, dependencyChain, generatorError, {
695
753
  generatorType: isGenerator(result) ? "sync" : "async",
@@ -697,19 +755,21 @@ var BaseScope = class {
697
755
  });
698
756
  throw factoryError;
699
757
  }
700
- const events = this.onEvents.change;
758
+ const events = this.scope["onEvents"].change;
701
759
  for (const event of events) {
702
- const updated = await event("resolve", requestor, current, this);
703
- if (updated !== void 0 && updated.executor === requestor) current = updated.value;
760
+ const updated = await event("resolve", this.requestor, current, this.scope);
761
+ if (updated !== void 0 && updated.executor === this.requestor) current = updated.value;
704
762
  }
705
- this.cache.set(requestor, {
706
- accessor,
763
+ this.scope["cache"].set(this.requestor, {
764
+ accessor: this,
707
765
  value: {
708
766
  kind: "resolved",
709
767
  value: current
710
768
  }
711
769
  });
712
- resolve$1(current);
770
+ this.scope["~removeFromResolutionChain"](this.requestor);
771
+ this.currentPromise = null;
772
+ resolve(current);
713
773
  }).catch((error) => {
714
774
  let enhancedError = error;
715
775
  let errorContext = void 0;
@@ -717,7 +777,7 @@ var BaseScope = class {
717
777
  enhancedError = error;
718
778
  errorContext = error.context;
719
779
  } else {
720
- const executorName = getExecutorName(requestor);
780
+ const executorName = getExecutorName(this.requestor);
721
781
  const dependencyChain = [executorName];
722
782
  enhancedError = createSystemError(ErrorCodes.INTERNAL_RESOLUTION_ERROR, executorName, dependencyChain, error, {
723
783
  errorType: error?.constructor?.name || "UnknownError",
@@ -725,8 +785,8 @@ var BaseScope = class {
725
785
  });
726
786
  errorContext = enhancedError.context;
727
787
  }
728
- this.cache.set(requestor, {
729
- accessor,
788
+ this.scope["cache"].set(this.requestor, {
789
+ accessor: this,
730
790
  value: {
731
791
  kind: "rejected",
732
792
  error,
@@ -734,49 +794,186 @@ var BaseScope = class {
734
794
  enhancedError
735
795
  }
736
796
  });
737
- this["~triggerError"](enhancedError, requestor);
797
+ this.scope["~removeFromResolutionChain"](this.requestor);
798
+ this.scope["~triggerError"](enhancedError, this.requestor);
799
+ this.currentPromise = null;
738
800
  reject(enhancedError);
739
801
  });
740
802
  });
741
- this.cache.set(requestor, {
742
- accessor,
803
+ this.scope["cache"].set(this.requestor, {
804
+ accessor: this,
743
805
  value: {
744
806
  kind: "pending",
745
- promise
807
+ promise: this.currentPromise
746
808
  }
747
809
  });
748
- return promise;
810
+ return this.currentPromise;
749
811
  };
750
- return Object.assign(accessor, {
751
- get: () => {
752
- this["~ensureNotDisposed"]();
753
- const cacheEntry = this.cache.get(requestor)?.value;
754
- if (!cacheEntry || cacheEntry.kind === "pending") throw new Error("Executor is not resolved");
755
- if (cacheEntry.kind === "rejected") throw cacheEntry.enhancedError || cacheEntry.error;
756
- return cacheEntry.value;
757
- },
758
- lookup: () => {
759
- this["~ensureNotDisposed"]();
760
- const cacheEntry = this.cache.get(requestor);
761
- if (!cacheEntry) return void 0;
762
- return cacheEntry.value;
763
- },
764
- metas: e.metas,
765
- resolve,
766
- release: async (soft = false) => {
767
- this.release(requestor, soft);
768
- },
769
- update: (updateFn) => {
770
- return this.update(requestor, updateFn);
812
+ }
813
+ lookup() {
814
+ this.scope["~ensureNotDisposed"]();
815
+ const cacheEntry = this.scope["cache"].get(this.requestor);
816
+ if (!cacheEntry) return void 0;
817
+ return cacheEntry.value || void 0;
818
+ }
819
+ get() {
820
+ this.scope["~ensureNotDisposed"]();
821
+ const cacheEntry = this.scope["cache"].get(this.requestor)?.value;
822
+ if (!cacheEntry || cacheEntry.kind === "pending") throw new Error("Executor is not resolved");
823
+ if (cacheEntry.kind === "rejected") throw cacheEntry.enhancedError || cacheEntry.error;
824
+ return cacheEntry.value;
825
+ }
826
+ async release(soft = false) {
827
+ this.scope.release(this.requestor, soft);
828
+ }
829
+ async update(updateFn) {
830
+ return this.scope.update(this.requestor, updateFn);
831
+ }
832
+ async set(value) {
833
+ return this.scope.update(this.requestor, value);
834
+ }
835
+ subscribe(cb) {
836
+ this.scope["~ensureNotDisposed"]();
837
+ return this.scope.onUpdate(this.requestor, cb);
838
+ }
839
+ createController() {
840
+ return {
841
+ cleanup: (cleanup) => {
842
+ const currentSet = this.scope["cleanups"].get(this.requestor) ?? new Set();
843
+ this.scope["cleanups"].set(this.requestor, currentSet);
844
+ currentSet.add(cleanup);
771
845
  },
772
- set: async (value) => {
773
- return this.update(requestor, value);
846
+ release: async () => this.scope.release(this.requestor),
847
+ reload: async () => {
848
+ await this.scope.resolve(this.requestor, true);
774
849
  },
775
- subscribe: (cb) => {
776
- this["~ensureNotDisposed"]();
777
- return this.onUpdate(requestor, cb);
778
- }
779
- });
850
+ scope: this.scope
851
+ };
852
+ }
853
+ };
854
+ function getExecutor(e) {
855
+ if (isLazyExecutor(e) || isReactiveExecutor(e) || isStaticExecutor(e)) return e.executor;
856
+ return e;
857
+ }
858
+ var BaseScope = class {
859
+ disposed = false;
860
+ cache = new Map();
861
+ cleanups = new Map();
862
+ onUpdates = new Map();
863
+ onEvents = {
864
+ change: new Set(),
865
+ release: new Set(),
866
+ error: new Set()
867
+ };
868
+ onErrors = new Map();
869
+ isPod;
870
+ isDisposing = false;
871
+ resolutionChain = new Map();
872
+ plugins = [];
873
+ registry = [];
874
+ initialValues = [];
875
+ constructor(options) {
876
+ this.isPod = options?.pod || false;
877
+ if (options?.registry) this.registry = [...options.registry];
878
+ if (options?.initialValues) this.initialValues = options.initialValues;
879
+ if (options?.plugins) for (const plugin$1 of options.plugins) this.use(plugin$1);
880
+ }
881
+ "~checkCircularDependency"(executor, resolvingExecutor) {
882
+ const currentChain = this.resolutionChain.get(resolvingExecutor);
883
+ if (currentChain && currentChain.has(executor)) {
884
+ const chainArray = Array.from(currentChain);
885
+ const dependencyChain = buildDependencyChain(chainArray);
886
+ throw createDependencyError(ErrorCodes.CIRCULAR_DEPENDENCY, getExecutorName(executor), dependencyChain, getExecutorName(executor), void 0, {
887
+ circularPath: dependencyChain.join(" -> ") + " -> " + getExecutorName(executor),
888
+ detectedAt: getExecutorName(resolvingExecutor)
889
+ });
890
+ }
891
+ }
892
+ "~addToResolutionChain"(executor, resolvingExecutor) {
893
+ const currentChain = this.resolutionChain.get(resolvingExecutor) || new Set();
894
+ currentChain.add(executor);
895
+ this.resolutionChain.set(resolvingExecutor, currentChain);
896
+ }
897
+ "~removeFromResolutionChain"(executor) {
898
+ this.resolutionChain.delete(executor);
899
+ }
900
+ "~propagateResolutionChain"(fromExecutor, toExecutor) {
901
+ const fromChain = this.resolutionChain.get(fromExecutor);
902
+ if (fromChain) {
903
+ const newChain = new Set(fromChain);
904
+ newChain.add(fromExecutor);
905
+ this.resolutionChain.set(toExecutor, newChain);
906
+ }
907
+ }
908
+ async "~triggerCleanup"(e) {
909
+ const cs = this.cleanups.get(e);
910
+ if (cs) for (const c of Array.from(cs.values()).reverse()) await c();
911
+ }
912
+ async "~triggerUpdate"(e) {
913
+ const ce = this.cache.get(e);
914
+ if (!ce) throw new Error("Executor is not yet resolved");
915
+ const ou = this.onUpdates.get(e);
916
+ if (ou) for (const t of Array.from(ou.values())) if (isMainExecutor(t)) {
917
+ if (this.cleanups.has(t)) this["~triggerCleanup"](t);
918
+ const a = this.cache.get(t);
919
+ await a.accessor.resolve(true);
920
+ if (this.onUpdates.has(t)) await this["~triggerUpdate"](t);
921
+ } else await t(ce.accessor);
922
+ }
923
+ async "~triggerError"(error, executor) {
924
+ const executorCallbacks = this.onErrors.get(executor);
925
+ if (executorCallbacks) for (const callback of Array.from(executorCallbacks.values())) try {
926
+ await callback(error, executor, this);
927
+ } catch (callbackError) {
928
+ console.error("Error in error callback:", callbackError);
929
+ }
930
+ for (const callback of Array.from(this.onEvents.error.values())) try {
931
+ await callback(error, executor, this);
932
+ } catch (callbackError) {
933
+ console.error("Error in global error callback:", callbackError);
934
+ }
935
+ for (const plugin$1 of this.plugins) if (plugin$1.onError) try {
936
+ await plugin$1.onError(error, executor, this);
937
+ } catch (pluginError) {
938
+ console.error("Error in plugin error handler:", pluginError);
939
+ }
940
+ }
941
+ async "~resolveExecutor"(ie, ref) {
942
+ const e = getExecutor(ie);
943
+ this["~checkCircularDependency"](e, ref);
944
+ this["~propagateResolutionChain"](ref, e);
945
+ const a = this["~makeAccessor"](e);
946
+ if (isLazyExecutor(ie)) return a;
947
+ if (isReactiveExecutor(ie)) {
948
+ const c = this.onUpdates.get(ie.executor) ?? new Set();
949
+ this.onUpdates.set(ie.executor, c);
950
+ c.add(ref);
951
+ }
952
+ await a.resolve(false);
953
+ if (isStaticExecutor(ie)) return a;
954
+ return a.get();
955
+ }
956
+ async "~resolveDependencies"(ie, ref) {
957
+ if (ie === void 0) return void 0;
958
+ if (isExecutor(ie)) return this["~resolveExecutor"](ie, ref);
959
+ if (Array.isArray(ie)) return await Promise.all(ie.map((item) => this["~resolveDependencies"](item, ref)));
960
+ const r = {};
961
+ for (const k of Object.keys(ie)) {
962
+ const t = ie[k];
963
+ const rd = await this["~resolveDependencies"](t, ref);
964
+ r[k] = rd;
965
+ }
966
+ return r;
967
+ }
968
+ "~ensureNotDisposed"() {
969
+ if (this.disposed) throw new Error("Scope is disposed");
970
+ }
971
+ "~makeAccessor"(e) {
972
+ let requestor = isLazyExecutor(e) || isReactiveExecutor(e) || isStaticExecutor(e) ? e.executor : e;
973
+ const cachedAccessor = this.cache.get(requestor);
974
+ if (cachedAccessor && cachedAccessor.accessor) return cachedAccessor.accessor;
975
+ const accessor$1 = new AccessorImpl(this, requestor, e.metas);
976
+ return accessor$1;
780
977
  }
781
978
  accessor(executor) {
782
979
  this["~ensureNotDisposed"]();
@@ -789,25 +986,25 @@ var BaseScope = class {
789
986
  }
790
987
  async resolve(executor, force = false) {
791
988
  this["~ensureNotDisposed"]();
792
- const accessor = this["~makeAccessor"](executor);
793
- await accessor.resolve(force);
794
- return accessor.get();
989
+ const accessor$1 = this["~makeAccessor"](executor);
990
+ await accessor$1.resolve(force);
991
+ return accessor$1.get();
795
992
  }
796
993
  async resolveAccessor(executor, force = false) {
797
994
  this["~ensureNotDisposed"]();
798
- const accessor = this["~makeAccessor"](executor);
799
- await accessor.resolve(force);
800
- return accessor;
995
+ const accessor$1 = this["~makeAccessor"](executor);
996
+ await accessor$1.resolve(force);
997
+ return accessor$1;
801
998
  }
802
999
  async update(e, u) {
803
1000
  if (this.isDisposing) return;
804
1001
  this["~ensureNotDisposed"]();
805
1002
  this["~triggerCleanup"](e);
806
- const accessor = this["~makeAccessor"](e);
1003
+ const accessor$1 = this["~makeAccessor"](e);
807
1004
  let value;
808
1005
  if (typeof u === "function") {
809
1006
  const fn = u;
810
- value = fn(accessor.get());
1007
+ value = fn(accessor$1.get());
811
1008
  } else value = u;
812
1009
  const events = this.onEvents.change;
813
1010
  for (const event of events) {
@@ -815,7 +1012,7 @@ var BaseScope = class {
815
1012
  if (updated !== void 0 && e === updated.executor) value = updated.value;
816
1013
  }
817
1014
  this.cache.set(e, {
818
- accessor,
1015
+ accessor: accessor$1,
819
1016
  value: {
820
1017
  kind: "resolved",
821
1018
  value
@@ -856,6 +1053,7 @@ var BaseScope = class {
856
1053
  this.onEvents.release.clear();
857
1054
  this.onEvents.error.clear();
858
1055
  this.onErrors.clear();
1056
+ this.resolutionChain.clear();
859
1057
  }
860
1058
  onUpdate(e, cb) {
861
1059
  this["~ensureNotDisposed"]();
@@ -965,9 +1163,9 @@ var Pod = class extends BaseScope {
965
1163
  if (this.cache.has(executor)) return super.resolve(executor, force);
966
1164
  if (this.parentScope["cache"].has(executor)) {
967
1165
  const { value } = this.parentScope["cache"].get(executor);
968
- const accessor = super["~makeAccessor"](executor);
1166
+ const accessor$1 = super["~makeAccessor"](executor);
969
1167
  this["cache"].set(executor, {
970
- accessor,
1168
+ accessor: accessor$1,
971
1169
  value
972
1170
  });
973
1171
  if (value.kind === "rejected") throw value.error;
@@ -979,6 +1177,8 @@ var Pod = class extends BaseScope {
979
1177
  async "~resolveExecutor"(ie, ref) {
980
1178
  if (isReactiveExecutor(ie)) throw new Error("Reactive executors cannot be used in pod");
981
1179
  const t = getExecutor(ie);
1180
+ this["~checkCircularDependency"](t, ref);
1181
+ this["~propagateResolutionChain"](ref, t);
982
1182
  const a = this["~makeAccessor"](t);
983
1183
  if (this.parentScope["cache"].has(t)) {
984
1184
  const { value } = this.parentScope["cache"].get(t);
@@ -993,270 +1193,135 @@ var Pod = class extends BaseScope {
993
1193
 
994
1194
  //#endregion
995
1195
  //#region src/flow.ts
996
- var flow_exports = {};
997
- __export(flow_exports, {
998
- FlowError: () => FlowError,
999
- createInitialContext: () => createInitialContext,
1000
- derive: () => derive$1,
1001
- execute: () => execute,
1002
- provide: () => provide$1
1196
+ const ok = (data) => ({
1197
+ type: "ok",
1198
+ data
1003
1199
  });
1004
- function createInitialContext(initializers) {
1005
- const context = new Map();
1006
- for (const [_, config] of Object.entries(initializers)) {
1007
- const { accessor, value } = config;
1008
- context.set(accessor.key, value);
1200
+ const ko = (data) => ({
1201
+ type: "ko",
1202
+ data
1203
+ });
1204
+ const FlowExecutionContext = {
1205
+ depth: accessor("flow.depth", custom(), 0),
1206
+ flowName: accessor("flow.name", custom()),
1207
+ parentFlowName: accessor("flow.parentName", custom()),
1208
+ isParallel: accessor("flow.isParallel", custom(), false)
1209
+ };
1210
+ var FlowDefinition = class {
1211
+ constructor(name$1, version, input, success, error) {
1212
+ this.name = name$1;
1213
+ this.version = version;
1214
+ this.input = input;
1215
+ this.success = success;
1216
+ this.error = error;
1009
1217
  }
1010
- return context;
1011
- }
1012
- function provide$1(config, handler) {
1013
- const executor = createExecutor(() => ({
1014
- execution: handler,
1015
- input: config.input,
1016
- output: config.output,
1017
- plugins: config.plugins || [],
1018
- metas: config.metas || [],
1019
- name: config.name,
1020
- description: config.description
1021
- }), void 0, config.metas || []);
1022
- Object.assign(executor, config);
1023
- return executor;
1024
- }
1025
- function derive$1({ dependencies,...config }, handler) {
1026
- const executor = createExecutor((deps) => ({
1027
- execution: (input, controller) => handler(deps, input, controller),
1028
- input: config.input,
1029
- output: config.output,
1030
- plugins: config.plugins || [],
1031
- metas: config.metas || [],
1032
- name: config.name,
1033
- description: config.description
1034
- }), dependencies, config.metas || []);
1035
- Object.assign(executor, config);
1036
- return executor;
1218
+ provide(handler) {
1219
+ return createExecutor(() => {
1220
+ const flowHandler = async (ctx) => {
1221
+ return handler(ctx, ctx.input);
1222
+ };
1223
+ flowHandler.def = this;
1224
+ return flowHandler;
1225
+ }, void 0, void 0);
1226
+ }
1227
+ derive(dependencies, handler) {
1228
+ return createExecutor((deps) => {
1229
+ const flowHandler = async (ctx) => {
1230
+ return handler(deps, ctx, ctx.input);
1231
+ };
1232
+ flowHandler.def = this;
1233
+ return flowHandler;
1234
+ }, dependencies, void 0);
1235
+ }
1236
+ };
1237
+ function flow(config) {
1238
+ return new FlowDefinition(config.name, config.version || "1.0.0", config.input, config.success, config.error);
1037
1239
  }
1038
- var FlowError = class extends Error {
1039
- code;
1040
- category;
1041
- constructor(message, details) {
1042
- super(message);
1043
- this.details = details;
1044
- if (details && "cause" in details) this.cause = details.cause;
1045
- this.name = "FlowError";
1046
- if (details?.enhancedError) {
1047
- this.code = details.errorCode || ErrorCodes.FLOW_EXECUTION_FAILED;
1048
- this.category = details.errorCategory || "USER_ERROR";
1049
- } else {
1050
- this.code = ErrorCodes.FLOW_EXECUTION_FAILED;
1051
- this.category = "USER_ERROR";
1052
- }
1240
+ var FlowContext = class FlowContext {
1241
+ contextData = new Map();
1242
+ constructor(input, pod, plugins, parent) {
1243
+ this.input = input;
1244
+ this.pod = pod;
1245
+ this.plugins = plugins;
1246
+ this.parent = parent;
1053
1247
  }
1054
- /**
1055
- * Get the original enhanced error if available
1056
- */
1057
- getEnhancedError() {
1058
- return this.details?.enhancedError;
1248
+ initializeExecutionContext(flowName, isParallel = false) {
1249
+ const currentDepth = this.parent ? (FlowExecutionContext.depth.find(this.parent) || 0) + 1 : 0;
1250
+ const parentFlowName = this.parent ? FlowExecutionContext.flowName.find(this.parent) : void 0;
1251
+ FlowExecutionContext.depth.set(this, currentDepth);
1252
+ FlowExecutionContext.flowName.set(this, flowName);
1253
+ FlowExecutionContext.parentFlowName.set(this, parentFlowName);
1254
+ FlowExecutionContext.isParallel.set(this, isParallel);
1059
1255
  }
1060
- /**
1061
- * Get the error context from enhanced error if available
1062
- */
1063
- getErrorContext() {
1064
- return this.details?.errorContext;
1256
+ ok = (data) => ok(data);
1257
+ ko = (data) => ko(data);
1258
+ get(key) {
1259
+ if (this.contextData.has(key)) return this.contextData.get(key);
1260
+ return this.parent?.get(key);
1065
1261
  }
1066
- /**
1067
- * Get flow-specific context
1068
- */
1069
- getFlowContext() {
1070
- return {
1071
- flowName: this.details?.flowName,
1072
- flowDescription: this.details?.flowDescription
1073
- };
1262
+ set(key, value) {
1263
+ this.contextData.set(key, value);
1264
+ return value;
1074
1265
  }
1075
- };
1076
- function createController(context, opt) {
1077
- const controller = {
1078
- context,
1079
- safeExecute: async (flowDef, param, opts) => {
1080
- try {
1081
- let resolvedFlow;
1082
- if ("execution" in flowDef && typeof flowDef.execution === "function") resolvedFlow = flowDef;
1083
- else if (context.scope) resolvedFlow = await context.scope.pod().resolve(flowDef);
1084
- else throw new FlowError("Cannot resolve executor without a scope");
1085
- const validatedInput = validate(resolvedFlow.input, param);
1086
- const childData = new Map(context.data);
1087
- if (opts?.initialContext) for (const [key, value] of opts.initialContext) childData.set(key, value);
1088
- const childContext = {
1089
- data: childData,
1090
- parent: context,
1091
- scope: context.scope,
1092
- plugins: [...context.plugins, ...opts?.plugins || []],
1093
- flow: resolvedFlow,
1094
- get(key) {
1095
- return childData.get(key);
1096
- },
1097
- set(key, value) {
1098
- return childData.set(key, value);
1099
- }
1100
- };
1101
- const childController = createController(childContext, opt);
1102
- let execution = async () => resolvedFlow.execution(validatedInput, childController);
1103
- for (let i = childContext.plugins.length - 1; i >= 0; i--) {
1104
- const plugin$1 = childContext.plugins[i];
1105
- const prevExecution = execution;
1106
- execution = () => plugin$1.wrap(childContext, prevExecution);
1107
- }
1108
- const result = await execution();
1109
- return {
1110
- kind: "success",
1111
- value: result
1112
- };
1113
- } catch (error) {
1114
- const wrappedError = error instanceof FlowError ? error : new FlowError(error instanceof Error ? error.message : "Flow execution failed", { cause: error });
1115
- return {
1116
- kind: "error",
1117
- error: wrappedError
1118
- };
1119
- }
1120
- },
1121
- execute: async (flowDef, param, opts) => {
1122
- const result = await controller.safeExecute(flowDef, param, opts);
1123
- if (result.kind === "error") throw result.error;
1124
- return result.value;
1125
- }
1266
+ output = (success, value) => {
1267
+ return success ? this.ok(value) : this.ko(value);
1126
1268
  };
1127
- return controller;
1128
- }
1129
- async function execute(executor, input, opt) {
1130
- const isOwnScope = !opt?.scope;
1131
- const scope = opt?.scope || createScope();
1132
- const pod = scope.pod(...opt?.presets || []);
1133
- let flow;
1134
- try {
1135
- flow = await pod.resolve(executor);
1136
- } catch (error) {
1137
- if (error instanceof ExecutorResolutionError || error instanceof FactoryExecutionError) throw new FlowError(`Flow execution failed: ${error.message}`, {
1138
- cause: error,
1139
- enhancedError: error,
1140
- errorCode: error.code,
1141
- errorCategory: error.category,
1142
- errorContext: error.context,
1143
- flowName: opt?.name,
1144
- flowDescription: opt?.description
1145
- });
1146
- else {
1147
- const errorMessage = error instanceof Error ? error.message : String(error);
1148
- throw new FlowError(`Failed to resolve executor: ${errorMessage}`, {
1149
- cause: error,
1150
- flowName: opt?.name,
1151
- flowDescription: opt?.description
1152
- });
1153
- }
1269
+ async execute(flow$1, input) {
1270
+ const handler = await this.pod.resolve(flow$1);
1271
+ const childContext = new FlowContext(input, this.pod, this.plugins, this);
1272
+ childContext.initializeExecutionContext(handler.def.name, false);
1273
+ return await this.executeWithPlugins(handler, childContext);
1154
1274
  }
1155
- const contextData = opt?.initialContext || new Map();
1156
- const context = {
1157
- data: contextData,
1158
- parent: void 0,
1159
- scope,
1160
- plugins: opt?.plugins || [],
1161
- flow,
1162
- get(key) {
1163
- return contextData.get(key);
1164
- },
1165
- set(key, value) {
1166
- return contextData.set(key, value);
1275
+ async executeParallel(flows) {
1276
+ return Promise.all(flows.map(async ([flow$1, input]) => {
1277
+ const childContext = new FlowContext(input, this.pod, this.plugins, this);
1278
+ const handler = await this.pod.resolve(flow$1);
1279
+ childContext.initializeExecutionContext(handler.def.name, true);
1280
+ return this.executeWithPlugins(handler, childContext);
1281
+ }));
1282
+ }
1283
+ async executeWithPlugins(handler, context) {
1284
+ const executeCore = async () => handler(context);
1285
+ let executor = executeCore;
1286
+ for (const plugin$1 of [...this.plugins].reverse()) if (plugin$1.wrap) {
1287
+ const currentExecutor = executor;
1288
+ executor = async () => plugin$1.wrap(context, currentExecutor);
1167
1289
  }
1168
- };
1169
- const controller = createController(context, opt);
1170
- const flowWithContext = {
1171
- ...flow,
1172
- context
1173
- };
1174
- let executionResult;
1290
+ return executor();
1291
+ }
1292
+ };
1293
+ async function execute(flow$1, input, options) {
1294
+ const scope = options?.scope || createScope();
1295
+ const shouldDisposeScope = !options?.scope;
1296
+ const pod = scope.pod(...options?.presets || []);
1175
1297
  try {
1176
- const validatedInput = validate(flow.input, input);
1177
- let execution = async () => flow.execution(validatedInput, controller);
1178
- for (let i = context.plugins.length - 1; i >= 0; i--) {
1179
- const plugin$1 = context.plugins[i];
1180
- const prevExecution = execution;
1181
- execution = () => plugin$1.wrap(context, prevExecution);
1298
+ const context = new FlowContext(input, pod, options?.plugins || []);
1299
+ if (options?.initialContext) {
1300
+ if (Array.isArray(options.initialContext)) for (const [accessor$1, value] of options.initialContext) accessor$1.set(context, value);
1301
+ else if (options.initialContext instanceof Map) for (const [key, value] of options.initialContext) context.set(key, value);
1182
1302
  }
1183
- const result = await execution();
1184
- executionResult = {
1185
- kind: "success",
1186
- value: result
1303
+ for (const plugin$1 of options?.plugins || []) await plugin$1.init?.(pod, context);
1304
+ const executeCore = async () => {
1305
+ const handler = await pod.resolve(flow$1);
1306
+ const validated = validate(handler.def.input, input);
1307
+ context.input = validated;
1308
+ context.initializeExecutionContext(handler.def.name, false);
1309
+ const result = await handler(context);
1310
+ if (result.type === "ok") validate(handler.def.success, result.data);
1311
+ else validate(handler.def.error, result.data);
1312
+ return result;
1187
1313
  };
1188
- } catch (error) {
1189
- const wrappedError = error instanceof FlowError ? error : new FlowError(error instanceof Error ? error.message : "Flow execution failed", { cause: error });
1190
- executionResult = {
1191
- kind: "error",
1192
- error: wrappedError
1193
- };
1194
- }
1195
- if (isOwnScope) await scope.dispose();
1196
- else await scope.disposePod(pod);
1197
- return {
1198
- context: flowWithContext.context,
1199
- result: executionResult
1200
- };
1201
- }
1202
-
1203
- //#endregion
1204
- //#region src/data-accessor.ts
1205
- const data = (key, schema) => {
1206
- const _key = typeof key === "string" ? Symbol(key) : key;
1207
- return {
1208
- key: _key,
1209
- get(store) {
1210
- const value = store.get(_key);
1211
- if (value === void 0) throw new Error(`Data not found for key: ${String(key)}`);
1212
- return validate(schema, value);
1213
- },
1214
- find(store) {
1215
- const maybeValue = store.get(_key);
1216
- if (maybeValue) return validate(schema, maybeValue);
1217
- return void 0;
1218
- },
1219
- set(store, value) {
1220
- const validated = validate(schema, value);
1221
- store.set(_key, validated);
1222
- },
1223
- preset(value) {
1224
- return [_key, value];
1314
+ let executor = executeCore;
1315
+ for (const plugin$1 of [...options?.plugins || []].reverse()) if (plugin$1.wrap) {
1316
+ const currentExecutor = executor;
1317
+ executor = () => plugin$1.wrap(context, currentExecutor);
1225
1318
  }
1226
- };
1227
- };
1228
-
1229
- //#endregion
1230
- //#region src/helpers.ts
1231
- async function resolves(scope, executors) {
1232
- const objectOutput = {};
1233
- const arrayOutput = [];
1234
- const isArray = Array.isArray(executors);
1235
- for (const [index, executor] of Object.entries(executors)) {
1236
- const target = !isExecutor(executor) ? executor.escape() : isLazyExecutor(executor) || isReactiveExecutor(executor) || isStaticExecutor(executor) ? executor.executor : executor;
1237
- const result$1 = await scope.resolve(target);
1238
- if (isArray) arrayOutput.push(result$1);
1239
- else Object.assign(objectOutput, { [index]: result$1 });
1319
+ return await executor();
1320
+ } finally {
1321
+ for (const plugin$1 of options?.plugins || []) await plugin$1.dispose?.(pod);
1322
+ await scope.disposePod(pod);
1323
+ if (shouldDisposeScope) await scope.dispose();
1240
1324
  }
1241
- const result = isArray ? arrayOutput : objectOutput;
1242
- return result;
1243
- }
1244
- function prepare(scope, executor) {
1245
- const fn = async () => {
1246
- return await scope.resolve(executor);
1247
- };
1248
- return Object.assign(fn, { escape: () => {
1249
- return executor;
1250
- } });
1251
- }
1252
- function adapt(scope, executor) {
1253
- const fn = async (...args) => {
1254
- const fn$1 = await scope.resolve(executor);
1255
- return await fn$1(...args);
1256
- };
1257
- return Object.assign(fn, { escape: () => {
1258
- return executor;
1259
- } });
1260
1325
  }
1261
1326
 
1262
1327
  //#endregion
@@ -1283,8 +1348,9 @@ exports.ErrorCodes = ErrorCodes;
1283
1348
  exports.ErrorMessages = ErrorMessages;
1284
1349
  exports.ExecutorResolutionError = ExecutorResolutionError;
1285
1350
  exports.FactoryExecutionError = FactoryExecutionError;
1286
- exports.FlowError = FlowError;
1351
+ exports.FlowExecutionContext = FlowExecutionContext;
1287
1352
  exports.SchemaError = SchemaError;
1353
+ exports.accessor = accessor;
1288
1354
  exports.adapt = adapt;
1289
1355
  exports.buildDependencyChain = buildDependencyChain;
1290
1356
  exports.collectFromGenerator = collectFromGenerator;
@@ -1293,17 +1359,12 @@ exports.createFactoryError = createFactoryError;
1293
1359
  exports.createScope = createScope;
1294
1360
  exports.createSystemError = createSystemError;
1295
1361
  exports.custom = custom;
1296
- exports.dataAccessor = data;
1297
1362
  exports.derive = derive;
1363
+ exports.execute = execute;
1298
1364
  exports.executorSymbol = executorSymbol;
1299
1365
  exports.findValue = findValue;
1300
1366
  exports.findValues = findValues;
1301
- Object.defineProperty(exports, 'flow', {
1302
- enumerable: true,
1303
- get: function () {
1304
- return flow_exports;
1305
- }
1306
- });
1367
+ exports.flow = flow;
1307
1368
  exports.formatErrorMessage = formatErrorMessage;
1308
1369
  exports.getExecutorName = getExecutorName;
1309
1370
  exports.getValue = getValue;
@@ -1339,5 +1400,4 @@ exports.preset = preset;
1339
1400
  exports.processGenerator = processGenerator;
1340
1401
  exports.provide = provide;
1341
1402
  exports.resolves = resolves;
1342
- exports.validate = validate;
1343
- exports.validateAsync = validateAsync;
1403
+ exports.validate = validate;