1 | import { assign, isNil, isObject, uniqueId } from '@antv/util';
|
2 | import EventEmitter from 'wolfy87-eventemitter';
|
3 | import { View, ViewOptions } from './view';
|
4 | import CONSTANTS from './constants';
|
5 |
|
6 | export interface DataSetOptions {
|
7 | state: Record<string, any>;
|
8 | }
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | export class DataSet extends EventEmitter {
|
15 | |
16 |
|
17 |
|
18 | static CONSTANTS = CONSTANTS;
|
19 |
|
20 | |
21 |
|
22 |
|
23 | static connectors: Record<string, any> = {};
|
24 |
|
25 | |
26 |
|
27 |
|
28 | static transforms: Record<string, any> = {};
|
29 |
|
30 | |
31 |
|
32 |
|
33 |
|
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 |
|
45 |
|
46 |
|
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;
|
59 |
|
60 | static View: typeof View = View;
|
61 |
|
62 | static version = '____DATASET_VERSION____';
|
63 |
|
64 | |
65 |
|
66 |
|
67 | isDataSet = true;
|
68 |
|
69 | private _onChangeTimer: null | number = null;
|
70 | |
71 |
|
72 |
|
73 | views: Record<string, View> = {};
|
74 | |
75 |
|
76 |
|
77 | state: Record<string, any> = {};
|
78 |
|
79 | |
80 |
|
81 |
|
82 | constructor(initialProps: DataSetOptions = { state: {} }) {
|
83 | super();
|
84 |
|
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 |
|
99 |
|
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 |
|
121 |
|
122 |
|
123 | getView(name: string): View {
|
124 | return this.views[name];
|
125 | }
|
126 |
|
127 | |
128 |
|
129 |
|
130 |
|
131 |
|
132 | setView(name: string, view: View): void {
|
133 | this.views[name] = view;
|
134 | }
|
135 |
|
136 | |
137 |
|
138 |
|
139 |
|
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);
|
150 | }
|
151 | }
|
152 |
|
153 |
|
154 | assign(DataSet, CONSTANTS);
|
155 |
|
156 |
|
157 | assign(DataSet.prototype, {
|
158 | view: DataSet.prototype.createView,
|
159 | });
|
160 |
|
161 | View.DataSet = DataSet;
|