diff --git a/src/Engine/AsyncReply.js b/src/Engine/AsyncReply.js index 7cf5cd6..a9441a9 100644 --- a/src/Engine/AsyncReply.js +++ b/src/Engine/AsyncReply.js @@ -28,23 +28,15 @@ import AsyncException from './AsyncException.js'; -export default class AsyncReply +export default class AsyncReply extends Promise { then(callback) { this.callbacks.push(callback); if (this.ready) - { callback(this.result, this); - if (!this.taskExpired) - { - this.taskExpired = true; - this.resolveTask(this.result); - } - } - return this; } @@ -66,13 +58,7 @@ export default class AsyncReply if (this.exception.raised) { - callback(this.exception); - - if (!this.taskExpired) - { - this.taskExpired = true; - this.rejectTask(this.exception); - } + callback(this.exception); } return this; @@ -102,14 +88,7 @@ export default class AsyncReply this.ready = true; for(var i = 0; i < this.callbacks.length; i++) - this.callbacks[i](result, this); - - - if (!this.taskExpired) - { - this.taskExpired = true; - this.resolveTask(this.result); - } + this.callbacks[i](result, this); } @@ -118,15 +97,13 @@ export default class AsyncReply if (this.ready) return; - this.taskExpired = true; - if (type instanceof AsyncException) this.exception.raise(type.type, type.code, type.message); else this.exception.raise(type, code, message); if (this.errorCallbacks.length == 0) - this.rejectTask(this.exception); + throw this.exception; else for(var i = 0; i < this.errorCallbacks.length; i++) this.errorCallbacks[i](this.exception, this); @@ -152,6 +129,16 @@ export default class AsyncReply constructor(result) { + + if (result instanceof Function) + { + super(result); + this.awaiter = result; + } + else + super(()=>{}); + + this.callbacks = []; this.errorCallbacks = []; this.progressCallbacks = []; @@ -159,22 +146,13 @@ export default class AsyncReply this.exception = new AsyncException();// null; var self = this; - - this.task = new Promise(function(resolve, reject){ - self.resolveTask = resolve; - self.rejectTask = reject; - }); - - - if (result !== undefined) { + + if (result !== undefined && !(result instanceof Function)) { this.result = result; this.ready = true; - this.taskExpired = true; - this.resolveTask(result); } else { - this.taskExpired = false; this.ready = false; this.result = null; } diff --git a/src/Net/IIP/DistributedConnection.js b/src/Net/IIP/DistributedConnection.js index 875cdf9..d05afe4 100644 --- a/src/Net/IIP/DistributedConnection.js +++ b/src/Net/IIP/DistributedConnection.js @@ -201,8 +201,8 @@ export default class DistributedConnection extends IStore { this.socket.onclose = function(event) { - if (this.openReply) - this.openReply.triggerError(); + if (this.connection.openReply) + this.connection.openReply.triggerError(0, 0, "Host not reachable"); self.close(event); }; diff --git a/src/package.json b/src/package.json index 5477c2d..98abefe 100644 --- a/src/package.json +++ b/src/package.json @@ -1,6 +1,6 @@ { "name": "esiur", - "version": "1.2.4", + "version": "1.2.7", "description": "Distributed Object Framework", "main": "esiur.js", "scripts": { diff --git a/test/test.js b/test/test.js index fa3490a..978ced4 100644 --- a/test/test.js +++ b/test/test.js @@ -8,6 +8,15 @@ class MyStore extends IStore } +async function load() +{ + window.x = await wh.get("iip://localhost:5001/db/my", {username: "demo", password: "1234"}); + console.log(window.x); +} + +load(); + +/* wh.get("iip://localhost:5001/db/my", {username: "demo", password: "1234"}) .then(x=>{ console.log("connected", x); @@ -15,4 +24,5 @@ wh.get("iip://localhost:5001/db/my", {username: "demo", password: "1234"}) window.x = x; }).catch(x=>{ console.log("error", x); - }); \ No newline at end of file + }); + */ \ No newline at end of file