UNPKG

eventric-remote-socketio-endpoint

Version:
135 lines (119 loc) 4.59 kB
var SocketIORemoteEndpoint, slice = [].slice; SocketIORemoteEndpoint = (function() { function SocketIORemoteEndpoint() { this._rpcRequestMiddlewareArray = []; } SocketIORemoteEndpoint.prototype.initialize = function(arg) { var logger, socketIoServer; socketIoServer = arg.socketIoServer, logger = arg.logger; if (!socketIoServer) { throw new Error('No socket io server instance passed'); } this._socketIoServer = socketIoServer; this._logger = logger; return this._addSocketIoEventBindings(); }; SocketIORemoteEndpoint.prototype.addRpcRequestMiddleware = function(rpcRequestMiddleware) { return this._rpcRequestMiddlewareArray.push(rpcRequestMiddleware); }; SocketIORemoteEndpoint.prototype._addSocketIoEventBindings = function() { return this._socketIoServer.sockets.on('connection', (function(_this) { return function(socket) { socket.on('eventric:rpcRequest', function(rpcRequest) { return _this._handleRpcRequestEvent(rpcRequest, socket); }); socket.on('eventric:joinRoom', function(roomName) { return _this._handleJoinRoomEvent(roomName, socket); }); return socket.on('eventric:leaveRoom', function(roomName) { return socket.leave(roomName); }); }; })(this)); }; SocketIORemoteEndpoint.prototype.setRPCHandler = function(handleRPCRequest) { return this._handleRPCRequest = handleRPCRequest; }; SocketIORemoteEndpoint.prototype._handleRpcRequestEvent = function(rpcRequest, socket) { var emitRpcResponse; emitRpcResponse = (function(_this) { return function(error, response) { var rpcId; if (error) { error = _this._convertErrorToSerializableObject(error); } rpcId = rpcRequest.rpcId; return socket.emit('eventric:rpcResponse', { rpcId: rpcId, error: error, data: response }); }; })(this); return this._executeRpcRequestMiddleware(rpcRequest, socket).then((function(_this) { return function() { return _this._handleRPCRequest(rpcRequest, emitRpcResponse); }; })(this))["catch"](function(error) { return emitRpcResponse(error, null); }); }; SocketIORemoteEndpoint.prototype._handleJoinRoomEvent = function(roomName, socket) { return this._executeRpcRequestMiddleware(roomName, socket).then(function() { return socket.join(roomName); })["catch"]((function(_this) { return function(error) { if (_this._logger) { return _this._logger.error(error, '\n', error.stack); } else { throw error; } }; })(this)); }; SocketIORemoteEndpoint.prototype._executeRpcRequestMiddleware = function(data, socket) { var rpcRequestMiddlewarePromise; rpcRequestMiddlewarePromise = Promise.resolve(); this._rpcRequestMiddlewareArray.forEach(function(rpcRequestMiddleware) { return rpcRequestMiddlewarePromise = rpcRequestMiddlewarePromise.then(function() { return rpcRequestMiddleware(data, socket); }); }); return rpcRequestMiddlewarePromise; }; SocketIORemoteEndpoint.prototype._convertErrorToSerializableObject = function(error) { var serializableErrorObject; serializableErrorObject = { name: error.name, message: error.message }; Object.keys(error).forEach(function(key) { return serializableErrorObject[key] = error[key]; }); return serializableErrorObject; }; SocketIORemoteEndpoint.prototype.publish = function() { var aggregateId, arg, context, domainEventName, fullEventName, i, payload, ref; context = arguments[0], arg = 3 <= arguments.length ? slice.call(arguments, 1, i = arguments.length - 1) : (i = 1, []), payload = arguments[i++]; ref = arg, domainEventName = ref[0], aggregateId = ref[1]; fullEventName = this._getFullEventName(context, domainEventName, aggregateId); return this._socketIoServer.to(fullEventName).emit(fullEventName, payload); }; SocketIORemoteEndpoint.prototype._getFullEventName = function(context, domainEventName, aggregateId) { var fullEventName; fullEventName = context; if (domainEventName) { fullEventName += "/" + domainEventName; } if (aggregateId) { fullEventName += "/" + aggregateId; } return fullEventName; }; SocketIORemoteEndpoint.prototype.close = function() { return this._socketIoServer.close(); }; return SocketIORemoteEndpoint; })(); module.exports = SocketIORemoteEndpoint;