diff --git a/config.js b/config.js
index 5dd6187..59039c6 100644
--- a/config.js
+++ b/config.js
@@ -18,6 +18,7 @@ System.config({
map: {
"babel": "npm:babel-core@5.8.21",
"babel-runtime": "npm:babel-runtime@5.8.20",
+ "casperlamboo/EventDispatcher": "github:casperlamboo/EventDispatcher@master",
"clipper-lib": "npm:clipper-lib@1.0.0",
"core-js": "npm:core-js@0.9.18",
"json": "github:systemjs/plugin-json@0.1.0",
diff --git a/example/app.js b/example/app.js
index eaa4bfa..a5bbfbe 100644
--- a/example/app.js
+++ b/example/app.js
@@ -13,7 +13,7 @@ var slicer = new SLICER.Slicer();
//var slicer = new SLICER.SlicerWorker();
slicer.setGeometry(geometry.clone());
-slicer.onfinish = function (gCode) {
- document.getElementById('gcode').innerHTML = gCode.replace(/(?:\r\n|\r|\n)/g, '
');
-};
+slicer.addEventListener('finish', ({ gcode }) => {
+ document.getElementById('gcode').innerHTML = gcode.replace(/(?:\r\n|\r|\n)/g, '
');
+});
slicer.slice(settings);
diff --git a/package.json b/package.json
index 1b705de..233a5cb 100644
--- a/package.json
+++ b/package.json
@@ -5,6 +5,7 @@
"lib": "src"
},
"dependencies": {
+ "casperlamboo/EventDispatcher": "github:casperlamboo/EventDispatcher@master",
"clipper-lib": "npm:clipper-lib@^1.0.0",
"nodeca/js-yaml": "github:nodeca/js-yaml@^3.3.1",
"read-yaml": "npm:read-yaml@^1.0.0",
diff --git a/src/slicer.js b/src/slicer.js
index b28d854..ceae634 100644
--- a/src/slicer.js
+++ b/src/slicer.js
@@ -1,4 +1,5 @@
import THREE from 'three.js';
+import EventDispatcher from 'casperlamboo/EventDispatcher';
import calculateLayersIntersections from './sliceActions/calculateLayersIntersections.js';
import createLines from './sliceActions/createLines.js';
import generateInfills from './sliceActions/generateInfills.js';
@@ -9,8 +10,10 @@ import optimizePaths from './sliceActions/optimizePaths.js';
import shapesToSlices from './sliceActions/shapesToSlices.js';
import slicesToGCode from './sliceActions/slicesToGCode.js';
-export default class {
+export default class extends EventDispatcher {
constructor () {
+ super();
+
this.progress = {
createdLines: false,
calculatedLayerIntersections: false,
@@ -98,34 +101,36 @@ export default class {
this.progress.generatedGCode = true;
this._updateProgress(settings);
- if (this.onfinish !== undefined) {
- this.onfinish(gcode);
- }
+ this.dispatchEvent({
+ type: 'finish',
+ gcode
+ });
return gcode;
}
_updateProgress (settings) {
- if (this.onprogress !== undefined) {
- var supportEnabled = settings.config["supportEnabled"];
+ var supportEnabled = settings.config["supportEnabled"];
- var progress = {};
+ var progress = {};
- var procent = 0;
- var length = 0;
- for (var i in this.progress) {
- if (!(!supportEnabled && i === "generatedSupport")) {
- progress[i] = this.progress[i];
- if (progress[i]) {
- procent += 1;
- }
- length += 1;
+ var procent = 0;
+ var length = 0;
+ for (var i in this.progress) {
+ if (!(!supportEnabled && i === "generatedSupport")) {
+ progress[i] = this.progress[i];
+ if (progress[i]) {
+ procent += 1;
}
+ length += 1;
}
-
- progress.procent = procent / length;
-
- this.onprogress(progress);
}
+
+ progress.procent = procent / length;
+
+ this.dispatchEvent({
+ type: 'progress',
+ progress
+ });
}
}
diff --git a/src/slicerworker.js b/src/slicerworker.js
index 0ca3f9e..fd0e531 100644
--- a/src/slicerworker.js
+++ b/src/slicerworker.js
@@ -1,30 +1,35 @@
import THREE from 'three.js';
import Settings from './settings.js';
+import EventDispatcher from 'casperlamboo/EventDispatcher';
-export default class {
+export default class extends EventDispatcher {
constructor () {
+ super();
+
this.worker = new Worker('./worker.js');
this.worker.addEventListener('message', (event) => {
switch (event.data['cmd']) {
case 'PROGRESS':
- if (this.onprogress !== undefined) {
- var progress = event.data['progress'];
+ var progress = event.data['progress'];
- this.onprogress(progress);
- }
+ this.dispatchEvent({
+ type: 'progress',
+ progress
+ });
break;
case 'GCODE':
- if (this.onfinish !== undefined) {
- var reader = new FileReader();
- reader.addEventListener("loadend", () => {
- var gcode = reader.result;
- this.onfinish(gcode);
+ var reader = new FileReader();
+ reader.addEventListener("loadend", () => {
+ var gcode = reader.result;
+ this.dispatchEvent({
+ type: 'finish',
+ gcode
});
- reader.readAsBinaryString(event.data['gcode']);
- }
+ });
+ reader.readAsBinaryString(event.data['gcode']);
break;
}
}, false);
@@ -68,11 +73,11 @@ export default class {
delete geometry.boundingSphere;
this.worker.postMessage({
- 'cmd': 'SET_MESH',
+ 'cmd': 'SET_MESH',
'geometry': {
- 'attributes': geometry.attributes,
+ 'attributes': geometry.attributes,
'attributesKeys': geometry.attributesKeys
- },
+ },
'matrix': matrix.toArray()
}, buffers);
@@ -81,7 +86,7 @@ export default class {
slice (settings) {
this.worker.postMessage({
- 'cmd': 'SLICE',
+ 'cmd': 'SLICE',
'settings': settings.config
});
@@ -95,4 +100,4 @@ export default class {
return this;
}
-}
\ No newline at end of file
+}