Comparing version 1.0.2 to 1.0.3
@@ -16,2 +16,3 @@ "use strict"; | ||
this.instances = {}; | ||
this.queue = []; | ||
} | ||
@@ -93,14 +94,19 @@ register(target) { | ||
let resolved = false; | ||
let handler = () => { | ||
!resolved && (resolved = true) && resolve(); | ||
while (this.queue.length) { | ||
let msg = this.queue.shift(); | ||
this.clientSend(...msg); | ||
} | ||
}; | ||
let connect = () => { | ||
if (this.path) { | ||
this.client = net.createConnection(this.path); | ||
this.client = net.createConnection(this.path, handler); | ||
} | ||
else { | ||
this.client = net.createConnection(this.port, this.host); | ||
this.client = net.createConnection(this.port, this.host, handler); | ||
} | ||
}; | ||
connect(); | ||
this.client.once("connect", () => { | ||
(resolved = true) && resolve(); | ||
}).once("error", err => { | ||
this.client.once("error", err => { | ||
!resolved && (resolved = true) && reject(err); | ||
@@ -150,3 +156,3 @@ }).on("error", err => { | ||
this.instances[oid] = srv; | ||
this.client.write(util_1.send(util_1.RPCEvents.CONNECT, oid, target.name, clsId, ...args)); | ||
this.clientSend(util_1.RPCEvents.CONNECT, oid, target.name, clsId, ...args); | ||
srv[util_1.eventEmitter].once(util_1.RPCEvents[1], () => { | ||
@@ -173,5 +179,3 @@ resolve(util_1.proxify(srv, _oid, this)); | ||
delete this.instances[oid]; | ||
this.client.write(util_1.send(util_1.RPCEvents.DISCONNECT, oid), () => { | ||
resolve(); | ||
}); | ||
this.clientSend(util_1.RPCEvents.DISCONNECT, oid, () => resolve()); | ||
}); | ||
@@ -182,2 +186,17 @@ } | ||
} | ||
clientSend(...msg) { | ||
if (this.client && !this.client.destroyed) { | ||
let cb; | ||
if (typeof msg[msg.length - 1] == "function") { | ||
cb = msg.pop(); | ||
} | ||
else { | ||
cb = () => { }; | ||
} | ||
this.client.write(util_1.send.apply(void 0, msg), cb); | ||
} | ||
else { | ||
this.queue.push(msg); | ||
} | ||
} | ||
} | ||
@@ -184,0 +203,0 @@ exports.ServiceInstance = ServiceInstance; |
{ | ||
"name": "asrpc", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "A tool to make your class as an RPC service.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -45,2 +45,3 @@ import * as net from "net"; | ||
} = {}; | ||
private queue: any[][] = []; | ||
@@ -165,7 +166,14 @@ /** | ||
let resolved = false; | ||
let handler = () => { | ||
!resolved && (resolved = true) && resolve(); | ||
while (this.queue.length) { | ||
let msg = this.queue.shift(); | ||
this.clientSend(...msg); | ||
} | ||
}; | ||
let connect = () => { | ||
if (this.path) { | ||
this.client = net.createConnection(this.path); | ||
this.client = net.createConnection(this.path, handler); | ||
} else { | ||
this.client = net.createConnection(this.port, this.host); | ||
this.client = net.createConnection(this.port, this.host, handler); | ||
} | ||
@@ -176,5 +184,3 @@ }; | ||
this.client.once("connect", () => { | ||
(resolved = true) && resolve(); | ||
}).once("error", err => { | ||
this.client.once("error", err => { | ||
!resolved && (resolved = true) && reject(err); | ||
@@ -224,5 +230,3 @@ }).on("error", err => { | ||
this.instances[oid] = srv; | ||
this.client.write( | ||
send(RPCEvents.CONNECT, oid, target.name, clsId, ...args) | ||
); | ||
this.clientSend(RPCEvents.CONNECT, oid, target.name, clsId, ...args); | ||
@@ -257,5 +261,3 @@ srv[eventEmitter].once(RPCEvents[1], () => { | ||
delete this.instances[oid]; | ||
this.client.write(send(RPCEvents.DISCONNECT, oid), () => { | ||
resolve(); | ||
}); | ||
this.clientSend(RPCEvents.DISCONNECT, oid, () => resolve()); | ||
}); | ||
@@ -271,2 +273,16 @@ } | ||
} | ||
private clientSend(...msg) { | ||
if (this.client && !this.client.destroyed) { | ||
let cb: Function; | ||
if (typeof msg[msg.length - 1] == "function") { | ||
cb = msg.pop(); | ||
} else { | ||
cb = () => { }; | ||
} | ||
this.client.write(send.apply(void 0, msg), cb); | ||
} else { | ||
this.queue.push(msg); | ||
} | ||
} | ||
} | ||
@@ -273,0 +289,0 @@ |
Sorry, the diff of this file is not supported yet
55756
3
849