0
0
mirror of https://github.com/Doodle3D/Doodle3D-API synced 2024-11-14 16:27:56 +01:00

remove check alive logic from doodle3dmanager

This commit is contained in:
casperlamboo 2016-04-21 15:02:36 +02:00
parent 9f1f2bb5d8
commit 1de4664071

View File

@ -25,61 +25,47 @@ export default class Doodle3DManager extends EventDispatcher {
setAutoUpdate (autoUpdate = true, updateInterval = 1000) { setAutoUpdate (autoUpdate = true, updateInterval = 1000) {
this.updateInterval = updateInterval; this.updateInterval = updateInterval;
if (this.autoUpdate === autoUpdate) return;
if (this.autoUpdate === autoUpdate) {
return;
}
this.autoUpdate = autoUpdate; this.autoUpdate = autoUpdate;
if (autoUpdate) this._update();
if (autoUpdate) {
this._update();
}
return this; return this;
} }
async _update () { async _update () {
while (this.autoUpdate) { while (this.autoUpdate) {
await this._checkAlive();
await this._checkNew(); await this._checkNew();
await sleep(this.updateInterval); await sleep(this.updateInterval);
} }
} }
async _checkAlive () {
for (const box of boxes) {
const alive = await box.checkAlive();
if (!alive) {
this._removeBox(box);
}
}
}
async _checkNew () { async _checkNew () {
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');
return;
} }
if (this.checkNonServerBoxes) { if (this.checkNonServerBoxes) boxes = boxes.concat(this.nonServerBoxes);
boxes = [...boxes, ...this.nonServerBoxes];
}
const knownIPs = this.boxes.map((box) => box.boxData.localip); const knownIPsClient = this.boxes.map(box => box.boxData.localip);
const boxes = boxes.filter(({ localip }) => knownIPs.indexOf(localip) === -1); const knownIPsServer = boxes.map(box => box.localip);
for (const boxData of boxes) { const newBoxes = boxes.filter(box => knownIPsClient.indexOf(box.localip) === -1);
const removedBoxes = this.boxes.filter(box => knownIPsServer.indexOf(box.boxData.localip) === -1);
for (const boxData of newBoxes) {
const box = new Doodle3DAPI(boxData); const box = new Doodle3DAPI(boxData);
const alive = await box.checkAlive();
if (alive) {
this._addBox(box); this._addBox(box);
} }
for (const box of removedBoxes) {
this._removeBox(box);
} }
} }