@dwmt/comlink
Advanced tools
Comparing version 1.0.5 to 1.0.6
@@ -199,2 +199,11 @@ import Cookies from 'js-cookie'; | ||
answers: {}, | ||
listeners: {}, | ||
terminate: function terminate() { | ||
self._channels[options.name].connection.close(); | ||
self._channels[options.name].connection = null; | ||
self._channels[options.name].answers = {}; | ||
self._channels[options.name].listeners = {}; | ||
console.log(self._channels[options.name]); | ||
}, | ||
connect: function connect() { | ||
@@ -232,3 +241,2 @@ return new Promise(function (resolve, reject) { | ||
var tr = JSON.parse(msg.data); | ||
console.log("Socket message on channel ".concat(opts.name), tr); | ||
@@ -239,3 +247,3 @@ if (options.rpc && options.rpc.headerHandler) { | ||
if (tr.id) { | ||
if ((tr._type === 'rpcResponse' || tr._type === 'rpcError') && tr.id) { | ||
self._channels[options.name].answers[tr.id] = tr.result || { | ||
@@ -245,2 +253,33 @@ error: tr.error | ||
} | ||
if (tr._type === 'event') { | ||
console.log('Server event...', tr); | ||
var eventSubscribers = self._channels[options.name].listeners[tr.event]; | ||
if (eventSubscribers.length) { | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
try { | ||
for (var _iterator = eventSubscribers[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var subscriber = _step.value; | ||
subscriber(tr.message); | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator["return"] != null) { | ||
_iterator["return"](); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} catch (err) { | ||
@@ -288,2 +327,3 @@ console.error(err); | ||
channel.connect = this._channels[channelName].connect; | ||
channel.terminate = this._channels[channelName].terminate; | ||
} | ||
@@ -568,10 +608,28 @@ | ||
}, { | ||
key: "subscribe", | ||
value: function subscribe() { | ||
throw new Error('Not implemented yet!'); | ||
key: "subscribeToEvent", | ||
value: function subscribeToEvent(event, callback) { | ||
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var channelName = options.channel || this._deafultRPCChannel; | ||
var channel = this._channels[channelName]; | ||
if (!channel.listeners[event]) { | ||
channel.listeners[event] = []; | ||
} | ||
channel.listeners[event].push(callback); | ||
} | ||
}, { | ||
key: "unsubscribe", | ||
value: function unsubscribe() { | ||
throw new Error('Not implemented yet!'); | ||
key: "unsubscribeFromEvent", | ||
value: function unsubscribeFromEvent(event, callback) { | ||
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var channelName = options.channel || this._deafultRPCChannel; | ||
var channel = this._channels[channelName]; | ||
if (!channel.listeners[event]) { | ||
return; | ||
} | ||
channel.listeners[event] = channel.listeners[event].filter(function (fn) { | ||
return fn !== callback; | ||
}); | ||
} | ||
@@ -723,2 +781,19 @@ }, { | ||
var _require = require('events'), | ||
EventEmitter = _require.EventEmitter; | ||
function _async(f) { | ||
return function () { | ||
for (var args = [], i = 0; i < arguments.length; i++) { | ||
args[i] = arguments[i]; | ||
} | ||
try { | ||
return Promise.resolve(f.apply(this, args)); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}; | ||
} | ||
var Server = | ||
@@ -734,2 +809,3 @@ /*#__PURE__*/ | ||
this.session = {}; | ||
this.eventEmitter = new EventEmitter(); | ||
} | ||
@@ -756,2 +832,37 @@ | ||
}, { | ||
key: "sendMessageToClient", | ||
value: function sendMessageToClient(clientID, event, message) { | ||
if (!this.session[clientID]) { | ||
throw new Error('No user with given clientID'); | ||
} | ||
this.eventEmitter.emit("sendEventTo:".concat(clientID), { | ||
_type: 'event', | ||
event: event, | ||
message: message | ||
}); | ||
} | ||
}, { | ||
key: "getCLientIDByToken", | ||
value: function getCLientIDByToken(token) { | ||
var sessions = Object.keys(this.session); | ||
var clientID = false; | ||
for (var _i = 0, _sessions = sessions; _i < _sessions.length; _i++) { | ||
var id = _sessions[_i]; | ||
if (this.session[id].token === token) { | ||
clientID = id; | ||
break; | ||
} | ||
} | ||
if (!clientID) { | ||
throw new Error('No user with given token'); | ||
} | ||
console.log('ClientID: ', clientID); | ||
return clientID; | ||
} | ||
}, { | ||
key: "applyChannel", | ||
@@ -788,2 +899,6 @@ value: function applyChannel(channelName, wss) { | ||
_this.eventEmitter.on("sendEventTo:".concat(clientID), function (msg) { | ||
ws.send(JSON.stringify(msg)); | ||
}); | ||
ws.on('message', _async(function (message) { | ||
@@ -805,2 +920,3 @@ var _exit = false; | ||
ws.send(JSON.stringify({ | ||
_type: 'rpcError', | ||
id: id, | ||
@@ -821,2 +937,3 @@ error: "The used dialect ".concat(dialect, " is not supported on this channel") | ||
return ws.send(JSON.stringify({ | ||
_type: 'rpcError', | ||
id: id, | ||
@@ -834,2 +951,3 @@ error: err.message, | ||
ws.send(JSON.stringify({ | ||
_type: 'rpcResponse', | ||
id: id, | ||
@@ -844,2 +962,3 @@ result: returnValue, | ||
ws.send(JSON.stringify({ | ||
_type: 'rpcError', | ||
id: id, | ||
@@ -862,16 +981,2 @@ error: err.message, | ||
function _async(f) { | ||
return function () { | ||
for (var args = [], i = 0; i < arguments.length; i++) { | ||
args[i] = arguments[i]; | ||
} | ||
try { | ||
return Promise.resolve(f.apply(this, args)); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}; | ||
} | ||
function _catch$1(body, recover) { | ||
@@ -878,0 +983,0 @@ try { |
@@ -203,2 +203,11 @@ 'use strict'; | ||
answers: {}, | ||
listeners: {}, | ||
terminate: function terminate() { | ||
self._channels[options.name].connection.close(); | ||
self._channels[options.name].connection = null; | ||
self._channels[options.name].answers = {}; | ||
self._channels[options.name].listeners = {}; | ||
console.log(self._channels[options.name]); | ||
}, | ||
connect: function connect() { | ||
@@ -236,3 +245,2 @@ return new Promise(function (resolve, reject) { | ||
var tr = JSON.parse(msg.data); | ||
console.log("Socket message on channel ".concat(opts.name), tr); | ||
@@ -243,3 +251,3 @@ if (options.rpc && options.rpc.headerHandler) { | ||
if (tr.id) { | ||
if ((tr._type === 'rpcResponse' || tr._type === 'rpcError') && tr.id) { | ||
self._channels[options.name].answers[tr.id] = tr.result || { | ||
@@ -249,2 +257,33 @@ error: tr.error | ||
} | ||
if (tr._type === 'event') { | ||
console.log('Server event...', tr); | ||
var eventSubscribers = self._channels[options.name].listeners[tr.event]; | ||
if (eventSubscribers.length) { | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
try { | ||
for (var _iterator = eventSubscribers[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var subscriber = _step.value; | ||
subscriber(tr.message); | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator["return"] != null) { | ||
_iterator["return"](); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} catch (err) { | ||
@@ -292,2 +331,3 @@ console.error(err); | ||
channel.connect = this._channels[channelName].connect; | ||
channel.terminate = this._channels[channelName].terminate; | ||
} | ||
@@ -572,10 +612,28 @@ | ||
}, { | ||
key: "subscribe", | ||
value: function subscribe() { | ||
throw new Error('Not implemented yet!'); | ||
key: "subscribeToEvent", | ||
value: function subscribeToEvent(event, callback) { | ||
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var channelName = options.channel || this._deafultRPCChannel; | ||
var channel = this._channels[channelName]; | ||
if (!channel.listeners[event]) { | ||
channel.listeners[event] = []; | ||
} | ||
channel.listeners[event].push(callback); | ||
} | ||
}, { | ||
key: "unsubscribe", | ||
value: function unsubscribe() { | ||
throw new Error('Not implemented yet!'); | ||
key: "unsubscribeFromEvent", | ||
value: function unsubscribeFromEvent(event, callback) { | ||
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var channelName = options.channel || this._deafultRPCChannel; | ||
var channel = this._channels[channelName]; | ||
if (!channel.listeners[event]) { | ||
return; | ||
} | ||
channel.listeners[event] = channel.listeners[event].filter(function (fn) { | ||
return fn !== callback; | ||
}); | ||
} | ||
@@ -727,2 +785,19 @@ }, { | ||
var _require = require('events'), | ||
EventEmitter = _require.EventEmitter; | ||
function _async(f) { | ||
return function () { | ||
for (var args = [], i = 0; i < arguments.length; i++) { | ||
args[i] = arguments[i]; | ||
} | ||
try { | ||
return Promise.resolve(f.apply(this, args)); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}; | ||
} | ||
var Server = | ||
@@ -738,2 +813,3 @@ /*#__PURE__*/ | ||
this.session = {}; | ||
this.eventEmitter = new EventEmitter(); | ||
} | ||
@@ -760,2 +836,37 @@ | ||
}, { | ||
key: "sendMessageToClient", | ||
value: function sendMessageToClient(clientID, event, message) { | ||
if (!this.session[clientID]) { | ||
throw new Error('No user with given clientID'); | ||
} | ||
this.eventEmitter.emit("sendEventTo:".concat(clientID), { | ||
_type: 'event', | ||
event: event, | ||
message: message | ||
}); | ||
} | ||
}, { | ||
key: "getCLientIDByToken", | ||
value: function getCLientIDByToken(token) { | ||
var sessions = Object.keys(this.session); | ||
var clientID = false; | ||
for (var _i = 0, _sessions = sessions; _i < _sessions.length; _i++) { | ||
var id = _sessions[_i]; | ||
if (this.session[id].token === token) { | ||
clientID = id; | ||
break; | ||
} | ||
} | ||
if (!clientID) { | ||
throw new Error('No user with given token'); | ||
} | ||
console.log('ClientID: ', clientID); | ||
return clientID; | ||
} | ||
}, { | ||
key: "applyChannel", | ||
@@ -792,2 +903,6 @@ value: function applyChannel(channelName, wss) { | ||
_this.eventEmitter.on("sendEventTo:".concat(clientID), function (msg) { | ||
ws.send(JSON.stringify(msg)); | ||
}); | ||
ws.on('message', _async(function (message) { | ||
@@ -809,2 +924,3 @@ var _exit = false; | ||
ws.send(JSON.stringify({ | ||
_type: 'rpcError', | ||
id: id, | ||
@@ -825,2 +941,3 @@ error: "The used dialect ".concat(dialect, " is not supported on this channel") | ||
return ws.send(JSON.stringify({ | ||
_type: 'rpcError', | ||
id: id, | ||
@@ -838,2 +955,3 @@ error: err.message, | ||
ws.send(JSON.stringify({ | ||
_type: 'rpcResponse', | ||
id: id, | ||
@@ -848,2 +966,3 @@ result: returnValue, | ||
ws.send(JSON.stringify({ | ||
_type: 'rpcError', | ||
id: id, | ||
@@ -866,16 +985,2 @@ error: err.message, | ||
function _async(f) { | ||
return function () { | ||
for (var args = [], i = 0; i < arguments.length; i++) { | ||
args[i] = arguments[i]; | ||
} | ||
try { | ||
return Promise.resolve(f.apply(this, args)); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}; | ||
} | ||
function _catch$1(body, recover) { | ||
@@ -882,0 +987,0 @@ try { |
{ | ||
"name": "@dwmt/comlink", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "Communication library for lazy enthusiasts", | ||
@@ -5,0 +5,0 @@ "main": "dist/Comlink.js", |
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
65427
1888