@creamery/socket.io-rpc
Advanced tools
Comparing version 1.0.8 to 1.0.9
@@ -10,4 +10,6 @@ /// <reference types="socket.io-client" /> | ||
create<T extends object>(id: string): T; | ||
close(): void; | ||
private createCall; | ||
private createNotify; | ||
private send; | ||
} |
@@ -17,7 +17,12 @@ "use strict"; | ||
this.client.on('notify', (seq, message) => { | ||
const cb = this.handlers.get(seq); | ||
if (typeof cb === 'function') { | ||
cb(...message.params); | ||
const h = this.handlers.get(seq); | ||
if (h && typeof h.callback === 'function') { | ||
h.callback.apply(null, message.params); | ||
} | ||
}); | ||
this.client.on('reconnect', () => { | ||
this.handlers.forEach((h) => { | ||
this.send(h.seq); | ||
}); | ||
}); | ||
} | ||
@@ -41,2 +46,5 @@ create(id) { | ||
} | ||
close() { | ||
this.client.close(); | ||
} | ||
createCall(id, name, params) { | ||
@@ -64,4 +72,3 @@ return new Promise((resolve, reject) => { | ||
const seq = this.seq++; | ||
this.handlers.set(seq, cb); | ||
this.client.emit('listen', { | ||
const packet = { | ||
id, | ||
@@ -71,6 +78,22 @@ seq, | ||
params, | ||
}; | ||
this.handlers.set(seq, { | ||
...packet, | ||
callback: cb, | ||
}); | ||
this.send(seq); | ||
} | ||
send(seq) { | ||
const h = this.handlers.get(seq); | ||
if (h) { | ||
this.client.emit('listen', { | ||
id: h.id, | ||
seq: h.seq, | ||
channel: h.channel, | ||
params: h.params, | ||
}); | ||
} | ||
} | ||
} | ||
exports.SocketIORPCClient = SocketIORPCClient; | ||
//# sourceMappingURL=rpc-client.js.map |
@@ -7,2 +7,16 @@ "use strict"; | ||
const socket_io_1 = __importDefault(require("socket.io")); | ||
function createCallback({ seq, id, method }, client) { | ||
function cb(...result) { | ||
if (client.connected) { | ||
client.emit('notify', seq, { | ||
id, | ||
method, | ||
params: result, | ||
}); | ||
} | ||
else { | ||
} | ||
} | ||
return cb; | ||
} | ||
function NotFoundResponse(id, method) { | ||
@@ -51,6 +65,6 @@ return { | ||
client.on('listen', (message) => { | ||
this.notify(message, client.emit.bind(client)); | ||
this.notify(message, client); | ||
}); | ||
} | ||
notify(msg, emit) { | ||
notify(msg, client) { | ||
const { id, channel, params, seq } = msg; | ||
@@ -63,12 +77,5 @@ const vendor = this.pool.get(id); | ||
const method = `on${channel}`; | ||
function callback(...result) { | ||
emit('notify', seq, { | ||
id, | ||
method, | ||
params: result, | ||
}); | ||
} | ||
if (typeof vendor[method] === 'function') { | ||
try { | ||
params.push(callback); | ||
params.push(createCallback({ seq, id, method }, client)); | ||
vendor[method](...params); | ||
@@ -75,0 +82,0 @@ } |
{ | ||
"name": "@creamery/socket.io-rpc", | ||
"version": "1.0.8", | ||
"version": "1.0.9", | ||
"description": "RPC for SocketIO", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17618
308