Skip to content

Commit

Permalink
Taplytics 1.2.7
Browse files Browse the repository at this point in the history
- Improved callback timeout to ensure all callbacks and promises timeout.
  • Loading branch information
jonathannorris committed Sep 19, 2017
1 parent 1b53622 commit df1f565
Show file tree
Hide file tree
Showing 16 changed files with 221 additions and 108 deletions.
Binary file added .DS_Store
Binary file not shown.
36 changes: 19 additions & 17 deletions app/api/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,19 @@ exports.del = queueRequest(deleteRequest);
//
var minTimeout = 1000; // 1 seconds
var maxTimeout = 30 * 1000; // 30 seconds
var timeout = 4000; // default 4 second timeout
exports.timeout = 4000; // default 4 second timeout

exports.setTimeout = function(timeoutSec) {
var userTimeout = timeoutSec * 1000;
if (userTimeout > maxTimeout) {
log.error("Timeout is larger then max timeout! timeout: " + timeoutSec + "s, max timeout: " + (maxTimeout / 1000) + "s. Using max timeout value.", null, log.USER);
timeout = maxTimeout;
}
else if (userTimeout < minTimeout) {
exports.timeout = maxTimeout;
} else if (userTimeout < minTimeout) {
log.error("Timeout is smaller then the min timeout! timeout: " + timeoutSec + "s, min timeout: " + (minTimeout / 1000) + "s. Using min timeout value.", null, log.USER);
timeout = minTimeout;
}
else {
exports.timeout = minTimeout;
} else {
log.log("Set timeout: " + timeoutSec + "s", null, log.LOUD);
timeout = userTimeout;
exports.timeout = userTimeout;
}
};

Expand All @@ -47,7 +45,7 @@ function getRequest(path, queryDatum, cb) {

request.get(url)
.query(params.query)
.timeout(timeout)
.timeout(exports.timeout)
.end(callbackWrapper(url, cb));
}

Expand All @@ -56,7 +54,7 @@ function getJSONRequest(path, cb) {
log.log("GET JSON file request: " + url, null, log.LOUD);

request.get(url)
.timeout(timeout)
.timeout(exports.timeout)
.end(callbackWrapper(url, cb));
}

Expand All @@ -80,7 +78,7 @@ function deleteRequest(path, queryDatum, payloadDatum, cb) {
request.del(url)
.query(params.query)
.set('Content-Type', 'application/json')
.timeout(timeout)
.timeout(exports.timeout)
.send(params.payload)
.end(callbackWrapper(url, cb));
}
Expand Down Expand Up @@ -112,8 +110,9 @@ function queueRequest(requestFunction) {
args: arguments
});

if (!isRequesting)
if (!isRequesting) {
processQueue();
}
};
}

