connect Method

Connects the state to an external data source DataSource, which provides the data that the state will manage and emit. By connecting to a data source, the state can synchronize with external data, ensuring it remains consistent with the source. This is useful in scenarios where the state needs to reflect or react to data from an external provider.

Once connected, the state automatically updates from the data source whenever the source changes, and conversely, updates the data source whenever the state value is changed. This bidirectional synchronization ensures that both the state and the data source remain in sync

@param source: DataSource<T>
The external data source to connect to the state

@returns this
The instance of the current state, allowing for method chaining

API

connect(source: DataSource<T>): this;

Example

import {state, localStoragePart} from '@bitfiber/rx';
 
const theme = state<'dark' | 'light'>('dark', s => s
  // Connects the state with the local storage data stored under the key 'theme'.
  // Now, if you change the state, local storage will also be updated.
  // Conversely, if the local storage changes, the state will be updated.
  // Ensures two-way synchronization between the state and the 'theme' data in local storage
  .connect(localStoragePart('theme')));