forked from noflo/noflo-browser-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtime.js
99 lines (90 loc) · 2.72 KB
/
runtime.js
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
const noflo = require('noflo');
const postMessageRuntime = require('noflo-runtime-postmessage');
const qs = require('querystring');
const defaultPermissions = [
'protocol:graph',
'protocol:component',
'protocol:network',
'protocol:runtime',
'component:getsource',
'component:setsource',
];
function getParameterByName(name) {
const params = (new URL(document.location)).searchParams;
return params.get(name);
}
function getIdeUrl(options, ide = 'https://app.flowhub.io') {
const params = {
protocol: 'opener',
address: window.location.href,
};
if (options.id) {
params.id = options.id;
}
const query = qs.stringify(params);
return `${ide}#runtime/endpoint?${query}`;
}
function loadGraph(json) {
return new Promise((resolve, reject) => {
noflo.graph.loadJSON(json, (err, graphInstance) => {
if (err) {
reject(err);
return;
}
resolve(graphInstance);
});
});
}
function startRuntime(graph, options = {}) {
return new Promise((resolve, reject) => {
const protocol = options.protocol || getParameterByName('fbp_protocol') || 'opener';
const runtimeOptions = {
...options.runtimeOptions,
};
if (!runtimeOptions.defaultPermissions) {
runtimeOptions.defaultPermissions = defaultPermissions;
}
switch (protocol) {
case 'opener': {
if (!options.debugButton) {
reject(new Error('No debug button defined'));
return;
}
const button = options.debugButton;
button.classList.replace('nodebug', 'debug');
button.href = getIdeUrl(runtimeOptions, options.ide);
resolve(postMessageRuntime.opener(runtimeOptions, button));
return;
}
case 'iframe': {
resolve(postMessageRuntime.iframe(runtimeOptions));
return;
}
default: {
reject(new Error(`Unknown FBP protocol ${protocol}`));
}
}
});
}
function startNetwork(runtime, graph, options) {
const noLoad = options.noLoad || (getParameterByName('fbp_noload') === 'true');
if (noLoad) {
return Promise.resolve(runtime);
}
return new Promise((resolve, reject) => {
const graphName = `${runtime.options.namespace || 'default'}/${graph.name || 'main'}`;
runtime.graph.registerGraph(graphName, graph);
// eslint-disable-next-line no-underscore-dangle
runtime.network._startNetwork(graph, graphName, 'none', (err) => {
if (err) {
reject(err);
return;
}
runtime.runtime.setMainGraph(graphName, graph);
resolve(runtime);
});
});
}
module.exports = (graph, options) => loadGraph(graph)
.then(graphInstance => startRuntime(graphInstance, options)
.then(runtime => startNetwork(runtime, graphInstance, options)));