-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Oleksandr Bardanov
committed
Aug 14, 2014
1 parent
15a9bb5
commit 4164742
Showing
3 changed files
with
66 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}); |