pokemon-showdown/dev-tools/users-utils.js
Guangcong Luo 332a65f9c0 Convert some Users tables to ES6 Maps
Specifically, Users.users, Users.connections, and Users.pastUsers
are now ES6 Maps. In theory, this should be a minor performance
upgrade, but we still need to profile to make sure.
2015-12-10 11:52:26 -05:00

37 lines
1017 B
JavaScript

'use strict';
let EventEmitter = require('events').EventEmitter;
function createWorker() {
let fakeWorker = new EventEmitter();
fakeWorker.send = function () {};
Sockets.workers[fakeWorker.id] = fakeWorker;
return fakeWorker;
}
function createConnection(ip, workerid, socketid) {
if (!workerid || !socketid) {
workerid = Object.keys(Sockets.workers)[0];
if (!workerid) workerid = createWorker().id;
socketid = 1;
while (Users.connections.has(workerid + '-' + socketid)) {
socketid++;
}
}
let connectionid = workerid + '-' + socketid;
let connection = new Users.Connection(connectionid, Sockets.workers[workerid], socketid, null, ip || '127.0.0.1');
Users.connections.set(connectionid, connection);
return connection;
}
function createUser(connection) {
if (!connection) connection = createConnection();
let user = new Users.User(connection);
connection.user = user;
user.joinRoom('global', connection);
return user;
}
exports.Connection = createConnection;
exports.User = createUser;