Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
Add client identifier (#20)
Browse files Browse the repository at this point in the history
* Add client identifier

* Update demo

* Separate provider class

* Version bump
  • Loading branch information
wtrocki authored Jun 16, 2017
1 parent 87dd14b commit fc295e1
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 52 deletions.
47 changes: 5 additions & 42 deletions example/index.html
Original file line number Diff line number Diff line change
@@ -1,50 +1,13 @@
<html>

<head>
<script
src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>
<!-- TODO improve example to use local distribution build !-->
<script src="../dist/fh-sync.js" type="text/javascript"></script>
<script src="./sync.js" type="text/javascript"></script>
</head>
<body>

<script type="text/javascript">
// Standard sync setup for simple dataset
var datasetId = "myShoppingList";

//provide sync init options
$fh.sync.init({
"cloudUrl": "http://localhost:3000",
"sync_frequency": 10,
"do_console_log": true,
"storage_strategy": "dom"
});

//provide listeners for notifications.
$fh.sync.notify(function(notification){
var code = notification.code
console.log("Sync ", notification);
if('sync_complete' === code){
//a sync loop completed successfully, list the update data
$fh.sync.doList(datasetId,
function (res) {
console.log('Successful result from list:', JSON.stringify(res));
},
function (err) {
console.log('Error result from list:', JSON.stringify(err));
});
} else {
//choose other notifications the app is interested in and provide callbacks
}
});

//manage the data set, repeat this if the app needs to manage multiple datasets
var query_params = {}; //or something like this: {"eq": {"field1": "value"}}
var meta_data = {};
$fh.sync.manage(datasetId, {}, query_params, meta_data, function(){

});
</script>
Messages will be printed to console.
</body>
</html>
42 changes: 42 additions & 0 deletions example/sync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Standard sync setup for simple dataset
var datasetId = "myShoppingList";

//provide sync init options
$fh.sync.init({
"cloudUrl": "http://localhost:3000",
"sync_frequency": 10,
"do_console_log": true,
"storage_strategy": "dom"
});

//provide listeners for notifications.
$fh.sync.notify(function (notification) {
var code = notification.code
if ('sync_complete' === code) {
//a sync loop completed successfully, list the update data
$fh.sync.doList(datasetId,
function (res) {
console.log('Successful result from list:', JSON.stringify(res));
},
function (err) {
console.log('Error result from list:', JSON.stringify(err));
});
} else {
//choose other notifications the app is interested in and provide callbacks
}
});

//manage the data set, repeat this if the app needs to manage multiple datasets
var query_params = {}; //or something like this: {"eq": {"field1": "value"}}
var meta_data = {};
$fh.sync.manage(datasetId, {}, query_params, meta_data, function () {
// Save data
var data = { name: "Shoppping item1" };
$fh.sync.doCreate(datasetId, data,
function (res) {
console.log('Successful result from list:', JSON.stringify(res));
},
function (err) {
console.log('Error result from list:', JSON.stringify(err));
});
});
13 changes: 9 additions & 4 deletions npm-shrinkwrap.json

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

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fh-sync-js",
"version": "1.0.2",
"version": "1.0.3",
"description": "Javascript client for fh-sync offline synchronization library",
"main": "src/index.js",
"types": "./fh-sync-js.d.ts",
Expand Down Expand Up @@ -31,10 +31,11 @@
"author": "Feedhenry Team",
"license": "Apache 2.0",
"dependencies": {
"type-of": "~2.0.1",
"loglevel": "~0.6.0",
"process": "~0.6.0",
"type-of": "~2.0.1",
"underscore": "~1.6.0",
"process": "~0.6.0"
"uuid": "^3.0.1"
},
"devDependencies": {
"async": "0.2.10",
Expand Down
28 changes: 28 additions & 0 deletions src/clientIdProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var uuidGenerator = require('uuid').v1;
var CLIENT_ID_TAG = "feedhenry_sync_client";

/**
* Get unique client id for current browser/platform/user
*/
function getClientId() {
if (window && window.device) {
return window.device.uuid;
}
if (navigator && navigator.device) {
return navigator.device.uuid;
}
if (window && window.localStorage) {
var clientId = window.localStorage.getItem(CLIENT_ID_TAG);
if (!clientId) {
clientId = uuidGenerator();
localStorage.setItem(CLIENT_ID_TAG, clientId);
}
return clientId;
} else {
throw Error("Cannot create and store client id");
}
}

module.exports = {
getClientId: getClientId
};
10 changes: 7 additions & 3 deletions src/cloudHandler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var cloudURL;
var cloudPath;
var cidProvider = require('./clientIdProvider');

/**
* Default sync cloud handler responsible for making all sync requests to
Expand All @@ -11,8 +12,11 @@ var handler = function (params, success, failure) {
cloudPath = '/sync/';
}
var url = cloudURL + cloudPath + params.dataset_id;
var json = JSON.stringify(params.req);

var payload = params.req;
payload.__fh = {
cuid: cidProvider.getClientId()
};
var json = JSON.stringify(payload);
var xhr = new XMLHttpRequest();
xhr.open("POST", url, true);
xhr.setRequestHeader('Content-type', 'application/json; charset=utf-8');
Expand Down Expand Up @@ -49,5 +53,5 @@ var init = function (url, path) {

module.exports = {
handler: handler,
init: init
init: init,
};

0 comments on commit fc295e1

Please sign in to comment.