UNPKG

3.26 kBJavaScriptView Raw
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 *
19*/
20
21var exec = require('cordova/exec'),
22 cordova = require('cordova'),
23 channel = require('cordova/channel'),
24 utils = require('cordova/utils');
25
26// Link the onLine property with the Cordova-supplied network info.
27// This works because we clobber the navigator object with our own
28// object in bootstrap.js.
29// Browser platform do not need to define this property, because
30// it is already supported by modern browsers
31if (cordova.platformId !== 'browser' && typeof navigator != 'undefined') {
32 utils.defineGetter(navigator, 'onLine', function() {
33 return this.connection.type != 'none';
34 });
35}
36
37function NetworkConnection() {
38 this.type = 'unknown';
39}
40
41/**
42 * Get connection info
43 *
44 * @param {Function} successCallback The function to call when the Connection data is available
45 * @param {Function} errorCallback The function to call when there is an error getting the Connection data. (OPTIONAL)
46 */
47NetworkConnection.prototype.getInfo = function(successCallback, errorCallback) {
48 exec(successCallback, errorCallback, "NetworkStatus", "getConnectionInfo", []);
49};
50
51var me = new NetworkConnection();
52var timerId = null;
53var timeout = 500;
54
55channel.createSticky('onCordovaConnectionReady');
56channel.waitForInitialization('onCordovaConnectionReady');
57
58channel.onCordovaReady.subscribe(function() {
59 me.getInfo(function(info) {
60 me.type = info;
61 if (info === "none") {
62 // set a timer if still offline at the end of timer send the offline event
63 timerId = setTimeout(function(){
64 cordova.fireDocumentEvent("offline");
65 timerId = null;
66 }, timeout);
67 } else {
68 // If there is a current offline event pending clear it
69 if (timerId !== null) {
70 clearTimeout(timerId);
71 timerId = null;
72 }
73 cordova.fireDocumentEvent("online");
74 }
75
76 // should only fire this once
77 if (channel.onCordovaConnectionReady.state !== 2) {
78 channel.onCordovaConnectionReady.fire();
79 }
80 },
81 function (e) {
82 // If we can't get the network info we should still tell Cordova
83 // to fire the deviceready event.
84 if (channel.onCordovaConnectionReady.state !== 2) {
85 channel.onCordovaConnectionReady.fire();
86 }
87 console.log("Error initializing Network Connection: " + e);
88 });
89});
90
91module.exports = me;