diff --git a/example/index.html b/example/index.html index d980674..520a841 100644 --- a/example/index.html +++ b/example/index.html @@ -1,50 +1,13 @@ + - + + - - +Messages will be printed to console. \ No newline at end of file diff --git a/example/sync.js b/example/sync.js new file mode 100644 index 0000000..08b5e69 --- /dev/null +++ b/example/sync.js @@ -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)); + }); +}); \ No newline at end of file diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index 58e4daa..d179049 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -4,23 +4,28 @@ "dependencies": { "loglevel": { "version": "0.6.0", - "from": "loglevel@>=0.6.0 <0.7.0", + "from": "https://registry.npmjs.org/loglevel/-/loglevel-0.6.0.tgz", "resolved": "https://registry.npmjs.org/loglevel/-/loglevel-0.6.0.tgz" }, "process": { "version": "0.6.0", - "from": "process@>=0.6.0 <0.7.0", + "from": "https://registry.npmjs.org/process/-/process-0.6.0.tgz", "resolved": "https://registry.npmjs.org/process/-/process-0.6.0.tgz" }, "type-of": { "version": "2.0.1", - "from": "type-of@>=2.0.1 <2.1.0", + "from": "https://registry.npmjs.org/type-of/-/type-of-2.0.1.tgz", "resolved": "https://registry.npmjs.org/type-of/-/type-of-2.0.1.tgz" }, "underscore": { "version": "1.6.0", - "from": "underscore@>=1.6.0 <1.7.0", + "from": "https://registry.npmjs.org/underscore/-/underscore-1.6.0.tgz", "resolved": "https://registry.npmjs.org/underscore/-/underscore-1.6.0.tgz" + }, + "uuid": { + "version": "3.0.1", + "from": "uuid@latest", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.0.1.tgz" } } } diff --git a/package.json b/package.json index 8d08793..544ac67 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/src/clientIdProvider.js b/src/clientIdProvider.js new file mode 100644 index 0000000..4442a0b --- /dev/null +++ b/src/clientIdProvider.js @@ -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 +}; \ No newline at end of file diff --git a/src/cloudHandler.js b/src/cloudHandler.js index 2d4cf15..69438d4 100644 --- a/src/cloudHandler.js +++ b/src/cloudHandler.js @@ -1,5 +1,6 @@ var cloudURL; var cloudPath; +var cidProvider = require('./clientIdProvider'); /** * Default sync cloud handler responsible for making all sync requests to @@ -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'); @@ -49,5 +53,5 @@ var init = function (url, path) { module.exports = { handler: handler, - init: init + init: init, }; \ No newline at end of file