use async and promises properly

This commit is contained in:
casperlamboo 2016-04-16 18:07:11 +02:00
parent 7ed4183bb8
commit 1960d384d6
2 changed files with 70 additions and 111 deletions

View File

@ -48,37 +48,25 @@ export default class extends EventDispatcher {
return this; return this;
} }
checkAlive () { async checkAlive () {
return new Promise(async (resolve, reject) => { const alive = await this.network.alive();
let alive;
try {
await this.network.alive();
alive = true;
}
catch (error) {
alive = false;
}
if (alive !== this.alive) { if (alive !== this.alive) {
this.dispatchEvent({ const type = alive ? 'connect' : 'disconnect';
type: alive ? 'connect' : 'disconnect' this.dispatchEvent({ type });
});
} }
this.alive = alive; this.alive = alive;
resolve(alive); return alive;
});
} }
sendGCode (gcode) { async sendGCode (gcode) {
return new Promise(async (resolve, reject) => { const printerState = await this.printer.state();
let printerState = await this.printer.state();
if (printerState.state !== 'idle') { if (printerState.state !== 'idle') {
reject(`Cannot print, print state is ${printerState.state}`); throw `Cannot print, print state is ${printerState.state}`;
return;
} }
let autoUpdateState = this.autoUpdate; const autoUpdateState = this.autoUpdate;
this.autoUpdate = false; this.autoUpdate = false;
if (!gcode.endsWith('\n')) { if (!gcode.endsWith('\n')) {
@ -90,11 +78,10 @@ export default class extends EventDispatcher {
let lastIndex = 0; let lastIndex = 0;
let start = true; let start = true;
while (lastIndex !== gcode.length) { while (lastIndex !== gcode.length) {
let lastIndex = lastIndex + this.maxBatchSize; const index = gcode.lastIndexOf('\n', lastIndex + this.maxBatchSize);
let index = gcode.lastIndexOf('\n', lastIndex); const batch = gcode.substring(lastIndex, index);
let batch = gcode.substring(lastIndex, index);
let progress = await this.printer.progress(); // const progress = await this.printer.progress();
await this._sendBatch(batch, start); await this._sendBatch(batch, start);
@ -103,9 +90,6 @@ export default class extends EventDispatcher {
} }
this.autoUpdate = autoUpdateState; this.autoUpdate = autoUpdateState;
resolve();
});
} }
async _update () { async _update () {
@ -113,12 +97,8 @@ export default class extends EventDispatcher {
try { try {
this.state = await this.info.status(); this.state = await this.info.status();
this.dispatchEvent({ this.dispatchEvent({ type: 'update', state: this.state });
type: 'update', } catch(error) {
state: this.state
});
}
catch (error) {
await this.checkAlive(); await this.checkAlive();
} }
@ -126,21 +106,16 @@ export default class extends EventDispatcher {
} }
} }
_sendBatch (gcode, start) { async _sendBatch (gcode, start) {
return new Promise (async (resolve, reject) => {
try { try {
let response = await this.printer.print(gcode, start, start); const response = await this.printer.print(gcode, start, start);
// maybe do something with failing response // maybe do something with failing response
console.log(`batch sent: ${index}`, printRequest); console.log(`batch sent: ${index}`, printRequest);
} } catch(error) {
catch (error) {
await sleep(1000); await sleep(1000);
await this._sendBatch(gcode, index); await this._sendBatch(gcode, index);
} }
resolve();
});
} }
} }

View File

@ -48,57 +48,41 @@ export default class Doodle3DManager extends EventDispatcher {
} }
} }
_checkAlive () { async _checkAlive () {
return new Promise(async (resolve, reject) => { for (const box of boxes) {
const alive = await box.checkAlive();
await Promise.all(this.boxes.map((box) => {
let promise = box.checkAlive();
promise.then((alive) => {
if (!alive) { if (!alive) {
this._removeBox(box); this._removeBox(box);
} }
}) }
return promise;
}));
resolve();
});
} }
_checkNew () { async _checkNew () {
return new Promise(async (resolve, reject) => {
let boxes; let boxes;
try { try {
boxes = await rest.get(`${this.api}list.php`); boxes = await rest.get(`${this.api}list.php`);
} } catch(error) {
catch (error) { throw 'fail connecting to Doodle3D server';
console.warn('fail connecting to Doodle3D server', error);
return;
} }
if (this.checkNonServerBoxes) { if (this.checkNonServerBoxes) {
boxes = [...boxes, ...this.nonServerBoxes]; boxes = [...boxes, ...this.nonServerBoxes];
} }
let knownIPs = this.boxes.map((box) => box.boxData.localip); const knownIPs = this.boxes.map((box) => box.boxData.localip);
const boxes = boxes.filter(({ localip }) => knownIPs.indexOf(localip) === -1);
for (let boxData of boxes) { for (const boxData of boxes) {
if (knownIPs.indexOf(boxData.localip) === -1) { const box = new Doodle3DAPI(boxData);
let box = new Doodle3DAPI(boxData);
let alive = await box.checkAlive();
const alive = await box.checkAlive();
if (alive) { if (alive) {
this._addBox(box); this._addBox(box);
} }
} }
} }
resolve();
});
}
_addBox (box) { _addBox (box) {
this.boxes.push(box); this.boxes.push(box);