-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
184 lines (164 loc) · 4.89 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
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
'use strict';
import {GustavGraph} from './GustavGraph';
import {Workflow} from './Workflow';
import {IMetaConfig, ICoupler} from './defs';
import * as GR from './couplers/GustavRedis';
import * as GK from './couplers/GustavKafka';
export let GustavRedis = GR.GustavRedis;
export let GustavKafka = GK.GustavKafka;
export interface INodeFactory {
(...config: any[]): symbol;
}
export interface INodeCollection {
source: string[];
transformer: string[];
sink: string[];
}
export interface IRegisteredNode {
name: string;
type: string;
factory: Function;
}
let registeredNodes: IRegisteredNode[] = [];
let workflows = {};
let register;
let anonWfId = 0;
// Meta nodes
// TODO: move into its own file
let registerMetaNodes = (gustav) => {
gustav.transformer('__gmergeNode', (nodes, iO) => {
return iO.do(() => {});
});
};
export let gustav = {
makeNode: (nodeName: string, graph: GustavGraph, config: any, metaConfig?: IMetaConfig): symbol => {
let node = registeredNodes.filter((regNode) => regNode.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 (metaConfig && metaConfig.gid) {
symbolTag += '-' + metaConfig.gid;
}
let sym = Symbol(symbolTag);
graph.nodes[sym] = {
type: node.type,
config: config, // Storing config here for later toJSON calls
init: config ? node.factory.bind(null, config) : node.factory
};
return sym;
},
/**
* Create an empty workflow to be chained off of
* @param {string} uuid [description]
*/
createWorkflow (name?: string): Workflow {
let wf = new Workflow(name || `Unnamed Workflow ${anonWfId++}`);
workflows[wf.uuid] = wf;
return wf;
},
start: (uuid: string): void => {
workflows[uuid].start();
},
stop: (uuid: string): void => {
workflows[uuid].stop();
},
reset: (): void => {
anonWfId = 0;
workflows = {};
registeredNodes = [];
registerMetaNodes(gustav);
},
getNodeTypes: (): INodeCollection => {
return registeredNodes.reduce((obj, node) => {
obj[node.type].push(node.name);
return obj;
}, {source: [], transformer: [], sink: []});
},
makeGraph: (): any => {
let id = 0;
let graph = {
nodes: [],
links: []
};
let exInGraph = exName => graph.nodes.some(node => node.name === exName);
let getIdByName = name => graph.nodes.filter(node => node.name === name)[0].id;
Object.keys(workflows).forEach(wfKey => {
let wf = workflows[wfKey];
let sourceId = id;
graph.nodes.push({
type: 'workflow',
id: id,
name: wf.name
});
id++;
wf.external.source.forEach(exSource => {
let thisId;
if (!exInGraph(exSource)) {
graph.nodes.push({
type: 'external',
id: id,
name: exSource
});
thisId = id;
id++;
}
graph.links.push({
source: thisId || getIdByName(exSource),
target: sourceId
});
});
wf.external.sink.forEach(exSource => {
let thisId;
if (!exInGraph(exSource)) {
graph.nodes.push({
type: 'external',
id: id,
name: exSource
});
thisId = id;
id++;
}
graph.links.push({
source: sourceId,
target: thisId || getIdByName(exSource)
});
});
});
return graph;
},
addCoupler: (externalCoupler: ICoupler, couplerName?: string): void => {
if (!couplerName) {
couplerName = externalCoupler.defaultName;
}
gustav.source(`__from-${couplerName}`, (name) => externalCoupler.from(name));
gustav.sink(`__to-${couplerName}`, (name, iO) => externalCoupler.to(name, iO));
},
source: (name: string, factory: Function): Function => { return register('source', name, factory); },
transformer: (name: string, factory: Function): Function => { return register('transformer', name, factory); },
sink: (name: string, factory: Function): Function => { return register('sink', name, factory); }
};
// TODO: new type of registration that's just a singleton
// Just calls INodeFactory and returns the symbol
// Outside of the exported object so this is private
register = (type: string, name: string, factory): INodeFactory => {
if (!name) {
throw new Error('Attempted to register a node without providing a name');
}
if (!factory) {
throw new Error(`Attempted to register node ${name} without providing a factory`);
}
// Names must be unique
const exists = registeredNodes.filter((regNode) => regNode.name === name);
if (exists.length) {
throw new Error(`${name} already registered`);
}
registeredNodes.push({
type,
name,
factory
});
return gustav.makeNode.bind(null, name);
};
registerMetaNodes(gustav);