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

Commit

Permalink
Merge pull request #12 from feedhenry/default-handler
Browse files Browse the repository at this point in the history
fh-sync default handler support
  • Loading branch information
wtrocki authored Jun 8, 2017
2 parents ceae5d9 + 091deab commit 09edc3c
Show file tree
Hide file tree
Showing 20 changed files with 117 additions and 7,002 deletions.
9 changes: 2 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,11 @@ tmp/*
*~
*.swp
.fhclocal/
src/appforms/dist/
dist/
libs/generated/lawnchair.js
test/sync/testServer
src/modules-cov/*
src-cov/*
src/appforms/tests/feedhenry.js
test/browser/browserified_tests.js
test/browser/feedhenry-latest-require.js
libs/generated/appForms/appForms-backbone.js
libs/generated/appForms/appForms-backboneRequireJS.js
libs/generated/appForms/appForms-core.js
libs/generated/appForms/appForms-core-no-v2.js
test/browser/fh-sync-latest-require.js
test/browser
4 changes: 2 additions & 2 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
test/browser/browserified_tests.js
test/browser/feedhenry-latest-require.js
test
example
17 changes: 2 additions & 15 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,10 @@
language: node_js
sudo: false
node_js:
- '0.10'
- "4.4.3"
before_install:
- npm install -g [email protected]
- npm install -g grunt-cli
install: npm install
script:
- grunt
deploy:
provider: releases
api_key:
secure: AXggnsDClUChq5mRqudXKiDS8g5Mbjv0J/vEOj5vdVYxJgII2+pHcJpFAEtU0hKjiMgWPTtLDDUmjS61iGI9WcxrNKKwGohhORXQk/CPxRUA69AaXOPOUP1p0Sdcyy2NM1NJ1N75mhDkYfrnhlVuafSPKb8DaSjsyjD0JdIoXe4=
file:
- $TRAVIS_BUILD_DIR/dist/feedhenry-js.zip
- $TRAVIS_BUILD_DIR/dist/feedhenry-titanium.zip
- $TRAVIS_BUILD_DIR/dist/fh-starter-project-latest.zip
skip_cleanup: true
on:
repo: feedhenry/fh-js-sdk
tags: true
all_branches: true
- grunt
1 change: 0 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ module.exports = function(grunt) {
options: {
urls: [
"http://127.0.0.1:8200/test/browser/index.html?url=http://localhost:9999",
"http://127.0.0.1:8200/test/browser/index-require.html"
]
}
},
Expand Down
53 changes: 20 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,32 @@ FeedHenry Sync Javascript client
[Note] This repository it's currently in development for production version
please refer to fh-js-sdk npm module.

## Building

The JS SDK is now built using [Browserify](http://browserify.org/).

### Development
## Required setup

Because of Browserify, you can write any new functions as normal node modules, and use node's "require" to load any other modules, or to be consumed by other modules.
Sync client using default Ajax handler to call sync server.
Server URL can be configured using following variables

### Testing
- cloudUrl - URL to sync server
- cloudPath (optional) - allows to use custom endpoint for sync (defaults to `/sync/`)

Write your tests in test/tests directory. Add the tests to test/browser/suite.js file and run

```
grunt test
For example:
```javascript
syncClient.init({
cloudUrl:"http://localhost:3000",
do_console_log: true,
sync_frequency: 1,
sync_active: false,
storage_strategy: ['memory'],
crashed_count_wait: 0
});
```

This will use mocha and phatomjs to run the tests.

In fact, if your module and test don't require a browser environment, you can just run them purely in node. (You may need to add a new grunt task to run them).

To help debugging, you can run

```
grunt local
```

This will start mock servers locally and you can go to http://localhost:8200/example/index.html page to debug. You may want to run

```
grunt watch
```
## Building

In another terminal window to auto generate the combined js sdk file.
npm install
grunt

### Build
## Relation for fh-js-sdk

When finish developing and testing, run
Feedhenry JS SDK contains various libraries and also includes fh-sync-js library.

```
grunt
```
To generate the release builds.
16 changes: 7 additions & 9 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
{
"name": "fh-js-sdk",
"version": "2.17.0",
"homepage": "https://github.com/feedhenry/fh-js-sdk",
"name": "fh-sync-js",
"version": "1.0.0",
"homepage": "https://github.com/feedhenry/fh-sync-js",
"authors": [
"[email protected]"
],
"description": "FeedHenry JS SDK",
"main": "dist/feedhenry.js",
"description": "Feedhenry sync",
"main": "dist/fh-sync.js",
"keywords": [
"feedhenry",
"mobile",
"html5",
"mbaas",
"sdk"
"fhsync"
],
"license": "Copyright (c) 2014 FeedHenry Ltd, All Rights Reserved.",
"license": "Apache 2.0 License",
"ignore": [
"**/.*",
"node_modules",
Expand Down
17 changes: 2 additions & 15 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,12 @@
<body>

