From 760c98150cdaf5b44db62a466c52d4d3037d51ff Mon Sep 17 00:00:00 2001 From: Aaron Date: Thu, 20 Jun 2024 16:42:54 +0800 Subject: [PATCH] refactor: remove CallableValue type (#5903) --- packages/g6/src/exports.ts | 1 - packages/g6/src/plugins/bubble-sets.ts | 4 +- packages/g6/src/plugins/hull/index.ts | 4 +- packages/g6/src/runtime/graph.ts | 39 ++++++++++++------- .../src/transforms/process-parallel-edges.ts | 4 +- packages/g6/src/types/callable.ts | 12 ------ 6 files changed, 30 insertions(+), 34 deletions(-) diff --git a/packages/g6/src/exports.ts b/packages/g6/src/exports.ts index 08b2d49d07..b4b5a01a57 100644 --- a/packages/g6/src/exports.ts +++ b/packages/g6/src/exports.ts @@ -190,7 +190,6 @@ export type { CustomPluginOption } from './spec/plugin'; export type { BaseTransformOptions } from './transforms'; export type { BaseElementStyleProps, - CallableValue, CardinalPlacement, CollapsedMarkerStyleProps, Combo, diff --git a/packages/g6/src/plugins/bubble-sets.ts b/packages/g6/src/plugins/bubble-sets.ts index bb3aa01dc0..fbb9ae4a77 100644 --- a/packages/g6/src/plugins/bubble-sets.ts +++ b/packages/g6/src/plugins/bubble-sets.ts @@ -7,7 +7,7 @@ import type { ContourStyleProps } from '../elements/shapes'; import { Contour } from '../elements/shapes'; import type { Graph } from '../runtime/graph'; import type { RuntimeContext } from '../runtime/types'; -import type { CallableValue, ID } from '../types'; +import type { ID } from '../types'; import { getBBoxHeight, getBBoxWidth } from '../utils/bbox'; import { arrayDiff } from '../utils/diff'; import type { ElementLifeCycleEvent } from '../utils/event'; @@ -225,7 +225,7 @@ export class BubbleSets extends BasePlugin { * Update member elements * @param members - 值或者回调函数 | value or callback function */ - public updateMember(members: CallableValue) { + public updateMember(members: ID[] | ((prev: ID[]) => ID[])) { this.options.members = isFunction(members) ? members(this.options.members) : members; this.drawBubbleSets(); } diff --git a/packages/g6/src/plugins/hull/index.ts b/packages/g6/src/plugins/hull/index.ts index 07f94a0f08..3cc1612834 100644 --- a/packages/g6/src/plugins/hull/index.ts +++ b/packages/g6/src/plugins/hull/index.ts @@ -4,7 +4,7 @@ import { GraphEvent } from '../../constants'; import type { ContourStyleProps } from '../../elements/shapes'; import { Contour } from '../../elements/shapes'; import type { RuntimeContext } from '../../runtime/types'; -import type { CallableValue, ID, Point } from '../../types'; +import type { ID, Point } from '../../types'; import type { ElementLifeCycleEvent } from '../../utils/event'; import { idOf } from '../../utils/id'; import { positionOf } from '../../utils/position'; @@ -185,7 +185,7 @@ export class Hull extends BasePlugin { * Update Hull member * @param members - 元素 Ids | Element Ids */ - public updateMember(members: CallableValue) { + public updateMember(members: ID[] | ((prev: ID[]) => ID[])) { this.options.members = isFunction(members) ? members(this.options.members) : members; this.shape.update(this.getHullStyle(true)); } diff --git a/packages/g6/src/runtime/graph.ts b/packages/g6/src/runtime/graph.ts index 804dcacab6..fd3d897874 100644 --- a/packages/g6/src/runtime/graph.ts +++ b/packages/g6/src/runtime/graph.ts @@ -23,7 +23,6 @@ import type { UpdateBehaviorOption } from '../spec/behavior'; import type { UpdatePluginOption } from '../spec/plugin'; import type { UpdateTransformOption } from '../spec/transform'; import type { - CallableValue, DataID, EdgeDirection, ElementDatum, @@ -262,7 +261,7 @@ export class Graph extends EventEmitter { * @param theme - 主题配置 | theme configuration * @apiCategory theme */ - public setTheme(theme: CallableValue): void { + public setTheme(theme: ThemeOptions | ((prev: ThemeOptions) => ThemeOptions)): void { this.options.theme = isFunction(theme) ? theme(this.getTheme()) : theme; const { background } = getExtension('theme', this.options.theme) || {}; @@ -278,7 +277,7 @@ export class Graph extends EventEmitter { * @param layout - 布局配置 | layout configuration * @apiCategory layout */ - public setLayout(layout: CallableValue): void { + public setLayout(layout: LayoutOptions | ((prev: LayoutOptions) => LayoutOptions)): void { this.options.layout = isFunction(layout) ? layout(this.getLayout()) : layout; } @@ -300,7 +299,7 @@ export class Graph extends EventEmitter { * @param behaviors - 交互配置 | behavior configuration * @apiCategory behavior */ - public setBehaviors(behaviors: CallableValue): void { + public setBehaviors(behaviors: BehaviorOptions | ((prev: BehaviorOptions) => BehaviorOptions)): void { this.options.behaviors = isFunction(behaviors) ? behaviors(this.getBehaviors()) : behaviors; this.context.behavior?.setBehaviors(this.options.behaviors); } @@ -341,7 +340,7 @@ export class Graph extends EventEmitter { * @param plugins - 插件配置 | plugin configuration * @apiCategory plugin */ - public setPlugins(plugins: CallableValue): void { + public setPlugins(plugins: PluginOptions | ((prev: PluginOptions) => PluginOptions)): void { this.options.plugins = isFunction(plugins) ? plugins(this.getPlugins()) : plugins; this.context.plugin?.setPlugins(this.options.plugins); } @@ -394,7 +393,7 @@ export class Graph extends EventEmitter { * @param transforms - 数据转换配置 | data transform configuration * @apiCategory transform */ - public setTransforms(transforms: CallableValue): void { + public setTransforms(transforms: TransformOptions | ((prev: TransformOptions) => TransformOptions)): void { this.options.transforms = isFunction(transforms) ? transforms(this.getTransforms()) : transforms; this.context.transform?.setTransforms(this.options.transforms); } @@ -565,7 +564,7 @@ export class Graph extends EventEmitter { * @param data - 数据 | data * @apiCategory data */ - public setData(data: CallableValue): void { + public setData(data: GraphData | ((prev: GraphData) => GraphData)): void { this.context.model.setData(isFunction(data) ? data(this.getData()) : data); } @@ -576,7 +575,7 @@ export class Graph extends EventEmitter { * @param data - 元素数据 | element data * @apiCategory data */ - public addData(data: CallableValue): void { + public addData(data: GraphData | ((prev: GraphData) => GraphData)): void { this.context.model.addData(isFunction(data) ? data(this.getData()) : data); } @@ -587,7 +586,7 @@ export class Graph extends EventEmitter { * @param data - 节点数据 | node data * @apiCategory data */ - public addNodeData(data: CallableValue): void { + public addNodeData(data: NodeData[] | ((prev: NodeData[]) => NodeData[])): void { this.context.model.addNodeData(isFunction(data) ? data(this.getNodeData()) : data); } @@ -598,7 +597,7 @@ export class Graph extends EventEmitter { * @param data - 边数据 | edge data * @apiCategory data */ - public addEdgeData(data: CallableValue): void { + public addEdgeData(data: EdgeData[] | ((prev: EdgeData[]) => EdgeData[])): void { this.context.model.addEdgeData(isFunction(data) ? data(this.getEdgeData()) : data); } @@ -609,7 +608,7 @@ export class Graph extends EventEmitter { * @param data - 组合数据 | combo data * @apiCategory data */ - public addComboData(data: CallableValue): void { + public addComboData(data: ComboData[] | ((prev: ComboData[]) => ComboData[])): void { this.context.model.addComboData(isFunction(data) ? data(this.getComboData()) : data); } @@ -620,7 +619,7 @@ export class Graph extends EventEmitter { * @param data - 元素数据 | element data * @apiCategory data */ - public updateData(data: CallableValue): void { + public updateData(data: PartialGraphData | ((prev: PartialGraphData) => PartialGraphData)): void { this.context.model.updateData(isFunction(data) ? data(this.getData()) : data); } @@ -631,7 +630,11 @@ export class Graph extends EventEmitter { * @param data - 节点数据 | node data * @apiCategory data */ - public updateNodeData(data: CallableValue[]>): void { + public updateNodeData( + data: + | PartialNodeLikeData[] + | ((prev: PartialNodeLikeData[]) => PartialNodeLikeData[]), + ): void { this.context.model.updateNodeData(isFunction(data) ? data(this.getNodeData()) : data); } @@ -642,7 +645,9 @@ export class Graph extends EventEmitter { * @param data - 边数据 | edge data * @apiCategory data */ - public updateEdgeData(data: CallableValue[]>): void { + public updateEdgeData( + data: PartialEdgeData[] | ((prev: PartialEdgeData[]) => PartialEdgeData[]), + ): void { this.context.model.updateEdgeData(isFunction(data) ? data(this.getEdgeData()) : data); } @@ -653,7 +658,11 @@ export class Graph extends EventEmitter { * @param data - 组合数据 | combo data * @apiCategory data */ - public updateComboData(data: CallableValue[]>): void { + public updateComboData( + data: + | PartialNodeLikeData[] + | ((prev: PartialNodeLikeData[]) => PartialNodeLikeData[]), + ): void { this.context.model.updateComboData(isFunction(data) ? data(this.getComboData()) : data); } diff --git a/packages/g6/src/transforms/process-parallel-edges.ts b/packages/g6/src/transforms/process-parallel-edges.ts index 05758f4594..b014d7fdd2 100644 --- a/packages/g6/src/transforms/process-parallel-edges.ts +++ b/packages/g6/src/transforms/process-parallel-edges.ts @@ -2,7 +2,7 @@ import type { PathStyleProps } from '@antv/g'; import { isBoolean, isEmpty, isEqual, isFunction } from '@antv/util'; import type { RuntimeContext } from '../runtime/types'; import type { EdgeData } from '../spec'; -import type { CallableValue, ID, LoopPlacement, NodeLikeData } from '../types'; +import type { ID, LoopPlacement, NodeLikeData } from '../types'; import { groupByChangeType, reduceDataChanges } from '../utils/change'; import { idOf } from '../utils/id'; import { reassignTo } from '../utils/transform'; @@ -55,7 +55,7 @@ export interface ProcessParallelEdgesOptions extends BaseTransformOptions { * * The style of the merged edge, only valid for merging mode */ - style?: CallableValue; + style?: PathStyleProps | ((prev: EdgeData[]) => PathStyleProps); } /** diff --git a/packages/g6/src/types/callable.ts b/packages/g6/src/types/callable.ts index 4100b82709..fc1e093996 100644 --- a/packages/g6/src/types/callable.ts +++ b/packages/g6/src/types/callable.ts @@ -1,15 +1,3 @@ -/** - * 可回调值 - * - * Callable value - * @example - * type Prop = number; - * type CallableProp = Callable; - * const prop1: CallableProp = 1; - * const prop2: CallableProp = (value) => value; - */ -export type CallableValue = Returns | ((args: Param) => Returns); - /** * 可回调对象 *