refactor: remove CallableValue type (#5903)

This commit is contained in:
Aaron 2024-06-20 16:42:54 +08:00 committed by GitHub
parent 79bb790299
commit 760c98150c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 30 additions and 34 deletions

View File

@ -190,7 +190,6 @@ export type { CustomPluginOption } from './spec/plugin';
export type { BaseTransformOptions } from './transforms';
export type {
BaseElementStyleProps,
CallableValue,
CardinalPlacement,
CollapsedMarkerStyleProps,
Combo,

View File

@ -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<BubbleSetsOptions> {
* <en/> Update member elements
* @param members - <zh/> | <en/> value or callback function
*/
public updateMember(members: CallableValue<ID[]>) {
public updateMember(members: ID[] | ((prev: ID[]) => ID[])) {
this.options.members = isFunction(members) ? members(this.options.members) : members;
this.drawBubbleSets();
}

View File

@ -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<HullOptions> {
* <en/> Update Hull member
* @param members - <zh/> Ids | <en/> Element Ids
*/
public updateMember(members: CallableValue<ID[]>) {
public updateMember(members: ID[] | ((prev: ID[]) => ID[])) {
this.options.members = isFunction(members) ? members(this.options.members) : members;
this.shape.update(this.getHullStyle(true));
}

View File

@ -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 - <zh/> | <en/> theme configuration
* @apiCategory theme
*/
public setTheme(theme: CallableValue<ThemeOptions>): 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 - <zh/> | <en/> layout configuration
* @apiCategory layout
*/
public setLayout(layout: CallableValue<LayoutOptions>): 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 - <zh/> | <en/> behavior configuration
* @apiCategory behavior
*/
public setBehaviors(behaviors: CallableValue<BehaviorOptions>): 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 - <zh/> | <en/> plugin configuration
* @apiCategory plugin
*/
public setPlugins(plugins: CallableValue<PluginOptions>): 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 - <zh/> | <en/> data transform configuration
* @apiCategory transform
*/
public setTransforms(transforms: CallableValue<TransformOptions>): 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 - <zh/> | <en/> data
* @apiCategory data
*/
public setData(data: CallableValue<GraphData>): 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 - <zh/> | <en/> element data
* @apiCategory data
*/
public addData(data: CallableValue<GraphData>): 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 - <zh/> | <en/> node data
* @apiCategory data
*/
public addNodeData(data: CallableValue<NodeData[]>): 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 - <zh/> | <en/> edge data
* @apiCategory data
*/
public addEdgeData(data: CallableValue<EdgeData[]>): 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 - <zh/> | <en/> combo data
* @apiCategory data
*/
public addComboData(data: CallableValue<ComboData[]>): 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 - <zh/> | <en/> element data
* @apiCategory data
*/
public updateData(data: CallableValue<PartialGraphData>): 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 - <zh/> | <en/> node data
* @apiCategory data
*/
public updateNodeData(data: CallableValue<PartialNodeLikeData<NodeData>[]>): void {
public updateNodeData(
data:
| PartialNodeLikeData<NodeData>[]
| ((prev: PartialNodeLikeData<NodeData>[]) => PartialNodeLikeData<NodeData>[]),
): void {
this.context.model.updateNodeData(isFunction(data) ? data(this.getNodeData()) : data);
}
@ -642,7 +645,9 @@ export class Graph extends EventEmitter {
* @param data - <zh/> | <en/> edge data
* @apiCategory data
*/
public updateEdgeData(data: CallableValue<PartialEdgeData<EdgeData>[]>): void {
public updateEdgeData(
data: PartialEdgeData<EdgeData>[] | ((prev: PartialEdgeData<EdgeData>[]) => PartialEdgeData<EdgeData>[]),
): void {
this.context.model.updateEdgeData(isFunction(data) ? data(this.getEdgeData()) : data);
}
@ -653,7 +658,11 @@ export class Graph extends EventEmitter {
* @param data - <zh/> | <en/> combo data
* @apiCategory data
*/
public updateComboData(data: CallableValue<PartialNodeLikeData<ComboData>[]>): void {
public updateComboData(
data:
| PartialNodeLikeData<ComboData>[]
| ((prev: PartialNodeLikeData<ComboData>[]) => PartialNodeLikeData<ComboData>[]),
): void {
this.context.model.updateComboData(isFunction(data) ? data(this.getComboData()) : data);
}

View File

@ -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 {
*
* <en/> The style of the merged edge, only valid for merging mode
*/
style?: CallableValue<PathStyleProps, EdgeData[]>;
style?: PathStyleProps | ((prev: EdgeData[]) => PathStyleProps);
}
/**

View File

@ -1,15 +1,3 @@
/**
* <zh/>
*
* <en/> Callable value
* @example
* type Prop = number;
* type CallableProp = Callable<Prop>;
* const prop1: CallableProp = 1;
* const prop2: CallableProp = (value) => value;
*/
export type CallableValue<Returns, Param = Returns> = Returns | ((args: Param) => Returns);
/**
* <zh/>
*