Skip to content

Commit

Permalink
fix(network): bind listener to document instead of window (#2622)
Browse files Browse the repository at this point in the history
We noticed that in some cases network state listeners are stopped triggering, but `navigator.onLine` still reflects the state correctly. I used the following code to debug and found out an issue with `window` and `document` difference:

```js
window.addEventListener("online", ()=>console.warn('WINDOW ONLINE'), false); // won't be triggered
window.addEventListener("online", ()=>console.warn('WINDOW ONLINE'), false); // won't be triggered
document.addEventListener("online", ()=>console.warn('DOCUMENT ONLINE'), false); // triggered
document.addEventListener("online", ()=>console.warn('DOCUMENT ONLINE'), false); // triggered
```

Also, according to plugin documentation, listeners should be bound to `document`: https://github.com/apache/cordova-plugin-network-information#offline
  • Loading branch information
dyatko authored and ihadeed committed Aug 1, 2018
1 parent 4f26069 commit d10777a
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/@ionic-native/plugins/network/index.ts
Expand Up @@ -89,7 +89,8 @@ export class Network extends IonicNativePlugin {
*/
@Cordova({
eventObservable: true,
event: 'offline'
event: 'offline',
element: document
})
onDisconnect(): Observable<any> {
return;
Expand All @@ -101,7 +102,8 @@ export class Network extends IonicNativePlugin {
*/
@Cordova({
eventObservable: true,
event: 'online'
event: 'online',
element: document
})
onConnect(): Observable<any> {
return;
Expand Down

0 comments on commit d10777a

Please sign in to comment.