-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
create_handlers.ts
148 lines (127 loc) · 4.32 KB
/
create_handlers.ts
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
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { isEqual } from 'lodash';
import {
ExpressionRendererEvent,
IInterpreterRenderHandlers,
} from '@kbn/expressions-plugin/public';
import { updateEmbeddableExpression, fetchEmbeddableRenderable } from '../state/actions/embeddable';
import { RendererHandlers, CanvasElement } from '../../types';
import { getCanvasFiltersService } from '../services/canvas_filters_service';
import { clearValue } from '../state/actions/resolved_args';
// @ts-expect-error unconverted file
import { fetchAllRenderables } from '../state/actions/elements';
// This class creates stub handlers to ensure every element and renderer fulfills the contract.
// TODO: consider warning if these methods are invoked but not implemented by the renderer...?
// We need to move towards only using these handlers and ditching our canvas specific ones
export const createBaseHandlers = (): IInterpreterRenderHandlers => ({
done() {},
reload() {},
update() {},
event() {},
onDestroy() {},
getRenderMode: () => 'view',
isSyncColorsEnabled: () => false,
isSyncTooltipsEnabled: () => false,
isSyncCursorEnabled: () => true,
isInteractive: () => true,
getExecutionContext: () => undefined,
});
export const createHandlers = (baseHandlers = createBaseHandlers()): RendererHandlers => ({
...baseHandlers,
destroy() {},
getElementId() {
return '';
},
getFilter() {
return '';
},
onComplete(fn: () => void) {
this.done = fn;
},
onDestroy(fn: () => void) {
this.destroy = fn;
},
// TODO: these functions do not match the `onXYZ` and `xyz` pattern elsewhere.
onEmbeddableDestroyed() {},
onEmbeddableInputChange() {},
onResize(fn: (size: { height: number; width: number }) => void) {
this.resize = fn;
},
resize(_size: { height: number; width: number }) {},
});
export const assignHandlers = (handlers: Partial<RendererHandlers> = {}): RendererHandlers =>
Object.assign(createHandlers(), handlers);
// TODO: this is a legacy approach we should unravel in the near future.
export const createDispatchedHandlerFactory = (
dispatch: (action: any) => void
): ((element: CanvasElement) => RendererHandlers) => {
let isComplete = false;
let oldElement: CanvasElement | undefined;
let completeFn = () => {};
return (element: CanvasElement) => {
// reset isComplete when element changes
if (!isEqual(oldElement, element)) {
isComplete = false;
oldElement = element;
}
const filters = getCanvasFiltersService();
const handlers: RendererHandlers & {
event: IInterpreterRenderHandlers['event'];
done: IInterpreterRenderHandlers['done'];
} = {
...createHandlers(),
event(event: ExpressionRendererEvent) {
switch (event.name) {
case 'embeddableInputChange':
this.onEmbeddableInputChange(event.data);
break;
case 'applyFilterAction':
filters.updateFilter(element.id, event.data);
dispatch(fetchAllRenderables());
break;
case 'onComplete':
this.onComplete(event.data);
break;
case 'embeddableDestroyed':
this.onEmbeddableDestroyed();
break;
case 'resize':
this.resize(event.data);
break;
case 'onResize':
this.onResize(event.data);
break;
}
},
getFilter() {
return element.filter || '';
},
onComplete(fn: () => void) {
completeFn = fn;
},
getElementId: () => element.id,
onEmbeddableInputChange(embeddableExpression: string) {
dispatch(updateEmbeddableExpression({ elementId: element.id, embeddableExpression }));
},
onEmbeddableDestroyed() {
const argumentPath = [element.id, 'expressionRenderable'];
dispatch(clearValue({ path: argumentPath }));
dispatch(fetchEmbeddableRenderable(element.id));
},
done() {
// don't emit if the element is already done
if (isComplete) {
return;
}
isComplete = true;
completeFn();
},
};
return handlers;
};
};