Skip to content

Commit

Permalink
Use Promises
Browse files Browse the repository at this point in the history
  • Loading branch information
bergie committed Dec 23, 2020
1 parent f63e596 commit 9c8cfd5
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 83 deletions.
110 changes: 51 additions & 59 deletions app/main.js
Expand Up @@ -3,80 +3,72 @@ const postMessageRuntime = require('noflo-runtime-postmessage');
const mainGraph = require('../graphs/main.fbp');

window.addEventListener('WebComponentsReady', () => {
const loadGraphs = function (callback) {
noflo.graph.loadJSON(mainGraph, (err, g) => {
if (err) {
callback(err);
return;
}
noflo.createNetwork(g, (err2, n) => {
if (err2) {
callback(err2);
return;
}
n.on('process-error', (processError) => {
function loadGraphs() {
return noflo.graph
.loadJSON(mainGraph)
.then((graph) => noflo.createNetwork(graph))
.then((network) => {
network.on('process-error', (processError) => {
if (typeof console.error === 'function') {
console.error(processError.error);
} else {
console.log(processError.error);
}
});
callback();
return network;
});
});
};
const loadGraphsDebuggable = function (callback) {
}
function loadGraphsDebuggable() {
const secret = Math.random().toString(36).substring(7);
noflo.graph.loadJSON(mainGraph, (err, graph) => {
if (err) {
callback(err);
return;
}
const runtimeOptions = {
defaultGraph: graph,
baseDir: graph.baseDir,
permissions: {},
};
runtimeOptions.permissions[secret] = [
'protocol:component',
'protocol:runtime',
'protocol:graph',
'protocol:network',
'component:getsource',
'component:setsource',
];
runtimeOptions.label = process.env.NOFLO_APP_TITLE;
runtimeOptions.id = '2b487ea3-287b-43f7-b7eb-806f02b402f9';
runtimeOptions.namespace = 'ui';
runtimeOptions.repository = 'git+https://github.com/noflo/noflo-ui.git';
const debugButton = document.createElement('button');
debugButton.id = 'flowhub_debug_url';
debugButton.innerText = 'Debug in Flowhub';
const ide = 'https://app.flowhub.io';
const debugUrl = `${ide}#runtime/endpoint?${encodeURIComponent(`protocol=opener&address=${window.location.href}&id=${runtimeOptions.id}`)}`;
debugButton.setAttribute('href', debugUrl);
document.body.appendChild(debugButton);
postMessageRuntime.opener(runtimeOptions, debugButton);
callback();
});
};
return noflo.graph
.loadJSON(mainGraph)
.then((graph) => {
const runtimeOptions = {
defaultGraph: graph,
baseDir: 'noflo-ui',
permissions: {},
};
runtimeOptions.permissions[secret] = [
'protocol:component',
'protocol:runtime',
'protocol:graph',
'protocol:network',
'component:getsource',
'component:setsource',
];
runtimeOptions.label = process.env.NOFLO_APP_TITLE;
runtimeOptions.id = '2b487ea3-287b-43f7-b7eb-806f02b402f9';
runtimeOptions.namespace = 'ui';
runtimeOptions.repository = 'git+https://github.com/noflo/noflo-ui.git';
const debugButton = document.createElement('button');
debugButton.id = 'flowhub_debug_url';
debugButton.innerText = 'Debug in Flowhub';
const ide = 'https://app.flowhub.io';
const debugUrl = `${ide}#runtime/endpoint?${encodeURIComponent(`protocol=opener&address=${window.location.href}&id=${runtimeOptions.id}`)}`;
debugButton.setAttribute('href', debugUrl);
document.body.appendChild(debugButton);
postMessageRuntime.opener(runtimeOptions, debugButton);
return Promise.resolve();
});
}

window.nofloStarted = false;
let load = loadGraphs;
if (String(localStorage.getItem('flowhub-debug')) === 'true') {
load = loadGraphsDebuggable;
}
load((err) => {
if (err) {
load()
.then(() => {
document.body.classList.remove('loading');
window.nofloStarted = true;
setTimeout(() => {
const loader = document.getElementById('loading');
document.body.removeChild(loader);
}, 400);
}, (err) => {
console.error(err);
throw err;
}
document.body.classList.remove('loading');
window.nofloStarted = true;
setTimeout(() => {
const loader = document.getElementById('loading');
document.body.removeChild(loader);
}, 400);
});
});
});

if ('serviceWorker' in navigator) {
Expand Down
22 changes: 9 additions & 13 deletions spec/component/DispatchAction.js
Expand Up @@ -7,20 +7,16 @@ describe('DispatchAction component', () => {
let pass = null;
let handle = [];

before((done) => {
before(() => {
const loader = new noflo.ComponentLoader(baseDir);
loader.load('ui/DispatchAction', (err, instance) => {
if (err) {
done(err);
return;
}
c = instance;
routes = noflo.internalSocket.createSocket();
c.inPorts.routes.attach(routes);
ins = noflo.internalSocket.createSocket();
c.inPorts.in.attach(ins);
done();
});
return loader.load('ui/DispatchAction')
.then((instance) => {
c = instance;
routes = noflo.internalSocket.createSocket();
c.inPorts.routes.attach(routes);
ins = noflo.internalSocket.createSocket();
c.inPorts.in.attach(ins);
});
});
beforeEach(() => {
pass = noflo.internalSocket.createSocket();
Expand Down
19 changes: 8 additions & 11 deletions spec/utils/middleware.js
Expand Up @@ -26,17 +26,14 @@ class Middleware {

before(callback) {
const loader = new noflo.ComponentLoader(this.baseDir);
loader.load(this.component, (err, instance) => {
if (err) {
callback(err);
return;
}
if (instance.isReady()) {
this.attachAndStart(instance, callback);
return;
}
instance.once('ready', () => this.attachAndStart(instance, callback));
});
return loader.load(this.component)
.then((instance) => {
if (instance.isReady()) {
this.attachAndStart(instance, callback);
return;
}
instance.once('ready', () => this.attachAndStart(instance, callback));
}, callback);
}

beforeEach() {
Expand Down

0 comments on commit 9c8cfd5

Please sign in to comment.