From 178e88f76bb2ea8606bf9c610d0ca89fe12fad32 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Fri, 19 Aug 2016 15:12:20 +0200 Subject: [PATCH] splice slice sync and slice async --- example/app.js | 2 +- src/Slicer.js | 46 ++++++++++++++++++++++------------------------ 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/example/app.js b/example/app.js index 57a3931..f07c9c9 100644 --- a/example/app.js +++ b/example/app.js @@ -11,6 +11,6 @@ const geometry = new THREE.TorusGeometry(20, 10, 30, 30); const slicer = new SLICER.Slicer(); slicer.setGeometry(geometry); -slicer.slice(settings, true).then(gcode => { +slicer.slice(settings).then(gcode => { document.getElementById('gcode').innerHTML = gcode.replace(/(?:\r\n|\r|\n)/g, '
'); }); diff --git a/src/Slicer.js b/src/Slicer.js index e3d5327..4fb2121 100644 --- a/src/Slicer.js +++ b/src/Slicer.js @@ -30,34 +30,32 @@ export default class { return this; } - async slice(settings, async = false) { - let gcode; + sliceSync(settings) { + return slice(this.geometry, settings); + } + slice(settings) { + const sliceWorker = new SliceWorker(); - if (async) { - const sliceWorker = new SliceWorker(); + const geometry = this.geometry.toJSON(); + const { config } = settings; - const geometry = this.geometry.toJSON(); - const { config } = settings; + return new Promise((resolve, reject) => { + sliceWorker.onerror = reject; - gcode = await new Promise((resolve) => { - sliceWorker.addEventListener('message', (event) => { - const { message, data } = event.data; - switch (message) { - case 'SLICE': { - sliceWorker.terminate(); - resolve(data.gcode); - } + sliceWorker.addEventListener('message', (event) => { + const { message, data } = event.data; + switch (message) { + case 'SLICE': { + sliceWorker.terminate(); + resolve(data.gcode); } - }); - - sliceWorker.postMessage({ - message: 'SLICE', - data: { geometry, config } - }); + } }); - } else { - gcode = slice(this.geometry, settings); - } - return gcode; + + sliceWorker.postMessage({ + message: 'SLICE', + data: { geometry, config } + }); + }); } }