UNPKG

@agatee/socket

Version:

Socket module to use with Agatee CLI

42 lines (41 loc) 1.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.On = exports.GatSocket = exports.GatSocketModule = void 0; require("reflect-metadata"); console.log(process.cwd()); const core_1 = require("@agatee/core"); const onMethods = Symbol('onMethods'); // Decorators function GatSocketModule(params) { } exports.GatSocketModule = GatSocketModule; function GatSocket(constructorFunc) { let newConstructorFunc = function (...args) { let func = function () { let metadata = Reflect.getMetadata('design:paramtypes', constructorFunc) || []; let injections = metadata.map(token => core_1.Injector.resolve(token)); return new constructorFunc(...injections); }; func.prototype = constructorFunc.prototype; let newInstance = new func(); newInstance.io.on('connection', (socket) => { newInstance.onConnection(socket); if (func.prototype[onMethods]) { // cheking for methods decorate with @On for (let [method, event] of func.prototype[onMethods]) { socket.on(event, func.prototype[method].bind(newInstance, socket)); } } }); return newInstance; }; newConstructorFunc.prototype = constructorFunc.prototype; return newConstructorFunc; } exports.GatSocket = GatSocket; function On(event) { return function (target, key, descriptor) { target[onMethods] = target[onMethods] || new Map(); target[onMethods].set(key, event || key); }; } exports.On = On;