updated approach to deps, mod loading, main entrypoints

This commit is contained in:
Stewart Allen 2022-02-20 12:56:43 -05:00
commit 19522665b2
21 changed files with 299 additions and 321 deletions

28
app.js
View file

@ -54,7 +54,7 @@ function init(mod) {
let depcache = {};
function find_deps(path, seen = []) {
function find_deps(path, seen = [], uses = []) {
let cached = depcache[path];
if (cached) {
return cached;
@ -71,13 +71,21 @@ function init(mod) {
.toString()
.split('\n');
for (let line of lines) {
let upos = line.indexOf('// use:');
if (upos >= 0) {
let use = line.substring(upos + 7).trim().replace('.','/');
if (uses.indexOf(use) < 0) {
uses.push(use);
}
continue;
}
let dpos = line.indexOf('// dep:');
if (dpos >= 0) {
let dep = line.substring(dpos+7).trim().replace('.','/');
let dep = line.substring(dpos + 7).trim().replace('.','/');
if (deps.indexOf(dep) < 0) {
deps.push(dep);
}
let deep = find_deps(dep, seen);
let deep = find_deps(dep, seen, uses);
deps = deps.filter(p => deep.indexOf(p) < 0);
// deeper dependencies go first
deps = [...deep, ...deps];
@ -91,6 +99,7 @@ function init(mod) {
for (let [key,val] of Object.entries(script)) {
let nval = val.map(p => p.charAt(0) === '&' ? p.substring(1) : p);
let modd = false;
let uses = [];
for (let path of val) {
let fc = path.charAt(0);
if (fc === '@') {
@ -101,7 +110,16 @@ function init(mod) {
} else {
continue;
}
let deps = find_deps(path);
let uses = [];
let deps = find_deps(path, [], uses);
if (uses.length) {
let miss = uses.filter(u => deps.indexOf(u) < 0);
if (miss.length) {
val.appendAll(miss);
}
// console.log({key, uses, miss, deps});
deps.appendAll(miss);
}
if (deps) {
// remove deps already in list
deps = deps.filter(p => nval.indexOf(p) < 0);
@ -110,7 +128,7 @@ function init(mod) {
throw `missing ${path} in nval`;
}
// inject deps
nval.splice(ppos,0,...deps);
nval.splice(ppos, 0, ...deps);
modd = true;
}
// if list is modified, substitute

View file

@ -20,7 +20,7 @@
LOCAL = self.debug && !SETUP.remote,
EVENT = kiri.broker = gapp.broker,
SDB = data.local,
SPACE = kiri.space = moto.Space,
SPACE = kiri.space = moto.space,
FILES = kiri.catalog = kiri.openFiles(new data.Index(SETUP.d ? SETUP.d[0] : 'kiri')),
CONF = kiri.conf,
clone = Object.clone;

View file

@ -62,6 +62,8 @@ if (gapp.load) return;
let mods = [];
let modn = {};
gapp.overlay = Object.assign;
// register module without a load function
gapp.register = (name, deps, fn) => {
gapp.load(undefined, name, deps);
@ -70,14 +72,19 @@ gapp.register = (name, deps, fn) => {
let root = self;
let map = toks.pop();
for (let tok of toks) {
root = root[tok];
if (!root) {
console.log({missing_root: tok, at: name});
if (!root[tok]) {
dbug.debug({creating_root: tok, for: name});
root = root[tok] = {};
} else {
root = root[tok];
}
}
map = root[map] = root[map] || {};
fn(self, exports => Object.assign(map, exports));
let tmp = root[map] || {};
fn(self, exports => {
if (exports) {
return root[map] = Object.assign(tmp, exports);
}
});
}
};
@ -101,13 +108,15 @@ gapp.check = (name, deps = []) => {
};
// perform dependency checks and run module load functions
gapp.finalize = (app, deps) => {
gapp.main = (app, deps, fn) => {
// app loader may also pass deps
gapp.check(app, deps);
// load mods after checking if dependency is present
for (let mod of mods) {
let { fn, name, deps } = mod;
gapp.check(name, deps);
if (deps) {
gapp.check(name, deps);
}
// mods with no name can't be depended on, but are allowed
if (name) {
dbug.debug(`${app} | load | ${name}`);
@ -117,9 +126,10 @@ gapp.finalize = (app, deps) => {
mod.fn();
}
}
// produces an error if finalize is called twice
// or if load is called after finalize
// force runtime error if gapp.load() called gapp.main()
mods = null;
// optional if not called inline with startup
if (fn) fn(self);
};
})();

View file

@ -2,23 +2,34 @@
"use strict";
(function() {
// dep: moto.license
// dep: moto.webui
// dep: moto.client
// dep: moto.broker
// dep: moto.space
// dep: data.index
// dep: mesh.api
// dep: mesh.model
// dep: mesh.build
// dep: load.file
gapp.main("main.mesh", [], (root) => {
let broker = gapp.broker;
let call = broker.send;
let dbindex = [ "admin", "space" ];
let { Quaternion, Mesh, MeshPhongMaterial, PlaneGeometry, DoubleSide } = THREE;
const { Quaternion, Mesh, MeshPhongMaterial, PlaneGeometry, DoubleSide } = THREE;
const { broker } = gapp;
const { moto } = root;
const { space } = moto;
const call = broker.send;
const dbindex = [ "admin", "space" ];
// set below. called once the DOM readyState = complete
// this is the main() entrypoint called after all dependents load
function init() {
let stores = data.open('mesh', { stores: dbindex, version: 4 }).init(),
moto = self.moto,
dark = false,
ortho = false,
zoomrev = true,
zoomspd = 1,
space = moto.Space,
platform = space.platform,
db = mesh.db = {
admin: stores.promise('admin'),
@ -75,7 +86,7 @@ function init() {
function restore_space() {
let { api } = mesh;
let count = 0;
let space = moto.Space;
let space = moto.space;
let mcache = {};
mesh.db.admin.get("camera")
.then(saved => {
@ -142,7 +153,7 @@ let temp_mode;
// split functions
let split = {
start() {
let space = moto.Space;
let space = moto.space;
let { api, util } = mesh;
// highlight button
let button = event.target;
@ -215,7 +226,7 @@ let split = {
},
end() {
let space = moto.Space;
let space = moto.space;
let { button, obj } = split.state;
button.classList.remove('selected');
space.scene.remove(obj);
@ -484,7 +495,7 @@ function object_destroy(id) {
// listen for changes like dark mode toggle
function set_darkmode(dark) {
let { prefs, selection, model } = mesh.api;
let { sky, platform } = moto.Space;
let { sky, platform } = moto.space;
prefs.map.space.dark = dark;
if (dark) {
mesh.material.wireframe.color.set(0xaaaaaa);
@ -535,18 +546,4 @@ document.onreadystatechange = function() {
}
}
// finalize modules
gapp.finalize("main.mesh", [
"moto.license", // dep: moto.license
"moto.webui", // dep: moto.webui
"moto.client", // dep: moto.client
"moto.broker", // dep: moto.broker
"moto.space", // dep: moto.space
"data.index", // dep: data.index
"mesh.api", // dep: mesh.api
"mesh.model", // dep: mesh.model
"mesh.build", // dep: mesh.build
"load.file", // dep: load.file
]);
})();
});

View file

@ -2,27 +2,24 @@
"use strict";
// dep: moto.license
// dep: moto.client
// dep: moto.broker
// dep: moto.space
// dep: mesh.util
// dep: add.array
// dep: ext.three
// dep: ext.three-bgu
gapp.register("mesh.api", [
"moto.license", // dep: moto.license
"moto.client", // dep: moto.client
"moto.broker", // dep: moto.broker
"moto.space", // dep: moto.space
"data.index", // dep: data.index
"mesh.tool", // dep: mesh.tool
"mesh.util", // dep: mesh.util
"add.array", // dep: add.array
], (root, exports) => {
gapp.register("mesh.api", [], (root, exports) => {
const { Matrix4, Vector3 } = THREE;
const { mesh, moto } = root;
const { space } = moto;
const { util } = mesh;
let groups = [];
const worker = moto.client.fn
const groups = [];
let selected = [];
const selection = {
@ -628,7 +625,7 @@ const api = exports({
}
});
const broker = gapp.broker;
const { broker } = gapp;
// publish messages when api functions are called
broker.wrapObject(selection, 'selection');
broker.wrapObject(model, 'model');

View file

@ -2,19 +2,16 @@
"use strict";
(function() {
// dep: moto.broker
// dep: mesh.util
// dep: mesh.api
gapp.register("mesh.build", [], (root, exports) => {
gapp.register("mesh.build",[
"moto.broker", // dep: moto.broker
"mesh.util", // dep: mesh.util
"mesh.api", // dep: mesh.api
]);
const { broker } = gapp;
const { mesh } = root;
const { api, util } = mesh;
let mesh = self.mesh = self.mesh || {};
let broker = gapp.broker;
let call = broker.send;
let { api, util } = mesh;
let rad = 180 / Math.PI;
let und = undefined;
@ -32,7 +29,7 @@ util.download = (data, filename = "mesh-data") => {
};
// add modal dialog functions to api
let modal = api.modal = {
const modal = api.modal = {
show(title, contents) {
if (this.info.showing) {
throw `modal conflict showing "${title}"`;
@ -118,7 +115,7 @@ let modal = api.modal = {
};
// transient logging window bottom/left
let log = api.log = {
const log = api.log = {
age: 10000, // age out lines more than 10 seconds old
data: [], // last n messages
@ -601,4 +598,4 @@ function ui_build() {
});
}
})();
});

View file

@ -2,22 +2,16 @@
"use strict";
(function() {
// dep: moto.license
// dep: add.array
// dep: ext.three
// dep: ext.three-bgu
gapp.register("mesh.geom", [
"moto.license", // dep: moto.license
"add.array", // dep: add.array
]);
gapp.register("mesh.geom", [], (root, exports) => {
let mesh = self.mesh = self.mesh || {};
if (mesh.geom) return;
let { Matrix4, Matrix3, Vector3, Box3 } = THREE;
const { Matrix4, Matrix3, Vector3, Box3 } = THREE;
// geometry helper functions
let geom = mesh.geom = {
const geom = exports({
// given a closed polyline as an expanded point array
// return enclosed area with sign indicating winding order
@ -100,6 +94,6 @@ let geom = mesh.geom = {
return recs;
}
};
});
})();
});

View file

@ -2,27 +2,24 @@
"use strict";
(function() {
// dep: add.array
// dep: add.three
// dep: moto.license
// dep: moto.broker
// dep: moto.client
// dep: mesh.object
// use: mesh.api
// use: mesh.model
gapp.register("mesh.group", [], (root, exports) => {
gapp.register("mesh.group", [
"add.array", // dep: add.array
"add.three", // dep: add.three
"moto.license", // dep: moto.license
"moto.broker", // dep: moto.broker
"mesh.object", // dep: mesh.object
"mesh.model", // dep: mesh.model
"mesh.api", // dep: mesh.api
]);
const { mesh, moto } = root;
const { Quaternion, Group, Vector3 } = THREE;
const { broker } = gapp;
const { space } = moto;
let mesh = self.mesh = self.mesh || {};
if (mesh.group) return;
let { Quaternion, Group, Vector3 } = THREE;
let broker = gapp.broker;
let call = broker.send;
let space = moto.Space;
let worker = moto.client.fn;
let lookUp = new Vector3(0,0,-1);
const call = broker.send;
const worker = moto.client.fn;
const lookUp = new Vector3(0,0,-1);
mesh.group = class MeshGroup extends mesh.object {
@ -165,4 +162,4 @@ mesh.group = class MeshGroup extends mesh.object {
}
};
})();
});

View file

@ -2,26 +2,23 @@
"use strict";
(function() {
// dep: add.array
// dep: add.three
// dep: moto.license
// dep: moto.client
// dep: mesh.object
// use: mesh.api
// use: mesh.util
// use: mesh.group
gapp.register("mesh.model", [], (root, exports) => {
gapp.register("mesh.model", [
"add.array", // dep: add.array
"add.three", // dep: add.three
"moto.license", // dep: moto.license
"mesh.object", // dep: mesh.object
"mesh.group", // dep: mesh.group
"mesh.util", // dep: mesh.util
"mesh.api", // dep: mesh.api
]);
const { MeshPhongMaterial, MeshBasicMaterial } = THREE;
const { BufferGeometry, BufferAttribute, DoubleSide, Mesh } = THREE;
const { mesh, moto } = root;
const { space } = moto;
let mesh = self.mesh = self.mesh || {};
if (mesh.model) return;
let mapp = mesh;
let space = moto.Space;
let worker = moto.client.fn;
let { MeshPhongMaterial, MeshBasicMaterial } = THREE;
let { BufferGeometry, BufferAttribute, DoubleSide, Mesh } = THREE;
const mapp = mesh;
const worker = moto.client.fn;
/** default materials **/
let materials = mesh.material = {
@ -606,4 +603,4 @@ mesh.model = class MeshModel extends mesh.object {
};
})();
});

View file

@ -2,23 +2,18 @@
"use strict";
(function() {
// dep: add.three
// dep: moto.license
// dep: moto.broker
// use: mesh.api
// use: mesh.util
gapp.register("mesh.object", [], (root, exports) => {
gapp.register("mesh.object", [
"add.three", // dep: add.three
"moto.license", // dep: moto.license
"moto.broker", // dep: moto.broker
"mesh.util", // dep: mesh.util
"mesh.api", // dep: mesh.api
]);
const { Matrix4, Vector3, Box3, Box3Helper, Quaternion, Euler } = THREE;
const { mesh, moto } = root;
const { space } = moto;
let mesh = self.mesh = self.mesh || {};
if (mesh.object) return;
let space = moto.Space;
let worker = moto.client.fn;
let { Matrix4, Vector3, Box3, Box3Helper, Quaternion, Euler } = THREE;
const worker = moto.client.fn;
// broker updates generated by objects
let publish = {
@ -195,4 +190,4 @@ mesh.object = class MeshObject {
}
};
})();
});

View file

@ -2,11 +2,8 @@
"use strict";
(function() {
gapp.finalize("mesh.pool", [
"moto.license", // dep: moto.license
"moto.worker", // dep: moto.worker
]);
})();
// dep: moto.license
// dep: moto.worker
gapp.main("mesh.pool", [], (root) => {
// todo
});

View file

@ -2,17 +2,12 @@
"use strict";
(function() {
// dep: ext.earcut
gapp.register("mesh.tool", [
"mesh.geom", // dep: mesh.geom
]);
// dep: mesh.geom
gapp.register("mesh.tool", [], (root, exports) => {
let mesh = self.mesh = self.mesh || {};
if (mesh.tool) return;
let geom = mesh.geom;
const { mesh } = root;
const { geom } = mesh;
/**
* tool for identiying defects and healing them
@ -470,4 +465,4 @@ mesh.tool = class MeshTool {
}
};
})();
});

View file

@ -2,25 +2,22 @@
"use strict";
(function() {
// dep: moto.license
// dep: add.array
// dep: ext.three
// dep: ext.three-bgu
gapp.register("mesh.util", [
"moto.license", // dep: moto.license
"add.array", // dep: add.array
]);
// use: mesh.api
gapp.register("mesh.util", [], (root, exports) => {
let mesh = self.mesh = self.mesh || {};
if (mesh.util) return;
const { Matrix4, Matrix3, Vector3, Box3 } = THREE;
const { mesh } = root;
let { Matrix4, Matrix3, Vector3, Box3 } = THREE;
let deferFn = [];
let boundsCache = {};
const deferFn = [];
const boundsCache = {};
// util functions augmented in build (download)
let util = mesh.util = {
const util = exports({
uuid(segs = 1) {
let uid = [];
while (segs-- > 0) {
@ -62,7 +59,7 @@ let util = mesh.util = {
},
// @param object {THREE.Object3D | THREE.Object3D[] | MeshObject | MeshObject[]}
// @returns bounds modified for moto.Space
// @returns bounds modified for moto.space
bounds(object) {
let box = new Box3();
if (Array.isArray(object)) {
@ -258,6 +255,6 @@ let util = mesh.util = {
return new VertexNormalsHelper(mesh);
}
};
});
})();
});

View file

@ -2,13 +2,15 @@
"use strict";
(function() {
// start worker pool (disabled for now with *0)
moto.client.start(`/code/mesh_pool?${gapp.version}`, moto.client.max() * 0);
// dep: moto.license
// dep: ext.clip2
// dep: ext.three
// dep: ext.three-bgu
// dep: add.three
// dep: add.array
// dep: moto.client
// dep: moto.worker
// dep: mesh.util
// dep: geo.base
// dep: geo.line
// dep: geo.point
@ -17,24 +19,20 @@ moto.client.start(`/code/mesh_pool?${gapp.version}`, moto.client.max() * 0);
// dep: geo.bounds
// dep: geo.slicer
// dep: geo.csg
// dep: ext.clip2
gapp.finalize("mesh.work", [
"moto.license", // dep: moto.license
"moto.client", // dep: moto.client
"moto.worker", // dep: moto.worker
"mesh.tool", // dep: mesh.tool
"mesh.util", // dep: mesh.util
"add.three", // dep: add.three
]);
// dep: mesh.tool
gapp.main("mesh.work", [], (root) => {
let { Matrix4, Vector3, BufferGeometry, BufferAttribute, computeFaceNormal } = THREE;
const { Matrix4, Vector3, BufferGeometry, BufferAttribute, computeFaceNormal } = THREE;
const { mesh, moto } = root;
const { client, worker } = moto;
const { util } = mesh;
const cache = {};
// compensation for space/world/platform rotation
let core_matrix = new Matrix4().makeRotationX(Math.PI / 2);
const core_matrix = new Matrix4().makeRotationX(Math.PI / 2);
let { client, worker } = moto;
let { util } = mesh;
let cache = {};
// start worker pool (disabled for now with *0)
client.start(`/code/mesh_pool?${gapp.version}`, client.max() * 0);
function log(msg) {
return worker.publish("mesh.log", msg);
@ -445,4 +443,4 @@ worker.bindObject({
worker.ready();
})();
});

View file

@ -2,41 +2,29 @@
"use strict";
(function() {
gapp.register("moto.ajax", [], (root, exports) => {
gapp.register('moto.ajax');
function rnd() {
return Math.round(Math.random()*0xffffffff).toString(36);
}
let moto = self.moto = self.moto || {};
if (moto.Ajax) return;
const KV = data.local,
KEY = "moto-ajax",
TIME = Date.now,
MOKEY = moto.id = KV.getItem(KEY) || (TIME().toString(36)+rnd()+rnd());
moto.Ajax = Ajax;
KV.setItem(KEY, MOKEY);
moto.callAjax = function(url, handler) {
new Ajax(handler).request(url);
};
const STATES = [
"request not initialized", // 0
"server connection established", // 1
"request recieved", // 2
"processing request", // 3
"request complete" // 4
];
const AP = Ajax.prototype,
KV = data.local,
KEY = "moto-ajax",
TIME = function() { return new Date().getTime() },
MOKEY = moto.id = KV.getItem(KEY) || (TIME().toString(36)+rnd()+rnd());
moto.restore = function(id) {
MOKEY = moto.id = id;
KV.setItem(KEY, MOKEY);
}
KV.setItem(KEY, MOKEY);
const STATES = [
"request not initialized", // 0
"server connection established", // 1
"request recieved", // 2
"processing request", // 3
"request complete" // 4
];
function Ajax(callback, responseType) {
class Ajax {
constructor(callback, responseType) {
this.ajax = new XMLHttpRequest();
this.ajax.onreadystatechange = this.onStateChange.bind(this);
this.ajax.withCredentials = true;
@ -45,11 +33,7 @@
this.responseType = responseType;
}
function rnd() {
return Math.round(Math.random()*0xffffffff).toString(36);
}
AP.onStateChange = function() {
onStateChange() {
this.state = STATES[this.ajax.readyState];
if (this.ajax.readyState === 4 && this.callback) {
let status = this.ajax.status;
@ -59,14 +43,9 @@
this.callback(null, this.ajax);
}
}
};
}
/**
* @param {String} url
* @param {Object} [send]
* @param {Object} [headers]
*/
AP.request = function(url, send, headers) {
request(url, send, headers) {
this.ajax.open(send ? "POST" : "GET", url, true);
if (this.responseType) this.ajax.responseType = this.responseType;
headers = headers || {};
@ -79,6 +58,23 @@
} else {
this.ajax.send();
}
};
return this;
}
}
})();
function call(url, handler) {
return new Ajax(handler).request(url);
}
function callAjax(url, handler) {
return new Ajax(handler).request(url);
};
function restore(id) {
MOKEY = moto.id = id;
KV.setItem(KEY, MOKEY);
}
exports({ Ajax, call, callAjax, restore });
});

View file

@ -2,13 +2,10 @@
"use strict";
(function() {
gapp.register("moto.broker", [], (root, exports) => {
if (gapp.broker) return;
class Broker {
gapp.register('moto.broker');
gapp.broker = new class Broker {
constructor() {
this.topics = {};
this.used = {};
@ -137,4 +134,7 @@ gapp.broker = new class Broker {
}
})();
const { moto } = root;
gapp.broker = moto.broker = new Broker();
});

View file

@ -2,10 +2,14 @@
"use strict";
(function() {
gapp.register("moto.client", [
let moto = self.moto = self.moto || {},
ccvalue = self.navigator ? navigator.hardwareConcurrency || 1 : 1,
"add.array", // dep: add.array
"moto.broker", // dep: moto.broker
], (root, exports) => {
let ccvalue = self.navigator ? navigator.hardwareConcurrency || 1 : 1,
ccmax = ccvalue > 3 ? ccvalue - 1 : 1,
workcc = false, // concurrent or not
workurl = null, // url to load worker
@ -14,13 +18,6 @@ let moto = self.moto = self.moto || {},
queue = [], // array of work requests
topics = {}; // topic handler (broker lite for worker > client msgs)
if (moto.client) return;
gapp.register('moto.client', [
"add.array", // dep: add.array
"moto.broker", // dep: moto.broker
]);
function dispatch(topic, message) {
for (let fn of topics[topic] || []) {
fn(message);
@ -28,7 +25,7 @@ function dispatch(topic, message) {
}
// code is running in the browser / client context
let client = moto.client = {
const client = exports({
/** @returns [integer] total number of workers **/
live: () => {
@ -258,6 +255,6 @@ let client = moto.client = {
client.kick();
}
};
});
})();
});

View file

@ -2,19 +2,18 @@
"use strict";
(function() {
gapp.register("moto.orbit", [
let MOTO = self.moto = self.moto || {};
'add.three', // dep: add.three
gapp.register('moto.orbit', [
'add.three', // dep: add.three
]);
], (root, exports) => {
const { moto } = root;
/**
* Adapted from THREE.OrbitControls
*/
MOTO.Orbit = function (object, domElement, notify, slider) {
moto.Orbit = function (object, domElement, notify, slider) {
this.object = object;
this.domElement = ( domElement !== undefined ) ? domElement : document;
@ -630,6 +629,6 @@ MOTO.Orbit = function (object, domElement, notify, slider) {
window.addEventListener('keydown', onKeyDown, false);
};
MOTO.Orbit.prototype = Object.create(THREE.EventDispatcher.prototype);
moto.Orbit.prototype = Object.create(THREE.EventDispatcher.prototype);
})();
});

View file

@ -2,16 +2,18 @@
"use strict";
(function() {
// dep: ext.tween
gapp.register('moto.space', [
'add.three', // dep: add.three
'add.array', // dep: add.array
'moto.orbit' // dep: moto.orbit
]);
// dep: ext.tween
gapp.register("moto.space", [
let MOTO = self.moto = self.moto || {},
WIN = window,
'add.three', // dep: add.three
'add.array', // dep: add.array
'moto.orbit' // dep: moto.orbit
], (root, exports) => {
const { moto } = root;
let WIN = window,
DOC = document,
SCENE = new THREE.Scene(),
WORLD = new THREE.Group(),
@ -1000,7 +1002,7 @@
}
}
let Space = MOTO.Space = MOTO.space = {
let Space = exports({
refresh: refresh,
update: requestRefresh,
@ -1217,7 +1219,7 @@
raycaster = new THREE.Raycaster();
viewControl = new MOTO.Orbit(camera, domelement, (position, moved) => {
viewControl = new moto.Orbit(camera, domelement, (position, moved) => {
if (platform) {
platform.visible = hidePlatformBelow ?
initialized && position.y >= 0 && showPlatform : showPlatform;
@ -1326,13 +1328,15 @@
initialized = true;
}
};
});
let cycle = [
Space.view.front,
Space.view.right,
Space.view.back,
Space.view.left,
];
let cycleInd = 0;
})();
});

View file

@ -2,35 +2,12 @@
"use strict";
(function() {
gapp.register("moto.webui", [
gapp.register('moto.webui', [
"moto.license", // dep: moto.license
"add.array", // dep: add.array
]);
"moto.license", // dep: moto.license
"add.array", // dep: add.array
let moto = self.moto = self.moto || {};
if (moto.webui) return;
// window ui helpers
Object.assign(self, {
$: (id) => {
return document.getElementById(id);
},
$d: (id, v) => {
$(id).style.display = v;
},
$h: (id, h) => {
$(id).innerHTML = h;
},
estop: (evt) => {
evt.stopPropagation();
evt.preventDefault();
}
});
], (root, exports) => {
let nextid = 1;
@ -92,7 +69,7 @@ function build(data, context) {
}
// core html builder funtions
let h = self.h = moto.webui = {
let h = exports({
bind: (el, data, opt = {}) => {
let ctx = [];
let html = build(data, ctx).join('');
@ -120,7 +97,29 @@ let h = self.h = moto.webui = {
}
return { type, attr, innr };
}
};
});
// window ui helpers
gapp.overlay(root, {
$: (id) => {
return document.getElementById(id);
},
$d: (id, v) => {
$(id).style.display = v;
},
$h: (id, h) => {
$(id).innerHTML = h;
},
h,
estop: (evt) => {
evt.stopPropagation();
evt.preventDefault();
}
});
// add common element types
["a", "i", "div", "span", "label", "input", "button", "svg"].forEach(type => {
@ -129,4 +128,4 @@ let h = self.h = moto.webui = {
}
});
})();
});

View file

@ -2,18 +2,12 @@
"use strict";
(function() {
let moto = self.moto = self.moto || {};
if (moto.worker) return;
gapp.register('moto.worker');
gapp.register("moto.worker", [], (root, exports) => {
let endpoints = {};
// code is running in the worker / server context
const dispatch = moto.worker = {
const dispatch = exports({
// bind an endpoint name to a function
bind(name, fn) {
@ -100,9 +94,9 @@ const dispatch = moto.worker = {
}
}
};
});
// attach onmessage hanler
self.onmessage = dispatch.onmessage;
})();
});