Expand Down Expand Up @@ -151,20 +150,23 @@ function getRequestQueryAndPayload(queryDatum, payloadDatum) {
var query = {};
var payload = {};

if (queryDatum && typeof queryDatum == "function")
if (queryDatum && typeof queryDatum == "function") {
query = queryDatum();
else if (queryDatum)
} else if (queryDatum) {
query = queryDatum;
}

if (payloadDatum && typeof payloadDatum == "function")
if (payloadDatum && typeof payloadDatum == "function") {
payload = payloadDatum();
else if (payloadDatum)
} else if (payloadDatum) {
payload = payloadDatum;
}

query.r_v = '0'; // No btoa support, revert to normal JSON

if (exports.publicToken)
if (exports.publicToken) {
query.public_token = exports.publicToken;
}

return {
query: query,
Expand Down
13 changes: 6 additions & 7 deletions app/api/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ exports.get = function(skipFastMode) {
if (err) {
log.error("Failed to get config", err, log.DEBUG);
session.saveSessionConfig(null, true);
}
else {
} else {
var data = response.body;
session.saveSessionConfig(data);

if (data) {
events.clientConfig(time);
log.time("config.get: successfully got session config data", response, time, log.DEBUG);
Expand Down Expand Up @@ -57,8 +57,7 @@ function postFastModeConfig() {
if (err) {
log.error("Failed to post config", err, log.DEBUG);
session.saveSessionConfig(config, true);
}
else {
} else {
var data = response.body;
if (data) {
config.app_user_id = data.app_user_id;
Expand Down Expand Up @@ -205,8 +204,9 @@ function findWinningVariation(exp) {

for (var i=0; i < exp.variations.length; i++) {
var variation = exp.variations[i];
if (variation && exp.winning_variation === variation._id)
if (variation && exp.winning_variation === variation._id) {
return variation;
}
}
return null;
}
Expand All @@ -220,8 +220,7 @@ function chooseVariationFromExperiment(exp) {
if (baselinePer && (rand < baselinePer)) {
log.log("Show Baseline For Experiment: " + exp._id, null, log.DEBUG);
return "baseline";
}
else {
} else {
var per = baselinePer;
for (var i=0; i < exp.variations.length; i++) {
var variation = exp.variations[i];
Expand Down
7 changes: 2 additions & 5 deletions app/api/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,14 @@ exports.users_path = 'users';
exports.del = function(next) {
session.sessionConfigPromise(function() {
var appUserID = session.getAppUserID();
if (!appUserID)
return;
if (!appUserID) return;

var sessionAttrs = {};
sessionAttrs.sid = session.getSessionID();
sessionAttrs.ad = session.getDeviceUUID();

var params = {};
var body = {
session: sessionAttrs
};
var body = {session: sessionAttrs};

log.log("users_del", body, log.DEBUG);

Expand Down
3 changes: 1 addition & 2 deletions app/functions/codeBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ module.exports = function(name, codeBlock) {
if (codeBlock && dynamicVar.value) {
codeBlock();
}
}
else {
} else {
// upload new variable to server
log.log("New Taplytics Code Block: " + name, null, log.DEBUG);
variableAPI.post({
Expand Down
27 changes: 18 additions & 9 deletions app/functions/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,33 @@ module.exports = function(token, options) {

if (options) {
this.api.config.startOptions = options;
if (_.isNumber(options.log_level))
if (_.isNumber(options.log_level)) {
log.setPriorityLevel(options.log_level);
}

if (options.auto_page_view === false)
if (options.auto_page_view === false) {
auto_page_view = false;
}

if (options.env)
if (options.env) {
this.env = options.env;
}

if (options.test_experiments)
if (options.test_experiments) {
session.test_experiments = options.test_experiments;
}

if (options.fast_mode)
if (options.fast_mode) {
this.api.config.fastMode = options.fast_mode;
}

if (options.cookie_domain)
if (options.cookie_domain) {
this.api.config.cookieDomain = options.cookie_domain;
}

if (_.isNumber(options.timeout))
if (_.isNumber(options.timeout)) {
this.api.request.setTimeout(options.timeout);
}
}

/* Initialization */
Expand All @@ -58,8 +65,9 @@ module.exports = function(token, options) {
/* Track current page and other page views. */
// location.listen(app);

if (auto_page_view && this.page)
if (auto_page_view && this.page) {
this.page();
}

// Initiate flushQueue:
this.api.events.scheduleTick();
Expand All @@ -72,8 +80,9 @@ module.exports = function(token, options) {

// Helper functions
function isValidToken(token) {
if (!token || typeof token !== "string")
if (!token || typeof token !== "string") {
return false;
}

return (!!token.length);
}
39 changes: 39 additions & 0 deletions app/lib/__tests__/callbackPromiseHandler_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
jest.dontMock('../callbackPromiseHandler');

var CallbackPromiseHandler = require('../callbackPromiseHandler');

describe("CallbackPromiseHandler", function() {
it("should push promise into queue", function() {
var promiseHandler = new CallbackPromiseHandler();
promiseHandler.push(function() { });
expect(promiseHandler.promises.length).toEqual(1);
});

it("should handle pushing a null promise", function() {
var promiseHandler = new CallbackPromiseHandler();
promiseHandler.push(null);
});

it("should call all callbacks when completePromises is called", function(done) {
var promiseHandler = new CallbackPromiseHandler();
var value = 610;
promiseHandler.push(null);
promiseHandler.push(function(returnValue) {
expect(returnValue).toEqual(value);
done();
});
promiseHandler.push();

setTimeout(function() {
promiseHandler.completePromises(value);
}, 500);
});

it("should call all callbacks if completePromises is not called", function(done) {
var promiseHandler = new CallbackPromiseHandler();
promiseHandler.push(null);
promiseHandler.push(function(returnValue) {
done();
});
});
});
42 changes: 42 additions & 0 deletions app/lib/callbackPromiseHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
var log = require('../lib/logger');
var request = require('../api/base');

// This class holds callback promises and ensures that they will get called
// before the request.timeout time.
function CallbackPromiseHandler(opts) {
this.promises = [];
this.name = opts ? opts.name : null;
}

CallbackPromiseHandler.prototype.push = function(callback) {
var hasExecutedCallback = false;
var self = this;

function executeCallback(value) {
if (hasExecutedCallback) return;

if (callback) {
hasExecutedCallback = true;
callback(value);
}
}

this.promises.push(executeCallback);

setTimeout(function() {
if (!hasExecutedCallback) {
log.log(self.name + " promise timed out", null, log.LOG);
executeCallback();
}
}, request.timeout);
}

CallbackPromiseHandler.prototype.completePromises = function(value) {
for (var i=0; i < this.promises.length; i++) {
var promise = this.promises[i];
if (promise) promise(value);
}
this.promises = [];
}

module.exports = CallbackPromiseHandler;
29 changes: 12 additions & 17 deletions app/lib/cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ exports.getJSON = function(key, useLS) {
log.error("JSON parse cookie value", ex, log.DEBUG);
return null;
}
}
else {
} else {
return jsonValue;
}
};
Expand All @@ -38,16 +37,14 @@ exports.getLS = function(key) {
exports.get = function(key, useLS) {
if (getCookieSupport() && !useLS) {
var jar = getJar();
var accessInfo = new CookieAccess(exports.getCookieDomain())
var accessInfo = new CookieAccess(exports.getCookieDomain());
var cookie = jar.getCookie(key, accessInfo);
if (!cookie) return;

return cookie.value;
}
else if (lscache) {
} else if (lscache) {
return exports.getLS(key);
}
else if (useLS) {
} else if (useLS) {
return exports.get(key);
}
};
Expand Down Expand Up @@ -80,11 +77,9 @@ exports.set = function(key, value, options, useLS) {
cookieJar.setCookie(cookieStr);

log.log("Setting cookies to:", cookieStr, log.LOUD);
}
else if (lscache) {
} else if (lscache) {
exports.setLS(key, value, options);
}
else if (useLS) {
} else if (useLS) {
exports.set(key, value, options);
}
};
Expand All @@ -100,12 +95,10 @@ exports.setLS = function(key, value, options) {
exports.expire = function(key, useLS) {
if (getCookieSupport() && !useLS) {
exports.set(key, "-", {expires: new Date()});
}
else if (lscache) {
log.log("Deleting local storage key: " + key, log.LOUD);
} else if (lscache) {
log.log("Deleting local storage key: " + key, null, log.LOUD);
lscache.remove(key);
}
else if (useLS) {
} else if (useLS) {
exports.expire(key);
}
};
Expand Down Expand Up @@ -151,7 +144,8 @@ function keyValueToCookie(key, value, expiration) {
}

exports.getCookieDomain = function() {
if (config.obj().cookieDomain) return config.obj().cookieDomain
if (config.obj().cookieDomain) return config.obj().cookieDomain;

var hostname = window.location.hostname;
var parts = hostname ? hostname.split('.').reverse() : null;
if (parts && parts.length >= 3) {
Expand Down Expand Up @@ -185,6 +179,7 @@ function getCookieSupport() {
return persist;
}
} while (!(persist = !persist));

cookieSupport = null;
return null;
}
Loading

0 comments on commit df1f565

Please sign in to comment.