Capítulo 58 de 80

Chapter 58: OnlineManager

Core Idea

Since v4, connectivity detection no longer trusts navigator.onLine (unreliable, false negatives on Chromium) — it optimistically starts online: true and only updates from online/offline window events, which trades a small false-positive risk (serviceWorker-backed offline apps that actually work) for far fewer false negatives.

Key Concepts

  • setEventListener(callback): replaces the default online/offline event source — receives a setOnline function to call with the new boolean state; used to wire alternative connectivity sources like React Native's NetInfo.
  • subscribe(callback): notified with the current online boolean on every change — returns an unsubscribe.
  • setOnline(online): force the online state directly.
  • isOnline(): synchronously reads the current state.

Code Examples

onlineManager.setEventListener((setOnline) =>
  NetInfo.addEventListener((state) => setOnline(!!state.isConnected))
)
  • What it demonstrates: wiring React Native's NetInfo as the connectivity source in place of the (browser-only) default.

Key Takeaways

  1. Don't try to reintroduce navigator.onLine-based detection — the library moved away from it deliberately due to Chromium false negatives.
  2. Any non-browser runtime (React Native, Electron, embedded WebViews) needs setEventListener wired to its own real connectivity signal.

Connects To

  • Network Mode: this manager's role in the online/offlineFirst/always pause logic.
  • React Native: the concrete NetInfo wiring.