Skip to content

Commit

Permalink
Replace _jobs and _jobsGC with TTLCache from StdLib
Browse files Browse the repository at this point in the history
  • Loading branch information
DoctorMcKay committed Oct 1, 2023
1 parent 5e01a94 commit a91cede
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions components/02-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ class SteamUserConnection extends SteamUserEnums {
clearInterval(this._heartbeatInterval);

this._incomingMessageQueue = []; // clear the incoming message queue. If we're disconnecting, we don't care about anything else in the queue.
this._jobCleanupTimers.forEach(timer => clearTimeout(timer));
this._jobCleanupTimers = [];

this._clearChangelistUpdateTimer();
}
Expand Down
12 changes: 5 additions & 7 deletions components/03-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,10 +441,7 @@ class SteamUserMessages extends SteamUserConnection {
let jobIdSource = null;
if (callback) {
jobIdSource = ++this._currentJobID;
this._jobs[jobIdSource] = callback;

// Clean up old job callbacks after 2 minutes
this._jobCleanupTimers.push(setTimeout(() => delete this._jobs[jobIdSource], 1000 * 60 * 2));
this._jobs.add(jobIdSource.toString(), callback);
}

let emsgName = EMsg[emsg] || emsg;
Expand Down Expand Up @@ -604,7 +601,7 @@ class SteamUserMessages extends SteamUserConnection {
this.emit('debug', debugPrefix + 'Got unknown target_job_name ' + header.proto.target_job_name + ' for msg ' + msgName);
}

if (!this._handlerManager.hasHandler(handlerName) && !this._jobs[header.targetJobID]) {
if (!this._handlerManager.hasHandler(handlerName) && this._jobs.get(header.targetJobID.toString()) === null) {
this.emit(VERBOSE_EMSG_LIST.includes(header.msg) ? 'debug-verbose' : 'debug', debugPrefix + 'Unhandled message: ' + msgName);
return;
}
Expand Down Expand Up @@ -638,9 +635,10 @@ class SteamUserMessages extends SteamUserConnection {
};
}

if (this._jobs[header.targetJobID]) {
let jobCallback = this._jobs.get(header.targetJobID.toString());
if (jobCallback) {
// this is a response to something, so invoke the appropriate callback
this._jobs[header.targetJobID].call(this, body, header, cb);
jobCallback.call(this, body, header, cb);
} else {
this._handlerManager.emit(this, handlerName, body, header, cb);
}
Expand Down
1 change: 0 additions & 1 deletion components/09-logon.js
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,6 @@ class SteamUserLogon extends SteamUserMachineAuth {
delete this.cellID;
this.contentServersReady = false;

this._jobCleanupTimers.forEach(timer => clearTimeout(timer));
this._initProperties();

this._clearChangelistUpdateTimer();
Expand Down
10 changes: 4 additions & 6 deletions components/gamecoordinator.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ class SteamUserGameCoordinator extends SteamUserFriends {
let sourceJobId = JOBID_NONE;
if (typeof callback === 'function') {
sourceJobId = ++this._currentGCJobID;
this._jobsGC[sourceJobId] = callback;

// Clean up job callbacks after 2 minutes
this._jobCleanupTimers.push(setTimeout(() => delete this._jobsGC[sourceJobId], 1000 * 60 * 2));
this._jobsGC.add(sourceJobId.toString(), callback);
}

this.emit('debug', `Sending ${appid} GC message ${msgType}`);
Expand Down Expand Up @@ -80,8 +77,9 @@ SteamUserBase.prototype._handlerManager.add(EMsg.ClientFromGC, function(body) {

this.emit('debug', `Received ${body.appid} GC message ${msgType}`);

if (targetJobID && this._jobsGC[targetJobID]) {
this._jobsGC[targetJobID].call(this, body.appid, msgType, payload);
let jobCallback = this._jobsGC.get(targetJobID.toString());
if (targetJobID && jobCallback) {
jobCallback.call(this, body.appid, msgType, payload);
} else {
this.emit('receivedFromGC', body.appid, msgType, payload);
this.emit('recievedFromGC', body.appid, msgType, payload); // make typos work because why not
Expand Down
5 changes: 2 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,8 @@ class SteamUser extends SteamUserTwoFactor {
this._sessionID = 0;
this._currentJobID = 0;
this._currentGCJobID = 0;
this._jobs = {};
this._jobsGC = {};
this._jobCleanupTimers = [];
this._jobs = new StdLib.DataStructures.TTLCache(1000 * 60 * 2); // job callbacks are cleaned up after 2 minutes
this._jobsGC = new StdLib.DataStructures.TTLCache(1000 * 60 * 2);
this._richPresenceLocalization = {};
this._incomingMessageQueue = [];
this._useMessageQueue = false; // we only use the message queue while we're processing a multi message
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"dependencies": {
"@bbob/parser": "^2.2.0",
"@doctormckay/stdlib": "^2.7.1",
"@doctormckay/stdlib": "^2.8.1",
"@doctormckay/steam-crypto": "^1.2.0",
"adm-zip": "^0.5.10",
"binarykvparser": "^2.2.0",
Expand Down

0 comments on commit a91cede

Please sign in to comment.