Skip to content

Instantly share code, notes, and snippets.

@bramblex
Created April 4, 2018 08:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bramblex/c927e7724d610ac93d2632ae6022b4f3 to your computer and use it in GitHub Desktop.
Save bramblex/c927e7724d610ac93d2632ae6022b4f3 to your computer and use it in GitHub Desktop.
function startup(zone, module_path) {
const code
= "eval((function () {"
+ " var path = require('path');"
+ " var startup_module_path = " + JSON.stringify(module_path) + ";"
+ " var startup_module = require(startup_module_path);"
+ " var startup_code = Object"
+ " .getOwnPropertyNames(startup_module)"
+ " .map(function (name) { return { name: name, value: startup_module[name] }; })"
+ " .reduce(function (prev, curr) {"
+ " if (typeof curr.value === 'function') {"
+ " return (prev + 'function ' + curr.name + '() { return require(' + JSON.stringify(startup_module_path) + ').' + curr.name + '.apply(undefined, arguments); };\\n')"
+ " } else { return prev; }"
+ " }, '');"
+ " return startup_code;"
+ "})());";
return zone.broadcast(code);
}
function run(zone, name, args) {
return new Promise(function (resolve, reject) {
zone.execute(function (_name, _args) {
var __args = _args ? _args : [];
return global[_name].apply(undefined, _args);
}, [name, args]).then(function (result) {
resolve(result.value)
}).catch(reject)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment