From 41647425c050b36c6232e68599119e749099df6b Mon Sep 17 00:00:00 2001 From: Oleksandr Bardanov Date: Thu, 14 Aug 2014 15:13:31 +0300 Subject: [PATCH] tracking service added --- main.js | 2 +- package.json | 2 +- services/onlineTracking.js | 64 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 services/onlineTracking.js diff --git a/main.js b/main.js index ce5de94..1e7ae7d 100644 --- a/main.js +++ b/main.js @@ -7,5 +7,5 @@ define(function (require, exports, module) { require('./services/injector').init(); - //require('./services/onlineTracking').init(); + require('./services/onlineTracking').init(); }); diff --git a/package.json b/package.json index 6354463..6a4d6c4 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "title": "Documents Toolbar", "description": "Adds toolbar with a list of open documents on the top of the editor.", "homepage": "https://github.com/dnbard/brackets-documents-toolbar", - "version": "0.1.0", + "version": "0.1.1", "author": "Alex Bardanov ", "license": "MIT", "engines": { diff --git a/services/onlineTracking.js b/services/onlineTracking.js new file mode 100644 index 0000000..eac9387 --- /dev/null +++ b/services/onlineTracking.js @@ -0,0 +1,64 @@ +// This is example of a client for extenstion online tracking service +// Part of Brackets Extension Rating extension by Alex Bardanov +// https://github.com/dnbard/brackets-extension-rating + +// use require('./onlineTrackingClient').init() to activate tracking + +define(function(require, exports){ + var trackingServiceUrl = 'http://brackets-online.herokuapp.com/', + // http://brackets-online.herokuapp.com/ is an address of default tracking service + // Change it if you use self-hosting instance of online tracking service + appToken = '53eca7616073b96ad100017a', + // read https://github.com/dnbard/brackets-extension-rating/wiki/Online-and-max-users-counters-in-this-extension + // to learn on how to obtain an application token for your extension + mins60 = 60 * 60 * 1000, + mins5 = 5 * 60 * 1000, + keyId = 'ext-online-id'; + + function tick(){ + var userId = getUserId(appToken, keyId), + url; + + if (userId){ + url = trackingServiceUrl + 'tick/' + appToken + '/' + userId; + } else { + url = trackingServiceUrl + 'tick/' + appToken; + } + + $.ajax({ url: url }) + .success(function(data){ + //TODO: create complex model of data in local storage to support any number of extensions + if (data && data !== 'OK' && data !== 'ERROR'){ + saveUserId(data, appToken, keyId); + } + }).error(function(){ + console.log('Can\'t track online status, retry in 5 mins'); + setTimeout(tick, mins5); + }); + } + + function init(){ + tick(); + setInterval(tick, mins60); + } + + function getUserId(appToken, keyId){ + if (typeof appToken !== 'string' || typeof keyId !== 'string'){ + throw new Error('Invalid argument'); + } + + return JSON.parse(localStorage.getItem(keyId) || '{ }')[appToken]; + } + + function saveUserId(id, appToken, keyId){ + if (typeof id !== 'string' || typeof appToken !== 'string' || typeof keyId !== 'string'){ + throw new Error('Invalid argument'); + } + + var obj = JSON.parse(localStorage.getItem(keyId) || '{ }'); + obj[appToken] = id; + localStorage.setItem(keyId, JSON.stringify(obj)); + } + + exports.init = init; +});