Skip to content

Commit 3e0a874

Browse files
committed
docs: update onlineManager documentation
1 parent 6b3840b commit 3e0a874

File tree

3 files changed

+29
-32
lines changed

3 files changed

+29
-32
lines changed

docs/src/pages/reference/onlineManager.md

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,13 @@ Its available methods are:
1818
`setEventListener` can be used to set a custom event listener:
1919

2020
```js
21+
import NetInfo from '@react-native-community/netinfo'
2122
import { onlineManager } from 'react-query'
2223

23-
onlineManager.setEventListener(handleOnline => {
24-
// Listen to visibillitychange and online
25-
if (typeof window !== 'undefined' && window.addEventListener) {
26-
window.addEventListener('visibilitychange', handleOnline, false)
27-
window.addEventListener('online', handleOnline, false)
28-
}
29-
30-
return () => {
31-
// Be sure to unsubscribe if a new handler is set
32-
window.removeEventListener('visibilitychange', handleOnline)
33-
window.removeEventListener('online', handleOnline)
34-
}
24+
onlineManager.setEventListener(setOnline => {
25+
return NetInfo.addEventListener(state => {
26+
setOnline(state.isConnected)
27+
})
3528
})
3629
```
3730

src/core/focusManager.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,31 @@ class FocusManager extends Subscribable {
1111
}
1212
}
1313

14-
setEventListener(setup: (onFocus: () => void) => () => void): void {
14+
setEventListener(
15+
setup: (onFocus: () => void) => (focused?: boolean) => void
16+
): void {
1517
if (this.removeEventListener) {
1618
this.removeEventListener()
1719
}
18-
this.removeEventListener = setup(() => {
19-
this.onFocus()
20-
})
21-
}
22-
23-
onFocus(): void {
24-
this.listeners.forEach(listener => {
25-
listener()
20+
this.removeEventListener = setup((focused?: boolean) => {
21+
this.setFocused(focused)
2622
})
2723
}
2824

29-
setFocused(focused: boolean | undefined): void {
25+
setFocused(focused?: boolean): void {
3026
this.focused = focused
3127

3228
if (focused) {
3329
this.onFocus()
3430
}
3531
}
3632

33+
onFocus(): void {
34+
this.listeners.forEach(listener => {
35+
listener()
36+
})
37+
}
38+
3739
isFocused(): boolean {
3840
if (typeof this.focused === 'boolean') {
3941
return this.focused

src/core/onlineManager.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,31 @@ class OnlineManager extends Subscribable {
1111
}
1212
}
1313

14-
setEventListener(setup: (onOnline: () => void) => () => void): void {
14+
setEventListener(
15+
setup: (setOnline: () => void) => (online?: boolean) => void
16+
): void {
1517
if (this.removeEventListener) {
1618
this.removeEventListener()
1719
}
18-
this.removeEventListener = setup(() => {
19-
this.onOnline()
20-
})
21-
}
22-
23-
onOnline(): void {
24-
this.listeners.forEach(listener => {
25-
listener()
20+
this.removeEventListener = setup((online?: boolean) => {
21+
this.setOnline(online)
2622
})
2723
}
2824

29-
setOnline(online: boolean | undefined): void {
25+
setOnline(online?: boolean): void {
3026
this.online = online
3127

3228
if (online) {
3329
this.onOnline()
3430
}
3531
}
3632

33+
onOnline(): void {
34+
this.listeners.forEach(listener => {
35+
listener()
36+
})
37+
}
38+
3739
isOnline(): boolean {
3840
if (typeof this.online === 'boolean') {
3941
return this.online

0 commit comments

Comments
 (0)