subscribeIntl

subscribeIntl(listener, scope?) subscribes to live runtime snapshots.

It is useful for UI that must react to locale, loading, or message load state outside components.

ts
1import { subscribeIntl } from '@beforesemicolon/intl'2 3const unsubscribe = subscribeIntl((snapshot) => {4  console.log(snapshot.locale)5  console.log(snapshot.status)6})

Signature

ts
1function subscribeIntl(2  listener: (snapshot: IntlRuntimeSnapshot) => void,3  scope?: IntlRuntime4): () => void

Callback contract

subscribeIntl does two things immediately:

  1. adds the listener
  2. calls it once with the current snapshot

It then calls the listener for all future locale/message/state updates.

Snapshot fields in practice

ts
1const unsubscribe = subscribeIntl((snapshot) => {2  if (snapshot.status === 'loading') {3    showSpinner()4    return5  }6 7  if (snapshot.status === 'error') {8    showWarning(snapshot.error)9    return10  }11 12  if (snapshot.status === 'ready') {13    render(snapshot.locale)14  }15})

Cleanup

Always unsubscribe when the listener is no longer needed.

ts
1const cleanup = subscribeIntl((snapshot) => {2  // component paint function3})4 5window.addEventListener('unload', cleanup)

For low-level component internals, this can replace manual polling for runtime state.

edit this doc