UNPKG

4.07 kBPlain TextView Raw
1import { assign, isNil, isObject, uniqueId } from '@antv/util';
2import EventEmitter from 'wolfy87-eventemitter';
3import { View, ViewOptions } from './view';
4import CONSTANTS from './constants';
5
6export interface DataSetOptions {
7 state: Record<string, any>;
8}
9
10/**
11 * 数据集
12 * @public
13 */
14export class DataSet extends EventEmitter {
15 /**
16 * 常量,譬如 DataSet.CONSTANTS.HIERARCHY 是树形结构的名称
17 */
18 static CONSTANTS = CONSTANTS;
19
20 /**
21 * 注册的 Connector(key-value 对)
22 */
23 static connectors: Record<string, any> = {};
24
25 /**
26 * 已注册的 Transform(key-value 对)
27 */
28 static transforms: Record<string, any> = {};
29
30 /**
31 * 注册一个数据连接函数,注册后所有数据视图都可以使用 name 来引用这个数据连接函数,从而接入某种数据源。
32 * @param name - 类型
33 * @param connector - 解析逻辑
34 */
35 static registerConnector(name: string, connector: (data: any, options: any, view: View) => any): void {
36 DataSet.connectors[name] = connector;
37 }
38
39 static getConnector(name: string): Function {
40 return DataSet.connectors[name] || DataSet.connectors.default;
41 }
42
43 /**
44 * 注册一个数据处理函数,注册后所有数据视图都可以使用 name 来引用这个数据处理函数,从而进行某种数据处理
45 * @param name - transform 类型
46 * @param transform - transform逻辑
47 */
48 static registerTransform(name: string, transform: any): void {
49 DataSet.transforms[name] = transform;
50 }
51
52 static getTransform(name?: string): Function {
53 return DataSet.transforms[name] || DataSet.transforms.default;
54 }
55
56 static DataSet: typeof DataSet = DataSet;
57
58 static DataView: typeof View = View; // alias
59
60 static View: typeof View = View;
61
62 static version = '____DATASET_VERSION____';
63
64 /**
65 * 否是 DataSet
66 */
67 isDataSet = true;
68
69 private _onChangeTimer: null | number = null;
70 /**
71 * 所有挂在数据集上的数据视图(key-value 对)
72 */
73 views: Record<string, View> = {};
74 /**
75 * 存储数据集上的状态量(key-value 对)
76 */
77 state: Record<string, any> = {};
78
79 /**
80 * @param initialProps - 初始状态
81 */
82 constructor(initialProps: DataSetOptions = { state: {} }) {
83 super();
84 // assign(me, initialProps);
85 this.state = initialProps.state;
86 }
87
88 private _getUniqueViewName(): string {
89 let name = uniqueId('view_');
90 while (this.views[name]) {
91 name = uniqueId('view_');
92 }
93 return name;
94 }
95
96 /**
97 * 创建并返回一个数据视图实例
98 * @param name - 数据视图名称
99 * @param options - 视图配置
100 */
101 createView(name: ViewOptions): View;
102 createView(name?: string, options?: ViewOptions): View;
103 createView(name: any, options?: any): View {
104 if (isNil(name)) {
105 name = this._getUniqueViewName();
106 }
107 if (isObject(name)) {
108 options = name;
109 name = this._getUniqueViewName();
110 }
111 if (this.views[name]) {
112 throw new Error(`data view exists: ${name}`);
113 }
114 const view = new View(this, options);
115 this.views[name] = view;
116 return view;
117 }
118
119 /**
120 * 返回 name 对应的数据视图实例
121 * @param name - name
122 */
123 getView(name: string): View {
124 return this.views[name];
125 }
126
127 /**
128 * 设置 name 对应的数据视图实例为 dv
129 * @param name - 名称
130 * @param view - data view
131 */
132 setView(name: string, view: View): void {
133 this.views[name] = view;
134 }
135
136 /**
137 * 设置状态量 name 的值为 value
138 * @param name - 状态名
139 * @param value - 值
140 */
141 setState(name: string, value: any): void {
142 this.state[name] = value;
143 if (this._onChangeTimer) {
144 window.clearTimeout(this._onChangeTimer);
145 this._onChangeTimer = null;
146 }
147 this._onChangeTimer = window.setTimeout(() => {
148 this.emit('statechange', name, value);
149 }, 16); // execute after one frame
150 }
151}
152
153// @ts-ignore
154assign(DataSet, CONSTANTS);
155
156// @ts-ignore
157assign(DataSet.prototype, {
158 view: DataSet.prototype.createView, // alias
159});
160
161View.DataSet = DataSet;