From 4cae0b945aa8ac36ccc98c0ebdea824241397627 Mon Sep 17 00:00:00 2001 From: Stewart Allen Date: Tue, 16 Jun 2020 11:08:50 -0400 Subject: [PATCH] namespace cleanup for workers --- src/kiri/codec.js | 11 ++++++++--- src/kiri/slicer.js | 2 +- src/kiri/worker.js | 40 +++++++++++++++++++++++----------------- src/mode/sla/driver.js | 5 +++-- 4 files changed, 35 insertions(+), 23 deletions(-) diff --git a/src/kiri/codec.js b/src/kiri/codec.js index bdb1977a..d9bacfd1 100644 --- a/src/kiri/codec.js +++ b/src/kiri/codec.js @@ -8,12 +8,17 @@ const BASE = self.base, KIRI = self.kiri, handlers = {}; - KIRI.codec = { + const codec = KIRI.codec = { encode: encode, decode: decode, - registerDecoder: registerDecoder + registerDecoder: registerDecoder, + allocFloat32Array: allocFloat32Array }; + function allocFloat32Array(len) { + return new Float32Array(len); + } + function encode(o, state) { state = state || {}; if (o === null) return null; @@ -140,7 +145,7 @@ function encodePointArray(points, state) { if (!points) return null; - let array = new Float32Array(points.length * 3), + let array = codec.allocFloat32Array(points.length * 3), pos = 0; points.forEach(function(point) { diff --git a/src/kiri/slicer.js b/src/kiri/slicer.js index f61c2306..9f528d22 100644 --- a/src/kiri/slicer.js +++ b/src/kiri/slicer.js @@ -858,7 +858,7 @@ // find new endpoints that are not shared point let p1 = l1.p1 != point ? l1.p1 : l1.p2, p2 = l2.p1 != point ? l2.p1 : l2.p2, - newline = base.newOrderedLine(p1,p2); + newline = BASE.newOrderedLine(p1,p2); // remove deleted lines from associated points p1.group.remove(l1); p1.group.remove(l2); diff --git a/src/kiri/worker.js b/src/kiri/worker.js index dd8b8006..c5a120bd 100644 --- a/src/kiri/worker.js +++ b/src/kiri/worker.js @@ -2,16 +2,20 @@ "use strict"; +// (function() { + const base = self.base, util = base.util, time = util.time, kiri = self.kiri, ver = kiri.version, - Widget = kiri.Widget; + Widget = kiri.Widget, + current = self.worker = { + print: null, + snap: null + }; -let currentPrint, - currentSnap, - cache = {}; +let cache = {}; console.log(`kiri | init work | ${ver}`); base.debug.disable(); @@ -29,7 +33,7 @@ let dispatch = { }, snap: function(data, send) { - currentSnap = data; + current.snap = data; send.done(); }, @@ -109,8 +113,8 @@ let dispatch = { send.data({update:0.05, updateStatus:"preview"}); - currentPrint = kiri.newPrint(data.settings, widgets, data.id); - currentPrint.setup(false, function(update, msg) { + current.print = kiri.newPrint(data.settings, widgets, data.id); + current.print.setup(false, function(update, msg) { send.data({ update: update, updateStatus: msg @@ -118,13 +122,13 @@ let dispatch = { }, function() { send.done({ done: true, - output: currentPrint.encodeOutput() + output: current.print.encodeOutput() }); }); }, printExport: function(data, send) { - currentPrint.export(false, function(line, direct) { + current.print.export(false, function(line, direct) { send.data({line}, direct); }, function(done, direct) { send.done({done}, direct); @@ -132,14 +136,14 @@ let dispatch = { }, printGCode: function(data, send) { - currentPrint.exportGCode(false, function(gcode) { + current.print.exportGCode(false, function(gcode) { send.done({ gcode: gcode, - lines: currentPrint.lines, - bytes: currentPrint.bytes, - bounds: currentPrint.bounds, - distance: currentPrint.distance, - time: currentPrint.time + lines: current.print.lines, + bytes: current.print.bytes, + bounds: current.print.bounds, + distance: current.print.distance, + time: current.print.time }); }, function(line) { send.data({line:line}); @@ -147,8 +151,8 @@ let dispatch = { }, clear: function(data, send) { - currentSnap = null; - currentPrint = null; + current.snap = null; + current.print = null; if (!data.id) { cache = {}; send.done({ clear: true }); @@ -206,3 +210,5 @@ self.onmessage = function(e) { console.log({kiri_msg:e}); } }; + +// })(); diff --git a/src/mode/sla/driver.js b/src/mode/sla/driver.js index c06ef9a1..8388b0ec 100644 --- a/src/mode/sla/driver.js +++ b/src/mode/sla/driver.js @@ -78,9 +78,10 @@ }); } - let b64 = atob(currentSnap); + let b64 = atob(self.worker.snap); let bin = Uint8Array.from(b64, c => c.charCodeAt(0)); - let img = new png.PNG().parse(bin, (err, data) => { + let img = new png.PNG(); + img.parse(bin, (err, data) => { preview = img; previewSmall = samplePNG(img, 200, 125); previewLarge = samplePNG(img, 400, 300);