Home Reference Source

src/registry.js

/*
 * Export the UserRegistry object to the servlet can use it.
 */
module.exports = {
	UserRegistry: UserRegistry
}

/*
 * Definition of helper class
 * to represent registrar of users
 */
function UserRegistry() {
    this.usersById = {};
    this.usersByName = {};

    this.register = registerUser;
    this.unregister = registerUser;
    this.getByName = getUserByName;
    this.getById = getUserById;
    this.removeById = removeUserById;
}

function registerUser(user) {
    this.usersById[user.id] = user;
    this.usersByName[user.name] = user;
}

function unregisterUser(id) {
    var user = this.getById(id);
    if (user) delete this.usersById[id]
    if (user && this.getByName(user.name)) delete this.usersByName[user.name];
}

function getUserById(id) {
    return this.usersById[id];
}

function getUserByName(name) {
    return this.usersByName[name];
}

function removeUserById(id) {
    var userSession = this.usersById[id];
    if (!userSession) return;
    delete this.usersById[id];
    delete this.usersByName[userSession.name];
}