/
commonStore.ts
315 lines (276 loc) · 9.84 KB
/
commonStore.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
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
import { action, computed, observable, autorun, IReactionDisposer } from 'mobx';
import GQLClient from '../api/gqlClient';
import { ipcRenderer, IpcRendererEvent } from 'electron';
import Plugin from './plugin';
import Stream from './stream';
import Store from 'electron-store';
import { JSONSchema } from 'json-schema-typed';
import { ZenObservable } from 'zen-observable-ts';
interface RendererPayload {
apiPort: number;
adminOTP?: string;
adminToken?: string;
}
interface TreeViewSpec {
id: string;
name: string;
children?: TreeViewSpec[];
[k: string]: any;
}
const schema: { [key: string]: JSONSchema } = {
defaultPlugins: {
type: 'array',
items: {
required: ['name', 'url'],
properties: {
name: { type: 'string', pattern: '[0-9a-zA-Z\-\_]+' },
url: { type: 'string', format: 'uri' },
},
},
default: [],
},
switchToNewStream: {
type: 'boolean',
default: true,
},
savedSessions: {
type: 'number',
default: 1,
},
};
export default class CommonStore {
@observable public streams = new Map<string, Stream>();
@observable public apiPort = 0;
@observable public apiVersion = '';
@observable public switchToNewStream = true;
@observable public savedSessions: string = '';
private gqlClient?: GQLClient;
private subscription?: ZenObservable.Subscription;
@observable private streamSel?: string;
@observable private navSel?: string;
@observable private defaultPlugins = new Map<string, Plugin>();
private diskStore = new Store({ schema });
private autoSaveDispose?: IReactionDisposer;
private timeUpdateTimeout?: NodeJS.Timeout;
@action public init = async () => {
const payload = await new Promise<RendererPayload>((resolve) => {
ipcRenderer.once('renderer-payload', (_: IpcRendererEvent, arg: RendererPayload) => {
resolve(arg);
});
ipcRenderer.send('renderer-payload');
});
this.apiPort = payload.apiPort;
this.gqlClient = new GQLClient(payload.apiPort);
if (payload.adminToken) {
await this.gqlClient.initializeAdminToken(payload.adminToken, true);
} else if (payload.adminOTP) {
const adminToken = await this.gqlClient.initializeAdminToken(payload.adminOTP);
ipcRenderer.send('save-admin-token', adminToken);
}
this.apiVersion = await this.gqlClient.getAPIVersion();
const storeDefaultPlugins = this.diskStore.get('defaultPlugins') as Array<{ name: string, url: string }>;
for (const { name, url } of storeDefaultPlugins) {
await this.addPlugin('default', name, url);
}
this.switchToNewStream = this.diskStore.get('switchToNewStream') as boolean;
this.savedSessions = String(this.diskStore.get('savedSessions'));
this.autoSaveDispose = autorun(() => {
const plugins = Array.from(this.defaultPlugins.values()).map((plugin) => ({
name: plugin.name,
url: plugin.url,
}));
this.diskStore.set('defaultPlugins', plugins);
this.diskStore.set('switchToNewStream', this.switchToNewStream);
this.diskStore.set('savedSessions', this.sanitizedSavedSessions);
});
this.timeUpdateTimeout = setInterval(() => {
this.streams.forEach((stream) => stream.updateDisplayStartTime());
}, 60000);
this.subscription = await this.addStreamListeners(
action(async (streamID: number) => {
// tslint:disable-next-line: no-console
console.log('Stream Created:', streamID);
const newStream = new Stream(streamID);
this.streams.set(newStream.uniqID, newStream);
for (const [_, plugin] of this.defaultPlugins) {
const pluginToken = await this.gqlAddPlugin(plugin.url);
const newPlugin = plugin.withParams(newStream.uniqID, {
apiURL: this.apiURL,
apiToken: pluginToken,
streamID,
});
newStream.plugins.set(newPlugin.id, newPlugin);
}
if (this.switchToNewStream) {
this.selectStream(newStream.uniqID);
}
}),
action(async (streamID: number) => {
// tslint:disable-next-line: no-console
console.log('Stream Removed', streamID);
for (const [, stream] of this.streams) {
if (stream.id === streamID) {
stream.active = false;
for (const [, plugin] of stream.plugins) {
if (plugin.params) {
await this.gqlRemovePlugin(plugin.params.apiToken);
}
}
}
}
this.pruneInactiveStreams();
}),
);
}
@action public dispose = () => {
if (this.autoSaveDispose) { this.autoSaveDispose(); }
if (this.timeUpdateTimeout) { clearInterval(this.timeUpdateTimeout); }
this.streams.forEach((stream) => {
stream.plugins.forEach(async (plugin) => {
if (plugin.params) {
await this.gqlRemovePlugin(plugin.params.apiToken);
}
});
});
if (this.subscription) {
this.subscription.unsubscribe();
}
}
@action public selectStream(streamID: string) {
this.streamSel = streamID;
}
@action public selectNav(nav: string) {
this.navSel = nav;
}
@action public async addPlugin(streamUniqID: string, name: string, url: string) {
if (streamUniqID === 'default') {
const newPlugin = new Plugin(name, url);
this.defaultPlugins.set(newPlugin.id, newPlugin);
} else if (this.streams.has(streamUniqID)) {
const stream = this.streams.get(streamUniqID)!;
const pluginToken = await this.gqlAddPlugin(url);
const newPlugin = new Plugin(name, url, streamUniqID, {
apiURL: this.apiURL,
apiToken: pluginToken,
streamID: stream.id,
});
stream.plugins.set(newPlugin.id, newPlugin);
}
}
@action public removePlugins(plugins: Array<{ id: string, streamUniqID: string }>) {
plugins.forEach(async ({ id, streamUniqID }) => {
if (streamUniqID === 'default') {
this.defaultPlugins.delete(id);
} else if (this.streams.has(streamUniqID)) {
const streamPlugins = this.streams.get(streamUniqID)!.plugins;
if (streamPlugins.has(id)) {
const plugin = streamPlugins.get(id)!;
if (plugin.params) {
await this.gqlRemovePlugin(plugin.params.apiToken);
}
}
streamPlugins.delete(id);
}
});
}
@computed public get apiURL(): string {
return `http://localhost:${this.apiPort}/query`;
}
@computed public get selectedStream(): Stream | undefined {
if (this.streams.size > 0) {
if (this.streamSel && this.streams.has(this.streamSel)) {
return this.streams.get(this.streamSel)!;
} else {
return this.streams.values().next().value;
}
}
return undefined;
}
@computed public get selectedStreamPlugins(): Plugin[] | undefined {
if (!this.selectedStream) { return undefined; }
return Array.from(this.selectedStream.plugins.values());
}
@computed public get selectedNav(): string {
if (this.navSel === 'nav-settings') { return this.navSel; }
if (this.selectedStream && this.selectedStream.plugins.size > 0) {
if (!this.navSel || !this.navSel.startsWith(`plugin-${this.selectedStream.uniqID}`)) {
return this.selectedStream.plugins.values().next().value.id;
}
return this.navSel;
}
return 'nav-settings';
}
@computed public get pluginsTree(): TreeViewSpec[] {
const pluginsToTree = (streamUniqID: string, plugins: Map<string, Plugin>) =>
Array.from(plugins).map(([_, plugin]) => ({
id: plugin.id,
name: `${plugin.name} - ${plugin.url}`,
icon: plugin.icon,
streamUniqID,
}));
const loadedPlugins = Array.from(this.streams).map(([streamUniqID, stream]) => ({
id: streamUniqID,
name: stream.displayName,
children: pluginsToTree(streamUniqID, stream.plugins),
}));
return loadedPlugins.concat([{
id: 'default',
name: `Default Plugins`,
children: pluginsToTree('default', this.defaultPlugins),
}]);
}
@computed public get sanitizedSavedSessions(): number {
const savedSessions = Number(this.savedSessions);
if (!this.savedSessions || isNaN(savedSessions)) {
return 1;
}
return savedSessions;
}
@computed public get displaySavedSessions(): string {
if (this.sanitizedSavedSessions < 0) {
return 'infinite';
}
return `${this.sanitizedSavedSessions}`;
}
@action private pruneInactiveStreams() {
const candidatesForRemoval: string[] = [];
for (const [streamUniqID, stream] of this.streams) {
if (!stream.active) {
candidatesForRemoval.push(streamUniqID);
}
}
const sss = this.sanitizedSavedSessions;
if (sss >= 0 && sss < candidatesForRemoval.length) {
const toRemove = (sss === 0) ? candidatesForRemoval : candidatesForRemoval.slice(0, -sss);
for (const streamUniqID of toRemove) {
this.streams.delete(streamUniqID);
}
}
}
private gqlAddPlugin = (pluginURL: string): Promise<string> => {
if (!this.gqlClient) { throw new Error('Must start GQL client first.'); }
return this.gqlClient.addPlugin(pluginURL);
}
private gqlRemovePlugin = (apiToken: string): Promise<boolean> => {
if (!this.gqlClient) { throw new Error('Must start GQL client first.'); }
return this.gqlClient.removePlugin(apiToken);
}
private addStreamListeners = async (
onStreamCreated: (streamID: number) => void,
onStreamRemoved: (streamID: number) => void,
) => {
if (!this.gqlClient) { throw new Error('Must start GQL client first.'); }
const streams = await this.gqlClient.listStreams();
streams.map(({ id }) => onStreamCreated(id));
return this.gqlClient.subscribeToStreamEvents((streamID, type) => {
switch (type) {
case 'AddStream':
onStreamCreated(streamID);
break;
case 'RemoveStream':
onStreamRemoved(streamID);
break;
}
});
}
}