cordova-plugin-hotpushes
Advanced tools
Comparing version 0.1.5 to 0.2.1
@@ -6,7 +6,7 @@ # `replace` Example | ||
src: 'http://myserver/hot/', | ||
versionJSONPFileName: 'version.jsonp', | ||
versionJSONFileName: 'version.json', | ||
versionFileName: 'version.json', | ||
type: 'merge' | ||
}); | ||
hotpushes.loadAllLocalFiles() // load local files | ||
@@ -25,6 +25,5 @@ hotpushes.check(); // check for update | ||
On `http://myserver/hot/`, there should be 2 files + the files listed in `version.json` : | ||
On `http://myserver/hot/` and in the `bundle` of the app, there should be 1 file + the files listed in `version.json` : | ||
- version.json | ||
- version.jsonp | ||
@@ -35,3 +34,2 @@ ### Example | ||
version.json | ||
version.jsonp | ||
libs.js | ||
@@ -100,9 +98,1 @@ app.js | ||
``` | ||
## version.jsonp | ||
This file contains the same content as `version.json` wrapped inside the `hotPushJSONP` callback. | ||
### Example | ||
``` | ||
hotPushJSONP({...}); | ||
``` |
@@ -6,4 +6,3 @@ # `replace` Example | ||
src: 'http://myserver/hot/', | ||
versionJSONPFileName: 'version.jsonp', | ||
versionJSONFileName: 'version.json', | ||
versionFileName: 'version.json', | ||
type: 'replace', | ||
@@ -13,2 +12,3 @@ archiveURL: 'http://myserver/hot/assets.zip' | ||
hotpushes.loadAllLocalFiles() // load local files | ||
@@ -32,2 +32,18 @@ hotpushes.check(); // check for update | ||
In the `bundle` of the app, there should be 1 file + the files listed in `version.json` : | ||
- version.json | ||
### Example | ||
``` | ||
www/ | ||
version.json | ||
libs.js | ||
app.js | ||
styles/ | ||
app.css | ||
libs.css | ||
``` | ||
## version.json | ||
@@ -86,3 +102,3 @@ This file contains a timestamp of its creation and a array describing files to load. | ||
## assets.zip | ||
This file contains the files listed in `version.json` as well as the file `version.jsonp`. | ||
This file contains the files listed in `version.json`. | ||
@@ -92,3 +108,2 @@ ### Example | ||
/ | ||
version.jsonp | ||
libs.js | ||
@@ -101,9 +116,1 @@ app.js | ||
``` | ||
## version.jsonp | ||
This file contains the same content as `version.json` wrapped inside the `hotPushJSONP` callback. | ||
### Example | ||
``` | ||
hotPushJSONP({...}); | ||
``` |
{ | ||
"name": "cordova-plugin-hotpushes", | ||
"version": "0.1.5", | ||
"version": "0.2.1", | ||
"cordova": { | ||
@@ -5,0 +5,0 @@ "id": "cordova-plugin-hotpushes", |
@@ -31,4 +31,3 @@ #cordova-plugin-hotpushes | ||
`options.src` | `String` URL to hot push endpoint | ||
`options.versionJSONPFileName` | `String` Name of the jsonp file containing the version information. | ||
`options.versionJSONFileName` | `String` Name of the json file containing the version information | ||
`options.versionFileName` | `String` Name of the json file containing the version information | ||
`options.type` | `String` _(Optional)_ Defines the hot push strategy applied to the content.<br/>The type `replace` is the default behaviour that completely removes existing content then copies new content from a zip file.<br/> The type `merge` will download and replace only content which has changed. | ||
@@ -48,4 +47,3 @@ `options.headers` | `Object` _(Optional)_ Set of headers to use when requesting the remote content from `options.src`. | ||
src: 'http://myserver/hot/', | ||
versionJSONPFileName: 'version.jsonp', | ||
versionJSONFileName: 'version.json', | ||
versionFileName: 'version.json', | ||
type: 'replace', | ||
@@ -56,5 +54,5 @@ archiveURL: 'http://myserver/hot/assets.zip' | ||
## HotPush.check() | ||
## hotpushes.loadAllLocalFiles() | ||
Load the local files and check if there is a new version available on the server. | ||
Load the local files. | ||
@@ -65,4 +63,12 @@ Parameter | Description | ||
## HotPush.update() | ||
## hotpushes.check() | ||
Check if there is a new version available on the server. | ||
Parameter | Description | ||
--------- | ------------ | ||
`no parameters` | | ||
## hotpushes.update() | ||
Download the files on the server. | ||
@@ -69,0 +75,0 @@ |
150
www/index.js
@@ -14,4 +14,3 @@ /* global cordova:false */ | ||
* @param {String} src is a URL to hot push endpoint | ||
* @param {String} versionJSONPFileName is the name of the jsonp file containing the version information | ||
* @param {String} versionJSONFileName is the name of the json file containing the version information | ||
* @param {String} versionFileName is the name of the json file containing the version information | ||
* @param {Object} type defines the hot push strategy applied to the content. | ||
@@ -46,10 +45,5 @@ * @param {String} replace completely removes existing content then copies new content from a zip file. | ||
// require options.versionJSONPFileName parameter | ||
if (typeof options.versionJSONPFileName === 'undefined') { | ||
throw new Error('The options.versionJSONPFileName argument is required.'); | ||
} | ||
// require options.versionJSONFileName parameter | ||
if (typeof options.versionJSONFileName === 'undefined') { | ||
throw new Error('The options.versionJSONFileName argument is required.'); | ||
if (typeof options.versionFileName === 'undefined') { | ||
throw new Error('The options.versionFileName argument is required.'); | ||
} | ||
@@ -84,25 +78,4 @@ | ||
this.remoteVersion = null; | ||
this.countForCallback = null; | ||
this.fetchFromBundle = false; | ||
this._waitingToLoad = true; | ||
this._syncs = []; | ||
var self = this; | ||
window.hotPushJSONP = function(version) { | ||
if (!version && !self.fetchFromBundle) { // error when we tried to fetch from /Documents | ||
// search in bundle | ||
self.fetchFromBundle = true; | ||
self._loadLocalVersion(); | ||
} else if (version) { | ||
self.localVersion = version; | ||
if (self._waitingToLoad) { | ||
self._loadAllLocalFiles(); | ||
} | ||
self._callback(); | ||
} else { // error when we tried to fetch from Bundle | ||
console.log('error when we tried to fetch from Bundle'); | ||
self.emit('error', new Error('error when we tried to fetch from Bundle')); | ||
} | ||
}; | ||
}; | ||
@@ -114,32 +87,26 @@ | ||
HotPush.prototype.check = function() { | ||
var self = this; | ||
// reset variable | ||
this.fetchFromBundle = false; | ||
this.countForCallback = 2; | ||
// fetch localVersion | ||
this._loadLocalVersion(); | ||
this._loadLocalVersion(function() { | ||
// fetch remoteVersion | ||
var remoteRequest = new XMLHttpRequest(); | ||
remoteRequest.open('GET', this.options.src + this.options.versionFileName, true); | ||
// fetch remoteVersion | ||
var remoteRequest = new XMLHttpRequest(); | ||
remoteRequest.open('GET', this.options.src + this.options.versionJSONFileName, true); | ||
remoteRequest.onload = function() { | ||
if (remoteRequest.status >= 200 && remoteRequest.status < 400) { | ||
// Success! | ||
this.remoteVersion = JSON.parse(remoteRequest.responseText); | ||
this._verifyVersions(); | ||
} else { | ||
console.log('nothing on the remote'); | ||
this.emit('noUpdateFound'); | ||
} | ||
}.bind(this); | ||
remoteRequest.onload = function() { | ||
if (remoteRequest.status >= 200 && remoteRequest.status < 400) { | ||
// Success! | ||
self.remoteVersion = JSON.parse(remoteRequest.responseText); | ||
self._callback(); | ||
} else { | ||
console.log('nothing on the remote'); | ||
self.emit('noUpdateFound'); | ||
} | ||
}; | ||
remoteRequest.onerror = function(err) { | ||
console.log(err); | ||
this.emit('error', err); | ||
}.bind(this); | ||
remoteRequest.onerror = function(err) { | ||
console.log(err); | ||
self.emit('error', err); | ||
}; | ||
remoteRequest.send(); | ||
remoteRequest.send(); | ||
}.bind(this)); | ||
}; | ||
@@ -150,7 +117,6 @@ | ||
*/ | ||
HotPush.prototype._loadAllLocalFiles = function() { | ||
HotPush.prototype.loadAllLocalFiles = function() { | ||
var self = this; | ||
if (this.localVersion) { | ||
this._waitingToLoad = false; | ||
var files = this.localVersion.files; | ||
var self = this; | ||
var loadfile = function(filename) { | ||
@@ -163,3 +129,3 @@ return function() { | ||
if (files[i].position) { | ||
setTimeout(loadfile(files[i].name), files[i].position * 100); | ||
setTimeout(loadfile(files[i].name), files[i].position * 10); | ||
} else { | ||
@@ -170,3 +136,3 @@ loadfile(files[i].name)(); | ||
} else { | ||
this._waitingToLoad = true; | ||
this._loadLocalVersion(this.loadAllLocalFiles.bind(this)); | ||
} | ||
@@ -188,2 +154,4 @@ }; | ||
this._syncs[0].on('complete', function() { | ||
self.remoteVersion.location = 'documents'; | ||
localStorage.setItem("hotpushes_localVersion", JSON.stringify(self.remoteVersion)); | ||
self.emit('updateComplete'); | ||
@@ -193,3 +161,2 @@ }); | ||
this._syncs[0].on('error', function(e) { | ||
console.log(e) | ||
self.emit('error', e); | ||
@@ -208,3 +175,3 @@ }); | ||
HotPush.prototype._getLocalPath = function(filename) { | ||
if (this.fetchFromBundle) { | ||
if (this.localVersion.location === 'bundle') { | ||
return '/' + filename; | ||
@@ -220,10 +187,34 @@ } else { | ||
HotPush.prototype._loadLocalVersion = function() { | ||
var head = document.getElementsByTagName("head")[0]; | ||
var time = new Date().getTime(); | ||
var domEl = document.createElement("script"); | ||
domEl.setAttribute("type", "text/javascript"); | ||
domEl.setAttribute("src", this._getLocalPath(this.options.versionJSONPFileName) + '?' + time); | ||
domEl.onerror = function(err) {window.hotPushJSONP(null);} | ||
head.appendChild(domEl); | ||
HotPush.prototype._loadLocalVersion = function(callback) { | ||
this.localVersion = this.localVersion || JSON.parse(localStorage.getItem("hotpushes_localVersion")); | ||
if (this.localVersion) { | ||
return callback(); | ||
} else { | ||
var self = this; | ||
// fetch bundleVersion | ||
var request = new XMLHttpRequest(); | ||
request.open('GET', this.options.versionFileName, true); | ||
request.onload = function() { | ||
if (request.status >= 200 && request.status < 400) { | ||
// Success! | ||
self.localVersion = JSON.parse(request.responseText); | ||
self.localVersion.location = 'bundle'; | ||
localStorage.setItem("hotpushes_localVersion", JSON.stringify(self.localVersion)); | ||
callback(); | ||
} else { | ||
console.log('nothing on the bundle'); | ||
self.emit('error', new Error('now version.json in the bundle')); | ||
} | ||
}; | ||
request.onerror = function(err) { | ||
console.log(err); | ||
self.emit('error', err); | ||
}; | ||
request.send(); | ||
} | ||
}; | ||
@@ -234,12 +225,9 @@ | ||
*/ | ||
HotPush.prototype._callback = function() { | ||
this.countForCallback--; | ||
if (this.countForCallback === 0) { | ||
if (this.localVersion.timestamp !== this.remoteVersion.timestamp) { | ||
console.log('Not the last version, ' + this.localVersion.timestamp +' !== ' + this.remoteVersion.timestamp); | ||
this.emit('updateFound'); | ||
} else { | ||
console.log('All good, last version running'); | ||
this.emit('noUpdateFound'); | ||
} | ||
HotPush.prototype._verifyVersions = function() { | ||
if (this.localVersion.timestamp !== this.remoteVersion.timestamp) { | ||
console.log('Not the last version, ' + this.localVersion.timestamp +' !== ' + this.remoteVersion.timestamp); | ||
this.emit('updateFound'); | ||
} else { | ||
console.log('All good, last version running'); | ||
this.emit('noUpdateFound'); | ||
} | ||
@@ -246,0 +234,0 @@ }; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
197
18512
289