🐛 Fix a few cache-related bugs and add weak-value-map support

parent f7c50934
......@@ -9,6 +9,13 @@ const ConnectStates = require("./Enums/ConnectStates");
const IrcCommands = require("./Enums/IrcCommands");
const Nodesu = require("nodesu");
let CacheMap;
try {
CacheMap = require("weak-value-map");
} catch(err) {
CacheMap = Map;
}
const ignoredSplits = [
"312", // Whois server info (useless on Bancho)
"333", // Time when topic was set
......@@ -88,9 +95,9 @@ class BanchoClient extends EventEmitter {
this.connectCallback = null;
this.joinCallbacks = [];
this.partCallbacks = [];
this.users = new Map();
this.usersById = new Map();
this.channels = new Map();
this.users = new CacheMap();
this.usersById = new CacheMap();
this.channels = new CacheMap();
// Register our own error listener so there are no uncaught exceptions
// for exceptions WE throw for the end-user that don't need to be caught (such as timeouts)
......@@ -255,13 +262,12 @@ class BanchoClient extends EventEmitter {
*/
getUser(username) {
username = username.replace(/ /g, "_").split("\n")[0].substring(0, 28);
if(this.users.has(username.toLowerCase()))
return this.users.get(username.toLowerCase());
else {
const user = new BanchoUser(this, username);
let user = this.users.get(username.toLowerCase());
if(!user) {
user = new BanchoUser(this, username);
this.users.set(username.toLowerCase(), user);
return user;
}
return user;
}
/**
......@@ -282,13 +288,14 @@ class BanchoClient extends EventEmitter {
return new Promise((resolve, reject) => {
if(isNaN(userid))
reject(new Error("id needs to be a number!"));
if(this.usersById.has(userid))
resolve(this.usersById.get(userid));
let user = this.usersById.get(userid);
if(user)
resolve(user);
else
this.osuApi.user.get(userid, Nodesu.LookupType.id).then((apiUser) => {
const user = this.getUser(apiUser.username);
user = this.getUser(apiUser.username);
user.updateFromAPI(apiUser);
resolve(apiUser);
resolve(user);
}, reject);
});
}
......@@ -302,13 +309,12 @@ class BanchoClient extends EventEmitter {
getChannel(channelName) {
if(channelName.indexOf("#") != 0 || channelName.indexOf(",") != -1 || channelName.indexOf("") != -1)
throw new Error("Invalid channel name!");
if(this.channels.has(channelName))
return this.channels.get(channelName);
else {
const channel = (channelName.indexOf("#mp_") == 0 && this.osuApi != null) ? new BanchoMultiplayerChannel(this, channelName) : new BanchoChannel(this, channelName);
let channel = this.channels.get(channelName);
if(!channel) {
channel = (channelName.indexOf("#mp_") == 0 && this.osuApi != null) ? new BanchoMultiplayerChannel(this, channelName) : new BanchoChannel(this, channelName);
this.channels.set(channelName, channel);
return channel;
}
return channel;
}
/**
......
......@@ -225,10 +225,10 @@ class BanchoUser extends EventEmitter {
set id(newId) {
if(isNaN(newId))
throw new Error("id needs to be a number!");
if(this._id != null && this.banchojs.usersById.has(this._id))
if(this._id != null && this.banchojs.usersById.get(this._id))
this.banchojs.usersById.delete(this._id);
this._id = newId;
this.banchojs.usersById[this._id] = this;
this.banchojs.usersById.set(this._id, this);
}
}
......
......@@ -184,6 +184,15 @@
"integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=",
"dev": true
},
"bindings": {
"version": "1.5.0",
"resolved": "https://registry.npmjs.org/bindings/-/bindings-1.5.0.tgz",
"integrity": "sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==",
"dev": true,
"requires": {
"file-uri-to-path": "1.0.0"
}
},
"bluebird": {
"version": "3.7.2",
"resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.7.2.tgz",
......@@ -603,6 +612,12 @@
"flat-cache": "^2.0.1"
}
},
"file-uri-to-path": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz",
"integrity": "sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==",
"dev": true
},
"flat-cache": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/flat-cache/-/flat-cache-2.0.1.tgz",
......@@ -1118,6 +1133,12 @@
"integrity": "sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==",
"dev": true
},
"node-addon-api": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-2.0.0.tgz",
"integrity": "sha512-ASCL5U13as7HhOExbT6OlWJJUV/lLzL2voOSP1UVehpRD8FbSrSDjfScK/KwAvVTI5AS6r4VwbOMlIqtvRidnA==",
"dev": true
},
"nodesu": {
"version": "0.7.2",
"resolved": "https://registry.npmjs.org/nodesu/-/nodesu-0.7.2.tgz",
......@@ -1670,6 +1691,16 @@
"integrity": "sha512-usZBT3PW+LOjM25wbqIlZwPeJV+3OSz3M1k1Ws8snlW39dZyYL9lOGC5FgPVHfk0jKmjiDV8Z0mIbVQPiwFs7g==",
"dev": true
},
"weak-value-map": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/weak-value-map/-/weak-value-map-1.0.1.tgz",
"integrity": "sha512-faMJa3W3rK68KEIoTiS4r0p96G9QP9qpKRwpfgeO75Y4/gsQFNGPec/g8Keiln9Hb61ibQIex7C5UEyY9QwKgA==",
"dev": true,
"requires": {
"bindings": "^1.5.0",
"node-addon-api": "^2.0.0"
}
},
"which": {
"version": "1.3.1",
"resolved": "https://registry.npmjs.org/which/-/which-1.3.1.tgz",
......
......@@ -26,9 +26,18 @@
"eslint": "^6.8.0",
"ink-docstrap": "^1.3.2",
"jsdoc": "^3.6.4",
"log-symbols": "^3.0.0"
"log-symbols": "^3.0.0",
"weak-value-map": "^1.0.1"
},
"dependencies": {
"nodesu": "^0.7.2"
},
"peerDependencies": {
"weak-value-map": "^1.0.1"
},
"peerDependenciesMeta": {
"weak-value-map": {
"optional": true
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment