Skip to content

Instantly share code, notes, and snippets.

@falkolab
Last active February 7, 2016 02:58
Show Gist options
  • Select an option

  • Save falkolab/d7462a1cee3e93a4bf3e to your computer and use it in GitHub Desktop.

Select an option

Save falkolab/d7462a1cee3e93a4bf3e to your computer and use it in GitHub Desktop.

Revisions

  1. falkolab revised this gist Jan 29, 2016. 2 changed files with 9 additions and 9 deletions.
    10 changes: 5 additions & 5 deletions index.js
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,10 @@

    var networkMonitor = require('networkMonitor');
    function anyAction() {
    networkMonitor.run(function() {
    require('networkMonitor').run(function online() {
    // cancel monitoring for single action after success online
    require('networkMonitor').cancel(online);
    hideNetworkNotification();
    ...
    // your action code

    // your action code here
    }, function() {
    showNetworkNotification();
    }, $)
    8 changes: 4 additions & 4 deletions networkMonitor.js
    Original file line number Diff line number Diff line change
    @@ -13,11 +13,11 @@ function testOnline(evt) {

    exports.run = function(func, failure, context) {
    if(typeof func !== 'function') throw "Can't run not function object.";
    if(!Ti.Network.online) {
    this.on('online', func, context);
    _.isFunction(failure) && failure();
    } else {
    if(Ti.Network.online) {
    func();
    } else {
    this.on('online', func, context);
    _.isFunction(failure) && failure();
    }
    };

  2. falkolab revised this gist Jan 29, 2016. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions networkMonitor.js
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ var initilaized = false;

    function testOnline(evt) {
    if (evt.online) {
    exports.trigger('online')
    exports.trigger('online');
    }
    }

    @@ -19,24 +19,24 @@ exports.run = function(func, failure, context) {
    } else {
    func();
    }
    }
    };

    exports.cancel = function(func, context) {
    this.off('online', func, context)
    }
    this.off('online', func, context);
    };

    exports.cancelAll = function(context) {
    this.off('online', null, context || null);
    }
    };

    exports.init = function() {
    !initilaized && Ti.Network.addEventListener("change", testOnline);
    }
    };

    exports.cleanup = function() {
    if(initilaized) {
    Ti.Network.removeEventListener('change', testOnline);
    this.off();
    initilaized = false;
    }
    }
    };
  3. falkolab revised this gist Jan 29, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions networkMonitor.js
    Original file line number Diff line number Diff line change
    @@ -21,8 +21,8 @@ exports.run = function(func, failure, context) {
    }
    }

    exports.cancel = function(func) {
    this.off('online', func)
    exports.cancel = function(func, context) {
    this.off('online', func, context)
    }

    exports.cancelAll = function(context) {
  4. falkolab revised this gist Jan 29, 2016. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions networkMonitor.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,7 @@
    // Network Monitor
    // Author: Andrey Tkachenko <[email protected]>
    // Source: https://gist.github.com/falkolab/d7462a1cee3e93a4bf3e

    _.extend(exports, Backbone.Events);
    var initilaized = false;

  5. falkolab revised this gist Nov 27, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions alloy.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    require('networkMonitor').init();
  6. falkolab revised this gist Nov 27, 2015. 2 changed files with 11 additions and 7 deletions.
    4 changes: 2 additions & 2 deletions index.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@

    var networkMonitor = require('networkMonitor');
    function anyAction() {
    networkMonitor.runAction(function() {
    networkMonitor.run(function() {
    hideNetworkNotification();
    ...
    // your action code
    @@ -11,5 +11,5 @@ function anyAction() {
    }

    function cleanup() {
    networkMonitor.cancelActions($);
    networkMonitor.cancelAll($);
    }
    14 changes: 9 additions & 5 deletions networkMonitor.js
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,13 @@
    _.extend(exports, Backbone.Events);
    var initilaized = false;

    function testOnline(evt) {
    if (evt.online) {
    exports.trigger('online')
    }
    }

    exports.runAction = function(func, failure, context) {
    exports.run = function(func, failure, context) {
    if(typeof func !== 'function') throw "Can't run not function object.";
    if(!Ti.Network.online) {
    this.on('online', func, context);
    @@ -16,7 +17,7 @@ exports.runAction = function(func, failure, context) {
    }
    }

    exports.cancelAction = function(func) {
    exports.cancel = function(func) {
    this.off('online', func)
    }

    @@ -25,10 +26,13 @@ exports.cancelAll = function(context) {
    }

    exports.init = function() {
    Ti.Network.addEventListener("change", testOnline);
    !initilaized && Ti.Network.addEventListener("change", testOnline);
    }

    exports.cleanup = function() {
    Ti.Network.removeEventListener('change', testOnline);
    this.off()
    if(initilaized) {
    Ti.Network.removeEventListener('change', testOnline);
    this.off();
    initilaized = false;
    }
    }
  7. falkolab created this gist Nov 27, 2015.
    15 changes: 15 additions & 0 deletions index.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@

    var networkMonitor = require('networkMonitor');
    function anyAction() {
    networkMonitor.runAction(function() {
    hideNetworkNotification();
    ...
    // your action code
    }, function() {
    showNetworkNotification();
    }, $)
    }

    function cleanup() {
    networkMonitor.cancelActions($);
    }
    34 changes: 34 additions & 0 deletions networkMonitor.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    _.extend(exports, Backbone.Events);

    function testOnline(evt) {
    if (evt.online) {
    exports.trigger('online')
    }
    }

    exports.runAction = function(func, failure, context) {
    if(typeof func !== 'function') throw "Can't run not function object.";
    if(!Ti.Network.online) {
    this.on('online', func, context);
    _.isFunction(failure) && failure();
    } else {
    func();
    }
    }

    exports.cancelAction = function(func) {
    this.off('online', func)
    }

    exports.cancelAll = function(context) {
    this.off('online', null, context || null);
    }

    exports.init = function() {
    Ti.Network.addEventListener("change", testOnline);
    }

    exports.cleanup = function() {
    Ti.Network.removeEventListener('change', testOnline);
    this.off()
    }