Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move canvas interpreter to OSS #25711

Merged
merged 12 commits into from
Nov 20, 2018
67 changes: 40 additions & 27 deletions packages/kbn-interpreter/public/browser_registries.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,51 +20,64 @@
import chrome from 'ui/chrome';
import $script from 'scriptjs';

let resolve = null;
let resolvePromise = null;
let called = false;

let populatePromise = new Promise(_resolve => {
resolve = _resolve;
resolvePromise = _resolve;
});

export const getBrowserRegistries = () => {
return populatePromise;
};

const loadBrowserRegistries = (registries) => {
const remainingTypes = Object.keys(registries);
const populatedTypes = {};

return new Promise(resolve => {
function loadType() {
if (!remainingTypes.length) {
resolve(populatedTypes);
return;
}
const type = remainingTypes.pop();
window.canvas = window.canvas || {};
window.canvas.register = d => registries[type].register(d);

// Load plugins one at a time because each needs a different loader function
// $script will only load each of these once, we so can call this as many times as we need?
const pluginPath = chrome.addBasePath(`/api/canvas/plugins?type=${type}`);
$script(pluginPath, () => {
markov00 marked this conversation as resolved.
Show resolved Hide resolved
populatedTypes[type] = registries[type];
loadType();
});
}

loadType();
});
};

export const populateBrowserRegistries = (registries) => {
if (called) {
ppisljar marked this conversation as resolved.
Show resolved Hide resolved
const oldPromise = populatePromise;
let newResolve;
populatePromise = new Promise(_resolve => {
newResolve = _resolve;
});
return oldPromise.then(() => {
resolve = newResolve;
called = false;
return populateBrowserRegistries(registries);
oldPromise.then(oldTypes => {
loadBrowserRegistries(registries).then(newTypes => {
newResolve({
...oldTypes,
...newTypes,
});
});
});
return populatePromise;
}
called = true;

const remainingTypes = Object.keys(registries);
const populatedTypes = {};

function loadType() {
const type = remainingTypes.pop();
window.canvas = window.canvas || {};
window.canvas.register = d => registries[type].register(d);

// Load plugins one at a time because each needs a different loader function
// $script will only load each of these once, we so can call this as many times as we need?
const pluginPath = chrome.addBasePath(`/api/canvas/plugins?type=${type}`);
$script(pluginPath, () => {
populatedTypes[type] = registries[type];

if (remainingTypes.length) loadType();
else resolve(populatedTypes);
});
}

if (remainingTypes.length) loadType();
loadBrowserRegistries(registries).then(registries => {
resolvePromise(registries);
});
return populatePromise;
};