CB-9542 Browser Proxy not defined correctly

This commit is contained in:
Jesse MacFadyen 2015-08-25 15:23:44 -07:00
parent 6550b937d0
commit cae65dd093
2 changed files with 22 additions and 65 deletions

View File

@ -146,14 +146,14 @@ xmlns:android="http://schemas.android.com/apk/res/android"
<!-- windows8 --> <!-- windows8 -->
<platform name="windows8"> <platform name="windows8">
<js-module src="src/windows/NetworkInfoProxy.js" name="NetworkInfoProxy"> <js-module src="src/windows/NetworkInfoProxy.js" name="NetworkInfoProxy">
<merges target="" /> <runs/>
</js-module> </js-module>
</platform> </platform>
<!-- windows --> <!-- windows -->
<platform name="windows"> <platform name="windows">
<js-module src="src/windows/NetworkInfoProxy.js" name="NetworkInfoProxy"> <js-module src="src/windows/NetworkInfoProxy.js" name="NetworkInfoProxy">
<merges target="" /> <runs/>
</js-module> </js-module>
</platform> </platform>
@ -166,9 +166,8 @@ xmlns:android="http://schemas.android.com/apk/res/android"
<!-- browser --> <!-- browser -->
<platform name="browser"> <platform name="browser">
<js-module src="www/browser/network.js" name="browserNetwork"> <js-module src="www/browser/network.js" name="NetworkInfoProxy">
<clobbers target="navigator.connection" /> <runs/>
<clobbers target="navigator.network.connection" />
</js-module> </js-module>
</platform> </platform>
</plugin> </plugin>

View File

@ -21,72 +21,30 @@
/*global module, require*/ /*global module, require*/
var cordova = require('cordova'), var cordova = require('cordova'),
proxy = require("cordova/exec/proxy"),
Connection = require('./Connection'); Connection = require('./Connection');
var DOCUMENT_EVENTS_CHECK_INTERVAL = 500; // ms var type = navigator.onLine ? Connection.UNKNOWN : Connection.NONE;
// Flag that indicates that ew need to re-fire online/offline events at document level
// (Workaround for Chrome, since it fires such events only for window object)
var NEED_FIRE_DOCUMENT_EVENT_MANUALLY = false;
function NetworkConnection() { // Subscribe to 'native' online/offline events
this.type = Connection.UNKNOWN; function onStatusChange(evt) {
type = navigator.onLine ? Connection.UNKNOWN : Connection.NONE;
// force async
setTimeout(function(){
cordova.fireDocumentEvent(evt.type);
},0);
} }
/** window.addEventListener('online', onStatusChange);
* Get connection info window.addEventListener('offline', onStatusChange);
*
* @param {Function} successCallback The function to call when the Connection data is available
*/
NetworkConnection.prototype.getInfo = function(successCallback) {
successCallback(this.type);
};
Object.defineProperty(NetworkConnection.prototype, 'type', { proxy.add("NetworkStatus", {
get: function () { getConnectionInfo:function(cbSuccess) {
// It is not possible to determine real connection type in browser // force async
// so we always report Connection.UNKNOWN when online setTimeout(function(){
return (window.navigator.onLine === false ? Connection.NONE : Connection.UNKNOWN); cbSuccess(type);
}, },0);
configurable: true, }
enumerable: true
}); });
// This function tries to detect if document online/offline events is being fired
// after corresponding window events, and if not, then fires them manually
// This is workaround for Chrome, which fires only window online/offline events
// and regarding to plugin spec we need these events at document object
var eventRedirectHandler = function (e) {
// NEED_FIRE_DOCUMENT_EVENT_MANUALLY flag is already set,
// just fire corresponding document event and return
if (NEED_FIRE_DOCUMENT_EVENT_MANUALLY) {
cordova.fireDocumentEvent(e.type);
return;
}
// Flag that indicates whether corresponding document even is fired
var documentStateEventFired = false;
var setDocumentStateEventFired = function() {
documentStateEventFired = true;
};
document.addEventListener(e.type, setDocumentStateEventFired);
setTimeout(function () {
// Remove unnecessary listener
document.removeEventListener(e.type, setDocumentStateEventFired);
// if document event hasn't been fired in specified interval (500 ms by default),
// then we're in chrome and need to fire it manually
if (!documentStateEventFired) {
NEED_FIRE_DOCUMENT_EVENT_MANUALLY = true;
cordova.fireDocumentEvent(e.type);
}
}, DOCUMENT_EVENTS_CHECK_INTERVAL);
};
// Subscribe to native online/offline events
window.addEventListener('online', eventRedirectHandler);
window.addEventListener('offline', eventRedirectHandler);
var me = new NetworkConnection();
require("cordova/exec/proxy").add("NetworkStatus", { getConnectionInfo: me.getConnectionInfo });
module.exports = me;