2
0
mirror of https://github.com/esiur/esiur-js.git synced 2025-05-07 12:52:58 +00:00

AsyncReply is Promise

This commit is contained in:
Ahmed Zamil 2019-07-21 05:26:43 +03:00
parent 3ac2eccf0f
commit 399a096c58
4 changed files with 30 additions and 42 deletions

View File

@ -28,23 +28,15 @@
import AsyncException from './AsyncException.js'; import AsyncException from './AsyncException.js';
export default class AsyncReply export default class AsyncReply extends Promise
{ {
then(callback) then(callback)
{ {
this.callbacks.push(callback); this.callbacks.push(callback);
if (this.ready) if (this.ready)
{
callback(this.result, this); callback(this.result, this);
if (!this.taskExpired)
{
this.taskExpired = true;
this.resolveTask(this.result);
}
}
return this; return this;
} }
@ -66,13 +58,7 @@ export default class AsyncReply
if (this.exception.raised) if (this.exception.raised)
{ {
callback(this.exception); callback(this.exception);
if (!this.taskExpired)
{
this.taskExpired = true;
this.rejectTask(this.exception);
}
} }
return this; return this;
@ -102,14 +88,7 @@ export default class AsyncReply
this.ready = true; this.ready = true;
for(var i = 0; i < this.callbacks.length; i++) for(var i = 0; i < this.callbacks.length; i++)
this.callbacks[i](result, this); this.callbacks[i](result, this);
if (!this.taskExpired)
{
this.taskExpired = true;
this.resolveTask(this.result);
}
} }
@ -118,15 +97,13 @@ export default class AsyncReply
if (this.ready) if (this.ready)
return; return;
this.taskExpired = true;
if (type instanceof AsyncException) if (type instanceof AsyncException)
this.exception.raise(type.type, type.code, type.message); this.exception.raise(type.type, type.code, type.message);
else else
this.exception.raise(type, code, message); this.exception.raise(type, code, message);
if (this.errorCallbacks.length == 0) if (this.errorCallbacks.length == 0)
this.rejectTask(this.exception); throw this.exception;
else else
for(var i = 0; i < this.errorCallbacks.length; i++) for(var i = 0; i < this.errorCallbacks.length; i++)
this.errorCallbacks[i](this.exception, this); this.errorCallbacks[i](this.exception, this);
@ -152,6 +129,16 @@ export default class AsyncReply
constructor(result) constructor(result)
{ {
if (result instanceof Function)
{
super(result);
this.awaiter = result;
}
else
super(()=>{});
this.callbacks = []; this.callbacks = [];
this.errorCallbacks = []; this.errorCallbacks = [];
this.progressCallbacks = []; this.progressCallbacks = [];
@ -159,22 +146,13 @@ export default class AsyncReply
this.exception = new AsyncException();// null; this.exception = new AsyncException();// null;
var self = this; var self = this;
this.task = new Promise(function(resolve, reject){ if (result !== undefined && !(result instanceof Function)) {
self.resolveTask = resolve;
self.rejectTask = reject;
});
if (result !== undefined) {
this.result = result; this.result = result;
this.ready = true; this.ready = true;
this.taskExpired = true;
this.resolveTask(result);
} }
else else
{ {
this.taskExpired = false;
this.ready = false; this.ready = false;
this.result = null; this.result = null;
} }

View File

@ -201,8 +201,8 @@ export default class DistributedConnection extends IStore {
this.socket.onclose = function(event) this.socket.onclose = function(event)
{ {
if (this.openReply) if (this.connection.openReply)
this.openReply.triggerError(); this.connection.openReply.triggerError(0, 0, "Host not reachable");
self.close(event); self.close(event);
}; };

View File

@ -1,6 +1,6 @@
{ {
"name": "esiur", "name": "esiur",
"version": "1.2.4", "version": "1.2.7",
"description": "Distributed Object Framework", "description": "Distributed Object Framework",
"main": "esiur.js", "main": "esiur.js",
"scripts": { "scripts": {

View File

@ -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"}) wh.get("iip://localhost:5001/db/my", {username: "demo", password: "1234"})
.then(x=>{ .then(x=>{
console.log("connected", x); console.log("connected", x);
@ -15,4 +24,5 @@ wh.get("iip://localhost:5001/db/my", {username: "demo", password: "1234"})
window.x = x; window.x = x;
}).catch(x=>{ }).catch(x=>{
console.log("error", x); console.log("error", x);
}); });
*/