From 6420cb73ece20913491ad64510c19d74802e75ab Mon Sep 17 00:00:00 2001 From: everbrez Date: Sun, 14 Jan 2024 13:01:50 +0800 Subject: [PATCH] feat: add new Operators --- packages/core/src/operators/index.ts | 2 +- .../core/src/operators/streams/combine.ts | 23 ++++ packages/core/src/operators/streams/index.ts | 2 + .../core/src/operators/streams/transform.ts | 17 +++ .../Operators/CombineOperator/index.tsx | 128 ++++++++++++++++++ .../Operators/CustomOperator/index.tsx | 2 +- .../Operators/EffectOperator/index.tsx | 46 +++++++ .../Operators/InputOperator/index.tsx | 2 +- .../flow/src/Diagrams/Operators/Operator.tsx | 3 +- .../src/Diagrams/Operators/OperatorMap.ts | 4 + .../Operators/OutputOperator/index.tsx | 2 +- .../StateOperator/ConstStateOperator.tsx | 2 +- .../Operators/StateOperator/StateOperator.tsx | 2 +- .../Diagrams/Operators/SumOperator/index.tsx | 2 +- .../Operators/TransformOperator/index.tsx | 51 +++++++ packages/flow/src/Diagrams/Operators/index.ts | 22 ++- .../src/Diagrams/Operators/types/index.ts | 19 +++ 17 files changed, 314 insertions(+), 15 deletions(-) create mode 100644 packages/core/src/operators/streams/combine.ts create mode 100644 packages/core/src/operators/streams/transform.ts create mode 100644 packages/flow/src/Diagrams/Operators/CombineOperator/index.tsx create mode 100644 packages/flow/src/Diagrams/Operators/EffectOperator/index.tsx create mode 100644 packages/flow/src/Diagrams/Operators/TransformOperator/index.tsx diff --git a/packages/core/src/operators/index.ts b/packages/core/src/operators/index.ts index f618091..63d471a 100644 --- a/packages/core/src/operators/index.ts +++ b/packages/core/src/operators/index.ts @@ -3,7 +3,7 @@ // 之后可以区分为操作流和数据流 // ================ streams operators ================= // -export { constValue, sum, proxyData } from './streams'; +export { constValue, sum, proxyData, combine, transform } from './streams'; // ================ values operators ================= // export { sumValue } from './values'; diff --git a/packages/core/src/operators/streams/combine.ts b/packages/core/src/operators/streams/combine.ts new file mode 100644 index 0000000..46316af --- /dev/null +++ b/packages/core/src/operators/streams/combine.ts @@ -0,0 +1,23 @@ +import { type Atom } from '../../ModelState/State'; +import { ModelState } from '../..'; + +export function combine< + ObservableSubjectList extends Array>, + AppendObservableSubjectList extends Array>, +>( + streamSource: ObservableSubjectList, + appendSource: AppendObservableSubjectList, +) { + const result = new ModelState([]); + + streamSource.forEach((input, index) => { + input.subscribe((_value, extraInfo) => { + const allValue = [...streamSource, ...appendSource].map( + (item) => item.current, + ); + result.update(allValue, extraInfo.concat('combine')); + }); + }); + + return result; +} diff --git a/packages/core/src/operators/streams/index.ts b/packages/core/src/operators/streams/index.ts index 5631c53..b5ad36f 100644 --- a/packages/core/src/operators/streams/index.ts +++ b/packages/core/src/operators/streams/index.ts @@ -1,3 +1,5 @@ export { constValue } from './constValue'; export { proxyData } from './proxyData'; export { sum } from './sum'; +export { combine } from './combine'; +export { transform } from './transform'; diff --git a/packages/core/src/operators/streams/transform.ts b/packages/core/src/operators/streams/transform.ts new file mode 100644 index 0000000..17bceb5 --- /dev/null +++ b/packages/core/src/operators/streams/transform.ts @@ -0,0 +1,17 @@ +import { type Atom } from '../../ModelState/State'; +import { ModelState } from '../..'; + +export function transform( + steam: Atom, + action: (value: StreamValue) => ResultValue, +) { + return steam.pipe((observer) => { + const result = new ModelState(undefined); + + observer.subscribe((value, extra) => { + result.update(action(value), extra.concat('transform')); + }); + + return result; + }); +} diff --git a/packages/flow/src/Diagrams/Operators/CombineOperator/index.tsx b/packages/flow/src/Diagrams/Operators/CombineOperator/index.tsx new file mode 100644 index 0000000..da59bfc --- /dev/null +++ b/packages/flow/src/Diagrams/Operators/CombineOperator/index.tsx @@ -0,0 +1,128 @@ +import { type Node } from 'reactflow'; +import { type IGenerationOption } from '../../Compiler/graph'; +import { NodeTypeEnum } from '../../Nodes/NodeTypeEnum'; +import { MetaOperator } from '../Operator'; +import { EndPoint, type ICombineOperatorData } from '../types'; +import { EosOperatorsSymbol } from '../../Compiler/runtime'; + +export class CombineOperator + extends MetaOperator + implements MetaOperator +{ + constructor() { + super({ + operatorName: 'Combine', + operatorType: 'CombineOperator', + nodeType: NodeTypeEnum.Node, + }); + } + + nodeColor?: string | undefined = '#C21292'; + + create(): Node>> { + return super.create({ + endPointOptions: { + endPointList: [ + new EndPoint({ + type: 'group', + hint: 'output', + children: [ + new EndPoint({ + label: 'output', + hint: 'output', + variableType: 'array', + type: 'source', + }), + ], + }), + new EndPoint({ + type: 'group', + hint: 'mainInput', + label: 'Main Stream', + defaultChildData: { + hint: 'mainSource', + type: 'target', + }, + allowAddAndRemoveChildren: true, + children: [ + new EndPoint({ + hint: 'mainSource', + type: 'target', + }), + ], + }), + new EndPoint({ + type: 'group', + hint: 'appendInput', + label: 'Append Stream', + defaultChildData: { + hint: 'appendSource', + type: 'target', + }, + allowAddAndRemoveChildren: true, + children: [ + new EndPoint({ + hint: 'appendSource', + type: 'target', + }), + ], + }), + ], + }, + }); + } + + getHintPorts(node: Node, hint: string) { + return node.data.endPointOptions?.endPointList?.find( + (item) => item.hint === hint, + )?.children; + } + + getMainInputPorts(node: Node) { + return this.getHintPorts(node, 'mainInput'); + } + + getAppendInputPorts(node: Node) { + return this.getHintPorts(node, 'appendInput'); + } + + generateBlockDeclarations( + options: IGenerationOption, + ): string[] { + const { node, formatVariableName, nodeGraph } = options; + const handleId = + this.getHintPorts(node, 'output')?.find((item) => item.hint === 'output') + ?.id || ''; + + const mainInputSet = new Set( + this.getMainInputPorts(node)?.map((item) => item.id), + ); + const appendInputSet = new Set( + this.getAppendInputPorts(node)?.map((item) => item.id), + ); + + const sourceItems = nodeGraph.findSourceNodes(node.id) || []; + const mainInputSourceIds = sourceItems + .filter((item) => mainInputSet.has(item.handleId)) + .map((item) => item.relatedHandleId); + + const appendInputSourceIds = sourceItems + .filter((item) => appendInputSet.has(item.handleId)) + .map((item) => item.relatedHandleId); + + return [ + `const ${formatVariableName(handleId)} = ${EosOperatorsSymbol}.combine( + [${mainInputSourceIds.map((id) => formatVariableName(id)).join(',')}], + [${appendInputSourceIds.map((id) => formatVariableName(id)).join(',')}] + )`, + ]; + } + + generateBlockOutput(options: IGenerationOption): string[] { + return []; + } + + generateBlockRelation(options: IGenerationOption): string[] { + return []; + } +} diff --git a/packages/flow/src/Diagrams/Operators/CustomOperator/index.tsx b/packages/flow/src/Diagrams/Operators/CustomOperator/index.tsx index bb4acf3..f66be40 100644 --- a/packages/flow/src/Diagrams/Operators/CustomOperator/index.tsx +++ b/packages/flow/src/Diagrams/Operators/CustomOperator/index.tsx @@ -67,7 +67,7 @@ export class CustomOperator constructor() { super({ - operatorName: 'CustomOperator', + operatorName: 'Custom', operatorType: 'CustomOperator', nodeType: NodeTypeEnum.Node, endPointOptions: { diff --git a/packages/flow/src/Diagrams/Operators/EffectOperator/index.tsx b/packages/flow/src/Diagrams/Operators/EffectOperator/index.tsx new file mode 100644 index 0000000..15e5191 --- /dev/null +++ b/packages/flow/src/Diagrams/Operators/EffectOperator/index.tsx @@ -0,0 +1,46 @@ +import { type Node } from 'reactflow'; +import { type IGenerationOption } from '../../Compiler/graph'; +import { NodeTypeEnum } from '../../Nodes/NodeTypeEnum'; +import { MetaOperator } from '../Operator'; +import { EndPoint, type IEffectOperatorData } from '../types'; + +export class EffectOperator + extends MetaOperator + implements MetaOperator +{ + constructor() { + super({ + operatorName: 'Effect', + operatorType: 'EffectOperator', + nodeType: NodeTypeEnum.Node, + }); + } + + create(): Node>> { + return super.create({ + endPointOptions: { + endPointList: [ + new EndPoint({ + type: 'target', + hint: 'input', + label: 'input', + }), + ], + }, + }); + } + + nodeColor?: string | undefined = '#F3F8FF'; + + generateBlockDeclarations(options: IGenerationOption): string[] { + return []; + } + + generateBlockOutput(options: IGenerationOption): string[] { + return []; + } + + generateBlockRelation(options: IGenerationOption): string[] { + return []; + } +} diff --git a/packages/flow/src/Diagrams/Operators/InputOperator/index.tsx b/packages/flow/src/Diagrams/Operators/InputOperator/index.tsx index ed1bea8..5bcd96d 100644 --- a/packages/flow/src/Diagrams/Operators/InputOperator/index.tsx +++ b/packages/flow/src/Diagrams/Operators/InputOperator/index.tsx @@ -18,7 +18,7 @@ export class InputOperator constructor() { super({ - operatorName: 'InputOperator', + operatorName: 'Input', operatorType: 'InputOperator', nodeType: NodeTypeEnum.Node, }); diff --git a/packages/flow/src/Diagrams/Operators/Operator.tsx b/packages/flow/src/Diagrams/Operators/Operator.tsx index c4a769e..2dcfd1e 100644 --- a/packages/flow/src/Diagrams/Operators/Operator.tsx +++ b/packages/flow/src/Diagrams/Operators/Operator.tsx @@ -6,6 +6,7 @@ import { type IMetaOperatorData, } from './types'; import { type IGenerationOption } from '../Compiler/graph'; +import { Input } from 'antd'; import { pick } from 'lodash'; export abstract class MetaOperator< @@ -135,7 +136,7 @@ export abstract class MetaOperator< } return ( <> - ( operatorType: string, ) { diff --git a/packages/flow/src/Diagrams/Operators/OutputOperator/index.tsx b/packages/flow/src/Diagrams/Operators/OutputOperator/index.tsx index 783569a..a1f4cf4 100644 --- a/packages/flow/src/Diagrams/Operators/OutputOperator/index.tsx +++ b/packages/flow/src/Diagrams/Operators/OutputOperator/index.tsx @@ -13,7 +13,7 @@ export class OutputOperator constructor() { super({ - operatorName: 'OutputOperator', + operatorName: 'Output', operatorType: 'OutputOperator', nodeType: NodeTypeEnum.Node, }); diff --git a/packages/flow/src/Diagrams/Operators/StateOperator/ConstStateOperator.tsx b/packages/flow/src/Diagrams/Operators/StateOperator/ConstStateOperator.tsx index 7d97ec0..1c2fae2 100644 --- a/packages/flow/src/Diagrams/Operators/StateOperator/ConstStateOperator.tsx +++ b/packages/flow/src/Diagrams/Operators/StateOperator/ConstStateOperator.tsx @@ -8,7 +8,7 @@ import { type Node } from 'reactflow'; export class ConstStateOperator extends StateOperator implements MetaOperator { constructor() { super(); - this.defaultOperatorData.operatorName = 'ConstStateOperator'; + this.defaultOperatorData.operatorName = 'ConstState'; this.defaultOperatorData.operatorType = 'ConstStateOperator'; } diff --git a/packages/flow/src/Diagrams/Operators/StateOperator/StateOperator.tsx b/packages/flow/src/Diagrams/Operators/StateOperator/StateOperator.tsx index c94fd59..d2c1f99 100644 --- a/packages/flow/src/Diagrams/Operators/StateOperator/StateOperator.tsx +++ b/packages/flow/src/Diagrams/Operators/StateOperator/StateOperator.tsx @@ -35,7 +35,7 @@ export class StateOperator constructor() { super({ - operatorName: 'StateOperator', + operatorName: 'State', operatorType: 'StateOperator', nodeType: NodeTypeEnum.Node, }); diff --git a/packages/flow/src/Diagrams/Operators/SumOperator/index.tsx b/packages/flow/src/Diagrams/Operators/SumOperator/index.tsx index 3672e0b..a778ffd 100644 --- a/packages/flow/src/Diagrams/Operators/SumOperator/index.tsx +++ b/packages/flow/src/Diagrams/Operators/SumOperator/index.tsx @@ -13,7 +13,7 @@ export class SumOperator constructor() { super({ - operatorName: 'SumOperator', + operatorName: 'Sum', operatorType: 'SumOperator', nodeType: NodeTypeEnum.Node, }); diff --git a/packages/flow/src/Diagrams/Operators/TransformOperator/index.tsx b/packages/flow/src/Diagrams/Operators/TransformOperator/index.tsx new file mode 100644 index 0000000..9c4147e --- /dev/null +++ b/packages/flow/src/Diagrams/Operators/TransformOperator/index.tsx @@ -0,0 +1,51 @@ +import { type Node } from 'reactflow'; +import { type IGenerationOption } from '../../Compiler/graph'; +import { NodeTypeEnum } from '../../Nodes/NodeTypeEnum'; +import { MetaOperator } from '../Operator'; +import { EndPoint, type ITranformOperatorData } from '../types'; + +export class TransformOperator + extends MetaOperator + implements MetaOperator +{ + constructor() { + super({ + operatorName: 'Transform', + operatorType: 'TransformOperator', + nodeType: NodeTypeEnum.Node, + }); + } + + create(): Node>> { + return super.create({ + endPointOptions: { + endPointList: [ + new EndPoint({ + type: 'source', + hint: 'output', + label: 'output', + }), + new EndPoint({ + type: 'target', + hint: 'input', + label: 'input', + }), + ], + }, + }); + } + + nodeColor?: string | undefined = '#F3F8FF'; + + generateBlockDeclarations(options: IGenerationOption): string[] { + return []; + } + + generateBlockOutput(options: IGenerationOption): string[] { + return []; + } + + generateBlockRelation(options: IGenerationOption): string[] { + return []; + } +} diff --git a/packages/flow/src/Diagrams/Operators/index.ts b/packages/flow/src/Diagrams/Operators/index.ts index 4787ee6..e900c53 100644 --- a/packages/flow/src/Diagrams/Operators/index.ts +++ b/packages/flow/src/Diagrams/Operators/index.ts @@ -3,15 +3,23 @@ import { InputOperator } from './InputOperator'; import { OutputOperator } from './OutputOperator'; import { SumOperator } from './SumOperator'; import { CustomOperator } from './CustomOperator'; +import { CombineOperator } from './CombineOperator'; +import { TransformOperator } from './TransformOperator'; +import { EffectOperator } from './EffectOperator'; -import { registerOperator } from './OperatorMap'; +import { registerOperators } from './OperatorMap'; -registerOperator(new InputOperator()); -registerOperator(new OutputOperator()); -registerOperator(new CustomOperator()); -registerOperator(new StateOperator()); -registerOperator(new ConstStateOperator()); -registerOperator(new SumOperator()); +registerOperators([ + new InputOperator(), + new OutputOperator(), + new CustomOperator(), + new StateOperator(), + new ConstStateOperator(), + new SumOperator(), + new CombineOperator(), + new TransformOperator(), + new EffectOperator(), +]); export { OperatorMap, diff --git a/packages/flow/src/Diagrams/Operators/types/index.ts b/packages/flow/src/Diagrams/Operators/types/index.ts index 67c6d82..be65ed1 100644 --- a/packages/flow/src/Diagrams/Operators/types/index.ts +++ b/packages/flow/src/Diagrams/Operators/types/index.ts @@ -115,8 +115,27 @@ export interface IStateOperatorData< valueType: StateOperatorValueTypeEnum; value: string | number | boolean | undefined; } + export interface ISumOperatorData< NodeOptions extends Record = Record, > extends IMetaOperatorData { // noop } + +export interface ICombineOperatorData< + NodeOptions extends Record = Record, +> extends IMetaOperatorData { + // noop +} + +export interface ITranformOperatorData< + NodeOptions extends Record = Record, +> extends IMetaOperatorData { + // noop +} + +export interface IEffectOperatorData< + NodeOptions extends Record = Record, +> extends IMetaOperatorData { + // noop +}