-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSDK.ts
372 lines (320 loc) · 11 KB
/
SDK.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
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
import { getPanelTabName } from './utils'
export interface SDKOptions {
/**
* When set to `true`, all logs will be printed to console.
*/
debug?: boolean
/**
* When set to `true`, the extensions will be loaded and executed.
*/
disabled?: boolean
}
export interface SDKExtension {
/**
* A link to the CDN from which to load the extension executable file.
*/
cdnURL: string
/**
* Extension ID.
*/
id: string
}
export enum event {
/**
* The event is triggered when the dashboard loads a new set of analytics data.
* For example, when user opens dashboard for the first time, changes the data range or time bucket.
*/
LOAD = 'load',
/**
* The event is triggered when user changes the filters.
*/
FILTERS_UPDATE = 'filtersupdate',
/**
* The event is triggered when user changes the time bucket, time perios or sets the date range.
*/
TIME_UPDATE = 'timeupdate',
/**
* The event is triggered on load and it supplies information about the project it's running on.
*/
PROJECT_INFO = 'projectinfo',
}
export enum PanelTab {
cc = 'cc',
pg = 'pg',
lc = 'lc',
ref = 'ref',
dv = 'dv',
br = 'br',
os = 'os',
so = 'so',
me = 'me',
ca = 'ca',
lt = 'lt',
}
enum DebugType {
LOG = 'log',
ERROR = 'error',
WARN = 'warn',
INFO = 'info',
}
type EventsSubObject = {
[key: string]: (eventData: any) => any
}
type EventsObject = {
[key in event]?: EventsSubObject
}
type SwetrixCallbacks = {
onAddExportDataRow: (name: string, onClick: () => void) => void
onRemoveExportDataRow: (name: string) => void
onAddPanelTab: (extensionID: string, panelID: string, tabContent: string, onClick: () => void) => void
onRemovePanelTab: (extensionID: string, panelID: string) => void
onCustomizePanelTab: (extensionID: string, panelID: string, tabContent: string) => void
onAddNewPanelTab: (extensionID: string, panelID: string, tabContent: string) => void
}
/**
* Initialise the SDK instance.
*
* @param {SDKExtension[]} extensions A list of extension to load and execute.
* @param {SDKOptions} options SDK options.
* @param {SwetrixCallbacks} swetrixCallbacks Callbacks to interact with Swetrix website.
* @returns {SDK} Instance of the Swetrix SDK.
*/
export class SDK {
private events: EventsObject = {}
private exportDataRowValues: Array<string> = []
private panelTabValues: Array<string> = []
private _sdkInitialised: boolean = false
private _emitQueue: Array<{ event: event, eventData: any }> = []
/**
* Initialise the SDK instance.
*
* @param {SDKExtension[]} extensions A list of extension to load and execute.
* @param {SDKOptions} options Swetrix SDK options.
* @param {SwetrixCallbacks} swetrixCallbacks Callbacks to interact with Swetrix website.
*/
constructor(private extensions: SDKExtension[], private options?: SDKOptions, private swetrixCallbacks?: SwetrixCallbacks) {
this._init()
}
private _init(): void {
if (this.options?.disabled) {
this.debug('SDK is disabled, skipping initialisation')
return
}
const promisified = this.extensions.map(({ cdnURL, id }) => this._loadExtension(cdnURL, id))
Promise.all(promisified)
.then(() => {
this.debug('SDK initialised')
this._sdkInitialised = true
this._emitQueue.forEach(({ event, eventData }) => {
this._emitEvent(event, eventData)
})
this._emitQueue = []
})
}
private _loadExtension = (cdnURL: string, id: string): Promise<any> => {
return fetch(cdnURL)
// Parse the response as text, return a dummy script if the response is not ok
.then((res) => {
if (!res.ok) {
this.debug(`Error while loading extension from ${cdnURL}`, DebugType.ERROR)
return '(() => {})'
}
return res.text()
})
// Execute the extension
.then(code => {
eval(code)({
...this,
// Keeping this here as for some reason ...this does not include the methods
addExportDataRow: this.addExportDataRow,
debug: this.debug,
removeExportDataRow: this.removeExportDataRow,
// Presetting functions which require extension id
addPanelTab: this.addPanelTab(id),
removePanelTab: this.removePanelTab(id),
addEventListener: this.addEventListener(id),
removeEventListener: this.removeEventListener(id),
// Functions that should not be exposed to the extensions
_emitEvent: undefined,
_destroy: undefined,
_loadExtension: undefined,
_init: undefined,
_emitQueue: undefined,
})
this.debug(`Extension ${id} loaded and executed`)
})
}
private debug(message: string, type: DebugType = DebugType.LOG): void {
if (this.options?.debug) {
console[type]('[Swetrix SDK]', message)
}
}
public _emitEvent(event: event, eventData: any): void {
if (!this._sdkInitialised) {
this.debug(`Trying to emit event '${event}', but it as added to queue as the SDK is not initialised yet`, DebugType.WARN)
this._emitQueue.push({ event, eventData })
return
}
this.debug(`Emitting event '${event}'`)
if (this.events[event]) {
// @ts-ignore - TS does not like the fact that we are iterating over an object
Object.values(this.events[event]).forEach(callback => {
// Adding a delay before calling events to make sure that the dashboard has time to render
// in case some callbacks taking a long time to execute
setTimeout(() => {
callback(eventData)
}, 300)
})
}
}
public _destroy(): void {
this.debug('Destroying the SDK instance')
this.events = {}
this.extensions = []
}
// -----------
// Public methods that are avaliable to the extension developers.
// -----------
public addEventListener(extensionID: string): (event: event, callback: (eventData: any) => any) => void {
/**
* Add an event listener.
*
* @param {event} event The event to listen to.
* @param {(eventData: any) => any} callback The callback to execute when the event is triggered.
* @returns {void}
*/
return (event: event, callback: (eventData: any) => any) => {
this.debug(`Adding event listener for ${event} (extension: ${extensionID})`)
if (typeof callback !== 'function') {
this.debug(`Callback is not a function (extension: ${extensionID})`, DebugType.ERROR)
return
}
this.events = {
...this.events,
[event]: {
...this.events[event],
[extensionID]: callback,
},
}
}
}
public removeEventListener(extensionID: string): (event: event) => void {
/**
* Remove an event listener.
*
* @param {event} event The event to remove the listener from.
* @returns {void}
*/
return (event: event) => {
this.debug(`Removing event listener for ${event}`)
if (this.events[event]) {
delete this.events[event]?.[extensionID]
}
}
}
/**
* Add a new export data row into the dropdown.
*
* @param name The name of the export data row.
* @param onClick The callback to execute when the export data row is clicked.
* @returns {void}
*/
public addExportDataRow(name: string, onClick: () => void): void {
this.debug(`Adding export data row ${name}`)
if (this.exportDataRowValues.includes(name)) {
this.debug(`Export data row ${name} already exists`, DebugType.WARN)
return
}
this.exportDataRowValues.push(name)
this.swetrixCallbacks?.onAddExportDataRow(name, onClick)
}
/**
* Remove an export data row from the dropdown.
*
* @param name The name of the export data row.
* @returns {void}
*/
public removeExportDataRow(name: string): void {
this.debug(`Removing export data row ${name}`)
if (!this.exportDataRowValues.includes(name)) {
this.debug(`Export data row ${name} does not exist`, DebugType.WARN)
return
}
this.exportDataRowValues = this.exportDataRowValues.filter(value => value !== name)
this.swetrixCallbacks?.onRemoveExportDataRow(name)
}
public addPanelTab(extensionID: string): (panelID: PanelTab, tabContent: string, onOpen: () => void) => void {
/**
* Add a new panel tab into the dashboard panels.
*
* @param extensionID The ID of the extension.
* @param panelID The ID of the panel.
* @param onOpen The callback to execute when the panel tab is opened.
* @returns {void}
*/
return (panelID: PanelTab, tabContent: string, onOpen: () => void = () => {}): void => {
this.debug(`Adding panel tab ${panelID}`)
const panelName = getPanelTabName(extensionID, panelID)
if (this.panelTabValues.includes(panelName)) {
this.debug(`Panel tab ${panelID} (${extensionID}) already exists`, DebugType.WARN)
return
}
this.panelTabValues.push(panelName)
this.swetrixCallbacks?.onAddPanelTab(extensionID, panelID, tabContent, onOpen)
}
}
public removePanelTab(extensionID: string): (panelID: PanelTab) => void {
/**
* Remove a panel tab from the dashboard panels.
*
* @param panelID The ID of the panel.
* @returns {void}
*/
return (panelID: PanelTab) => {
this.debug(`Removing panel tab ${panelID}`)
const panelName = getPanelTabName(extensionID, panelID)
if (!this.panelTabValues.includes(panelName)) {
this.debug(`Panel tab ${panelID} (${extensionID}) does not exist`, DebugType.WARN)
return
}
this.swetrixCallbacks?.onRemovePanelTab(extensionID, panelID)
}
}
public customizePanelTab(extensionID: string): (panelID: PanelTab, tabContent: string) => void {
/**
* Customize a panel tab.
*
* @param panelID The ID of the panel.
* @param tabContent The new content of the tab.
* @returns {void}
*/
return (panelID: PanelTab, tabContent: string): void => {
this.debug(`Customizing panel tab ${panelID}`)
const panelName = getPanelTabName(extensionID, panelID)
if (!this.panelTabValues.includes(panelName)) {
this.debug(`Panel tab ${panelID} (${extensionID}) does not exist`, DebugType.WARN)
return
}
this.swetrixCallbacks?.onCustomizePanelTab(extensionID, panelID, tabContent)
}
}
public addNewPanelTab(extensionID: string): (panelID: PanelTab, tabContent: string) => void {
/**
* Add a new panel tab into the dashboard panels.
*
* @param extensionID The ID of the extension.
* @param panelID The ID of the panel.
* @returns {void}
*/
return (panelID: PanelTab, tabContent: string): void => {
this.debug(`Adding panel tab ${panelID}`)
const panelName = getPanelTabName(extensionID, panelID)
if (this.panelTabValues.includes(panelName)) {
this.debug(`Panel tab ${panelID} (${extensionID}) already exists`, DebugType.WARN)
return
}
this.panelTabValues.push(panelName)
this.swetrixCallbacks?.onAddNewPanelTab(extensionID, panelID, tabContent)
}
}
}