Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alternate fix for doPoll timer spam at startup #238

Merged
merged 3 commits into from
Aug 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,6 @@ function TradeOfferManager(options) {
}

TradeOfferManager.prototype.setCookies = function(cookies, familyViewPin, callback) {
if (this.hasShutDown) {
delete this.hasShutDown;
}

if (typeof familyViewPin === 'function') {
callback = familyViewPin;
familyViewPin = null;
Expand All @@ -101,9 +97,8 @@ TradeOfferManager.prototype.setCookies = function(cookies, familyViewPin, callba
this._community.setCookies(['Steam_Language=' + this._languageName]);
}

if (!this._pollTimer && this.pollInterval >= 1000) {
this.doPoll();
}
clearTimeout(this._pollTimer);
this.doPoll();
}

if (callback) {
Expand Down Expand Up @@ -131,7 +126,6 @@ TradeOfferManager.prototype.shutdown = function() {
this._community = new SteamCommunity();
this._steam = null;
this.apiKey = null;
this.hasShutDown = true;
};

TradeOfferManager.prototype.parentalUnlock = function(pin, callback) {
Expand Down Expand Up @@ -200,7 +194,7 @@ TradeOfferManager.prototype.loadInventory = function(appid, contextid, tradableO
/**
* Get the contents of a user's specific inventory context.
* @deprecated Use getUserInventoryContents instead
* @property {SteamID|string} sid - The user's SteamID as a SteamID object or a string which can parse into one
* @param {SteamID|string} sid - The user's SteamID as a SteamID object or a string which can parse into one
* @param {int} appid - The Steam application ID of the game for which you want an inventory
* @param {int} contextid - The ID of the "context" within the game you want to retrieve
* @param {boolean} tradableOnly - true to get only tradable items and currencies
Expand Down
18 changes: 11 additions & 7 deletions lib/polling.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const EOfferFilter = TradeOfferManager.EOfferFilter;
const EConfirmationMethod = TradeOfferManager.EConfirmationMethod;
const deepEqual = require('deep-equal');

const minimumPollInterval = 1000;

/*
* pollData is an object which has the following structure:
* - `offersSince` is the STANDARD unix time (Math.floor(Date.now() / 1000)) of the last known offer change
Expand All @@ -18,15 +20,17 @@ const deepEqual = require('deep-equal');
*/

TradeOfferManager.prototype.doPoll = function(doFullUpdate) {
if (this.hasShutDown) {
// In case a race condition causes this to be called after we've shutdown
if (!this.apiKey) {
// In case a race condition causes this to be called after we've shutdown or before we have an API key
return;
}

if (!this.apiKey || Date.now() - this._lastPoll < 1000) {
// Either we don't have an API key, or we last polled less than a second ago... we shouldn't spam the API
// Reset the timer to poll one second after the last one
this._resetPollTimer(Date.now() - this._lastPoll);
const timeSinceLastPoll = Date.now() - this._lastPoll;

if (timeSinceLastPoll < minimumPollInterval) {
// We last polled less than a second ago... we shouldn't spam the API
// Reset the timer to poll minimumPollInterval after the last one
this._resetPollTimer(minimumPollInterval - timeSinceLastPoll);
return;
}

Expand Down Expand Up @@ -269,7 +273,7 @@ TradeOfferManager.prototype.doPoll = function(doFullUpdate) {
};

TradeOfferManager.prototype._resetPollTimer = function(time) {
if (time || this.pollInterval >= 1000) {
if (time || this.pollInterval >= minimumPollInterval) {
clearTimeout(this._pollTimer);
this._pollTimer = setTimeout(this.doPoll.bind(this), time || this.pollInterval);
}
Expand Down