forked from deephaven/web-client-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIrisGridProxyModel.ts
More file actions
487 lines (405 loc) · 14.3 KB
/
IrisGridProxyModel.ts
File metadata and controls
487 lines (405 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
import deepEqual from 'fast-deep-equal';
import { Formatter, TableUtils } from '@deephaven/jsapi-utils';
import Log from '@deephaven/log';
import {
type CancelablePromise,
EventShimCustomEvent,
PromiseUtils,
} from '@deephaven/utils';
import type { dh as DhType } from '@deephaven/jsapi-types';
import IrisGridTableModel from './IrisGridTableModel';
import IrisGridPartitionedTableModel from './IrisGridPartitionedTableModel';
import IrisGridTreeTableModel from './IrisGridTreeTableModel';
import IrisGridModel from './IrisGridModel';
import { type ColumnName, type UITotalsTableConfig } from './CommonTypes';
import { isIrisGridTableModelTemplate } from './IrisGridTableModelTemplate';
import {
type PartitionConfig,
type PartitionedGridModel,
isPartitionedGridModelProvider,
isPartitionedGridModel,
} from './PartitionedGridModel';
const log = Log.module('IrisGridProxyModel');
function makeModel(
dh: typeof DhType,
table: DhType.Table | DhType.TreeTable | DhType.PartitionedTable,
formatter?: Formatter,
inputTable?: DhType.InputTable | null
): IrisGridModel {
if (TableUtils.isTreeTable(table)) {
return new IrisGridTreeTableModel(dh, table, formatter);
}
if (TableUtils.isPartitionedTable(table)) {
return new IrisGridPartitionedTableModel(dh, table, formatter);
}
return new IrisGridTableModel(dh, table, formatter, inputTable);
}
/**
* Model which proxies calls to other IrisGridModels.
* This allows for operations that generate new tables, like rollups.
* The proxy model will call any methods it has implemented and delegate any
* it does not implement to the underlying model.
*/
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
class IrisGridProxyModel extends IrisGridModel implements PartitionedGridModel {
/**
* @param dh JSAPI instance
* @param table Iris data table to be used in the model
* @param formatter The formatter to use when getting formats
* @param inputTable Iris input table associated with this table
*/
model: IrisGridModel;
dh: typeof DhType;
private originalModel: IrisGridModel;
private modelPromise: CancelablePromise<IrisGridModel> | null;
private rollup: DhType.RollupConfig | null;
private partition: PartitionConfig | null;
private selectDistinct: ColumnName[];
private currentViewport?: {
top: number;
bottom: number;
columns?: DhType.Column[];
};
constructor(
dh: typeof DhType,
table: DhType.Table | DhType.TreeTable | DhType.PartitionedTable,
formatter = new Formatter(dh),
inputTable: DhType.InputTable | null = null
) {
super(dh);
// The EventTarget methods must be bound to this instance
// Otherwise they throw errors because of the Proxy
this.addEventListener = this.addEventListener.bind(this);
this.removeEventListener = this.removeEventListener.bind(this);
this.dispatchEvent = this.dispatchEvent.bind(this);
this.handleModelEvent = this.handleModelEvent.bind(this);
const model = makeModel(dh, table, formatter, inputTable);
this.dh = dh;
this.originalModel = model;
this.model = model;
this.modelPromise = null;
this.rollup = null;
this.partition = null;
this.selectDistinct = [];
// eslint-disable-next-line no-constructor-return
return new Proxy(this, {
// We want to use any properties on the proxy model if defined
// If not, then proxy to the underlying model
get(target, prop, receiver) {
// Does this class have a getter for the prop
// Getter functions are on the prototype
const proxyHasGetter =
Object.getOwnPropertyDescriptor(Object.getPrototypeOf(target), prop)
?.get != null;
if (proxyHasGetter) {
return Reflect.get(target, prop, receiver);
}
// Does this class implement the property
const proxyHasProp = Object.prototype.hasOwnProperty.call(target, prop);
// Does the class implement a function for the property
const proxyHasFn = Object.prototype.hasOwnProperty.call(
Object.getPrototypeOf(target),
prop
);
const trueTarget = proxyHasProp || proxyHasFn ? target : target.model;
return Reflect.get(trueTarget, prop);
},
set(target, prop, value) {
const proxyHasSetter =
Object.getOwnPropertyDescriptor(Object.getPrototypeOf(target), prop)
?.set != null;
const proxyHasProp = Object.prototype.hasOwnProperty.call(target, prop);
if (proxyHasSetter || proxyHasProp) {
return Reflect.set(target, prop, value, target);
}
return Reflect.set(target.model, prop, value, target.model);
},
});
}
close(): void {
this.originalModel.close();
if (this.model !== this.originalModel) {
this.model.close();
}
if (this.modelPromise != null) {
this.modelPromise.cancel();
}
}
handleModelEvent(event: CustomEvent): void {
log.debug2('handleModelEvent', event);
const { detail, type } = event;
this.dispatchEvent(new EventShimCustomEvent(type, { detail }));
}
setModel(model: IrisGridModel): void {
log.debug('setModel', model);
const oldModel = this.model;
const { columns: oldColumns } = oldModel;
if (oldModel !== this.originalModel) {
oldModel.close();
}
this.model = model;
if (this.listenerCount > 0) {
this.addListeners(model);
}
if (oldColumns !== model.columns) {
this.dispatchEvent(
new EventShimCustomEvent(IrisGridModel.EVENT.COLUMNS_CHANGED, {
detail: model.columns,
})
);
} else if (this.currentViewport != null) {
// If the columns haven't changed, the current viewport should still valid, and needs to be set on the new model
const { top, bottom, columns } = this.currentViewport;
model.setViewport(top, bottom, columns);
}
if (isIrisGridTableModelTemplate(model)) {
this.dispatchEvent(
new EventShimCustomEvent(IrisGridModel.EVENT.TABLE_CHANGED, {
detail: model.table,
})
);
}
}
setNextModel(modelPromise: Promise<IrisGridModel>): void {
log.debug2('setNextModel');
if (this.modelPromise) {
this.modelPromise.cancel();
}
if (this.listenerCount > 0) {
this.removeListeners(this.model);
}
this.modelPromise = PromiseUtils.makeCancelable(
modelPromise,
(model: IrisGridModel) => model.close()
);
this.modelPromise
.then(model => {
this.modelPromise = null;
this.setModel(model);
})
.catch((err: unknown) => {
if (PromiseUtils.isCanceled(err)) {
log.debug2('setNextModel cancelled');
return;
}
log.error('Unable to set next model', err);
this.modelPromise = null;
this.dispatchEvent(
new EventShimCustomEvent(IrisGridModel.EVENT.REQUEST_FAILED, {
detail: err,
})
);
});
}
startListening(): void {
super.startListening();
this.addListeners(this.model);
}
stopListening(): void {
super.stopListening();
this.removeListeners(this.model);
}
addListeners(model: IrisGridModel): void {
const events = Object.keys(IrisGridModel.EVENT);
for (let i = 0; i < events.length; i += 1) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
model.addEventListener(events[i], this.handleModelEvent);
}
}
removeListeners(model: IrisGridModel): void {
const events = Object.keys(IrisGridModel.EVENT);
for (let i = 0; i < events.length; i += 1) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
model.removeEventListener(events[i], this.handleModelEvent);
}
}
get isCustomColumnsAvailable(): boolean {
return (
this.model.isCustomColumnsAvailable &&
// Disable for selectDistinct tables
!(this.isSelectDistinctAvailable && this.selectDistinctColumns.length > 0)
);
}
get isRollupAvailable(): boolean {
return (
(this.originalModel.isRollupAvailable || this.rollup != null) &&
this.selectDistinct.length === 0
);
}
get isSelectDistinctAvailable(): boolean {
return (
(this.originalModel.isSelectDistinctAvailable ||
this.selectDistinct.length > 0) &&
this.rollup == null
);
}
get originalColumns(): readonly DhType.Column[] {
return this.originalModel.columns;
}
get partitionColumns(): readonly DhType.Column[] {
if (!isPartitionedGridModelProvider(this.originalModel)) {
return [];
}
return this.originalModel.partitionColumns;
}
get partitionConfig(): PartitionConfig | null {
if (
!isPartitionedGridModelProvider(this.originalModel) ||
!this.originalModel.isPartitionRequired
) {
if (!isPartitionedGridModel(this.originalModel)) {
// @ts-expect-error If the original model has an undefined partitionConfig return undefined to make the proxy model also return false in isPartitionedGridModel
return undefined;
}
return null;
}
return this.partition;
}
set partitionConfig(partitionConfig: PartitionConfig | null) {
if (!this.isPartitionRequired) {
throw new Error('Partitions are not available');
}
log.debug('set partitionConfig', partitionConfig);
this.partition = partitionConfig;
let modelPromise = Promise.resolve(this.originalModel);
if (
partitionConfig != null &&
isPartitionedGridModelProvider(this.originalModel)
) {
if (partitionConfig.mode === 'keys') {
modelPromise = this.originalModel
.partitionBaseTable()
.then(table => makeModel(this.dh, table, this.formatter));
} else if (partitionConfig.mode === 'merged') {
modelPromise = this.originalModel
.partitionMergedTable()
.then(table => makeModel(this.dh, table, this.formatter));
} else {
modelPromise = this.originalModel
.partitionTable(partitionConfig.partitions)
.then(table => makeModel(this.dh, table, this.formatter));
}
}
this.setNextModel(modelPromise);
}
partitionKeysTable(): Promise<DhType.Table> {
if (!isPartitionedGridModelProvider(this.originalModel)) {
throw new Error('Partitions are not available');
}
return this.originalModel.partitionKeysTable();
}
partitionMergedTable(): Promise<DhType.Table> {
if (!isPartitionedGridModelProvider(this.originalModel)) {
throw new Error('Partitions are not available');
}
return this.originalModel.partitionMergedTable();
}
partitionTable(partitions: unknown[]): Promise<DhType.Table> {
if (!isPartitionedGridModelProvider(this.originalModel)) {
throw new Error('Partitions are not available');
}
return this.originalModel.partitionTable(partitions);
}
get rollupConfig(): DhType.RollupConfig | null {
return this.rollup;
}
set rollupConfig(rollupConfig: DhType.RollupConfig | null) {
log.debug('set rollupConfig', rollupConfig);
if (!this.isRollupAvailable) {
throw new Error('Rollup Rows are not available');
}
// Prevent model update when IrisGridModelUpdater is mounted
// if rollup is already initialized in IrisGridPanel
if (deepEqual(rollupConfig, this.rollup)) {
return;
}
this.rollup = rollupConfig;
let modelPromise = Promise.resolve(this.originalModel);
if (
isIrisGridTableModelTemplate(this.originalModel) &&
rollupConfig != null
) {
modelPromise = this.originalModel.table
.rollup(rollupConfig)
.then(table => makeModel(this.dh, table, this.formatter));
}
this.setNextModel(modelPromise);
}
get selectDistinctColumns(): ColumnName[] {
return this.selectDistinct;
}
set selectDistinctColumns(columnNames: string[]) {
log.debug('set selectDistinctColumns', columnNames);
if (!this.isSelectDistinctAvailable) {
throw new Error('Select distinct is not available');
}
if (
columnNames === this.selectDistinctColumns ||
(columnNames.length === 0 && this.selectDistinctColumns.length === 0)
) {
log.debug('Ignore same selectDistinctColumns', columnNames);
return;
}
this.selectDistinct = columnNames;
const selectDistinctColumns = columnNames
.map(name => this.originalColumns.find(column => column.name === name))
.filter(column => column != null) as DhType.Column[];
let modelPromise = Promise.resolve(this.originalModel);
if (
isIrisGridTableModelTemplate(this.originalModel) &&
selectDistinctColumns.length > 0
) {
modelPromise = this.originalModel.table
.selectDistinct(selectDistinctColumns)
.then(table => makeModel(this.dh, table, this.formatter));
}
this.setNextModel(modelPromise);
}
set totalsConfig(totalsConfig: UITotalsTableConfig | null) {
if (this.modelPromise != null) {
// Model switch in progress. Don't forward, as the config may reference stale columns.
// COLUMNS_CHANGED will reapply the correct config after.
return;
}
this.model.totalsConfig = totalsConfig;
}
get isFilterRequired(): boolean {
return this.originalModel.isFilterRequired;
}
get isPartitionRequired(): boolean {
// @ts-expect-error If the original model is not a partitioned model return undefined to make the proxy model also return false in isPartitionedGridModelProvider
return isPartitionedGridModelProvider(this.originalModel)
? this.originalModel.isPartitionRequired
: undefined;
}
get isPartitionAwareSourceTable(): boolean {
return (
isPartitionedGridModelProvider(this.originalModel) &&
this.originalModel.isPartitionAwareSourceTable
);
}
get formatter(): Formatter {
return this.originalModel.formatter;
}
set formatter(formatter: Formatter) {
this.originalModel.formatter = formatter;
}
get columnAlignmentMap(): ReadonlyMap<string, CanvasTextAlign> {
return this.originalModel.columnAlignmentMap;
}
set columnAlignmentMap(columnAlignmentMap: Map<string, CanvasTextAlign>) {
this.originalModel.columnAlignmentMap = columnAlignmentMap;
}
setViewport = (
top: number,
bottom: number,
columns?: DhType.Column[]
): void => {
this.currentViewport = { top, bottom, columns };
this.model.setViewport(top, bottom, columns);
};
}
export default IrisGridProxyModel;