start updating kiri package to use gapp.register()

This commit is contained in:
Stewart Allen 2022-02-20 18:00:31 -05:00
commit 64d825b48c
11 changed files with 77 additions and 69 deletions

View file

@ -102,7 +102,6 @@
synthPlunge = false,
spindle = 0,
spindleMax = device.spindleMax,
addOutput = print.addOutput,
maxToolDiam = widget.maxToolDiam,
terrain = widget.terrain ? widget.terrain.map(data => {
return {
@ -222,7 +221,7 @@
*/
function layerPush(point, emit, speed, tool) {
layerOut.mode = lastMode;
addOutput(layerOut, point, emit, speed, tool);
print.addOutput(layerOut, point, emit, speed, tool);
return point;
}
@ -498,7 +497,7 @@
// last layer/move is to zmax
// injected into the last layer generated
if (lastPoint && newOutput.length)
addOutput(newOutput[newOutput.length-1], printPoint = lastPoint.clone().setZ(zmax_outer), 0, 0, tool.getNumber());
print.addOutput(newOutput[newOutput.length-1], printPoint = lastPoint.clone().setZ(zmax_outer), 0, 0, tool.getNumber());
// replace output single flattened layer with all points
print.output = newOutput;

View file

@ -2,7 +2,8 @@
"use strict";
(function () {
// dep: kiri.api
gapp.register("kiri.alerts", [], (root, exports) => {
const { kiri } = self;
const { api } = kiri;
@ -72,4 +73,4 @@ api.alerts = {
update
};
})();
});

View file

@ -2,7 +2,12 @@
"use strict";
(function () {
// dep: main.kiri
// dep: data.local
// dep: kiri.utils
// use: kiri.client
// use: kiri.settings
gapp.register("kiri.api", [], (root, exports) => {
const { data, kiri, noop } = self;
const { consts, utils } = kiri;
@ -43,7 +48,7 @@ const tweak = {
gcode_decimals(v) { api.work.config({base:{gcode_decimals: v}}) }
};
const api = kiri.api = {
const api = exports({
clone: Object.clone,
sdb: data.local,
ajax: ajax,
@ -62,6 +67,6 @@ const api = kiri.api = {
feature,
devel,
tweak,
};
});
})();
});

View file

@ -2,16 +2,18 @@
"use strict";
(function() {
// use: kiri.api
gapp.register("kiri.client", [], (root, exports) => {
const { kiri } = root;
// this code runs in kiri's main loop
let KIRI = self.kiri = self.kiri || {},
loc = self.location,
let loc = self.location,
host = loc.hostname,
port = loc.port,
proto = loc.protocol,
debug = self.debug === true,
time = function() { return new Date().getTime() },
time = Date.now,
seqid = 1,
syncd = {},
running = {},
@ -21,7 +23,7 @@ let KIRI = self.kiri = self.kiri || {},
;
/**
* @param {Function} fn name of function in KIRI.worker
* @param {Function} fn name of function in kiri.worker
* @param {Object} data to send to server
* @param {Function} onreply function to call on reply messages
* @param {Object[]} zerocopy array of objects to pass using zerocopy
@ -49,9 +51,7 @@ function send(fn, data, onreply, zerocopy) {
}
// code is running in the browser / client context
const CLIENT =
KIRI.client =
KIRI.work = {
const client = exports({
send: send,
newWorker: function() {
@ -93,9 +93,9 @@ KIRI.work = {
syncd = {};
running = {};
worker = KIRI.work.newWorker();
worker = client.newWorker();
CLIENT.onmessage = worker.onmessage = function(e) {
client.onmessage = worker.onmessage = function(e) {
let now = time(),
reply = e.data,
record = running[reply.seq],
@ -120,10 +120,10 @@ KIRI.work = {
},
decimate: function(vertices, options, callback) {
let alert = KIRI.api.show.alert('processing model', 1000);
let alert = kiri.api.show.alert('processing model', 1000);
vertices = vertices.buffer.slice(0);
send("decimate", {vertices, options}, function(output) {
KIRI.api.hide.alert(alert);
kiri.api.hide.alert(alert);
callback(output);
});
},
@ -154,7 +154,7 @@ KIRI.work = {
// widget sync
sync: function(widgets) {
if (!widgets) {
widgets = KIRI.api.widgets.all();
widgets = kiri.api.widgets.all();
}
// send list of currently valid widgets
send("sync", { valid: widgets.map(w => w.id) }, () => {});
@ -180,7 +180,7 @@ KIRI.work = {
rotate: function(settings, callback) {
send("rotate", { settings }, reply => {
if (reply.group) {
for (let widget of KIRI.Widget.Groups.forid(reply.group)) {
for (let widget of kiri.Widget.Groups.forid(reply.group)) {
widget.belt = reply.belt;
}
} else if (callback) {
@ -192,7 +192,7 @@ KIRI.work = {
unrotate: function(settings, callback) {
send("unrotate", { settings }, reply => {
if (reply.group) {
for (let widget of KIRI.Widget.Groups.forid(reply.group)) {
for (let widget of kiri.Widget.Groups.forid(reply.group)) {
widget.rotinfo = reply.rotinfo;
}
} else if (callback) {
@ -247,7 +247,7 @@ KIRI.work = {
ondone(null, reply.error);
}
if (reply.debug) {
KIRI.api.event.emit("export.debug", reply.debug);
kiri.api.event.emit("export.debug", reply.debug);
}
});
},
@ -268,10 +268,10 @@ KIRI.work = {
y: -origin.y,
z: origin.z
};
const output = KIRI.newPrint().parseSVG(code, offset);
const output = kiri.newPrint().parseSVG(code, offset);
send("parse_svg", output, function(reply) {
if (reply.parsed) {
done(KIRI.codec.decode(reply.parsed));
done(kiri.codec.decode(reply.parsed));
}
});
return;
@ -281,7 +281,7 @@ KIRI.work = {
progress(reply.progress);
}
if (reply.parsed) {
done(KIRI.codec.decode(reply.parsed), reply.maxSpeed, reply.minSpeed);
done(kiri.codec.decode(reply.parsed), reply.maxSpeed, reply.minSpeed);
}
});
},
@ -312,9 +312,9 @@ KIRI.work = {
// console.log({wasm_worker_said: reply});
});
}
};
});
// start worker
KIRI.work.restart();
client.restart();
})();
});

View file

@ -2,15 +2,15 @@
"use strict";
(function() {
if (self.kiri.codec) return;
// dep: main.kiri
// dep: geo.base
gapp.register("kiri.codec", [], (root, exports) => {
const BASE = self.base, KIRI = self.kiri, handlers = {};
const freeMem = true;
const zeroOut = true;
const codec = KIRI.codec = {
const codec = exports({
undef: undefined,
encode: encode,
decode: decode,
@ -19,7 +19,7 @@
encodePointArray,
decodePointArray,
toCodable
};
});
function toCodable(object) {
switch (typeof object) {
@ -389,5 +389,4 @@
return poly;
});
})();
});

View file

@ -2,11 +2,11 @@
"use strict";
(function() {
gapp.register("kiri.conf", [], (root, exports) => {
if (self.kiri.conf) return;
const { kiri } = root;
const KIRI = self.kiri, CVER = 185, clone = Object.clone;
const CVER = 185, clone = Object.clone;
function genID() {
while (true) {
@ -96,7 +96,7 @@
// presence of internal field indicates already converted
if (code.internal >= 0) return code;
// if (self.navigator) console.log({mode, convert: code});
let API = KIRI.api,
let API = kiri.api,
cmd = code.cmd || {},
set = code.settings || {},
ext = code.extruders;
@ -145,7 +145,7 @@
if (ext) {
// synthesize extruders from new style settings
ext.forEach(rec => {
let e = API.clone(CONF.defaults.fdm.d.extruders[0]);
let e = API.clone(conf.defaults.fdm.d.extruders[0]);
if (rec.nozzle) e.extNozzle = rec.nozzle;
if (rec.filament) e.extFilament = rec.filament;
if (rec.offset_x) e.extOffsetX = rec.offset_x;
@ -156,7 +156,7 @@
});
} else {
// synthesize extruders from old style settings
device.extruders = [API.clone(CONF.defaults.fdm.d.extruders[0])];
device.extruders = [API.clone(conf.defaults.fdm.d.extruders[0])];
device.extruders[0].extNozzle = valueOf(set.nozzle_size, 0.4);
device.extruders[0].extFilament = valueOf(set.filament_diameter, 1.75);
}
@ -166,9 +166,9 @@
// ensure settings structure is up-to-date
function normalize(settings) {
let API = KIRI.api,
defaults = CONF.defaults,
template = CONF.template,
let API = kiri.api,
defaults = conf.defaults,
template = conf.template,
mode = settings.mode.toLowerCase(),
default_dev = defaults[mode].d,
default_pro = defaults[mode].p;
@ -246,7 +246,7 @@
outputClockwise: "camConventional"
};
let CONF = KIRI.conf = {
const conf = exports({
// --------------- helper functions
normalize,
device_from_code,
@ -707,9 +707,9 @@
id: genID(),
ver: CVER
}
};
});
let settings = CONF.template;
const settings = conf.template;
// seed defaults. will get culled on save
settings.sproc.FDM.default = clone(settings.process);
@ -721,4 +721,4 @@
settings.cdev.CAM = clone(settings.device);
settings.cdev.LASER = clone(settings.device);
})();
});

View file

@ -64,7 +64,7 @@
if (vertices.length < (options.threshold || 500000)) {
return callback(vertices);
}
kiri.work.decimate(vertices, this.options, function(reply) {
kiri.client.decimate(vertices, this.options, function(reply) {
callback(reply);
});
};

View file

@ -175,7 +175,7 @@
unit_scale: unitScale,
wireframe: setWireframe,
},
work: kiri.work
work: kiri.client
});
function updateStackLabelState() {
@ -598,7 +598,7 @@
function clearWorkspace() {
// free up worker cache/mem
kiri.work.clear();
kiri.client.clear();
platform.select_all();
platform.delete(selection.meshes());
}

View file

@ -453,7 +453,7 @@ function platformDelete(widget) {
}
return;
}
kiri.work.clear(widget);
kiri.client.clear(widget);
api.widgets.remove(widget);
api.selection.remove(widget);
Widget.Groups.remove(widget);

View file

@ -2,6 +2,9 @@
"use strict";
// dep: data.local
// dep: main.kiri
// use: moto.ajax
(function () {
const { kiri, moto, data } = self;
@ -30,7 +33,7 @@ function encodeOpt(opt) {
}
function ajax(url, fn, rt, po, hd) {
return new moto.Ajax(fn, rt).request(url, po, hd);
return moto.ajax.new(fn, rt).request(url, po, hd);
}
function o2js(o,def) {

View file

@ -8,12 +8,13 @@ function rnd() {
return Math.round(Math.random()*0xffffffff).toString(36);
}
const KV = data.local,
KEY = "moto-ajax",
TIME = Date.now,
MOKEY = moto.id = KV.getItem(KEY) || (TIME().toString(36)+rnd()+rnd());
const { data } = root;
const { local } = data;
KV.setItem(KEY, MOKEY);
const KEY = "moto-ajax";
const MOID = moto.id = local.getItem(KEY) || (Date.now().toString(36)+rnd()+rnd());
local.setItem(KEY, MOID);
const STATES = [
"request not initialized", // 0
@ -49,7 +50,7 @@ class Ajax {
this.ajax.open(send ? "POST" : "GET", url, true);
if (this.responseType) this.ajax.responseType = this.responseType;
headers = headers || {};
headers["X-Moto-Ajax"] = MOKEY;
headers["X-Moto-Ajax"] = MOID;
for (let k in headers) {
this.ajax.setRequestHeader(k, headers[k]);
}
@ -66,15 +67,15 @@ 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);
MOID = moto.id = id;
local.setItem(KEY, MOID);
}
exports({ Ajax, call, callAjax, restore });
exports({
new(cb, rt) { return new Ajax(cb, rt) },
call,
restore,
});
});