2
0
mirror of https://github.com/esiur/esiur-js.git synced 2025-05-06 04:22: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';
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;
}

View File

@ -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);
};

View File

@ -1,6 +1,6 @@
{
"name": "esiur",
"version": "1.2.4",
"version": "1.2.7",
"description": "Distributed Object Framework",
"main": "esiur.js",
"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"})
.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);
});
});
*/