UNPKG

uspring

Version:

A very fast Webserver which has interface like springboot

124 lines (123 loc) 3.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var HASH_TABLE = /** @class */ (function () { function HASH_TABLE() { this.name = ""; this.size = 0; this.createdAt = Date.now(); this[0] = new Map(); this[1] = new Map(); this[2] = new Map(); this[3] = new Map(); this[4] = new Map(); this[5] = new Map(); this[6] = new Map(); this[7] = new Map(); this[8] = new Map(); this[9] = new Map(); } HASH_TABLE.prototype.add = function (ws) { ws.id = Date.now(); var index = ws.id % 10; var a = this[index]; this.size++; return a.set(ws.id, ws); }; HASH_TABLE.prototype.remove = function (id) { var index = id % 10; var a = this[index]; var isDeleted = a.delete(id); if (isDeleted) { this.size++; } return isDeleted; }; HASH_TABLE.prototype.findUser = function (id) { var index = id % 10; var a = this[index]; var ws = a.get(id); return ws; }; HASH_TABLE.prototype.forEach = function (func) { for (var i = 0; i < 10; i++) { this[i].forEach(func); } }; HASH_TABLE.prototype.clear = function () { for (var i = 0; i < 10; i++) { this[i].clear(); } this.size = 0; }; return HASH_TABLE; }()); exports.HASH_TABLE = HASH_TABLE; var Route = /** @class */ (function () { function Route() { } Route.joinRoute = function (roomName, ws) { ws.id = Date.now(); if (!this.data.get(roomName)) { this.data.set(roomName, new HASH_TABLE()); } var roomMembers = this.data.get(roomName); roomMembers.add(ws); }; Route.publishToRoute = function (roomName, message) { if (!this.data.get(roomName)) { return false; } var roomMembers = this.data.get(roomName); roomMembers.forEach(function (ws, index, mapData) { ws.send(message); }); return true; }; Route.broadcastToRoute = function (roomName, senderId, message) { if (!this.data.get(roomName)) { return false; } var roomMembers = this.data.get(roomName); roomMembers.forEach(function (ws, index, mapData) { if (ws.id !== senderId) { ws.send(message); } }); return true; }; Route.findUser = function (id) { // search in each topic; var iterator = this.data.keys(); var data = this.data; return (function loop() { var nextKey = iterator.next().value(); if (typeof nextKey === "undefined") return null; var user = data.get(nextKey); if (user) { return user.findUser(id); } return loop(); })(); }; Route.removeUserFromAllRoute = function (id) { // search in each topic; this.data.forEach(function (hashTable) { hashTable.remove(id); }); return null; }; Route.deleteTopic = function (topic) { if (!this.data.get(topic)) { return false; } var topicGroup = this.data.get(topic); topicGroup.clear(); return this.data.delete(topic); }; Route.findUserInOneTopic = function (topic, id) { }; Route.findTopicsOfUser = function (id) { }; Route.data = new Map(); return Route; }()); exports.Route = Route;