HubSpot / offline

Automatically display online/offline indication to your users
http://github.hubspot.com/offline/docs/welcome
MIT License
8.65k stars 848 forks source link

Get a callback from .check()? #146

Open elemenofi opened 9 years ago

elemenofi commented 9 years ago

I am using offline.js for an HTML5 offline application. It works great but I would like to now if the client has a connection before loading my application.

Offline.js defaults to true until the first ajax call returns. Does Offline.check() support async by returning a promise or accepting a callback?

I'd like to continue my JS thread only once I know if the client is connected.

Dont know if I am being clear enough, thanks for your time!

onyx1337 commented 9 years ago

it will be great if Offline.check() will return promise

elemenofi commented 9 years ago

I'd be glad to implement this feature with some guidance or pointers on where to look at.

onyx1337 commented 8 years ago

So, any updates ? Because right now we use timeout for this purpose

                    Offline.check();
                    setTimeout(function() {
                        if (Offline.state === 'up'){
                              // do smth.
                        }
                    }, 500);
zackbloom commented 8 years ago

Is binding to the confirmed-up event not an option? https://github.com/HubSpot/offline#properties

onyx1337 commented 8 years ago

In my case I think no, because this check need to be in middle of the code and I need to be totally sure in user status. So best solution is to get promise from Offline.check() and then continue checking

zackbloom commented 8 years ago

It seems like you could bind to both confirmed-up and confirmed-down, and then remove the bindings once one of the two is called. How would that be different than a promise?

zackbloom commented 8 years ago

I'm thinking something along the lines of:

var initialCheck = function(cb){
  var upFunc = function(){
    cb(true);
    unbind();
  }
  var downFunc = function(){
    cb(false);
    unbind();
  }
  var unbind = function(){
    Offline.off('confirmed-up', upFunc);
    Offline.off('confirmed-down', downFunc);
  }

  Offline.on('confirmed-up', upFunc);
  Offline.on('confirmed-down', downFunc);

  Offline.check();
}
onyx1337 commented 8 years ago

oh , this looks good, thanks.