<script type="text/javascript">
// setup sync cloud handler
$fh.sync.setCloudHandler(function (params, success, failure) {
$.ajax({
'path': 'http://localhost:8000/mbaas/sync/' + params.dataset_id,
'method': 'post',
'data': params.req
}, function (res) {
callbackCalled = true;
success(res);
}, function (msg, err) {
callbackCalled = true;
failure(msg, err);
});
});

// 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"
Expand All @@ -38,6 +24,7 @@
//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,
Expand Down
2 changes: 1 addition & 1 deletion libs/README.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
This dir is for third party libraries. To add a library to the final dist/feedhenry-latest.js you will need to add it to the grunt.js file
This dir is for third party libraries. To add a library to the final dist you will need to add it to the grunt.js file
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "fh-sync-js",
"version": "1.0.0",
"version": "1.0.0-pre4",
"description": "Javascript client for fh-sync offline synchronization library",
"main": "dist/fh-sync.js",
"main": "src/index.js",
"types": "./fh-js-sdk.d.ts",
"browserify-shim": {
"JSON": {
Expand All @@ -16,7 +16,7 @@
}
},
"browserify": {
"standalone": "feedhenry",
"standalone": "fh-sync",
"transform": [
"browserify-shim"
]
Expand Down
53 changes: 53 additions & 0 deletions src/cloudHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
var cloudURL;
var cloudPath;

/**
* Default sync cloud handler responsible for making all sync requests to
* server.
*/
var handler = function (params, success, failure) {
if (!cloudPath) {
// Default server sync api route
cloudPath = '/sync/';
}
var url = cloudURL + cloudPath + params.dataset_id;
var json = JSON.stringify(params.req);

var xhr = new XMLHttpRequest();
xhr.open("POST", url, true);
xhr.setRequestHeader('Content-type', 'application/json; charset=utf-8');
xhr.onreadystatechange = function () {
if (xhr.readyState === XMLHttpRequest.DONE) {
if ((xhr.status >= 200 && xhr.status < 300) || xhr.status === 304) {
var responseJson;
try {
if (xhr.responseText) {
responseJson = JSON.parse(xhr.responseText);
}
} catch (e) {
return failure(e);
}
success(responseJson);
} else {
failure(xhr.responseText);
}
}
};
xhr.send(json);
};

/**
* Default sync cloud handler init method
*
* @param url - for example http://example.com:7000
* @param path - api path (will default to '/sync')
*/
var init = function (url, path) {
cloudURL = url;
cloudPath = path;
};

module.exports = {
handler: handler,
init: init
};
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ var api_sync = require("./sync-client");

// Mounting into global fh namespace
var fh = window.$fh = window.$fh || {};
fh.sync = api_sync;
fh.sync = api_sync;

module.exports = fh.sync;
11 changes: 7 additions & 4 deletions src/sync-client.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var CryptoJS = require("../libs/generated/crypto");
var Lawnchair = require('../libs/generated/lawnchair');
var defaultCloudHandler = require('./cloudHandler');

var self = {

Expand Down Expand Up @@ -100,6 +101,7 @@ var self = {
self.init_is_called = true;
self.datasetMonitor();
}
defaultCloudHandler.init(self.config.cloudUrl, self.config.cloudPath);
},

notify: function(datasetId, callback) {
Expand All @@ -113,7 +115,7 @@ var self = {
manage: function(dataset_id, opts, query_params, meta_data, cb) {
self.consoleLog('manage - START');

// Currently we do not enforce the rule that init() funciton should be called before manage().
// Currently we do not enforce the rule that init() function should be called before manage().
// We need this check to guard against self.config undefined
if (!self.config){
self.config = JSON.parse(JSON.stringify(self.defaults));
Expand Down Expand Up @@ -1219,10 +1221,10 @@ var self = {

(function() {
self.config = self.defaults;
//Initialse the sync service with default config
//self.init({});
})();

self.setCloudHandler(defaultCloudHandler.handler);

module.exports = {
init: self.init,
manage: self.manage,
Expand Down Expand Up @@ -1251,5 +1253,6 @@ module.exports = {
generateHash: self.generateHash,
loadDataSet: self.loadDataSet,
clearCache: self.clearCache,
setCloudHandler: self.setCloudHandler
setCloudHandler: self.setCloudHandler,
doCloudCall: self.doCloudCall
};
Loading

0 comments on commit 09edc3c

Please sign in to comment.