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

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;
})();
});