-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
97 lines (86 loc) · 2.55 KB
/
index.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
/// <reference path="typings/tsd.d.ts" />
/// <reference path="Symbol.d.ts" />
'use strict';
import {GustavGraph} from './GustavGraph';
import {Workflow, NodeDef} from './Workflow';
interface NodeFactory {
(...config:any[]): symbol;
}
interface NodeCollection {
source: string[];
transformer: string[];
sink: string[];
}
interface RegisteredNode {
name: string;
type: string;
factory: Function;
}
class Gustav {
registeredNodes: RegisteredNode[];
workflows: any;
constructor() {
this.registeredNodes = [];
this.workflows = {};
}
// TODO: new type of registration that's just a singleton
// Just calls NodeFactory and returns the symbol
private register(type: string, name: string, factory): NodeFactory {
// TODO: Return some sort of object so this can be chained
// let splitText = SplitText()
// .addDep(fetchPageText);
// Names must be unique
const exists = this.registeredNodes.filter((x) => x.name === name);
if (exists.length) {
throw new Error(name + ' already registered');
}
this.registeredNodes.push({
type,
name,
factory
});
return this.makeNode.bind(this, name);
}
makeNode (nodeName:string, graph: GustavGraph, ...config) {
var node = this.registeredNodes.filter((x) => x.name === nodeName)[0];
if (!node) {
throw new Error(nodeName + ' not registered');
}
// Attempt to detect config to make symbol tag more descriptive
let symbolTag = node.name;
if (config.length) {
if(!(config[0] instanceof Object)) {
symbolTag += '-' + config[0];
} else if (config[0].id) {
symbolTag += '-' + config[0].id;
}
}
let sym = Symbol(symbolTag);
graph.nodes[sym] = {
type: node.type,
init: node.factory.apply(null, config)
};
return sym;
}
makeWorkflow (config:NodeDef[]) {
let wf = new Workflow(config);
this.workflows[wf.guid] = wf;
return wf;
}
start (guid:string) {
this.workflows[guid].start();
}
stop (guid:string) {
this.workflows[guid].stop();
}
getNodeTypes ():NodeCollection {
return this.registeredNodes.reduce((obj, node) => {
obj[node.type].push(node.name);
return obj;
}, {source: [], transformer: [], sink: []});
}
source(name: string, factory: Function) { return this.register('source', name, factory)}
transformer(name: string, factory: Function) { return this.register('transformer', name, factory)}
sink(name: string, factory: Function) { return this.register('sink', name, factory)}
};
export var gustav = new Gustav();