MobileControllerBase

MobileControllerBase - Shared base class for all mobile Stimulus controllers

Provides centralized online/offline state management, connection event handling, and utility methods for network requests with retry logic.

Features:

  • Single source of truth for online/offline state (static isOnline property)
  • Automatic connection listener registration/cleanup
  • fetchWithRetry() for network requests with exponential backoff
  • Connection state change notifications via onConnectionStateChanged()
  • Proper event handler cleanup on disconnect

Usage: class MyMobileController extends MobileControllerBase { onConnectionStateChanged(isOnline) { // React to connection changes } }

Constructor

new MobileControllerBase()

Members

online

Get current online status

Methods

bindHandler(name, handler) → {function}

Bind and track an event handler for later cleanup

Parameters:
NameTypeDescription
namestring

Identifier for the handler

handlerfunction

Function to bind

Returns:

Bound handler

Type: 
function

connect()

Connect controller to DOM Registers controller for connection state notifications

disconnect()

Disconnect controller from DOM Unregisters controller and cleans up event handlers

dispatchConnectionEvent(eventName, detail)

Dispatch a custom event with connection status

Parameters:
NameTypeDescription
eventNamestring

Event name to dispatch

detailObject

Additional event detail

(async) fetchWithRetry(url, options, retries, timeout) → {Promise.<Response>}

Fetch with retry logic and exponential backoff

Parameters:
NameTypeDefaultDescription
urlstring

URL to fetch

optionsObject

Fetch options

retriesnumber3

Number of retry attempts (default: 3)

timeoutnumber10000

Request timeout in ms (default: 10000)

Returns:

Fetch response

Type: 
Promise.<Response>

getHandler(name) → {function|undefined}

Get a previously bound handler

Parameters:
NameTypeDescription
namestring

Handler identifier

Returns:

Bound handler or undefined

Type: 
function | undefined

initialize()

Initialize instance Sets up bound handler tracking map

onConnectionStateChanged(isOnline)

Override in subclass to handle connection state changes

Parameters:
NameTypeDescription
isOnlineboolean

Current connection state

(static) initializeConnectionListeners()

Initialize static connection listeners once Sets up window online/offline event handlers

(static) notifyListeners(isOnline)

Notify all registered controllers of connection state change

Parameters:
NameTypeDescription
isOnlineboolean

Current connection state

(static) setOnlineState(isOnline, notify)

Set global online state and optionally notify listeners.

Parameters:
NameTypeDefaultDescription
isOnlineboolean
notifybooleantrue

(static) syncOnlineState()

Sync static isOnline with current navigator.onLine state Called on each controller connect to handle page loads while offline