-
Notifications
You must be signed in to change notification settings - Fork 3
/
api.ts
430 lines (366 loc) · 9.68 KB
/
api.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
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
/**
* String identifier for nodes (e.g., a generated UUID)
*/
export type NodeIdentifier = string;
/**
* Action can be either reversible or irreversible
*/
export type Action = IrreversibleAction | ReversibleAction;
/**
* ActionMetadata
*/
export interface ActionMetadata {
/**
* List of tags
*/
tags?: string[];
/**
* The intent of the user to trigger this action
*/
userIntent?: string;
/**
* Enable custom properties
*/
[key: string]: any;
}
/**
* NodeMetadata
*/
export interface NodeMetadata {
/**
* Username
*/
createdBy: string;
/**
* UNIX timestamp
*/
createdOn: number;
/**
* Enable custom properties
*/
[key: string]: any;
}
/**
* Artifacts that are attached to a node
*/
export interface Artifacts {
/**
* Enable custom properties
*/
[key: string]: any;
}
/**
* Generic node
*/
export interface RootNode {
/**
* Node identifier
*/
id: NodeIdentifier;
/**
* Label
*/
label: string;
/**
* Node metadata
*/
metadata: NodeMetadata;
/**
* Children
*/
children: StateNode[];
/**
* Artifacts
*/
artifacts: Artifacts;
}
/**
* State node extending the RootNode
*/
export type StateNode = RootNode & {
/**
* Action
*/
action: Action;
/**
* Action result
*/
actionResult: any;
/**
* Parent node of this node
*/
parent: ProvenanceNode;
};
/**
* Provenance node generic type for both root and state nodes
*/
export type ProvenanceNode = RootNode | StateNode;
/**
* Irreversible action that can only be applied, but cannot be reverted
*/
export interface IrreversibleAction {
/**
* Metadata (optional)
*/
metadata?: ActionMetadata;
/**
* Function name to a registered function that is executed when applying an action
*/
do: string;
/**
* Multiple arguments that are passed to the registered do function.
* The arguments should be immutable!
*/
doArguments: any[]; // should be immutable
}
/**
* Reversible action that can be applied and reverted
*/
export interface ReversibleAction {
/**
* Metadata (optional)
*/
metadata?: ActionMetadata;
/**
* Function name to a registered function that is executed when applying an action
*/
do: string;
/**
* Multiple arguments that are passed to the registered do function.
* The arguments should be immutable and serializable to json!
*/
doArguments: any[];
/**
* Function name to a registered function that is executed when reverting an action
*/
undo: string;
/**
* Multiple arguments that are passed to the registered do function.
* The arguments should be immutable and serializable to json!
*/
undoArguments: any[];
}
/**
* Action function that can be registered and will be executed when applying an Action
*/
export type ActionFunction = (...args: any[]) => Promise<any>;
/**
* Bundle of an ActionFunction with a reference to the `this` context
*/
export interface ActionFunctionWithThis {
/**
* Action function
*/
func: ActionFunction;
/**
* Value to use as this (i.e the reference Object) when executing callback
*/
thisArg: any;
}
/**
* Metadata about the application
*/
export interface Application {
/**
* Application name
*/
name: string;
/**
* Application version
*/
version: string;
}
export type Handler = (event?: any) => void;
/**
* Provenance graph stores the nodes, the current pointer and is bound to a specific application
*/
export interface IProvenanceGraph {
/**
* Application metadata
*/
application: Application;
/**
* Pointer to the currently active node
*/
current: ProvenanceNode;
root: RootNode;
/**
* Add a new node to the provenance graph
* @param node ProvenanceNode to add
*/
addNode(node: ProvenanceNode): void;
/**
* Find a node by a given identifier and return the node if found
* @param id ProvenanceNode identifier
*/
getNode(id: NodeIdentifier): ProvenanceNode;
emitNodeChangedEvent(node: ProvenanceNode): void;
/**
* Available events:
* * nodeAdded, emitted when node is added via this.addNode()
* * currentChanged, emitted when this.current is changed
* * nodeChanged, emitted when this.emitNodeChangedEvent() is called
*
* @param type
* @param handler
*/
on(type: string, handler: Handler): void;
off(type: string, handler: Handler): void;
}
/**
* Action function registry stores all available functions that can be applied
*/
export interface IActionFunctionRegistry {
/**
* Add a new function to the registry
* @param name The name of the new function to register
* @param func Function that get called with the doArguments or undoArguments
* @param thisArg Value to use as this (i.e the reference Object) when executing callback
*/
register(name: string, func: ActionFunction, thisArg?: any): void;
/**
* Find a registered function by name and return the function if found
* @param name Name of the registered function
*/
getFunctionByName(name: string): ActionFunctionWithThis;
}
/**
* The provenance tracker takes a action function registry.
* New actions are executed and if successful stored as new StateNode in the graph.
*/
export interface IProvenanceTracker {
/**
* When acceptActions is false, the Tracker will ignore calls to applyAction
*/
acceptActions: boolean;
/**
* Action function registry
*/
registry: IActionFunctionRegistry;
screenShotProvider: IScreenShotProvider | null;
/**
* If true, adds a screenshot to all provenance nodes metadata
* (if screenShotProvider is set)
*/
autoScreenShot: boolean;
/**
* 1. Calls the action.do function with action.doArguments
* 2. Append action to graph via a StateEdge and StateNode
* 3. Makes the created StateNode the current state node
*
* @param action
* @param skipFirstDoFunctionCall If set to true, the do-function will not be called this time,
* it will only be called when traversing.
*/
applyAction(action: Action, skipFirstDoFunctionCall: boolean): Promise<StateNode>;
}
/**
* The traverser changes the current node in the provenance graph
* and executes the undo/redo function while moving through the graph structure.
*/
export interface IProvenanceGraphTraverser {
/**
* trackingWhenTraversing === false disables tracking when traversing to prevent feedback.
* When applying an action, the object we're tracking might trigger its event listeners. This
* means that more Nodes are added to the ProvenanceGraph when traversing, which is most likely
* unwanted behaviour.
*
* It will enable/disable immediately before/after calling the action. So if the event is emitted
* asynchronously after the action, it will not work.
*/
trackingWhenTraversing: boolean;
graph: IProvenanceGraph;
/**
* Finds shortest path between current node and node with request identifer.
* Calls the do/undo functions of actions on the path.
*
* @param id
*/
toStateNode(id: NodeIdentifier, transitionTime?: number): Promise<ProvenanceNode | undefined>;
/**
* Available events:
* * invalidTraversal, emitted when node is invalid to traverse to, because of a irreversible node or disconnected graph.
*
* @param type
* @param handler
*/
on(type: string, handler: Handler): void;
off(type: string, handler: Handler): void;
}
export interface SerializedProvenanceGraph {
nodes: SerializedProvenanceNode[];
root: NodeIdentifier;
application: Application;
current: NodeIdentifier;
}
export interface SerializedRootNode {
id: NodeIdentifier;
children: NodeIdentifier[];
label: string;
metadata: NodeMetadata;
artifacts: Artifacts;
}
export type SerializedStateNode = SerializedRootNode & {
parent: NodeIdentifier;
action: Action;
actionResult: any;
};
export type SerializedProvenanceNode = SerializedStateNode | SerializedRootNode;
export interface ISlideAnnotation<T> {
id: string;
data: T | null;
on(type: string, handler: Handler): any;
off(type: string, handler: Handler): any;
}
export interface IProvenanceSlide {
id: string;
node: ProvenanceNode | null;
name: string;
duration: number;
transitionTime: number;
annotations: ISlideAnnotation<any>[];
xPosition: number;
metadata: any;
addAnnotation(annotation: ISlideAnnotation<any>): void;
removeAnnotation(annotation: ISlideAnnotation<any>): void;
}
export interface IProvenanceSlidedeck {
/**
* Application metadata
*/
readonly application: Application;
slides: IProvenanceSlide[];
selectedSlide: IProvenanceSlide | null;
graph: IProvenanceGraph;
screenShotProvider: IScreenShotProvider | null;
/**
* If true, adds a screenshot to all slides
* (if screenShotProvider is set)
*/
autoScreenShot: boolean;
addSlide(slide?: IProvenanceSlide, index?: number): IProvenanceSlide;
removeSlide(slide: IProvenanceSlide): void;
removeSlideAtIndex(index: number): void;
moveSlide(indexFrom: number, indexTo: number, count?: number): void;
next(): void;
previous(): void;
startTime(slide: IProvenanceSlide): number;
slideAtTime(time: number): IProvenanceSlide | null;
/**
* Available events:
* * slideAdded, emitted when slide is added via this.addSlide()
* * slideSelected, emitted when this.selectedSlide is changed
* * slidesMoved, emitted when this.moveSlide() is called
* * slideRemoved, emitted when this.removeSlide() is called
*
* @param type
* @param handler
*/
on(type: string, handler: Handler): void;
off(type: string, handler: Handler): void;
}
export type IScreenShot = any;
/* A ScreenShotProvider provides screenshots from the application
that can be attached to provenance nodes or to SlideDeck slides.
*/
export type IScreenShotProvider = () => IScreenShot;