@flowgram.ai/variable-core 0.1.0-alpha.10

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.
@@ -0,0 +1,2218 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __decorateClass = (decorators, target, key, kind) => {
4
+ var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
5
+ for (var i = decorators.length - 1, decorator; i >= 0; i--)
6
+ if (decorator = decorators[i])
7
+ result = (kind ? decorator(target, key, result) : decorator(result)) || result;
8
+ if (kind && result) __defProp(target, key, result);
9
+ return result;
10
+ };
11
+ var __decorateParam = (index, decorator) => (target, key) => decorator(target, key, index);
12
+
13
+ // src/variable-container-module.ts
14
+ import { ContainerModule } from "inversify";
15
+
16
+ // src/variable-engine.ts
17
+ import { Subject as Subject5 } from "rxjs";
18
+ import { inject as inject2, injectable as injectable3, preDestroy } from "inversify";
19
+ import { Disposable as Disposable4, DisposableCollection as DisposableCollection5 } from "@flowgram.ai/utils";
20
+ import { Emitter as Emitter3 } from "@flowgram.ai/utils";
21
+
22
+ // src/utils/toDisposable.tsx
23
+ import { Disposable } from "@flowgram.ai/utils";
24
+ function subsToDisposable(subscription) {
25
+ return Disposable.create(() => subscription.unsubscribe());
26
+ }
27
+
28
+ // src/utils/memo.ts
29
+ var createMemo = () => {
30
+ const _memoCache = /* @__PURE__ */ new Map();
31
+ const memo = (key, fn) => {
32
+ if (_memoCache.has(key)) {
33
+ return _memoCache.get(key);
34
+ }
35
+ const data = fn();
36
+ _memoCache.set(key, data);
37
+ return data;
38
+ };
39
+ const clear = (key) => {
40
+ if (key) {
41
+ _memoCache.delete(key);
42
+ } else {
43
+ _memoCache.clear();
44
+ }
45
+ };
46
+ memo.clear = clear;
47
+ return memo;
48
+ };
49
+
50
+ // src/scope/variable-table.ts
51
+ import { Subject, merge, share, skip, switchMap } from "rxjs";
52
+ import { DisposableCollection, Emitter } from "@flowgram.ai/utils";
53
+ var VariableTable = class {
54
+ constructor(parentTable) {
55
+ this.parentTable = parentTable;
56
+ this.table = /* @__PURE__ */ new Map();
57
+ this.onDataChangeEmitter = new Emitter();
58
+ this.variables$ = new Subject();
59
+ // 监听变量列表中的单个变量变化
60
+ this.anyVariableChange$ = this.variables$.pipe(
61
+ switchMap(
62
+ (_variables) => merge(
63
+ ..._variables.map(
64
+ (_v) => _v.value$.pipe(
65
+ // 跳过 BehaviorSubject 第一个
66
+ skip(1)
67
+ )
68
+ )
69
+ )
70
+ ),
71
+ share()
72
+ );
73
+ /**
74
+ * @deprecated use onListOrAnyVarChange instead
75
+ */
76
+ this.onDataChange = this.onDataChangeEmitter.event;
77
+ this._version = 0;
78
+ }
79
+ /**
80
+ * listen to any variable update in list
81
+ * @param observer
82
+ * @returns
83
+ */
84
+ onAnyVariableChange(observer) {
85
+ return subsToDisposable(this.anyVariableChange$.subscribe(observer));
86
+ }
87
+ /**
88
+ * listen to variable list change
89
+ * @param observer
90
+ * @returns
91
+ */
92
+ onVariableListChange(observer) {
93
+ return subsToDisposable(this.variables$.subscribe(observer));
94
+ }
95
+ /**
96
+ * listen to variable list change + any variable update in list
97
+ * @param observer
98
+ */
99
+ onListOrAnyVarChange(observer) {
100
+ const disposables = new DisposableCollection();
101
+ disposables.pushAll([this.onVariableListChange(observer), this.onAnyVariableChange(observer)]);
102
+ return disposables;
103
+ }
104
+ fireChange() {
105
+ this._version++;
106
+ this.onDataChangeEmitter.fire();
107
+ this.variables$.next(this.variables);
108
+ this.parentTable?.fireChange();
109
+ }
110
+ get version() {
111
+ return this._version;
112
+ }
113
+ get variables() {
114
+ return Array.from(this.table.values());
115
+ }
116
+ get variableKeys() {
117
+ return Array.from(this.table.keys());
118
+ }
119
+ /**
120
+ * 根据 keyPath 找到对应的变量,或 Property 节点
121
+ * @param keyPath
122
+ * @returns
123
+ */
124
+ getByKeyPath(keyPath) {
125
+ const [variableKey, ...propertyKeys] = keyPath || [];
126
+ if (!variableKey) {
127
+ return;
128
+ }
129
+ const variable = this.getVariableByKey(variableKey);
130
+ return propertyKeys.length ? variable?.getByKeyPath(propertyKeys) : variable;
131
+ }
132
+ /**
133
+ * 根据 key 值找到相应的变量
134
+ * @param key
135
+ * @returns
136
+ */
137
+ getVariableByKey(key) {
138
+ return this.table.get(key);
139
+ }
140
+ /**
141
+ * 往 variableTable 添加输出变量
142
+ * @param variable
143
+ */
144
+ addVariableToTable(variable) {
145
+ this.table.set(variable.key, variable);
146
+ if (this.parentTable) {
147
+ this.parentTable.addVariableToTable(variable);
148
+ }
149
+ }
150
+ /**
151
+ * 从 variableTable 中移除变量
152
+ * @param key
153
+ */
154
+ removeVariableFromTable(key) {
155
+ this.table.delete(key);
156
+ if (this.parentTable) {
157
+ this.parentTable.removeVariableFromTable(key);
158
+ }
159
+ }
160
+ dispose() {
161
+ this.variableKeys.forEach(
162
+ (_key) => this.parentTable?.removeVariableFromTable(_key)
163
+ );
164
+ this.parentTable?.fireChange();
165
+ this.variables$.complete();
166
+ this.variables$.unsubscribe();
167
+ this.onDataChangeEmitter.dispose();
168
+ }
169
+ };
170
+
171
+ // src/scope/scope-chain.ts
172
+ import { inject, injectable } from "inversify";
173
+ import { DisposableCollection as DisposableCollection2 } from "@flowgram.ai/utils";
174
+
175
+ // src/providers.ts
176
+ var VariableEngineProvider = Symbol("DynamicVariableEngine");
177
+ var ContainerProvider = Symbol("ContainerProvider");
178
+
179
+ // src/scope/scope-chain.ts
180
+ var ScopeChain = class {
181
+ constructor() {
182
+ this.toDispose = new DisposableCollection2();
183
+ }
184
+ get variableEngine() {
185
+ return this.variableEngineProvider();
186
+ }
187
+ /**
188
+ * 所有作用域依赖关系刷新
189
+ */
190
+ refreshAllChange() {
191
+ this.variableEngine.getAllScopes().forEach((_scope) => {
192
+ _scope.refreshCovers();
193
+ _scope.refreshDeps();
194
+ });
195
+ }
196
+ dispose() {
197
+ this.toDispose.dispose();
198
+ }
199
+ get disposed() {
200
+ return this.toDispose.disposed;
201
+ }
202
+ get onDispose() {
203
+ return this.toDispose.onDispose;
204
+ }
205
+ };
206
+ __decorateClass([
207
+ inject(VariableEngineProvider)
208
+ ], ScopeChain.prototype, "variableEngineProvider", 2);
209
+ ScopeChain = __decorateClass([
210
+ injectable()
211
+ ], ScopeChain);
212
+
213
+ // src/scope/scope.ts
214
+ import { DisposableCollection as DisposableCollection4 } from "@flowgram.ai/utils";
215
+
216
+ // src/ast/types.ts
217
+ var ASTKind = /* @__PURE__ */ ((ASTKind2) => {
218
+ ASTKind2["String"] = "String";
219
+ ASTKind2["Number"] = "Number";
220
+ ASTKind2["Integer"] = "Integer";
221
+ ASTKind2["Boolean"] = "Boolean";
222
+ ASTKind2["Object"] = "Object";
223
+ ASTKind2["Array"] = "Array";
224
+ ASTKind2["Map"] = "Map";
225
+ ASTKind2["Union"] = "Union";
226
+ ASTKind2["Any"] = "Any";
227
+ ASTKind2["CustomType"] = "CustomType";
228
+ ASTKind2["Property"] = "Property";
229
+ ASTKind2["VariableDeclaration"] = "VariableDeclaration";
230
+ ASTKind2["VariableDeclarationList"] = "VariableDeclarationList";
231
+ ASTKind2["KeyPathExpression"] = "KeyPathExpression";
232
+ ASTKind2["EnumerateExpression"] = "EnumerateExpression";
233
+ ASTKind2["WrapArrayExpression"] = "WrapArrayExpression";
234
+ ASTKind2["ListNode"] = "ListNode";
235
+ ASTKind2["DataNode"] = "DataNode";
236
+ ASTKind2["MapNode"] = "MapNode";
237
+ return ASTKind2;
238
+ })(ASTKind || {});
239
+
240
+ // src/ast/ast-registers.ts
241
+ import { omit } from "lodash";
242
+ import { injectable as injectable2 } from "inversify";
243
+
244
+ // src/ast/utils/inversify.ts
245
+ var injectToAST = (serviceIdentifier) => function(target, propertyKey) {
246
+ if (!serviceIdentifier) {
247
+ throw new Error(
248
+ `ServiceIdentifier ${serviceIdentifier} in @lazyInject is Empty, it might be caused by file circular dependency, please check it.`
249
+ );
250
+ }
251
+ const descriptor = {
252
+ get() {
253
+ const container = this.scope.variableEngine.container;
254
+ return container.get(serviceIdentifier);
255
+ },
256
+ set() {
257
+ },
258
+ configurable: true,
259
+ enumerable: true
260
+ };
261
+ return descriptor;
262
+ };
263
+ var POST_CONSTRUCT_AST_SYMBOL = Symbol("post_construct_ast");
264
+ var postConstructAST = () => (target, propertyKey) => {
265
+ if (!Reflect.hasMetadata(POST_CONSTRUCT_AST_SYMBOL, target)) {
266
+ Reflect.defineMetadata(POST_CONSTRUCT_AST_SYMBOL, propertyKey, target);
267
+ } else {
268
+ throw Error("Duplication Post Construct AST");
269
+ }
270
+ };
271
+
272
+ // src/ast/match.ts
273
+ var ASTMatch;
274
+ ((ASTMatch2) => {
275
+ ASTMatch2.isString = (node) => node?.kind === "String" /* String */;
276
+ ASTMatch2.isNumber = (node) => node?.kind === "Number" /* Number */;
277
+ ASTMatch2.isBoolean = (node) => node?.kind === "Boolean" /* Boolean */;
278
+ ASTMatch2.isInteger = (node) => node?.kind === "Integer" /* Integer */;
279
+ ASTMatch2.isObject = (node) => node?.kind === "Object" /* Object */;
280
+ ASTMatch2.isArray = (node) => node?.kind === "Array" /* Array */;
281
+ ASTMatch2.isMap = (node) => node?.kind === "Map" /* Map */;
282
+ ASTMatch2.isCustomType = (node) => node?.kind === "CustomType" /* CustomType */;
283
+ ASTMatch2.isVariableDeclaration = (node) => node?.kind === "VariableDeclaration" /* VariableDeclaration */;
284
+ ASTMatch2.isProperty = (node) => node?.kind === "Property" /* Property */;
285
+ ASTMatch2.isVariableDeclarationList = (node) => node?.kind === "VariableDeclarationList" /* VariableDeclarationList */;
286
+ ASTMatch2.isEnumerateExpression = (node) => node?.kind === "EnumerateExpression" /* EnumerateExpression */;
287
+ ASTMatch2.isKeyPathExpression = (node) => node?.kind === "KeyPathExpression" /* KeyPathExpression */;
288
+ function is(node, targetType) {
289
+ return node?.kind === targetType?.kind;
290
+ }
291
+ ASTMatch2.is = is;
292
+ })(ASTMatch || (ASTMatch = {}));
293
+
294
+ // src/ast/utils/helpers.ts
295
+ function updateChildNodeHelper({
296
+ getChildNode,
297
+ updateChildNode,
298
+ removeChildNode,
299
+ nextJSON
300
+ }) {
301
+ const currNode = getChildNode();
302
+ const isNewKind = currNode?.kind !== nextJSON?.kind;
303
+ const isNewKey = nextJSON?.key && nextJSON?.key !== currNode?.key;
304
+ if (isNewKind || isNewKey) {
305
+ if (currNode) {
306
+ currNode.dispose();
307
+ removeChildNode();
308
+ }
309
+ if (nextJSON) {
310
+ const newNode = this.createChildNode(nextJSON);
311
+ updateChildNode(newNode);
312
+ this.fireChange();
313
+ return newNode;
314
+ } else {
315
+ this.fireChange();
316
+ }
317
+ } else if (nextJSON) {
318
+ currNode?.fromJSON(nextJSON);
319
+ }
320
+ return currNode;
321
+ }
322
+ function parseTypeJsonOrKind(typeJSONOrKind) {
323
+ return typeof typeJSONOrKind === "string" ? { kind: typeJSONOrKind } : typeJSONOrKind;
324
+ }
325
+ function getAllChildren(ast) {
326
+ return [...ast.children, ...ast.children.map((_child) => getAllChildren(_child)).flat()];
327
+ }
328
+ function isMatchAST(node, targetType) {
329
+ return ASTMatch.is(node, targetType);
330
+ }
331
+
332
+ // src/ast/flags.ts
333
+ var ASTNodeFlags = /* @__PURE__ */ ((ASTNodeFlags2) => {
334
+ ASTNodeFlags2[ASTNodeFlags2["None"] = 0] = "None";
335
+ ASTNodeFlags2[ASTNodeFlags2["VariableField"] = 1] = "VariableField";
336
+ ASTNodeFlags2[ASTNodeFlags2["Expression"] = 4] = "Expression";
337
+ ASTNodeFlags2[ASTNodeFlags2["BasicType"] = 8] = "BasicType";
338
+ ASTNodeFlags2[ASTNodeFlags2["DrilldownType"] = 16] = "DrilldownType";
339
+ ASTNodeFlags2[ASTNodeFlags2["EnumerateType"] = 32] = "EnumerateType";
340
+ ASTNodeFlags2[ASTNodeFlags2["UnionType"] = 64] = "UnionType";
341
+ ASTNodeFlags2[ASTNodeFlags2["VariableType"] = 120] = "VariableType";
342
+ return ASTNodeFlags2;
343
+ })(ASTNodeFlags || {});
344
+
345
+ // src/ast/ast-node.ts
346
+ import {
347
+ BehaviorSubject,
348
+ animationFrameScheduler,
349
+ debounceTime,
350
+ distinctUntilChanged,
351
+ map,
352
+ skip as skip2,
353
+ tap
354
+ } from "rxjs";
355
+ import { nanoid } from "nanoid";
356
+ import { shallowEqual } from "fast-equals";
357
+ import { Disposable as Disposable2, DisposableCollection as DisposableCollection3 } from "@flowgram.ai/utils";
358
+ var ASTNode = class _ASTNode {
359
+ /**
360
+ * 构造函数
361
+ * @param createParams 创建 ASTNode 的必要参数
362
+ * @param injectOptions 依赖注入各种模块
363
+ */
364
+ constructor({ key, parent, scope }, opts) {
365
+ /**
366
+ * 节点 Flags,记录一些 Flag 信息
367
+ */
368
+ this.flags = 0 /* None */;
369
+ /**
370
+ * 节点的版本号,每 fireChange 一次 version + 1
371
+ */
372
+ this._version = 0;
373
+ /**
374
+ * 更新锁
375
+ */
376
+ this.changeLocked = false;
377
+ /**
378
+ * Batch Update 相关参数
379
+ */
380
+ this._batch = {
381
+ batching: false,
382
+ hasChangesInBatch: false
383
+ };
384
+ /**
385
+ * AST 节点变化事件,基于 Rxjs 实现
386
+ * - 使用了 BehaviorSubject, 在订阅时会自动触发一次事件,事件为当前值
387
+ */
388
+ this.value$ = new BehaviorSubject(this);
389
+ /**
390
+ * 子节点
391
+ */
392
+ this._children = /* @__PURE__ */ new Set();
393
+ /**
394
+ * 删除节点处理事件列表
395
+ */
396
+ this.toDispose = new DisposableCollection3(
397
+ Disposable2.create(() => {
398
+ this.parent?.fireChange();
399
+ this.children.forEach((child) => child.dispose());
400
+ })
401
+ );
402
+ /**
403
+ * 销毁时触发的回调
404
+ */
405
+ this.onDispose = this.toDispose.onDispose;
406
+ this.scope = scope;
407
+ this.parent = parent;
408
+ this.opts = opts;
409
+ this.key = key || nanoid();
410
+ this.fromJSON = this.withBatchUpdate(this.fromJSON.bind(this));
411
+ this.dispatchGlobalEvent({ type: "NewAST" });
412
+ }
413
+ /**
414
+ * AST 节点的类型
415
+ */
416
+ get kind() {
417
+ if (!this.constructor.kind) {
418
+ throw new Error(`ASTNode Registry need a kind: ${this.constructor.name}`);
419
+ }
420
+ return this.constructor.kind;
421
+ }
422
+ /**
423
+ * 获取当前节点所有子节点
424
+ */
425
+ get children() {
426
+ return Array.from(this._children);
427
+ }
428
+ /**
429
+ * 转化为 ASTNodeJSON
430
+ * @returns
431
+ */
432
+ toJSON() {
433
+ console.warn("[VariableEngine] Please Implement toJSON method for " + this.kind);
434
+ return {
435
+ kind: this.kind
436
+ };
437
+ }
438
+ /**
439
+ * 创建子节点
440
+ * @param json 子节点的 AST JSON
441
+ * @returns
442
+ */
443
+ createChildNode(json) {
444
+ const astRegisters = this.scope.variableEngine.astRegisters;
445
+ const child = astRegisters.createAST(json, {
446
+ parent: this,
447
+ scope: this.scope
448
+ });
449
+ this._children.add(child);
450
+ child.toDispose.push(
451
+ Disposable2.create(() => {
452
+ this._children.delete(child);
453
+ })
454
+ );
455
+ return child;
456
+ }
457
+ /**
458
+ * 更新子节点,快速实现子节点更新消费逻辑
459
+ * @param keyInThis 当前对象上的指定 key
460
+ */
461
+ updateChildNodeByKey(keyInThis, nextJSON) {
462
+ this.withBatchUpdate(updateChildNodeHelper).call(this, {
463
+ getChildNode: () => this[keyInThis],
464
+ updateChildNode: (_node) => this[keyInThis] = _node,
465
+ removeChildNode: () => this[keyInThis] = void 0,
466
+ nextJSON
467
+ });
468
+ }
469
+ /**
470
+ * 批处理更新,批处理函数内所有的 fireChange 都合并成一个
471
+ * @param updater 批处理函数
472
+ * @returns
473
+ */
474
+ withBatchUpdate(updater) {
475
+ return (...args) => {
476
+ if (this._batch.batching) {
477
+ return updater.call(this, ...args);
478
+ }
479
+ this._batch.hasChangesInBatch = false;
480
+ this._batch.batching = true;
481
+ const res = updater.call(this, ...args);
482
+ this._batch.batching = false;
483
+ if (this._batch.hasChangesInBatch) {
484
+ this.fireChange();
485
+ }
486
+ this._batch.hasChangesInBatch = false;
487
+ return res;
488
+ };
489
+ }
490
+ /**
491
+ * 触发当前节点更新
492
+ */
493
+ fireChange() {
494
+ if (this.changeLocked || this.disposed) {
495
+ return;
496
+ }
497
+ if (this._batch.batching) {
498
+ this._batch.hasChangesInBatch = true;
499
+ return;
500
+ }
501
+ this._version++;
502
+ this.value$.next(this);
503
+ this.dispatchGlobalEvent({ type: "UpdateAST" });
504
+ this.parent?.fireChange();
505
+ }
506
+ /**
507
+ * 节点的版本值
508
+ * - 通过 NodeA === NodeB && versionA === versionB 可以比较两者是否相等
509
+ */
510
+ get version() {
511
+ return this._version;
512
+ }
513
+ /**
514
+ * 节点唯一 hash 值
515
+ */
516
+ get hash() {
517
+ return `${this._version}${this.kind}${this.key}`;
518
+ }
519
+ /**
520
+ * 监听 AST 节点的变化
521
+ * @param observer 监听回调
522
+ * @param selector 监听指定数据
523
+ * @returns
524
+ */
525
+ subscribe(observer, { selector, debounceAnimation, triggerOnInit } = {}) {
526
+ return subsToDisposable(
527
+ this.value$.pipe(
528
+ map(() => selector ? selector(this) : this),
529
+ distinctUntilChanged(
530
+ (a, b) => shallowEqual(a, b),
531
+ (value) => {
532
+ if (value instanceof _ASTNode) {
533
+ return value.hash;
534
+ }
535
+ return value;
536
+ }
537
+ ),
538
+ // 默认跳过 BehaviorSubject 第一次触发
539
+ triggerOnInit ? tap(() => null) : skip2(1),
540
+ // 每个 animationFrame 内所有更新合并成一个
541
+ debounceAnimation ? debounceTime(0, animationFrameScheduler) : tap(() => null)
542
+ ).subscribe(observer)
543
+ );
544
+ }
545
+ dispatchGlobalEvent(event) {
546
+ this.scope.event.dispatch({
547
+ ...event,
548
+ ast: this
549
+ });
550
+ }
551
+ /**
552
+ * 销毁
553
+ */
554
+ dispose() {
555
+ if (this.toDispose.disposed) {
556
+ return;
557
+ }
558
+ this.toDispose.dispose();
559
+ this.dispatchGlobalEvent({ type: "DisposeAST" });
560
+ this.value$.complete();
561
+ this.value$.unsubscribe();
562
+ }
563
+ get disposed() {
564
+ return this.toDispose.disposed;
565
+ }
566
+ };
567
+
568
+ // src/ast/type/base-type.ts
569
+ var BaseType = class extends ASTNode {
570
+ constructor() {
571
+ super(...arguments);
572
+ this.flags = 8 /* BasicType */;
573
+ }
574
+ /**
575
+ * 类型是否一致
576
+ * @param targetTypeJSON
577
+ */
578
+ isTypeEqual(targetTypeJSONOrKind) {
579
+ const targetTypeJSON = parseTypeJsonOrKind(targetTypeJSONOrKind);
580
+ if (targetTypeJSON?.kind === "Union" /* Union */) {
581
+ return (targetTypeJSON?.types || [])?.some(
582
+ (_subType) => this.isTypeEqual(_subType)
583
+ );
584
+ }
585
+ return this.kind === targetTypeJSON?.kind;
586
+ }
587
+ /**
588
+ * 可下钻类型需实现
589
+ * @param keyPath
590
+ */
591
+ getByKeyPath(keyPath = []) {
592
+ throw new Error(`Get By Key Path is not implemented for Type: ${this.kind}`);
593
+ }
594
+ /**
595
+ * Get AST JSON for current base type
596
+ * @returns
597
+ */
598
+ toJSON() {
599
+ return {
600
+ kind: this.kind
601
+ };
602
+ }
603
+ };
604
+
605
+ // src/ast/type/array.ts
606
+ var ArrayType = class extends BaseType {
607
+ constructor() {
608
+ super(...arguments);
609
+ this.flags = 16 /* DrilldownType */ | 32 /* EnumerateType */;
610
+ }
611
+ fromJSON({ items }) {
612
+ this.updateChildNodeByKey("items", parseTypeJsonOrKind(items));
613
+ }
614
+ // items 类型是否可下钻
615
+ get canDrilldownItems() {
616
+ return !!(this.items?.flags & 16 /* DrilldownType */);
617
+ }
618
+ getByKeyPath(keyPath) {
619
+ const [curr, ...rest] = keyPath || [];
620
+ if (curr === "0" && this.canDrilldownItems) {
621
+ return this.items.getByKeyPath(rest);
622
+ }
623
+ return void 0;
624
+ }
625
+ isTypeEqual(targetTypeJSONOrKind) {
626
+ const targetTypeJSON = parseTypeJsonOrKind(targetTypeJSONOrKind);
627
+ const isSuperEqual = super.isTypeEqual(targetTypeJSONOrKind);
628
+ if (targetTypeJSON?.weak || targetTypeJSON?.kind === "Union" /* Union */) {
629
+ return isSuperEqual;
630
+ }
631
+ return targetTypeJSON && isSuperEqual && // 弱比较,只需要比较 Kind 即可
632
+ (targetTypeJSON?.weak || this.customStrongEqual(targetTypeJSON));
633
+ }
634
+ /**
635
+ * Array 强比较
636
+ * @param targetTypeJSON
637
+ * @returns
638
+ */
639
+ customStrongEqual(targetTypeJSON) {
640
+ if (!this.items) {
641
+ return !targetTypeJSON?.items;
642
+ }
643
+ return this.items?.isTypeEqual(targetTypeJSON.items);
644
+ }
645
+ toJSON() {
646
+ return {
647
+ kind: "Array" /* Array */,
648
+ items: this.items?.toJSON()
649
+ };
650
+ }
651
+ };
652
+ ArrayType.kind = "Array" /* Array */;
653
+
654
+ // src/ast/type/string.ts
655
+ var StringType = class extends BaseType {
656
+ constructor() {
657
+ super(...arguments);
658
+ this.flags = 8 /* BasicType */;
659
+ }
660
+ fromJSON() {
661
+ }
662
+ };
663
+ StringType.kind = "String" /* String */;
664
+
665
+ // src/ast/type/integer.ts
666
+ var IntegerType = class extends BaseType {
667
+ constructor() {
668
+ super(...arguments);
669
+ this.flags = 8 /* BasicType */;
670
+ }
671
+ fromJSON() {
672
+ }
673
+ };
674
+ IntegerType.kind = "Integer" /* Integer */;
675
+
676
+ // src/ast/type/boolean.ts
677
+ var BooleanType = class extends BaseType {
678
+ fromJSON() {
679
+ }
680
+ };
681
+ BooleanType.kind = "Boolean" /* Boolean */;
682
+
683
+ // src/ast/type/number.ts
684
+ var NumberType = class extends BaseType {
685
+ fromJSON() {
686
+ }
687
+ };
688
+ NumberType.kind = "Number" /* Number */;
689
+
690
+ // src/ast/type/map.ts
691
+ var MapType = class extends BaseType {
692
+ fromJSON({ keyType = "String" /* String */, valueType }) {
693
+ this.updateChildNodeByKey("keyType", parseTypeJsonOrKind(keyType));
694
+ this.updateChildNodeByKey("valueType", parseTypeJsonOrKind(valueType));
695
+ }
696
+ // Value 类型是否可下钻,后续实现
697
+ // get canDrilldownValue(): boolean {
698
+ // return !!(this.valueType.flags & ASTNodeFlags.DrilldownType);
699
+ // }
700
+ // getByKeyPath(keyPath: string[]): BaseVariableField | undefined {
701
+ // const [curr, ...rest] = keyPath || [];
702
+ // if (curr === '*' && this.canDrilldownValue) {
703
+ // return this.valueType.getByKeyPath(rest);
704
+ // }
705
+ // return undefined;
706
+ // }
707
+ isTypeEqual(targetTypeJSONOrKind) {
708
+ const targetTypeJSON = parseTypeJsonOrKind(targetTypeJSONOrKind);
709
+ const isSuperEqual = super.isTypeEqual(targetTypeJSONOrKind);
710
+ if (targetTypeJSON?.weak || targetTypeJSON?.kind === "Union" /* Union */) {
711
+ return isSuperEqual;
712
+ }
713
+ return targetTypeJSON && isSuperEqual && // 弱比较,只需要比较 Kind 即可
714
+ (targetTypeJSON?.weak || this.customStrongEqual(targetTypeJSON));
715
+ }
716
+ /**
717
+ * Map 强比较
718
+ * @param targetTypeJSON
719
+ * @returns
720
+ */
721
+ customStrongEqual(targetTypeJSON) {
722
+ const { keyType = "String" /* String */, valueType } = targetTypeJSON;
723
+ const isValueTypeEqual = !valueType && !this.valueType || this.valueType?.isTypeEqual(valueType);
724
+ return isValueTypeEqual && this.keyType?.isTypeEqual(keyType);
725
+ }
726
+ toJSON() {
727
+ return {
728
+ kind: "Map" /* Map */,
729
+ keyType: this.keyType?.toJSON(),
730
+ valueType: this.valueType?.toJSON()
731
+ };
732
+ }
733
+ };
734
+ // public flags: ASTNodeFlags = ASTNodeFlags.DrilldownType | ASTNodeFlags.EnumerateType;
735
+ MapType.kind = "Map" /* Map */;
736
+
737
+ // src/ast/type/object.ts
738
+ import { xor } from "lodash";
739
+ var ObjectType = class extends BaseType {
740
+ constructor() {
741
+ super(...arguments);
742
+ this.flags = 16 /* DrilldownType */;
743
+ this.propertyTable = /* @__PURE__ */ new Map();
744
+ }
745
+ fromJSON({ properties }) {
746
+ const removedKeys = new Set(this.propertyTable.keys());
747
+ const prev = [...this.properties || []];
748
+ this.properties = (properties || []).map((property) => {
749
+ const existProperty = this.propertyTable.get(property.key);
750
+ removedKeys.delete(property.key);
751
+ if (existProperty) {
752
+ existProperty.fromJSON(property);
753
+ return existProperty;
754
+ } else {
755
+ const newProperty = this.createChildNode({
756
+ ...property,
757
+ kind: "Property" /* Property */
758
+ });
759
+ this.fireChange();
760
+ this.propertyTable.set(property.key, newProperty);
761
+ return newProperty;
762
+ }
763
+ });
764
+ removedKeys.forEach((key) => {
765
+ const property = this.propertyTable.get(key);
766
+ property?.dispose();
767
+ this.propertyTable.delete(key);
768
+ this.fireChange();
769
+ });
770
+ this.dispatchGlobalEvent({
771
+ type: "ObjectPropertiesChange",
772
+ payload: {
773
+ prev,
774
+ next: [...this.properties]
775
+ }
776
+ });
777
+ }
778
+ toJSON() {
779
+ return {
780
+ kind: "Object" /* Object */,
781
+ properties: this.properties.map((_property) => _property.toJSON())
782
+ };
783
+ }
784
+ /**
785
+ * 根据 KeyPath 找到对应的变量
786
+ * @param keyPath 变量路径
787
+ * @returns
788
+ */
789
+ getByKeyPath(keyPath) {
790
+ const [curr, ...restKeyPath] = keyPath;
791
+ const property = this.propertyTable.get(curr);
792
+ if (!restKeyPath.length) {
793
+ return property;
794
+ }
795
+ if (property?.type && property?.type?.flags & 16 /* DrilldownType */) {
796
+ return property.type.getByKeyPath(restKeyPath);
797
+ }
798
+ return void 0;
799
+ }
800
+ isTypeEqual(targetTypeJSONOrKind) {
801
+ const targetTypeJSON = parseTypeJsonOrKind(targetTypeJSONOrKind);
802
+ const isSuperEqual = super.isTypeEqual(targetTypeJSONOrKind);
803
+ if (targetTypeJSON?.weak || targetTypeJSON?.kind === "Union" /* Union */) {
804
+ return isSuperEqual;
805
+ }
806
+ return targetTypeJSON && isSuperEqual && // 弱比较,只需要比较 Kind 即可
807
+ (targetTypeJSON?.weak || this.customStrongEqual(targetTypeJSON));
808
+ }
809
+ /**
810
+ * Object 类型强比较
811
+ * @param targetTypeJSON
812
+ * @returns
813
+ */
814
+ customStrongEqual(targetTypeJSON) {
815
+ const targetProperties = targetTypeJSON.properties || [];
816
+ const sourcePropertyKeys = Array.from(this.propertyTable.keys());
817
+ const targetPropertyKeys = targetProperties.map((_target) => _target.key);
818
+ const isKeyStrongEqual = !xor(sourcePropertyKeys, targetPropertyKeys).length;
819
+ return isKeyStrongEqual && targetProperties.every((targetProperty) => {
820
+ const sourceProperty = this.propertyTable.get(targetProperty.key);
821
+ return sourceProperty && sourceProperty.key === targetProperty.key && sourceProperty.type?.isTypeEqual(targetProperty?.type);
822
+ });
823
+ }
824
+ };
825
+ ObjectType.kind = "Object" /* Object */;
826
+
827
+ // src/ast/type/custom-type.ts
828
+ var CustomType = class extends BaseType {
829
+ get typeName() {
830
+ return this._typeName;
831
+ }
832
+ fromJSON(json) {
833
+ if (this._typeName !== json.typeName) {
834
+ this._typeName = json.typeName;
835
+ this.fireChange();
836
+ }
837
+ }
838
+ isTypeEqual(targetTypeJSONOrKind) {
839
+ const targetTypeJSON = parseTypeJsonOrKind(targetTypeJSONOrKind);
840
+ if (targetTypeJSON?.kind === "Union" /* Union */) {
841
+ return (targetTypeJSON?.types || [])?.some(
842
+ (_subType) => this.isTypeEqual(_subType)
843
+ );
844
+ }
845
+ return targetTypeJSON?.kind === this.kind && targetTypeJSON?.typeName === this.typeName;
846
+ }
847
+ };
848
+ CustomType.kind = "CustomType" /* CustomType */;
849
+
850
+ // src/ast/expression/base-expression.ts
851
+ import {
852
+ distinctUntilChanged as distinctUntilChanged2,
853
+ map as map2,
854
+ switchMap as switchMap2,
855
+ combineLatest,
856
+ of,
857
+ Subject as Subject2,
858
+ share as share2
859
+ } from "rxjs";
860
+ import { shallowEqual as shallowEqual2 } from "fast-equals";
861
+
862
+ // src/ast/utils/variable-field.ts
863
+ function getParentFields(ast) {
864
+ let curr = ast.parent;
865
+ const res = [];
866
+ while (curr) {
867
+ if (curr.flags & 1 /* VariableField */) {
868
+ res.push(curr);
869
+ }
870
+ curr = curr.parent;
871
+ }
872
+ return res;
873
+ }
874
+
875
+ // src/ast/expression/base-expression.ts
876
+ var BaseExpression = class extends ASTNode {
877
+ constructor(params, opts) {
878
+ super(params, opts);
879
+ this.flags = 4 /* Expression */;
880
+ /**
881
+ * 引用变量
882
+ */
883
+ this._refs = [];
884
+ this.refreshRefs$ = new Subject2();
885
+ /**
886
+ * 监听引用变量变化
887
+ * 监听 [a.b.c] -> [a.b]
888
+ */
889
+ this.refs$ = this.refreshRefs$.pipe(
890
+ map2(() => this.getRefFields()),
891
+ distinctUntilChanged2(shallowEqual2),
892
+ switchMap2(
893
+ (refs) => !refs?.length ? of([]) : combineLatest(
894
+ refs.map(
895
+ (ref) => ref ? ref.value$ : of(void 0)
896
+ )
897
+ )
898
+ ),
899
+ share2()
900
+ );
901
+ this.toDispose.push(
902
+ subsToDisposable(
903
+ this.refs$.subscribe((_refs) => {
904
+ this._refs = _refs;
905
+ this.fireChange();
906
+ })
907
+ )
908
+ );
909
+ }
910
+ /**
911
+ * 获取全局变量表,方便表达式获取引用变量
912
+ */
913
+ get globalVariableTable() {
914
+ return this.scope.variableEngine.globalVariableTable;
915
+ }
916
+ /**
917
+ * 父变量字段,通过由近而远的方式进行排序
918
+ */
919
+ get parentFields() {
920
+ return getParentFields(this);
921
+ }
922
+ get refs() {
923
+ return this._refs;
924
+ }
925
+ /**
926
+ * 刷新变量引用
927
+ */
928
+ refreshRefs() {
929
+ this.refreshRefs$.next();
930
+ }
931
+ };
932
+
933
+ // src/ast/expression/keypath-expression.ts
934
+ import { shallowEqual as shallowEqual3 } from "fast-equals";
935
+ var KeyPathExpression = class extends BaseExpression {
936
+ constructor(params, opts) {
937
+ super(params, opts);
938
+ this._keyPath = [];
939
+ this.toDispose.pushAll([
940
+ // 可以用变量列表变化时候 (有新增或者删除时)
941
+ this.scope.available.onVariableListChange(() => {
942
+ this.refreshRefs();
943
+ }),
944
+ // this._keyPath 指向的可引用变量发生变化时,刷新引用数据
945
+ this.scope.available.onAnyVariableChange((_v) => {
946
+ if (_v.key === this._keyPath[0]) {
947
+ this.refreshRefs();
948
+ }
949
+ })
950
+ ]);
951
+ }
952
+ get keyPath() {
953
+ return this._keyPath;
954
+ }
955
+ getRefFields() {
956
+ const ref = this.scope.available.getByKeyPath(this._keyPath);
957
+ return ref ? [ref] : [];
958
+ }
959
+ get returnType() {
960
+ const [refNode] = this._refs || [];
961
+ if (refNode && refNode.flags & 1 /* VariableField */) {
962
+ return refNode.type;
963
+ }
964
+ return;
965
+ }
966
+ /**
967
+ * 业务重改该方法可快速定制自己的 Path 表达式
968
+ * - 只需要将业务的 Path 解析为变量系统的 KeyPath 即可
969
+ * @param json 业务定义的 Path 表达式
970
+ * @returns
971
+ */
972
+ parseToKeyPath(json) {
973
+ return json.keyPath;
974
+ }
975
+ fromJSON(json) {
976
+ const keyPath = this.parseToKeyPath(json);
977
+ if (!shallowEqual3(keyPath, this._keyPath)) {
978
+ this._keyPath = keyPath;
979
+ this.refreshRefs();
980
+ }
981
+ }
982
+ toJSON() {
983
+ return {
984
+ kind: "KeyPathExpression" /* KeyPathExpression */,
985
+ keyPath: this._keyPath
986
+ };
987
+ }
988
+ };
989
+ KeyPathExpression.kind = "KeyPathExpression" /* KeyPathExpression */;
990
+
991
+ // src/ast/expression/enumerate-expression.ts
992
+ var EnumerateExpression = class extends BaseExpression {
993
+ get enumerateFor() {
994
+ return this._enumerateFor;
995
+ }
996
+ get returnType() {
997
+ const childReturnType = this.enumerateFor?.returnType;
998
+ if (childReturnType?.kind === "Array" /* Array */) {
999
+ return childReturnType.items;
1000
+ }
1001
+ return void 0;
1002
+ }
1003
+ getRefFields() {
1004
+ return [];
1005
+ }
1006
+ fromJSON({ enumerateFor: expression }) {
1007
+ this.updateChildNodeByKey("_enumerateFor", expression);
1008
+ }
1009
+ toJSON() {
1010
+ return {
1011
+ kind: "EnumerateExpression" /* EnumerateExpression */,
1012
+ enumerateFor: this.enumerateFor?.toJSON()
1013
+ };
1014
+ }
1015
+ };
1016
+ EnumerateExpression.kind = "EnumerateExpression" /* EnumerateExpression */;
1017
+
1018
+ // src/ast/expression/keypath-expression-v2.ts
1019
+ import { shallowEqual as shallowEqual4 } from "fast-equals";
1020
+
1021
+ // src/ast/utils/expression.ts
1022
+ import { intersection } from "lodash";
1023
+ function getAllRefs(ast) {
1024
+ return getAllChildren(ast).filter((_child) => _child.flags & 4 /* Expression */).map((_child) => _child.refs).flat().filter(Boolean);
1025
+ }
1026
+ function checkRefCycle(curr, refNodes) {
1027
+ if (intersection(curr.scope.coverScopes, refNodes.map((_ref) => _ref?.scope).filter(Boolean)).length === 0) {
1028
+ return false;
1029
+ }
1030
+ const visited = /* @__PURE__ */ new Set();
1031
+ const queue = [...refNodes];
1032
+ while (queue.length) {
1033
+ const currNode = queue.shift();
1034
+ visited.add(currNode);
1035
+ for (const ref of getAllRefs(currNode).filter((_ref) => !visited.has(_ref))) {
1036
+ queue.push(ref);
1037
+ }
1038
+ }
1039
+ return intersection(Array.from(visited), getParentFields(curr)).length > 0;
1040
+ }
1041
+
1042
+ // src/ast/expression/keypath-expression-v2.ts
1043
+ var KeyPathExpressionV2 = class extends BaseExpression {
1044
+ constructor(params, opts) {
1045
+ super(params, opts);
1046
+ this._keyPath = [];
1047
+ this.toDispose.pushAll([
1048
+ // 可以用变量列表变化时候 (有新增或者删除时)
1049
+ this.scope.available.onVariableListChange(() => {
1050
+ this.refreshRefs();
1051
+ }),
1052
+ // this._keyPath 指向的可引用变量发生变化时,刷新引用数据
1053
+ this.scope.available.onAnyVariableChange((_v) => {
1054
+ if (_v.key === this._keyPath[0]) {
1055
+ this.refreshRefs();
1056
+ }
1057
+ }),
1058
+ subsToDisposable(
1059
+ this.refs$.subscribe((_type) => {
1060
+ const [ref] = this._refs;
1061
+ if (this.prevRefTypeHash !== ref?.type?.hash) {
1062
+ this.prevRefTypeHash = ref?.type?.hash;
1063
+ this.updateChildNodeByKey("_returnType", this.getReturnTypeJSONByRef(ref));
1064
+ }
1065
+ })
1066
+ )
1067
+ ]);
1068
+ }
1069
+ get keyPath() {
1070
+ return this._keyPath;
1071
+ }
1072
+ getRefFields() {
1073
+ const ref = this.scope.available.getByKeyPath(this._keyPath);
1074
+ if (checkRefCycle(this, [ref])) {
1075
+ console.warn(
1076
+ "[CustomKeyPathExpression] checkRefCycle: Reference Cycle Existed",
1077
+ this.parentFields.map((_field) => _field.key).reverse()
1078
+ );
1079
+ return [];
1080
+ }
1081
+ return ref ? [ref] : [];
1082
+ }
1083
+ get returnType() {
1084
+ return this._returnType;
1085
+ }
1086
+ /**
1087
+ * 业务重改该方法可快速定制自己的 Path 表达式
1088
+ * - 只需要将业务的 Path 解析为变量系统的 KeyPath 即可
1089
+ * @param json 业务定义的 Path 表达式
1090
+ * @returns
1091
+ */
1092
+ parseToKeyPath(json) {
1093
+ return json.keyPath;
1094
+ }
1095
+ fromJSON(json) {
1096
+ const keyPath = this.parseToKeyPath(json);
1097
+ if (!shallowEqual4(keyPath, this._keyPath)) {
1098
+ this._keyPath = keyPath;
1099
+ this.refreshRefs();
1100
+ }
1101
+ }
1102
+ getReturnTypeJSONByRef(_ref) {
1103
+ return _ref?.type?.toJSON();
1104
+ }
1105
+ toJSON() {
1106
+ return {
1107
+ kind: "KeyPathExpression" /* KeyPathExpression */,
1108
+ keyPath: this._keyPath
1109
+ };
1110
+ }
1111
+ };
1112
+ KeyPathExpressionV2.kind = "KeyPathExpression" /* KeyPathExpression */;
1113
+
1114
+ // src/ast/expression/wrap-array-expression.ts
1115
+ var WrapArrayExpression = class extends BaseExpression {
1116
+ get wrapFor() {
1117
+ return this._wrapFor;
1118
+ }
1119
+ get returnType() {
1120
+ return this._returnType;
1121
+ }
1122
+ refreshReturnType() {
1123
+ const childReturnTypeJSON = this.wrapFor?.returnType?.toJSON();
1124
+ this.updateChildNodeByKey("_returnType", {
1125
+ kind: "Array" /* Array */,
1126
+ items: childReturnTypeJSON
1127
+ });
1128
+ }
1129
+ getRefFields() {
1130
+ return [];
1131
+ }
1132
+ fromJSON({ wrapFor: expression }) {
1133
+ this.updateChildNodeByKey("_wrapFor", expression);
1134
+ }
1135
+ toJSON() {
1136
+ return {
1137
+ kind: "WrapArrayExpression" /* WrapArrayExpression */,
1138
+ wrapFor: this.wrapFor?.toJSON()
1139
+ };
1140
+ }
1141
+ init() {
1142
+ this.refreshReturnType = this.refreshReturnType.bind(this);
1143
+ this.toDispose.push(
1144
+ this.subscribe(this.refreshReturnType, {
1145
+ selector: (curr) => curr.wrapFor?.returnType,
1146
+ triggerOnInit: true
1147
+ })
1148
+ );
1149
+ }
1150
+ };
1151
+ WrapArrayExpression.kind = "WrapArrayExpression" /* WrapArrayExpression */;
1152
+ __decorateClass([
1153
+ postConstructAST()
1154
+ ], WrapArrayExpression.prototype, "init", 1);
1155
+
1156
+ // src/ast/declaration/base-variable-field.ts
1157
+ import { shallowEqual as shallowEqual5 } from "fast-equals";
1158
+ var BaseVariableField = class extends ASTNode {
1159
+ constructor() {
1160
+ super(...arguments);
1161
+ this.flags = 1 /* VariableField */;
1162
+ this._meta = {};
1163
+ }
1164
+ /**
1165
+ * 父变量字段,通过由近而远的方式进行排序
1166
+ */
1167
+ get parentFields() {
1168
+ return getParentFields(this);
1169
+ }
1170
+ get keyPath() {
1171
+ return [...this.parentFields.reverse().map((_field) => _field.key), this.key];
1172
+ }
1173
+ get meta() {
1174
+ return this._meta;
1175
+ }
1176
+ get type() {
1177
+ return this._initializer?.returnType || this._type;
1178
+ }
1179
+ get initializer() {
1180
+ return this._initializer;
1181
+ }
1182
+ get hash() {
1183
+ return `[${this._version}]${this.keyPath.join(".")}`;
1184
+ }
1185
+ /**
1186
+ * 解析 VariableDeclarationJSON 从而生成变量声明节点
1187
+ */
1188
+ fromJSON({ type, initializer, meta }) {
1189
+ this.updateType(type);
1190
+ this.updateInitializer(initializer);
1191
+ this.updateMeta(meta);
1192
+ }
1193
+ updateType(type) {
1194
+ const nextTypeJson = typeof type === "string" ? { kind: type } : type;
1195
+ this.updateChildNodeByKey("_type", nextTypeJson);
1196
+ }
1197
+ updateInitializer(nextInitializer) {
1198
+ this.updateChildNodeByKey("_initializer", nextInitializer);
1199
+ }
1200
+ updateMeta(nextMeta) {
1201
+ if (!shallowEqual5(nextMeta, this._meta)) {
1202
+ this._meta = nextMeta;
1203
+ this.fireChange();
1204
+ }
1205
+ }
1206
+ /**
1207
+ * 根据 keyPath 去找下钻的变量字段
1208
+ * @param keyPath
1209
+ * @returns
1210
+ */
1211
+ getByKeyPath(keyPath) {
1212
+ if (this.type?.flags & 16 /* DrilldownType */) {
1213
+ return this.type.getByKeyPath(keyPath);
1214
+ }
1215
+ return void 0;
1216
+ }
1217
+ /**
1218
+ * 监听类型变化
1219
+ * @param observer
1220
+ * @returns
1221
+ */
1222
+ onTypeChange(observer) {
1223
+ return this.subscribe(observer, { selector: (curr) => curr.type });
1224
+ }
1225
+ /**
1226
+ * 转换为 JSON
1227
+ * @returns
1228
+ */
1229
+ toJSON() {
1230
+ return {
1231
+ kind: this.kind,
1232
+ key: this.key,
1233
+ type: this.type?.toJSON(),
1234
+ initializer: this.initializer?.toJSON(),
1235
+ meta: this._meta
1236
+ };
1237
+ }
1238
+ };
1239
+
1240
+ // src/ast/declaration/variable-declaration.ts
1241
+ var VariableDeclaration = class extends BaseVariableField {
1242
+ constructor(params) {
1243
+ super(params);
1244
+ this._order = 0;
1245
+ }
1246
+ get order() {
1247
+ return this._order;
1248
+ }
1249
+ /**
1250
+ * 解析 VariableDeclarationJSON 从而生成变量声明节点
1251
+ */
1252
+ fromJSON({ order, ...rest }) {
1253
+ this.updateOrder(order);
1254
+ super.fromJSON(rest);
1255
+ }
1256
+ updateOrder(order = 0) {
1257
+ if (order !== this._order) {
1258
+ this._order = order;
1259
+ this.dispatchGlobalEvent({
1260
+ type: "ReSortVariableDeclarations"
1261
+ });
1262
+ this.fireChange();
1263
+ }
1264
+ }
1265
+ // 监听类型变化
1266
+ onTypeChange(observer) {
1267
+ return this.subscribe(observer, { selector: (curr) => curr.type });
1268
+ }
1269
+ };
1270
+ VariableDeclaration.kind = "VariableDeclaration" /* VariableDeclaration */;
1271
+
1272
+ // src/ast/declaration/variable-declaration-list.ts
1273
+ var VariableDeclarationList = class extends ASTNode {
1274
+ constructor() {
1275
+ super(...arguments);
1276
+ this.declarationTable = /* @__PURE__ */ new Map();
1277
+ }
1278
+ fromJSON({ declarations, startOrder }) {
1279
+ const removedKeys = new Set(this.declarationTable.keys());
1280
+ const prev = [...this.declarations || []];
1281
+ this.declarations = (declarations || []).map(
1282
+ (declaration, idx) => {
1283
+ const order = (startOrder || 0) + idx;
1284
+ const declarationKey = declaration.key || this.declarations?.[idx]?.key;
1285
+ const existDeclaration = this.declarationTable.get(declarationKey);
1286
+ if (declarationKey) {
1287
+ removedKeys.delete(declarationKey);
1288
+ }
1289
+ if (existDeclaration) {
1290
+ existDeclaration.fromJSON({ order, ...declaration });
1291
+ return existDeclaration;
1292
+ } else {
1293
+ const newDeclaration = this.createChildNode({
1294
+ order,
1295
+ ...declaration,
1296
+ kind: "VariableDeclaration" /* VariableDeclaration */
1297
+ });
1298
+ this.fireChange();
1299
+ this.declarationTable.set(newDeclaration.key, newDeclaration);
1300
+ return newDeclaration;
1301
+ }
1302
+ }
1303
+ );
1304
+ removedKeys.forEach((key) => {
1305
+ const declaration = this.declarationTable.get(key);
1306
+ declaration?.dispose();
1307
+ this.declarationTable.delete(key);
1308
+ });
1309
+ this.dispatchGlobalEvent({
1310
+ type: "VariableListChange",
1311
+ payload: {
1312
+ prev,
1313
+ next: [...this.declarations]
1314
+ }
1315
+ });
1316
+ }
1317
+ toJSON() {
1318
+ return {
1319
+ kind: "VariableDeclarationList" /* VariableDeclarationList */,
1320
+ properties: this.declarations.map((_declaration) => _declaration.toJSON())
1321
+ };
1322
+ }
1323
+ };
1324
+ VariableDeclarationList.kind = "VariableDeclarationList" /* VariableDeclarationList */;
1325
+
1326
+ // src/ast/declaration/property.ts
1327
+ var Property = class extends BaseVariableField {
1328
+ };
1329
+ Property.kind = "Property" /* Property */;
1330
+
1331
+ // src/ast/common/data-node.ts
1332
+ import { shallowEqual as shallowEqual6 } from "fast-equals";
1333
+ var DataNode = class extends ASTNode {
1334
+ get data() {
1335
+ return this._data;
1336
+ }
1337
+ fromJSON(json) {
1338
+ const { kind, ...restData } = json;
1339
+ if (!shallowEqual6(restData, this._data)) {
1340
+ this._data = restData;
1341
+ this.fireChange();
1342
+ }
1343
+ }
1344
+ toJSON() {
1345
+ return {
1346
+ kind: "DataNode" /* DataNode */,
1347
+ ...this._data
1348
+ };
1349
+ }
1350
+ partialUpdate(nextData) {
1351
+ if (!shallowEqual6(nextData, this._data)) {
1352
+ this._data = {
1353
+ ...this._data,
1354
+ ...nextData
1355
+ };
1356
+ this.fireChange();
1357
+ }
1358
+ }
1359
+ };
1360
+ DataNode.kind = "DataNode" /* DataNode */;
1361
+
1362
+ // src/ast/common/list-node.ts
1363
+ var ListNode = class extends ASTNode {
1364
+ get list() {
1365
+ return this._list;
1366
+ }
1367
+ fromJSON({ list }) {
1368
+ this._list.slice(list.length).forEach((_item) => {
1369
+ _item.dispose();
1370
+ this.fireChange();
1371
+ });
1372
+ this._list = list.map((_item, idx) => {
1373
+ const prevItem = this._list[idx];
1374
+ if (prevItem.kind !== _item.kind) {
1375
+ prevItem.dispose();
1376
+ this.fireChange();
1377
+ return this.createChildNode(_item);
1378
+ }
1379
+ prevItem.fromJSON(_item);
1380
+ return prevItem;
1381
+ });
1382
+ }
1383
+ toJSON() {
1384
+ return {
1385
+ kind: "ListNode" /* ListNode */,
1386
+ list: this._list.map((item) => item.toJSON())
1387
+ };
1388
+ }
1389
+ };
1390
+ ListNode.kind = "ListNode" /* ListNode */;
1391
+
1392
+ // src/ast/common/map-node.ts
1393
+ var MapNode = class extends ASTNode {
1394
+ constructor() {
1395
+ super(...arguments);
1396
+ this.map = /* @__PURE__ */ new Map();
1397
+ }
1398
+ fromJSON({ map: map4 }) {
1399
+ const removedKeys = new Set(this.map.keys());
1400
+ for (const [key, item] of map4 || []) {
1401
+ removedKeys.delete(key);
1402
+ this.set(key, item);
1403
+ }
1404
+ for (const removeKey of Array.from(removedKeys)) {
1405
+ this.remove(removeKey);
1406
+ }
1407
+ }
1408
+ toJSON() {
1409
+ return {
1410
+ kind: "MapNode" /* MapNode */,
1411
+ map: Array.from(this.map.entries())
1412
+ };
1413
+ }
1414
+ /**
1415
+ * 往 Map 中设置 ASTNode
1416
+ * @param key ASTNode 的索引,
1417
+ * @param json
1418
+ */
1419
+ set(key, nextJSON) {
1420
+ return this.withBatchUpdate(updateChildNodeHelper).call(this, {
1421
+ getChildNode: () => this.get(key),
1422
+ removeChildNode: () => this.map.delete(key),
1423
+ updateChildNode: (nextNode) => this.map.set(key, nextNode),
1424
+ nextJSON
1425
+ });
1426
+ }
1427
+ /**
1428
+ * 移除指定 ASTNode
1429
+ * @param key
1430
+ */
1431
+ remove(key) {
1432
+ this.get(key)?.dispose();
1433
+ this.map.delete(key);
1434
+ this.fireChange();
1435
+ }
1436
+ /**
1437
+ * 获取 ASTNode
1438
+ * @param key
1439
+ * @returns
1440
+ */
1441
+ get(key) {
1442
+ return this.map.get(key);
1443
+ }
1444
+ };
1445
+ MapNode.kind = "MapNode" /* MapNode */;
1446
+
1447
+ // src/ast/ast-registers.ts
1448
+ var ASTRegisters = class {
1449
+ /**
1450
+ * 核心 AST 节点注册
1451
+ */
1452
+ constructor() {
1453
+ this.injectors = /* @__PURE__ */ new Map();
1454
+ this.astMap = /* @__PURE__ */ new Map();
1455
+ this.registerAST(StringType);
1456
+ this.registerAST(NumberType);
1457
+ this.registerAST(BooleanType);
1458
+ this.registerAST(IntegerType);
1459
+ this.registerAST(ObjectType);
1460
+ this.registerAST(ArrayType);
1461
+ this.registerAST(MapType);
1462
+ this.registerAST(CustomType);
1463
+ this.registerAST(Property);
1464
+ this.registerAST(VariableDeclaration);
1465
+ this.registerAST(VariableDeclarationList);
1466
+ this.registerAST(KeyPathExpressionV2);
1467
+ this.registerAST(EnumerateExpression);
1468
+ this.registerAST(WrapArrayExpression);
1469
+ this.registerAST(MapNode);
1470
+ this.registerAST(DataNode);
1471
+ }
1472
+ /**
1473
+ * 创建 AST 节点
1474
+ * @param param 创建参数
1475
+ * @returns
1476
+ */
1477
+ createAST(json, { parent, scope }) {
1478
+ const Registry = this.astMap.get(json.kind);
1479
+ if (!Registry) {
1480
+ throw Error(`ASTKind: ${String(json.kind)} can not find its ASTNode Registry`);
1481
+ }
1482
+ const injector = this.injectors.get(json.kind);
1483
+ const node = new Registry(
1484
+ {
1485
+ key: json.key,
1486
+ scope,
1487
+ parent
1488
+ },
1489
+ injector?.() || {}
1490
+ );
1491
+ node.changeLocked = true;
1492
+ node.fromJSON(omit(json, ["key", "kind"]));
1493
+ node.changeLocked = false;
1494
+ if (Reflect.hasMetadata(POST_CONSTRUCT_AST_SYMBOL, node)) {
1495
+ const postConstructKey = Reflect.getMetadata(POST_CONSTRUCT_AST_SYMBOL, node);
1496
+ node[postConstructKey]?.();
1497
+ }
1498
+ return node;
1499
+ }
1500
+ /**
1501
+ * 根据 AST 节点类型获取节点 Registry
1502
+ * @param kind
1503
+ * @returns
1504
+ */
1505
+ getASTRegistryByKind(kind) {
1506
+ return this.astMap.get(kind);
1507
+ }
1508
+ /**
1509
+ * 注册 AST 节点
1510
+ * @param ASTNode
1511
+ * @param injector
1512
+ */
1513
+ registerAST(ASTNode2, injector) {
1514
+ this.astMap.set(ASTNode2.kind, ASTNode2);
1515
+ if (injector) {
1516
+ this.injectors.set(ASTNode2.kind, injector);
1517
+ }
1518
+ }
1519
+ };
1520
+ ASTRegisters = __decorateClass([
1521
+ injectable2()
1522
+ ], ASTRegisters);
1523
+
1524
+ // src/ast/factory.ts
1525
+ var ASTFactory;
1526
+ ((ASTFactory2) => {
1527
+ ASTFactory2.createString = () => ({ kind: "String" /* String */ });
1528
+ ASTFactory2.createNumber = () => ({ kind: "Number" /* Number */ });
1529
+ ASTFactory2.createBoolean = () => ({ kind: "Boolean" /* Boolean */ });
1530
+ ASTFactory2.createInteger = () => ({ kind: "Integer" /* Integer */ });
1531
+ ASTFactory2.createObject = (json) => ({
1532
+ kind: "Object" /* Object */,
1533
+ ...json
1534
+ });
1535
+ ASTFactory2.createArray = (json) => ({
1536
+ kind: "Array" /* Array */,
1537
+ ...json
1538
+ });
1539
+ ASTFactory2.createMap = (json) => ({
1540
+ kind: "Map" /* Map */,
1541
+ ...json
1542
+ });
1543
+ ASTFactory2.createUnion = (json) => ({
1544
+ kind: "Union" /* Union */,
1545
+ ...json
1546
+ });
1547
+ ASTFactory2.createCustomType = (json) => ({
1548
+ kind: "CustomType" /* CustomType */,
1549
+ ...json
1550
+ });
1551
+ ASTFactory2.createVariableDeclaration = (json) => ({
1552
+ kind: "VariableDeclaration" /* VariableDeclaration */,
1553
+ ...json
1554
+ });
1555
+ ASTFactory2.createProperty = (json) => ({
1556
+ kind: "Property" /* Property */,
1557
+ ...json
1558
+ });
1559
+ ASTFactory2.createVariableDeclarationList = (json) => ({
1560
+ kind: "VariableDeclarationList" /* VariableDeclarationList */,
1561
+ ...json
1562
+ });
1563
+ ASTFactory2.createEnumerateExpression = (json) => ({
1564
+ kind: "EnumerateExpression" /* EnumerateExpression */,
1565
+ ...json
1566
+ });
1567
+ ASTFactory2.createKeyPathExpression = (json) => ({
1568
+ kind: "KeyPathExpression" /* KeyPathExpression */,
1569
+ ...json
1570
+ });
1571
+ ASTFactory2.createWrapArrayExpression = (json) => ({
1572
+ kind: "WrapArrayExpression" /* WrapArrayExpression */,
1573
+ ...json
1574
+ });
1575
+ ASTFactory2.create = (targetType, json) => ({ kind: targetType.kind, ...json });
1576
+ })(ASTFactory || (ASTFactory = {}));
1577
+
1578
+ // src/scope/datas/scope-output-data.ts
1579
+ var ScopeOutputData = class {
1580
+ constructor(scope) {
1581
+ this.scope = scope;
1582
+ this.memo = createMemo();
1583
+ this._hasChanges = false;
1584
+ this.variableTable = new VariableTable(scope.variableEngine.globalVariableTable);
1585
+ this.scope.toDispose.pushAll([
1586
+ // When root AST node is updated, check if there are any changes during this AST change
1587
+ this.scope.ast.subscribe(() => {
1588
+ if (this._hasChanges) {
1589
+ this.memo.clear();
1590
+ this.notifyCoversChange();
1591
+ this.variableTable.fireChange();
1592
+ this._hasChanges = false;
1593
+ }
1594
+ }),
1595
+ this.scope.event.on("DisposeAST", (_action) => {
1596
+ if (_action.ast?.kind === "VariableDeclaration" /* VariableDeclaration */) {
1597
+ this.removeVariableFromTable(_action.ast.key);
1598
+ }
1599
+ }),
1600
+ this.scope.event.on("NewAST", (_action) => {
1601
+ if (_action.ast?.kind === "VariableDeclaration" /* VariableDeclaration */) {
1602
+ this.addVariableToTable(_action.ast);
1603
+ }
1604
+ }),
1605
+ this.scope.event.on("ReSortVariableDeclarations", () => {
1606
+ this._hasChanges = true;
1607
+ }),
1608
+ this.variableTable
1609
+ ]);
1610
+ }
1611
+ get variableEngine() {
1612
+ return this.scope.variableEngine;
1613
+ }
1614
+ get globalVariableTable() {
1615
+ return this.scope.variableEngine.globalVariableTable;
1616
+ }
1617
+ /**
1618
+ * @deprecated use onListOrAnyVarChange instead
1619
+ */
1620
+ get onDataChange() {
1621
+ return this.variableTable.onDataChange.bind(this.variableTable);
1622
+ }
1623
+ /**
1624
+ * listen to variable list change
1625
+ */
1626
+ get onVariableListChange() {
1627
+ return this.variableTable.onVariableListChange.bind(this.variableTable);
1628
+ }
1629
+ /**
1630
+ * listen to any variable update in list
1631
+ */
1632
+ get onAnyVariableChange() {
1633
+ return this.variableTable.onAnyVariableChange.bind(this.variableTable);
1634
+ }
1635
+ /**
1636
+ * listen to variable list change + any variable update in list
1637
+ */
1638
+ get onListOrAnyVarChange() {
1639
+ return this.variableTable.onListOrAnyVarChange.bind(this.variableTable);
1640
+ }
1641
+ /**
1642
+ * Scope Output Variable Declarations
1643
+ */
1644
+ get variables() {
1645
+ return this.memo(
1646
+ "variables",
1647
+ () => this.variableTable.variables.sort((a, b) => a.order - b.order)
1648
+ );
1649
+ }
1650
+ /**
1651
+ * Output Variable Keys
1652
+ */
1653
+ get variableKeys() {
1654
+ return this.memo("variableKeys", () => this.variableTable.variableKeys);
1655
+ }
1656
+ addVariableToTable(variable) {
1657
+ if (variable.scope !== this.scope) {
1658
+ throw Error("VariableDeclaration must be a ast node in scope");
1659
+ }
1660
+ this.variableTable.addVariableToTable(variable);
1661
+ this._hasChanges = true;
1662
+ }
1663
+ removeVariableFromTable(key) {
1664
+ this.variableTable.removeVariableFromTable(key);
1665
+ this._hasChanges = true;
1666
+ }
1667
+ getVariableByKey(key) {
1668
+ return this.variableTable.getVariableByKey(key);
1669
+ }
1670
+ /**
1671
+ *
1672
+ */
1673
+ notifyCoversChange() {
1674
+ this.scope.coverScopes.forEach((scope) => scope.available.refresh());
1675
+ }
1676
+ };
1677
+
1678
+ // src/scope/datas/scope-available-data.ts
1679
+ import {
1680
+ Subject as Subject3,
1681
+ distinctUntilChanged as distinctUntilChanged3,
1682
+ distinctUntilKeyChanged,
1683
+ map as map3,
1684
+ merge as merge2,
1685
+ share as share3,
1686
+ skip as skip3,
1687
+ startWith,
1688
+ switchMap as switchMap3,
1689
+ tap as tap2
1690
+ } from "rxjs";
1691
+ import { flatten } from "lodash";
1692
+ import { shallowEqual as shallowEqual7 } from "fast-equals";
1693
+ import { Disposable as Disposable3 } from "@flowgram.ai/utils";
1694
+ import { Emitter as Emitter2 } from "@flowgram.ai/utils";
1695
+ var ScopeAvailableData = class {
1696
+ constructor(scope) {
1697
+ this.scope = scope;
1698
+ this.memo = createMemo();
1699
+ this.refresh$ = new Subject3();
1700
+ this._variables = [];
1701
+ /**
1702
+ * 监听
1703
+ */
1704
+ this.variables$ = this.refresh$.pipe(
1705
+ // 输出变量是否 version 发生变化
1706
+ map3(() => flatten(this.depScopes.map((scope) => scope.output.variables || []))),
1707
+ // 变量列表浅比较
1708
+ distinctUntilChanged3(shallowEqual7),
1709
+ share3()
1710
+ );
1711
+ // 监听变量列表中的单个变量变化
1712
+ this.anyVariableChange$ = this.variables$.pipe(
1713
+ switchMap3(
1714
+ (_variables) => merge2(
1715
+ ..._variables.map(
1716
+ (_v) => _v.value$.pipe(
1717
+ // 跳过 BehaviorSubject 第一个
1718
+ skip3(1)
1719
+ )
1720
+ )
1721
+ )
1722
+ ),
1723
+ share3()
1724
+ );
1725
+ /**
1726
+ * @deprecated
1727
+ */
1728
+ this.onDataChangeEmitter = new Emitter2();
1729
+ this.onListOrAnyVarChangeEmitter = new Emitter2();
1730
+ /**
1731
+ * @deprecated use available.onListOrAnyVarChange instead
1732
+ */
1733
+ this.onDataChange = this.onDataChangeEmitter.event;
1734
+ /**
1735
+ * listen to variable list change + any variable drilldown change
1736
+ */
1737
+ this.onListOrAnyVarChange = this.onListOrAnyVarChangeEmitter.event;
1738
+ this.scope.toDispose.pushAll([
1739
+ this.onVariableListChange((_variables) => {
1740
+ this._variables = _variables;
1741
+ this.memo.clear();
1742
+ this.onDataChangeEmitter.fire(this._variables);
1743
+ this.onListOrAnyVarChangeEmitter.fire(this._variables);
1744
+ }),
1745
+ this.onAnyVariableChange(() => {
1746
+ this.onDataChangeEmitter.fire(this._variables);
1747
+ this.onListOrAnyVarChangeEmitter.fire(this._variables);
1748
+ }),
1749
+ Disposable3.create(() => {
1750
+ this.refresh$.complete();
1751
+ this.refresh$.unsubscribe();
1752
+ })
1753
+ ]);
1754
+ }
1755
+ get globalVariableTable() {
1756
+ return this.scope.variableEngine.globalVariableTable;
1757
+ }
1758
+ // 刷新可访问变量列表
1759
+ refresh() {
1760
+ if (this.scope.disposed) {
1761
+ return;
1762
+ }
1763
+ this.refresh$.next();
1764
+ }
1765
+ /**
1766
+ * listen to any variable update in list
1767
+ * @param observer
1768
+ * @returns
1769
+ */
1770
+ onAnyVariableChange(observer) {
1771
+ return subsToDisposable(this.anyVariableChange$.subscribe(observer));
1772
+ }
1773
+ /**
1774
+ * listen to variable list change
1775
+ * @param observer
1776
+ * @returns
1777
+ */
1778
+ onVariableListChange(observer) {
1779
+ return subsToDisposable(this.variables$.subscribe(observer));
1780
+ }
1781
+ /**
1782
+ * 获取可消费变量
1783
+ */
1784
+ get variables() {
1785
+ return this._variables;
1786
+ }
1787
+ /**
1788
+ * 获取可访问的变量 keys
1789
+ */
1790
+ get variableKeys() {
1791
+ return this.memo("availableKeys", () => this._variables.map((_v) => _v.key));
1792
+ }
1793
+ /**
1794
+ * 返回依赖的作用域
1795
+ */
1796
+ get depScopes() {
1797
+ return this.scope.depScopes;
1798
+ }
1799
+ /**
1800
+ * 通过 keyPath 找到可用变量
1801
+ * @param keyPath
1802
+ * @returns
1803
+ */
1804
+ getByKeyPath(keyPath = []) {
1805
+ if (!this.variableKeys.includes(keyPath[0])) {
1806
+ return;
1807
+ }
1808
+ return this.globalVariableTable.getByKeyPath(keyPath);
1809
+ }
1810
+ /**
1811
+ * Track Variable Change (Includes type update and children update) By KeyPath
1812
+ * @returns
1813
+ */
1814
+ trackByKeyPath(keyPath = [], cb, opts) {
1815
+ const { triggerOnInit = true } = opts || {};
1816
+ return subsToDisposable(
1817
+ merge2(this.anyVariableChange$, this.variables$).pipe(
1818
+ triggerOnInit ? startWith() : tap2(() => null),
1819
+ map3(() => {
1820
+ const v = this.getByKeyPath(keyPath);
1821
+ return { v, hash: v?.hash };
1822
+ }),
1823
+ distinctUntilKeyChanged("hash"),
1824
+ map3(({ v }) => v)
1825
+ ).subscribe(cb)
1826
+ );
1827
+ }
1828
+ };
1829
+
1830
+ // src/scope/datas/scope-event-data.ts
1831
+ import { Subject as Subject4, filter } from "rxjs";
1832
+ var ScopeEventData = class {
1833
+ constructor(scope) {
1834
+ this.scope = scope;
1835
+ this.event$ = new Subject4();
1836
+ scope.toDispose.pushAll([
1837
+ this.subscribe((_action) => {
1838
+ scope.variableEngine.fireGlobalEvent(_action);
1839
+ })
1840
+ ]);
1841
+ }
1842
+ dispatch(action) {
1843
+ if (this.scope.disposed) {
1844
+ return;
1845
+ }
1846
+ this.event$.next(action);
1847
+ }
1848
+ subscribe(observer) {
1849
+ return subsToDisposable(this.event$.subscribe(observer));
1850
+ }
1851
+ on(type, observer) {
1852
+ return subsToDisposable(
1853
+ this.event$.pipe(filter((_action) => _action.type === type)).subscribe(observer)
1854
+ );
1855
+ }
1856
+ };
1857
+
1858
+ // src/scope/scope.ts
1859
+ var Scope = class {
1860
+ constructor(options) {
1861
+ /**
1862
+ * 数据缓存
1863
+ */
1864
+ this.memo = createMemo();
1865
+ this.toDispose = new DisposableCollection4();
1866
+ this.onDispose = this.toDispose.onDispose;
1867
+ this.id = options.id;
1868
+ this.meta = options.meta || {};
1869
+ this.variableEngine = options.variableEngine;
1870
+ this.event = new ScopeEventData(this);
1871
+ this.ast = this.variableEngine.astRegisters.createAST(
1872
+ {
1873
+ kind: "MapNode" /* MapNode */,
1874
+ key: String(this.id)
1875
+ },
1876
+ {
1877
+ scope: this
1878
+ }
1879
+ );
1880
+ this.output = new ScopeOutputData(this);
1881
+ this.available = new ScopeAvailableData(this);
1882
+ }
1883
+ refreshCovers() {
1884
+ this.memo.clear("covers");
1885
+ }
1886
+ refreshDeps() {
1887
+ this.memo.clear("deps");
1888
+ this.available.refresh();
1889
+ }
1890
+ get depScopes() {
1891
+ return this.memo(
1892
+ "deps",
1893
+ () => this.variableEngine.chain.getDeps(this).filter((_scope) => Boolean(_scope) && !_scope?.disposed)
1894
+ );
1895
+ }
1896
+ get coverScopes() {
1897
+ return this.memo(
1898
+ "covers",
1899
+ () => this.variableEngine.chain.getCovers(this).filter((_scope) => Boolean(_scope) && !_scope?.disposed)
1900
+ );
1901
+ }
1902
+ dispose() {
1903
+ this.ast.dispose();
1904
+ this.toDispose.dispose();
1905
+ this.coverScopes.forEach((_scope) => _scope.refreshDeps());
1906
+ this.depScopes.forEach((_scope) => _scope.refreshCovers());
1907
+ }
1908
+ get disposed() {
1909
+ return this.toDispose.disposed;
1910
+ }
1911
+ setVar(arg1, arg2) {
1912
+ if (typeof arg1 === "string" && arg2 !== void 0) {
1913
+ return this.ast.set(arg1, arg2);
1914
+ }
1915
+ if (typeof arg1 === "object" && arg2 === void 0) {
1916
+ return this.ast.set("outputs", arg1);
1917
+ }
1918
+ throw new Error("Invalid arguments");
1919
+ }
1920
+ /**
1921
+ * Retrieves a variable from the Scope by key.
1922
+ *
1923
+ * @param key - The key of the variable to retrieve. Defaults to 'outputs'.
1924
+ * @returns The value of the variable, or undefined if not found.
1925
+ */
1926
+ getVar(key = "outputs") {
1927
+ return this.ast.get(key);
1928
+ }
1929
+ /**
1930
+ * Clears a variable from the Scope by key.
1931
+ *
1932
+ * @param key - The key of the variable to clear. Defaults to 'outputs'.
1933
+ * @returns The updated AST node.
1934
+ */
1935
+ clearVar(key = "outputs") {
1936
+ return this.ast.remove(key);
1937
+ }
1938
+ };
1939
+
1940
+ // src/variable-engine.ts
1941
+ var VariableEngine = class {
1942
+ constructor(chain, astRegisters) {
1943
+ this.chain = chain;
1944
+ this.astRegisters = astRegisters;
1945
+ this.toDispose = new DisposableCollection5();
1946
+ this.memo = createMemo();
1947
+ this.scopeMap = /* @__PURE__ */ new Map();
1948
+ this.globalEvent$ = new Subject5();
1949
+ this.onScopeChangeEmitter = new Emitter3();
1950
+ this.globalVariableTable = new VariableTable();
1951
+ this.onScopeChange = this.onScopeChangeEmitter.event;
1952
+ this.toDispose.pushAll([
1953
+ chain,
1954
+ Disposable4.create(() => {
1955
+ this.getAllScopes().forEach((scope) => scope.dispose());
1956
+ this.globalVariableTable.dispose();
1957
+ })
1958
+ ]);
1959
+ }
1960
+ get container() {
1961
+ return this.containerProvider();
1962
+ }
1963
+ dispose() {
1964
+ this.toDispose.dispose();
1965
+ }
1966
+ // 根据 scopeId 找到作用域
1967
+ getScopeById(scopeId) {
1968
+ return this.scopeMap.get(scopeId);
1969
+ }
1970
+ // 移除作用域
1971
+ removeScopeById(scopeId) {
1972
+ this.getScopeById(scopeId)?.dispose();
1973
+ }
1974
+ /**
1975
+ * Get Scope, if Scope exists and type is same, will use it directly
1976
+ * @param id scope id
1977
+ * @param meta scope meta, defined by user
1978
+ * @param ScopeConstructor scope constructor, default is Scope. you can extends Scope to create your own scope
1979
+ * @returns
1980
+ */
1981
+ createScope(id, meta, options = {}) {
1982
+ const { ScopeConstructor = Scope } = options;
1983
+ let scope = this.getScopeById(id);
1984
+ if (!scope) {
1985
+ scope = new ScopeConstructor({ variableEngine: this, meta, id });
1986
+ this.scopeMap.set(id, scope);
1987
+ this.onScopeChangeEmitter.fire({ type: "add", scope });
1988
+ scope.toDispose.pushAll([
1989
+ scope.ast.subscribe(() => {
1990
+ this.onScopeChangeEmitter.fire({ type: "update", scope });
1991
+ }),
1992
+ // 可用变量发生变化
1993
+ scope.available.onDataChange(() => {
1994
+ this.onScopeChangeEmitter.fire({ type: "available", scope });
1995
+ })
1996
+ ]);
1997
+ scope.onDispose(() => {
1998
+ this.scopeMap.delete(id);
1999
+ this.onScopeChangeEmitter.fire({ type: "delete", scope });
2000
+ });
2001
+ }
2002
+ return scope;
2003
+ }
2004
+ // 获取系统中所有的作用域
2005
+ getAllScopes({
2006
+ sort
2007
+ } = {}) {
2008
+ const allScopes = Array.from(this.scopeMap.values());
2009
+ if (sort) {
2010
+ const sortScopes = this.chain.sortAll();
2011
+ const remainScopes = new Set(allScopes);
2012
+ sortScopes.forEach((_scope) => remainScopes.delete(_scope));
2013
+ return [...sortScopes, ...Array.from(remainScopes)];
2014
+ }
2015
+ return [...allScopes];
2016
+ }
2017
+ fireGlobalEvent(event) {
2018
+ this.globalEvent$.next(event);
2019
+ }
2020
+ onGlobalEvent(type, observer) {
2021
+ return subsToDisposable(
2022
+ this.globalEvent$.subscribe((_action) => {
2023
+ if (_action.type === type) {
2024
+ observer(_action);
2025
+ }
2026
+ })
2027
+ );
2028
+ }
2029
+ };
2030
+ __decorateClass([
2031
+ inject2(ContainerProvider)
2032
+ ], VariableEngine.prototype, "containerProvider", 2);
2033
+ __decorateClass([
2034
+ preDestroy()
2035
+ ], VariableEngine.prototype, "dispose", 1);
2036
+ VariableEngine = __decorateClass([
2037
+ injectable3(),
2038
+ __decorateParam(0, inject2(ScopeChain)),
2039
+ __decorateParam(1, inject2(ASTRegisters))
2040
+ ], VariableEngine);
2041
+
2042
+ // src/services/variable-field-key-rename-service.ts
2043
+ import { difference } from "lodash";
2044
+ import { inject as inject3, injectable as injectable4, postConstruct, preDestroy as preDestroy2 } from "inversify";
2045
+ import { DisposableCollection as DisposableCollection6, Emitter as Emitter4 } from "@flowgram.ai/utils";
2046
+ var VariableFieldKeyRenameService = class {
2047
+ constructor() {
2048
+ this.toDispose = new DisposableCollection6();
2049
+ this.renameEmitter = new Emitter4();
2050
+ // 没有被 rename 的字段通过 disposeInList 透出,让业务区分变量是 rename 删除的,还是真正从列表中删除的
2051
+ this.disposeInListEmitter = new Emitter4();
2052
+ this.onRename = this.renameEmitter.event;
2053
+ this.onDisposeInList = this.disposeInListEmitter.event;
2054
+ }
2055
+ handleFieldListChange(ast, prev, next) {
2056
+ if (!ast || !prev?.length || !next?.length) {
2057
+ this.notifyFieldsDispose(prev, next);
2058
+ return;
2059
+ }
2060
+ if (prev.length !== next.length) {
2061
+ this.notifyFieldsDispose(prev, next);
2062
+ return;
2063
+ }
2064
+ let renameNodeInfo = null;
2065
+ let existFieldChanged = false;
2066
+ for (const [index, prevField] of prev.entries()) {
2067
+ const nextField = next[index];
2068
+ if (prevField.key !== nextField.key) {
2069
+ if (existFieldChanged) {
2070
+ this.notifyFieldsDispose(prev, next);
2071
+ return;
2072
+ }
2073
+ existFieldChanged = true;
2074
+ if (prevField.type?.kind === nextField.type?.kind) {
2075
+ renameNodeInfo = { before: prevField, after: nextField };
2076
+ }
2077
+ }
2078
+ }
2079
+ if (!renameNodeInfo) {
2080
+ this.notifyFieldsDispose(prev, next);
2081
+ return;
2082
+ }
2083
+ this.renameEmitter.fire(renameNodeInfo);
2084
+ }
2085
+ notifyFieldsDispose(prev, next) {
2086
+ const removedFields = difference(prev || [], next || []);
2087
+ removedFields.forEach((_field) => this.disposeInListEmitter.fire(_field));
2088
+ }
2089
+ init() {
2090
+ this.toDispose.pushAll([
2091
+ this.variableEngine.onGlobalEvent(
2092
+ "VariableListChange",
2093
+ (_action) => {
2094
+ this.handleFieldListChange(_action.ast, _action.payload?.prev, _action.payload?.next);
2095
+ }
2096
+ ),
2097
+ this.variableEngine.onGlobalEvent(
2098
+ "ObjectPropertiesChange",
2099
+ (_action) => {
2100
+ this.handleFieldListChange(_action.ast, _action.payload?.prev, _action.payload?.next);
2101
+ }
2102
+ )
2103
+ ]);
2104
+ }
2105
+ dispose() {
2106
+ this.toDispose.dispose();
2107
+ }
2108
+ };
2109
+ __decorateClass([
2110
+ inject3(VariableEngine)
2111
+ ], VariableFieldKeyRenameService.prototype, "variableEngine", 2);
2112
+ __decorateClass([
2113
+ postConstruct()
2114
+ ], VariableFieldKeyRenameService.prototype, "init", 1);
2115
+ __decorateClass([
2116
+ preDestroy2()
2117
+ ], VariableFieldKeyRenameService.prototype, "dispose", 1);
2118
+ VariableFieldKeyRenameService = __decorateClass([
2119
+ injectable4()
2120
+ ], VariableFieldKeyRenameService);
2121
+
2122
+ // src/variable-container-module.ts
2123
+ var VariableContainerModule = new ContainerModule((bind) => {
2124
+ bind(VariableEngine).toSelf().inSingletonScope();
2125
+ bind(ASTRegisters).toSelf().inSingletonScope();
2126
+ bind(VariableFieldKeyRenameService).toSelf().inSingletonScope();
2127
+ bind(VariableEngineProvider).toDynamicValue((ctx) => () => ctx.container.get(VariableEngine));
2128
+ bind(ContainerProvider).toDynamicValue((ctx) => () => ctx.container);
2129
+ });
2130
+
2131
+ // src/react/context.tsx
2132
+ import { createContext, useContext } from "react";
2133
+ var ScopeContext = createContext(null);
2134
+ var ScopeProvider = ScopeContext.Provider;
2135
+ var useScopeContext = () => useContext(ScopeContext);
2136
+ var useCurrentScope = () => useContext(ScopeContext)?.scope;
2137
+
2138
+ // src/react/hooks/useScopeAvailable.ts
2139
+ import { useEffect } from "react";
2140
+ import { useRefresh } from "@flowgram.ai/core";
2141
+ function useScopeAvailable() {
2142
+ const scope = useCurrentScope();
2143
+ const refresh = useRefresh();
2144
+ useEffect(() => {
2145
+ const disposable = scope.available.onDataChange(() => {
2146
+ refresh();
2147
+ });
2148
+ return () => disposable.dispose();
2149
+ }, []);
2150
+ return scope.available;
2151
+ }
2152
+
2153
+ // src/react/hooks/useAvailableVariables.ts
2154
+ import { useEffect as useEffect2 } from "react";
2155
+ import { useRefresh as useRefresh2, useService } from "@flowgram.ai/core";
2156
+ function useAvailableVariables() {
2157
+ const scope = useCurrentScope();
2158
+ const variableEngine = useService(VariableEngine);
2159
+ const refresh = useRefresh2();
2160
+ useEffect2(() => {
2161
+ if (!scope) {
2162
+ const disposable2 = variableEngine.globalVariableTable.onListOrAnyVarChange(() => {
2163
+ refresh();
2164
+ });
2165
+ return () => disposable2.dispose();
2166
+ }
2167
+ const disposable = scope.available.onDataChange(() => {
2168
+ refresh();
2169
+ });
2170
+ return () => disposable.dispose();
2171
+ }, []);
2172
+ return scope ? scope.available.variables : variableEngine.globalVariableTable.variables;
2173
+ }
2174
+ export {
2175
+ ASTFactory,
2176
+ ASTKind,
2177
+ ASTMatch,
2178
+ ASTNode,
2179
+ ASTNodeFlags,
2180
+ ASTRegisters,
2181
+ ArrayType,
2182
+ BaseExpression,
2183
+ BaseType,
2184
+ BaseVariableField,
2185
+ BooleanType,
2186
+ CustomType,
2187
+ DataNode,
2188
+ EnumerateExpression,
2189
+ IntegerType,
2190
+ KeyPathExpression,
2191
+ KeyPathExpressionV2,
2192
+ ListNode,
2193
+ MapNode,
2194
+ MapType,
2195
+ NumberType,
2196
+ ObjectType,
2197
+ Property,
2198
+ Scope,
2199
+ ScopeChain,
2200
+ ScopeOutputData,
2201
+ ScopeProvider,
2202
+ StringType,
2203
+ VariableContainerModule,
2204
+ VariableDeclaration,
2205
+ VariableDeclarationList,
2206
+ VariableEngine,
2207
+ VariableEngineProvider,
2208
+ VariableFieldKeyRenameService,
2209
+ WrapArrayExpression,
2210
+ injectToAST,
2211
+ isMatchAST,
2212
+ postConstructAST,
2213
+ useAvailableVariables,
2214
+ useCurrentScope,
2215
+ useScopeAvailable,
2216
+ useScopeContext
2217
+ };
2218
+ //# sourceMappingURL=index.js.map