-
-
Notifications
You must be signed in to change notification settings - Fork 310
/
plugin-props-factory.ts
111 lines (92 loc) · 4.34 KB
/
plugin-props-factory.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
import { Injectable, NgZone } from '@angular/core';
import { Store } from '@ngrx/store';
import { PluginInstance, isAppLevelPluginType, PluginComponentDataProps } from './plugin';
import { map, distinctUntilChanged } from 'rxjs/operators';
import * as fromRoot from '../../reducers';
import * as queryActions from '../../actions/query/query';
import * as variableActions from '../../actions/variables/variables';
import { Observable, Subscription } from 'rxjs';
import { WindowService } from '../window.service';
import { ExportWindowState } from 'app/reducers/windows';
import { PluginEventService, PluginEvent } from './plugin-event.service';
import is_electron from 'app/utils/is_electron';
interface GetPluginPropsOptions {
windowId?: string;
}
type CreateWindowOptions = Pick<ExportWindowState, 'windowName' | 'apiUrl' | 'query' | 'headers' | 'variables' | 'subscriptionUrl'>
@Injectable()
export class PluginPropsFactory {
constructor(
private store: Store<fromRoot.State>,
private zone: NgZone,
private windowService: WindowService,
private pluginEvent: PluginEventService,
) {}
getPluginProps(plugin: PluginInstance, { windowId }: GetPluginPropsOptions = {}): Observable<PluginComponentDataProps> {
if (isAppLevelPluginType(plugin.type)) {
return this.store.pipe(
distinctUntilChanged(),
map(state => {
return {
ctx: {
setQuery: (_windowId: string, query: string) => this.zone.run(() => this.setQuery(_windowId, query)),
getQuery: (_windowId: string) => state.windows[_windowId].query.query || '',
setVariables: (_windowId: string, variables: string) => this.zone.run(() => this.setVariables(_windowId, variables)),
getVariables: (_windowId: string) => state.windows[_windowId].variables.variables,
setEndpoint: (_windowId: string, endpoint: string) => this.zone.run(() => this.setEndpoint(_windowId, endpoint)),
getEndpoint: (_windowId: string) => state.windows[_windowId].query.url,
getSDL: (_windowId: string) => state.windows[_windowId].schema.sdl,
createWindow: (options: CreateWindowOptions) => this.zone.run(() => this.createWindow(options)),
on: (ev: PluginEvent, handler: () => Subscription) => this.pluginEvent.on(ev, handler),
isElectron: () => is_electron,
}
};
})
);
} else {
if (windowId) {
return this.store.pipe(
map(state => state.windows[windowId]),
distinctUntilChanged(),
map(windowState => {
return {
sdl: windowState.schema.sdl,
query: windowState.query.query,
queryResponse: windowState.query.response,
variables: windowState.variables.variables,
ctx: {
setQuery: (query: string) => this.zone.run(() => this.setQuery(windowId, query)),
getQuery: () => windowState.query.query || '',
setVariables: (variables: string) => this.zone.run(() => this.setVariables(windowId, variables)),
getVariables: () => windowState.variables.variables,
setEndpoint: (endpoint: string) => this.zone.run(() => this.setEndpoint(windowId, endpoint)),
getEndpoint: () => windowState.query.url,
getSDL: () => windowState.schema.sdl,
on: (ev: PluginEvent, handler: () => Subscription) => this.pluginEvent.on(ev, handler),
isElectron: () => is_electron,
}
}
}),
);
}
throw new Error('root state and window id are required for window-level plugins.');
}
}
private setQuery(windowId: string, query: string) {
this.store.dispatch(new queryActions.SetQueryAction(query, windowId));
}
private setVariables(windowId: string, variables: string) {
this.store.dispatch(new variableActions.UpdateVariablesAction(variables, windowId));
}
private setEndpoint(windowId: string, url: string) {
this.store.dispatch(new queryActions.SetUrlAction({ url }, windowId));
this.store.dispatch(new queryActions.SendIntrospectionQueryRequestAction(windowId));
}
private createWindow(windowData: CreateWindowOptions) {
this.windowService.importWindowData({
version: 1,
type: 'window',
...windowData
});
}
}