allow for future updates to gapp function signatures
This commit is contained in:
parent
07e898f053
commit
5822cddedf
2 changed files with 48 additions and 52 deletions
|
|
@ -24,6 +24,6 @@
|
||||||
|
|
||||||
## Mesh:Tool
|
## Mesh:Tool
|
||||||
|
|
||||||
* `-` face selection and matchinbg preferences
|
* `-` face selection and matching preferences
|
||||||
* `-` add prefs for normal length, maybe color picker
|
* `-` add prefs for normal length, maybe color picker
|
||||||
* `-` svg and image import conversion (share code with Kiri:Moto)
|
* `-` svg and image import conversion (share code with Kiri:Moto)
|
||||||
|
|
|
||||||
100
src/main/gapp.js
100
src/main/gapp.js
|
|
@ -56,81 +56,77 @@ self.noop = () => {};
|
||||||
|
|
||||||
let gapp = self.gapp = self.gapp || {};
|
let gapp = self.gapp = self.gapp || {};
|
||||||
|
|
||||||
if (gapp.load) return;
|
|
||||||
|
|
||||||
// modules to load
|
// modules to load
|
||||||
let mods = [];
|
let mods = [];
|
||||||
let modn = {};
|
|
||||||
|
|
||||||
gapp.overlay = Object.assign;
|
gapp.overlay = Object.assign;
|
||||||
|
|
||||||
|
function exargs(args) {
|
||||||
|
return {
|
||||||
|
funcs: args.filter(a => typeof a === 'function'),
|
||||||
|
arrays: args.filter(a => Array.isArray(a)),
|
||||||
|
strings: args.filter(a => typeof a === 'string'),
|
||||||
|
objects: args.filter(a => typeof a === 'object' && !Array.isArray(a))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// register module without a load function
|
// register module without a load function
|
||||||
gapp.register = (name, deps, fn) => {
|
gapp.register = function() {
|
||||||
gapp.load(fn, name, deps);
|
const args = exargs([...arguments]);
|
||||||
};
|
const name = args.strings[0];
|
||||||
|
const fn = args.funcs[0];
|
||||||
// register module with a load function
|
const mod = { fn, name };
|
||||||
gapp.load = (fn, name, deps) => {
|
|
||||||
let mod = {fn, name, deps};
|
|
||||||
mods.push(mod);
|
mods.push(mod);
|
||||||
if (name) {
|
|
||||||
modn[name] = mod;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// check dependency list and warn if one is missing
|
|
||||||
gapp.check = (name, deps = []) => {
|
|
||||||
// warn if dependencies are not present
|
|
||||||
for (let dep of deps) {
|
|
||||||
if (!modn[dep]) {
|
|
||||||
dbug.warn(`'${name || 'module'}' missing dependency '${dep}'`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// perform dependency checks and run module load functions
|
// perform dependency checks and run module load functions
|
||||||
gapp.main = (app, deps, post, pre) => {
|
gapp.main = function() {
|
||||||
|
const args = exargs([...arguments]);
|
||||||
|
const app = args.strings[0];
|
||||||
|
const post = args.funcs[0];
|
||||||
|
const pre = args.funcs[1];
|
||||||
const root = self;
|
const root = self;
|
||||||
// optional fn to run before loading
|
// optional fn to run before loading
|
||||||
if (pre) pre(root);
|
if (pre) {
|
||||||
// app loader may also pass deps
|
pre(root);
|
||||||
gapp.check(app, deps);
|
}
|
||||||
// load mods after checking if dependency is present
|
// load mods after checking if dependency is present
|
||||||
for (let mod of mods) {
|
for (let mod of mods) {
|
||||||
let { fn, name, deps } = mod;
|
let { fn, name } = mod;
|
||||||
if (deps && deps.length) {
|
|
||||||
dbug.warn({app, mod, deps});
|
|
||||||
gapp.check(name, deps);
|
|
||||||
}
|
|
||||||
// mods with no name can't be depended on, but are allowed
|
// mods with no name can't be depended on, but are allowed
|
||||||
if (name) {
|
if (name) {
|
||||||
dbug.debug(`${app} | load | ${name}`);
|
dbug.debug(`${app} | load | ${name}`);
|
||||||
}
|
}
|
||||||
// mods with no function are allowed so they can be depended on
|
// modules without functions allowed so they can be depended on
|
||||||
if (fn) {
|
if (!fn) {
|
||||||
let toks = name.split('.');
|
dbug.debug(`${app} | ${mod.name} | missing fn()`);
|
||||||
let map = toks.pop();
|
continue;
|
||||||
let path = root;
|
|
||||||
for (let tok of toks) {
|
|
||||||
if (!path[tok]) {
|
|
||||||
dbug.debug({creating_root: tok, for: name});
|
|
||||||
path = path[tok] = {};
|
|
||||||
} else {
|
|
||||||
path = path[tok];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let tmp = path[map] || {};
|
|
||||||
fn(root, exports => {
|
|
||||||
if (exports) {
|
|
||||||
return path[map] = Object.assign(tmp, exports);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
// create namespace path in root and execute module load function
|
||||||
|
let toks = name.split('.');
|
||||||
|
let map = toks.pop();
|
||||||
|
let path = root;
|
||||||
|
for (let tok of toks) {
|
||||||
|
if (!path[tok]) {
|
||||||
|
dbug.debug({creating_root: tok, for: name});
|
||||||
|
path = path[tok] = {};
|
||||||
|
} else {
|
||||||
|
path = path[tok];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let tmp = path[map] || {};
|
||||||
|
fn(root, exports => {
|
||||||
|
if (exports) {
|
||||||
|
return path[map] = Object.assign(tmp, exports);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
// force runtime error if gapp.load() called gapp.main()
|
// force runtime error if gapp.load() called gapp.main()
|
||||||
mods = null;
|
mods = null;
|
||||||
// optional if not called inline with startup
|
// optional if not called inline with startup
|
||||||
if (post) post(root);
|
if (post) {
|
||||||
|
post(root);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue