npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@bitfiber/rx

v1.0.0

Published

Reactive State and Async Workflow Management Library based on RxJS

Downloads

57

Readme

@bitfiber/rx - Reactive State and Async Workflow Management Library

Release Notes GitHub star chart GitHub fork

@bitfiber/rx is a powerful and flexible library built on top of RxJS, designed to manage reactive state, asynchronous workflows, and events in modern JavaScript applications. It provides a structured approach to handling complex data flows using emitters, states, groups, and stores, allowing seamless integration of various reactive sources like emitters, states, and observables. Emitters and states can be organized into groups and stores, enabling efficient management and lifecycle control of related reactive sources.


Key Components

  1. Emitters
    Emitters are the core units of communication in the library, broadcasting values or events to multiple subscribers. They provide a straightforward mechanism for creating and managing reactive streams, allowing you to easily handle data emissions and manage complex reactive flows. Emitters can integrate with other reactive sources like emitters, states, and observables.

  2. States
    States are the primary data containers within the library, responsible for maintaining and broadcasting data updates to multiple subscribers. Like emitters, states can create and manage reactive streams to broadcast changes efficiently. They are designed to simplify state propagation and synchronization across stores or features, ensuring consistency. States can also integrate seamlessly with other reactive sources like emitters, states, and observables.

  3. Data sources
    Data sources serve as external containers for persistent or external data, which can be synchronized with states. They allow the connection of states to external data sources, ensuring that state data is always up to date and aligned with external systems or storage.

  4. Groups
    Groups are collections of store items, including emitters, states, or even other groups, and are used to implement the functionality of specific features. They help organize related reactive sources into a cohesive structure, ensuring that all items are properly initialized and completed as a unit.

  5. Stores
    Stores are the complete collections of store items and methods that manage the functionality of an entire module or application. They can contain emitters, states, and groups, providing an organized structure for managing related reactive sources, ensuring that all items are properly initialized and completed as a unit.


Key Features

  1. Integration with RxJS
    Since @bitfiber/rx is built on top of RxJS, it integrates seamlessly with the RxJS ecosystem. Emitters and states can easily interact with observables and subjects, and you can leverage all RxJS operators (e.g., map, filter, debounce, etc.) in various methods. The library simplifies handling reactive streams, allowing you to focus on logic without worrying about manual subscription and completion management.

  2. Strict Typing
    The library leverages TypeScript to enforce strict typing, ensuring robust type checking at compile time. This reduces the likelihood of runtime errors and enhances code reliability. TypeScript's powerful type inference also makes it easier to write cleaner, more maintainable code, providing developers with strong guarantees about the structure and behavior of their reactive components.

  3. Tree Shaking
    The modular design of @bitfiber/rx enables tree shaking, allowing developers to optimize bundle sizes by importing only the required functionalities. This eliminates unused code from the final build, leading to smaller, more efficient applications, which is particularly useful for performance-sensitive environments.


Installation

# NPM
npm install @bitfiber/rx

# YARN
yarn add @bitfiber/rx

Contributing

We welcome contributions from the community. Before contributing, please take the time to read our contributing guide to familiarize yourself with our contribution process. This guide can help you understand our expectations and save you time in the long run.


Support

Have questions, encountered problems, or want to request new features? Feel free to start a discussion in our community forum. Your feedback is valuable to us!


Found an Issue or Bug?

If you've found a bug or issue, please report it using GitHub Issues. Your reports help us improve the project for everyone.


Code of Conduct

This project adheres to the Code of Conduct to ensure a welcoming and inclusive community for all participants. By participating, you are expected to uphold this code.


License

This project is released under the Apache 2.0 License.
You can find the full text of the license in the LICENSE file.
Copyright © 2023-2024 Oleksandr Zmanovskyi. All rights reserved.


Table of Contents

Store

Data Source

RxJs Operators


Store


@class Store

Extends AbstractItem, implements the StoreHooks interface, and provides functionality for managing store items such as emitters, states, and groups.

The Store class handles the initialization and completion of these items, providing lifecycle hooks that allow custom logic to be executed before and after key events such as store initialization and completion. This class serves as a base for specific store implementations

@abstract


@method initialize(beforeInit?): this
Initializes the store and all of its items, preparing it for use. Optionally, a beforeInit callback function can be provided, which will be executed before the store is initialized
@param beforeInit?: (store: this) => void - An optional callback function that runs before the store is initialized
@returns this The current instance of the store, allowing for method chaining


@method complete(): void
Completes the store and all of its items, signaling that the store has finished its operations and is now in a completed state. Once the store is completed, no further changes or updates will be made to it or its items

Example:

import {delay, of, switchMap, take} from 'rxjs';
import {pluck} from '@bitfiber/utils';
import {asyncGroup, emitter, state, Store, transmit} from '@bitfiber/rx';

interface ProductsFilters {
  search: string;
  page: number;
}

interface DictItem {
  id: number;
  name: string;
}

interface Product {
  id: number;
  name: string;
  price: number;
}

interface ProductsState {
  products: Product[];
  isLoading: booleang;
}

interface ProductsState {
  dict1: DictItem[];
  dict2: DictItem[];
  dict3: DictItem[];
  products: Product[];
  isLoading: boolean;
}

class ProductsStore extends Store {
  // Provides the start of the first data loading process
  start = emitter<void>();

  // Provides the state of the products filters
  filters = state<ProductsFilters>({search: '', page: 1})
    // Will not emit data immediately upon subscription
    .useLazyEmission();

  // Provides a group of emitters for managing the loading process of `dict1`
  dict1Req = asyncGroup<void, DictItem[], Error>(dict1Req => {
    dict1Req.launch
      // Runs a query when data is received from the `start` emitter
      .receive(this.start)
      // Runs a query when data is received from the `filters` state
      .receive(this.filters)
      // Performs the effect each time new data is received
      .effect(
        switchMap((_, index) => of(
          [
            {id: 1, name: 'd1a'},
            {id: 2, name: 'd1b'},
            ...(index === 1 ? [{id: 3, name: 'd1c'}] : [])
          ],
          // 'transmit' operator takes either data or an error and transmits it to the `success`
          // or `fail` emitter of the group, respectively
        ).pipe(delay(30), transmit(dict1Req))),
      );
  }, []);

  // Provides a group of emitters for managing the loading process of `dict2`
  dict2Req = asyncGroup<void, DictItem[], Error>(dict2Req => {
    dict2Req.launch
      // Runs a query when data is received from the `start` emitter
      .receive(this.start)
      // Runs a query when data is received from the `filters` state
      .receive(this.filters)
      // Performs the effect each time new data is received
      .effect(
        switchMap((_, index) => of(
          [
            {id: 1, name: 'd2a'},
            {id: 2, name: 'd2b'},
            ...(index === 1 ? [{id: 3, name: 'd2c'}] : [])
          ],
          // 'transmit' operator takes either data or an error and transmits it to the `success`
          // or `fail` emitter of the group, respectively
        ).pipe(delay(30), transmit(dict2Req))),
      );
  }, []);

  // Provides a group of emitters for managing the loading process of `dict3`
  dict3Req = asyncGroup<[string[], string[]], DictItem[], Error>(dict3Req => {
    dict3Req.launch
      // Runs a query when data is received from the `dict1Req` group and `dict2Req` group
      .zip(this.dict1Req.success, this.dict2Req.success, (dict1, dict2) => {
        return [pluck(dict1, 'id'), pluck(dict2, 'id')];
      })
      // Performs the effect each time new data is received
      .effect(
        switchMap((_, index) => of(
          [
            {id: 1, name: 'd3a'},
            {id: 2, name: 'd3b'},
            ...(index === 1 ? [{id: 3, name: 'd3c'}] : [])
          ],
          // 'transmit' operator takes either data or an error and transmits it to the `success`
          // or `fail` emitter of the group, respectively
        ).pipe(delay(30), transmit(dict3Req))),
      );
  }, []);

  // Provides a group of emitters for managing the loading process of `products`
  productsReq = asyncGroup<ProductsFilters, Product[], Error>((productsReq, {launch}) => {
    launch
      // Runs a query when data is received from the `dict3Req` group
      .receive(this.dict3Req.success, () => this.filters())
      // Performs the effect each time new data is received
      .effect(
        switchMap((_, index) => of(
          [
            {id: 1, name: 'p1', price: 1},
            {id: 2, name: 'p2', price: 2},
            ...(index === 1 ? [{id: 3, name: 'p3', price: 3}] : [])
          ],
          // 'transmit' operator takes either data or an error and transmits it to the `success`
          // or `fail` emitter of the group, respectively
        ).pipe(delay(30), transmit(productsReq))),
      );
  }, []);

  // Provides the final state of the store data
  data = state<ProductsState>(
    // Will emit initial data to all subscribers upon subscription
    {dict1: [], dict2: [], dict3: [], products: [], isLoading: false},
    data => data
      // Updates the `isLoading` flag whenever any of the query states change
      .select(
        this.dict1Req.state,
        this.dict2Req.state,
        this.dict3Req.state,
        this.productsReq.state,
        (dict1State, dict2State, dict3State, productsState) => {
          return {
            ...data(),
            isLoading: dict1State.inProgress || dict2State.inProgress
              || dict3State.inProgress || productsState.inProgress,
          };
        },
      )
      // Updates the store data every time all success data is received from the query success emitters
      .zip(
        this.dict1Req.success,
        this.dict2Req.success,
        this.dict3Req.success,
        this.productsReq.success,
        (dict1, dict2, dict3, products) => {
          return {...data(), dict1, dict2, dict3, products};
        },
      ),
  );
}

// Creates a new store for managing products
const productsStore = new ProductsStore();

// Initializes the store and all items within the store
productsStore.initialize();

// Performs a tap callback each time data is updated
productsStore.data.tap(data => {
  if (data.products.length === 3) {
    console.log(productsStore.data());
    /* Expected output:
    {
       dict1: [{id: 1, name: 'd1a'}, {id: 2, name: 'd1b'}, {id: 3, name: 'd1c'}],
       dict2: [{id: 1, name: 'd2a'}, {id: 2, name: 'd2b'}, {id: 3, name: 'd2c'}],
       dict3: [{id: 1, name: 'd3a'}, {id: 2, name: 'd3b'}, {id: 3, name: 'd3c'}],
       products: [{id: 1, name: 'p1', price: 1}, {id: 2, name: 'p2', price: 2}, {id: 3, name: 'p3', price: 3}],
       isLoading: false,
    }
     */
  }
});

// Starts the first data loading process
productsStore.start.emit();

setTimeout(() => {
  // Changes the filters applied to the products
  productsStore.filters.update((state) => ({...state, page: 2}));
}, 150);

setTimeout(() => {
  // Completes the store and all items within the store
  productsStore.complete();
}, 300);

@interface StoreHooks

Represents optional hooks for Store that can be implemented to perform actions before and after the store is initialized and completed

beforeStoreInit?(): void
An optional hook that runs before the store is initialized

afterStoreInit?(): void
An optional hook that runs after the store has been initialized

beforeStoreComplete?(): void
An optional hook that runs before the store is completed

afterStoreComplete?(): void
An optional hook that runs after the store has been completed


@function emitter<T>

Creates and returns a new Emitter instance in a convenient way.

You can optionally provide an onInit callback that will be invoked just before the emitter's initialization, allowing you to perform setup tasks or configure the emitter before it starts emitting values

@template T - The type of data emitted by this emitter

@param onInit?: (emitter: Emitter<T>) => void - An optional callback function that is called with the newly created Emitter instance just before its initialization. This function can be used to set up or configure the emitter

@returns Emitter<T>

Example:

import {emitter, namedGroup} from '@bitfiber/rx';
import {take, filter, switchMap} from 'rxjs';

// Creates an emitter that emits the IDs
const currentId = emitter<number>(e => e
  // Transmits all emitted data to the 'productReq' emitter
  .transmit(productReq));

// The observable of the emitter
const currentId$ = currentId.$;

// Creates an emitter that emits the final ID
const lastId = emitter<number>();

// Creates an emitter that receives transmitted 'currentId' data and performs an effect that calls an API
const productReq = emitter<number>(e => e
  // All streams created by this emitter will filter the data
  .manage(
    filter(id => !!(id % 2)),
  )
  // Performs a tap callback each time the emitter emits new filtered data
  .tap(id => {
    console.log(id);
  })
  // Performs a effect each time the emitter emits new filtered data
  .effect(
    switchMap(id => productsService.get(`api/product${id}`)),
  ));

// Creates an emitter that receives used IDs and logs them through an effect
const log = emitter<string>(e => e
  // Runs an effect when data is received from the `currentId` emitter
  .receive(currentId, id => `A new id ${id} was received`)
  // Runs an effect when data is received from the `lastId` emitter
  .receive(lastId, id => `the last id ${id} was received`)
  // Performs an effect each time the emitter emits newly received logged data
  .effect(
    switchMap(log => logService.post(`api/log`, {log})),
  ));

// Creates an emitter that performs a tap callback each time data is selected
const result1 = emitter<[number, number]>(e => e
  // Runs a tap callback when all data is selected from the `currentId` and `lastId` emitters
  .select(currentId$, lastId, (currentId, lastId) => [currentId, lastId])
  // Performs a tap callback each time the emitter emits new data
  .tap(range => {
    console.log(range);
  }));

// Creates an emitter that performs a tap callback each time data is zipped
const result2 = emitter<[number, number]>(e => e
  // Runs a tap callback when all data is zipped from the `currentId` and `lastId` emitters
  .zip(currentId$, lastId, (currentId, lastId) => [currentId, lastId])
  // Performs a tap callback each time the emitter emits new data
  .tap(range => {
    console.log(range);
  }));

// Groups all emitters and states for mass initialization and completion
const group = namedGroup({currentId, lastId, productReq, log, result1, result2});

// Subscribe to the observable of the emitter
currentId$
  .pipe(take(1))
  .subscribe(id => {
    console.log(id);
  });

// Initializes the group and all items within the group
group.initialize();

// Emits data
currentId.emit(1);
currentId.emit(2);

// Accesses the 'lastId' emitter through the group
group.lastId.emit(3);

// Completes the group and all items within the group
group.complete();

@class Emitter<T>

Extends the AbstractEmitter class and provides functionality to create streams, handle subscriptions, emit values to subscribers, and integrate with other reactive sources such as emitters, states, subjects, observables.

The Emitter class is typically used when you need a straightforward emitter that can broadcast values or events to all its subscribers or other reactive sources, such as emitters, states, subjects

@template T - The type of data emitted by this emitter


@property $: Observable<T> An observable that serves as the source for all emitter streams. It allows subscribers to listen to and react to emitted values or events


@method manage(...operators): this
Defines management operators for all emitter streams. These operators are applied to the streams managed by this emitter, allowing you to modify or control their behavior, such as filtering, mapping, or handling errors, without altering the type of the emitted values

@param ...operators: OperatorFunction<T, T>[] - One or more RxJS operators to apply to the emitter streams

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

import {emitter} from '@bitfiber/rx';
import {delay, filter} from 'rxjs';

const launch = emitter<number>(e => e
  // All streams created by this emitter will delay and filter the data
  .manage(
    filter(id => !!(id % 2)),
    delay(100),
  ));

@method emit(value): this
Emits the specified value to all subscribers currently listening to the emitter. It is used to trigger reactive updates or actions in response to the emitted value

@param value: T - The value to be emitted to all subscribers

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

import {emitter} from '@bitfiber/rx';

const launch = emitter<number>();

// Emits a value to its subscribers, linked reactive sources, and triggers its own taps and effects
launch.emit(7);

@method select<I extends any[]>(...data): this
Combines values from multiple emitters, states, or observables, applies a reducer function to these values, and emits the resulting value to all subscribers of this emitter.

The first emission occurs only after all values have been received from the sources, ensuring that the reducer function operates on a complete set of inputs. Subsequent emissions occur whenever any of the sources emit a new value, triggering the reducer function to recompute the result based on the latest values. Works similarly to the RxJs 'combineLatest' operator

@param ...data: [...EmitterOrObservableTuple<I>, (...values: I) => T] - A spread of emitters, states, or observables, followed by a reducer function. The reducer function takes the latest values from each source as arguments and returns the value to be emitted

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

import {emitter, state} from '@bitfiber/rx';
import {of} from 'rxjs';

type Result = {launchId: number; data: string; count: number};

const launch = emitter<number>();
const data = state<string>(1);
const count$ = of(1);

const result = emitter<Result>(e => e
  // Selects data from all reactive sources and emits the result to its subscribers.
  // Works similarly to the RxJs 'combineLatest' operator
  .select(launch, data, count$, (launchId, data, count) => {
    launchId, data, count
  }));

@method zip<I extends any[]>(...data): this
Combines values from multiple emitters, states, or observables, applies a reducer function to these values, and emits the resulting value to all subscribers of this emitter.

The first emission occurs only after all values have been received from the sources, ensuring that the reducer function operates on a complete set of inputs. Subsequent emissions occur only when all sources emit new values, triggering the reducer function to recompute the result based on the latest values. Works similarly to the RxJs 'zip' operator

@param ...data: [...EmitterOrObservableTuple<I>, (...values: I) => T] - A spread of emitters, states, or observables, followed by a reducer function. The reducer function takes the latest values from each source as arguments and returns the value to be emitted

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

import {emitter, state} from '@bitfiber/rx';
import {of} from 'rxjs';

type Result = {launchId: number; data: string; count: number};

const launch = emitter<number>();
const data = state<string>(1);
const count$ = of(1);

const result = emitter<Result>(e => e
  // Selects data from all reactive sources and emits the result to its subscribers.
  // Works similarly to the RxJs 'zip' operator
  .zip(launch, data, count$, (launchId, data, count) => {
    launchId, data, count
  }));

@method receive(...inputs): this
Receives values from one or more emitters, states, or observables and emits them to all subscribers of this emitter.

This method allows the current emitter to listen to external sources and relay their emitted values to its own subscribers, effectively linking multiple data streams together

@param ...inputs: EmitterOrObservable<T extends void ? any : T>[] - One or more emitters, states, or observables that provide values to be emitted by this emitter

@returns the instance of the current emitter, allowing for method chaining

import {emitter, state} from '@bitfiber/rx';
import {of} from 'rxjs';

const source1 = emitter<number>();
const source2 = state<number>(1);
const source3$ = of(1);

const result = emitter<number>(e => e
  // Receives data from each reactive source separately and emits a value to its subscribers
  // immediately, without waiting for other sources to emit
  .receive(source1, source2, source3$));

@method receive<I>(input, reducer): this
Receives a value from an emitter, state, or observable, applies a reducer function to convert this value to the emitter's type, and emits the result to all subscribers of this emitter.

This method allows the current emitter to listen to external source and relay the transformed emitted value to its own subscribers, effectively linking data streams together

@param input: EmitterOrObservable<I> - an emitter, state or observable that provide values to be emitted by this emitter

@param reducer: (value: I) => T - A function that converts the received value from its original type to the type expected by this emitter, allowing for customization of the emitted value

@returns the instance of the current emitter, allowing for method chaining

import {emitter, state} from '@bitfiber/rx';

const source = state<number>(1);

const result = emitter<string>(e => e
  // Receives data from a reactive source, converts the value, and emits the result to its subscribers
  .receive(source, value => String(value)));

@method transmit(...outputs): this
Transmits values from the current emitter to one or more other emitters, states, or subjects. It enables the propagation of data or events across multiple sources, effectively creating a network of interconnected reactive sources

@param ...outputs: (EmitterOrSubject<T> | EmitterOrSubject<void>)[] - One or more emitters, states, or subjects that will receive the transmitted values from this emitter

@returns the instance of the current emitter, allowing for method chaining

import {emitter, state} from '@bitfiber/rx';
import {Subject} from 'rxjs';

const receiver1 = emitter<number>();
const receiver2 = state<number>(0);
const receiver3 = new Subject<number>();

const source = emitter<number>(e => e
  // Transmits every emitted value to all reactive sources for further processing or handling
  .transmit(receiver1, receiver2, receiver3));

@method transmit<O>(output, reducer): this
Transmits values from the current emitter to a state. By using a reducer function, the emitted values can be transformed or customized to match the expected format of the state

@param output: AbstractState<O> - A state that will receive the transmitted values from this emitter

@param reducer: (value: T, state: O) => O - A function that converts or transforms the emitted value from the emitter type to the type expected by the state

@returns the instance of the current emitter, allowing for method chaining

import {emitter, state} from '@bitfiber/rx';

const receiver = state<number>(0);

const source = emitter<string>(e => e
  // Transmits every emitted value to a state for further processing or handling
  .transmit(receiver, (value, state) => state + Number(value)));

@method transmit<O>(output, reducer): this
Transmits values from the current emitter to another emitter or subject. By using a reducer function, the emitted values can be transformed or customized to match the expected format of the target emitter or subject

@param output: EmitterOrSubject<O> - An emitter or subject that will receive the transmitted values from this emitter

@param reducer: (value: T) => O - A function that converts or transforms the emitted value from the current emitter's type to the type expected by the receiving emitter or subject

@returns the instance of the current emitter, allowing for method chaining

import {emitter} from '@bitfiber/rx';

const receiver = emitter<number>();

const source = emitter<string>(e => e
  // Transmits every emitted value to another emitter for further processing or handling
  .transmit(receiver, value => Number(value)));

@method effect(...operators): this
Creates a new stream with a side effect, similar to the RxJS pipe method.

This method allows you to apply a sequence of RxJS operators to the emitter's stream, performing actions or side effects whenever the emitter emits a value. This can be particularly useful for tasks like logging, debugging, or triggering external operations in response to emitted values

@param ...operators: OperatorFunction<any, any>[] - A sequence of RxJS operators that define the side effects to be applied to the emitted values

@returns the instance of the current emitter, allowing for method chaining

import {emitter} from '@bitfiber/rx';
import {switchMap} from 'rxjs';

const launchProductReq = emitter<number>(e => e
  // Performs a effect each time the emitter emits new value
  .effect(
    switchMap(id => productsService.get(`api/product${id}`)),
  ));

@method tap(observer): this
Creates a new stream with a side effect, similar to the RxJS tap operator.

This method allows you to perform actions or side effects whenever the emitter emits a value, without altering the value itself. It is useful for tasks like logging, debugging, or triggering external operations in response to emitted values

@param observer: Partial<Observer<T>> - a partial observer with lifecycle methods (next, error, complete)

@returns the instance of the current emitter, allowing for method chaining

import {emitter} from '@bitfiber/rx';
import {switchMap} from 'rxjs';

const log = emitter<number>(e => e
  // Performs a tap callback each time the emitter emits new data
  .tap({
    next: id => console.log(id),
    error: error => console.log(error),
  }));

@method tap(next): this
Creates a new stream with a side effect, similar to the RxJS tap operator.

This method allows you to perform actions or side effects whenever the emitter emits a value, without altering the value itself. It is useful for tasks like logging, debugging, or triggering external operations in response to emitted values

@param next: (value: T) => void - a function that takes the emitted value and performs a side effect

@returns the instance of the current emitter, allowing for method chaining

import {emitter} from '@bitfiber/rx';
import {switchMap} from 'rxjs';

const log = emitter<number>(e => e
  // Performs a tap callback each time the emitter emits new data
  .tap(id => console.log(id)));

@function state<T>

Creates an instance that combines the functionality of both the State class and the StateGetter function, initialized with the provided initialValue. Optionally, you can provide an onInit callback function, which is called just before the initialization process, allowing you to perform setup tasks or configure the state before it starts emitting values

@template T - The type of the state value

@param initialValue: T - The initial value of the state

@param onInit?: (state: State<T> & StateGetter<T>) => void - An optional callback function that is executed just before the initialization of the state, allowing you to perform setup tasks or configure the state before it starts emitting values

@returns a new State instance that also acts as the StateGetter function to get the current state value

Example:

import {state, namedGroup} from '@bitfiber/rx';
import {take, filter, switchMap} from 'rxjs';

// Creates a state that stores and emits the IDs
const currentId = state<number>(1, s => s
  // Uses a custom function for comparing values. By default, the 'equals' function is used
  .compareBy((a, b) => a === b)
  // Transmits all emitted data to the 'productReq' emitter
  .transmit(productReq));

// The observable of the state
const currentId$ = currentId.$;

// Creates a state that stores and emits the final ID
const lastId = state<number | null>(null, s => s
  // Uses '===' for comparing values. By default, the 'equals' function is used
  .compareBy('strict')
  // Forces the state not to emit a value at the time of subscription
  .useLazyEmission());

// Creates a state that receives transmitted 'currentId' data and performs an effect that calls an API
const productReq = state<number>(0, s => s
  // All streams created by this state will filter the data
  .manage(
    filter(id => !!(id % 2)),
  )
  // Performs a tap callback each time the state emits new filtered data
  .tap(id => {
    console.log(id);
  })
  // Performs a effect each time the state emits new filtered data
  .effect(
    switchMap(id => productsService.get(`api/product${id}`)),
  ));

// Creates a state that receives used IDs and logs them through an effect
const log = state<string>(0, s => s
  // Runs an effect when data is received from the `currentId` state
  .receive(currentId, id => `A new id ${id} was received`)
  // Runs an effect when data is received from the `lastId` state
  .receive(lastId, id => `the last id ${id} was received`)
  // Performs an effect each time the state emits newly received logged data
  .effect(
    switchMap(log => logService.post(`api/log`, {log})),
  ));

// Creates a state that performs a tap callback each time data is selected
const result1 = state<[number, number]>([0, 0], s => s
  // Runs a tap callback when all data is selected from the `currentId` and `lastId` states
  .select(currentId$, lastId, (currentId, lastId) => [currentId, lastId])
  // Performs a tap callback each time the state emits new data
  .tap(range => {
    console.log(range);
  }));

// Creates a state that performs a tap callback each time data is selected
const result2 = state<[number, number]>([0, 0], s => s
  // Runs a tap callback when all data is selected from the `currentId` and `lastId` states
  .zip(currentId$, lastId, (currentId, lastId) => [currentId, lastId])
  // Performs a tap callback each time the state emits new data
  .tap(range => {
    console.log(range);
  }));

// Groups all emitters and states for mass initialization and completion
const group = namedGroup({currentId, lastId, productReq, log, result1, result2});

// Initializes the group and all items within the group
group.initialize();

// Subscribes to the observable of the state
currentId$
  .pipe(take(1))
  .subscribe(id => console.log(id));

// Sets the state value and emits the new value to subscribers
currentId.set(2);

// Updates the state value and emits the new value to subscribers
currentId.update(state => state + 1);

// Accesses the 'lastId' state through the group and sets data
group.lastId.set(4);

// Gets the current state value in two ways
const id1 = currentId();
const id2 = currentId.get();

// Completes the group and all items within the group
group.complete();

@class State<T>

Represents a concrete state in a reactive store, extending the functionality of AbstractState. This class encapsulates the logic for updating, resetting and maintaining a state, reacting to changes, and notifying subscribers whenever the state is updated. It can also be connected to external data sources to synchronize its value with external data, ensuring consistency across different parts of an application

@template T - The type of data managed and emitted by the state


@property $: Observable<T> An observable that serves as the source for all state streams. It allows subscribers to reactively observe changes or updates to the state, allowing them to respond dynamically as new values are emitted


@method manage(...operators): this
Defines management operators for all state streams. These operators are applied to the streams managed by this state, allowing you to modify or control their behavior, such as filtering, mapping, or handling errors, without altering the type of the emitted values

@param ...operators: OperatorFunction<T, T>[] - One or more RxJS operators to apply to the state streams

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

import {state} from '@bitfiber/rx';
import {delay, filter} from 'rxjs';

const data = state<number>(0, s => s
  // All streams created by this state will delay and filter the data
  .manage(
    filter(id => !!(id % 2)),
    delay(100),
  ));

@method get(): T
Returns the current value of the state. This method is useful for accessing the state at any point in time, allowing other store items or consumers to retrieve the latest value

import {state} from '@bitfiber/rx';

const data = state<number>(10);

// Returns the current value
data.get(); // Output: 10

@method set(value): this
Updates the state to the provided value immediately, but the emission of this new value to subscribers will be performed asynchronously. This means that if multiple synchronous updates are made in quick succession, only the last update will be emitted, optimizing the emission process to prevent unnecessary updates

@param value: T - The new value to set as the current state

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

import {state} from '@bitfiber/rx';

const data = state<number>(0);

// Sets a new state value and emits the updated state to its subscribers
data.set(7);

@method update(updater): this
Updates the current state using an updater function that takes the current state as its argument and returns the new state. The state is updated immediately, but the emission of this new value to subscribers will occur asynchronously. This means that if multiple synchronous updates are made in quick succession, only the last update will be emitted, optimizing the emission process to prevent unnecessary updates

@param updater: (state: T) => T - A function that takes the current state as its argument and returns the new state

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

import {state} from '@bitfiber/rx';

const data = state<number>(0);

// Updates the current state and emits the updated state to its subscribers
data.update(state => state + 1);

@method mutate(updater: (state: T) => void): this
Mutates the current state using an updater function that takes the current state as its argument and mutates it directly. After the mutation, the new state is immediately emitted to subscribers

@param updater: (state: T) => T - A function that takes the current state as its argument and mutates it in place

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

import {state} from '@bitfiber/rx';

const data = state<{count: number}>({count: 0});

// Mutates the current state and emits the mutated state to its subscribers
data.mutate(state => {
  ++state.count;
});

@method reset(): this
Resets the state to its original value that was set during initialization. This is useful for reverting the state back to its starting condition, discarding any changes that have occurred since the state was first established

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

import {state} from '@bitfiber/rx';

const data = state<number>(10);

data.set(20);

// Resets the current state to initial value '10'
data.reset();

@method compareBy(comparison): this
Sets a custom comparison strategy that will be used to determine if the state has changed. This comparison can be one of the predefined comparison types ('equals' or 'strict') or a custom comparison function

@param comparison: Comparison - The comparison method to use for evaluating state changes

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

import {state, changeDefaultComparison} from '@bitfiber/rx';

const data1 = state<number>(10, s => s
  // Uses the `equals` function from the package '@bitfiber/utils' for comparing values
  // this comparison is by default
  .compareBy('equals'));

const data2 = state<number>(10, s => s
  // Uses '===' for comparing values
  .compareBy('strict'));

const data3 = state<number | string>(10, s => s
  // Uses a custom function for comparing values
  .compareBy((a, b) => Number(a) === Number(b)));

// By default, uses the `equals` function for comparing values.
// To set a different comparison type for all states by default, use this function
changeDefaultComparison('strict');

@method connect(source): this
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

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')));

@method useLazyEmission(): this
Enables lazy emission for the state, meaning that the state will defer emitting its initial value to subscribers until an explicit trigger occurs. This can be useful in scenarios where you want more control over when the state emits its value, rather than emitting immediately

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

import {state} from '@bitfiber/rx';

const data = state<number>(0, s => s
  // Forces the state not to emit a value at the time of subscription
  .useLazyEmission());

@method useLazyEmissionOnce(): this
Enables one-time lazy emission for the next created stream.

Once the useLazyEmissionOnce method is called, the state will defer emitting its initial value until an explicit trigger occurs. This lazy emission behavior will apply only once for the next stream that is created. After this initial deferred emission, subsequent streams will emit values immediately as changes occur.

This method can be called multiple times before creating streams, allowing you to control when the lazy emission behavior is applied.

By default, one-time lazy emission is disabled, meaning that streams will emit their initial values immediately upon creation unless this behavior is explicitly overridden

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

import {state} from '@bitfiber/rx';

const data = state<number>(0, s => s
  // Forces the next stream not to emit a value at the time of subscription
  .useLazyEmissionOnce()
  // Will not emit a value at the time of subscription
  .effect()
  // Will emit a value at the time of subscription
  .transmit());

@method select<I extends any[]>(...data): this
Combines values from multiple emitters, states, or observables, applies a reducer function to these values, and emits the resulting value to all subscribers of this state.

The first emission occurs only after all values have been received from the sources, ensuring that the reducer function operates on a complete set of inputs. Subsequent emissions occur whenever any of the sources emit a new value, triggering the reducer function to recompute the result based on the latest values. Works similarly to the RxJs 'combineLatest' operator

@param ...data: [...EmitterOrObservableTuple<I>, (...values: I) => T] - A spread of emitters, states, or observables, followed by a reducer function. The reducer function takes the latest values from each source as arguments and returns the value to be emitted

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

import {emitter, state} from '@bitfiber/rx';
import {of} from 'rxjs';

type Result = {launchId: number; data: string; count: number};

const launch = emitter<number>();
const data = state<string>(1);
const count$ = of(1);

const result = state<Result>({launchId: 0, data: '', count: 0}, s => s
  // Selects data from all reactive sources and emits the result to its subscribers.
  // Works similarly to the RxJs 'combineLatest' operator
  .select(launch, data, count$, (launchId, data, count) => {
    launchId, data, count
  }));

@method zip<I extends any[]>(...data): this
Combines values from multiple emitters, states, or observables, applies a reducer function to these values, and emits the resulting value to all subscribers of this state.

The first emission occurs only after all values have been received from the sources, ensuring that the reducer function operates on a complete set of inputs. Subsequent emissions occur only when all sources emit new values, triggering the reducer function to recompute the result based on the latest values. Works similarly to the RxJs 'zip' operator

@param ...data: [...EmitterOrObservableTuple<I>, (...values: I) => T] - A spread of emitters, states, or observables, followed by a reducer function. The reducer function takes the latest values from each source as arguments and returns the value to be emitted

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

import {emitter, state} from '@bitfiber/rx';
import {of} from 'rxjs';

type Result = {launchId: number; data: string; count: number};

const launch = emitter<number>();
const data = state<string>(1);
const count$ = of(1);

const result = state<Result>({launchId: 0, data: '', count: 0}, s => s
  // Selects data from all reactive sources and emits the result to its subscribers.
  // Works similarly to the RxJs 'zip' operator
  .zip(launch, data, count$, (launchId, data, count) => {
    launchId, data, count
  }));

@method receive(...inputs): this
Receives values from one or more emitters, states, or observables and emits them to all subscribers of this state.

This method allows this state to listen to external sources and relay their emitted values to its own subscribers, effectively linking multiple data streams together

@param ...inputs: EmitterOrObservable<T extends void ? any : T>[] - One or more emitters, states, or observables that provide values to be emitted by this state

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

import {emitter, state} from '@bitfiber/rx';
import {of} from 'rxjs';

const source1 = emitter<number>();
const source2 = state<number>(1);
const source3$ = of(1);

const result = state<number>(0, s => s
  // Receives data from each reactive source separately and emits a value to its subscribers
  // immediately, without waiting for other sources to emit
  .receive(source1, source2, source3$));

@method receive<I>(input, reducer): this
Receives a value from an emitter, state, or observable, applies a reducer function to convert this value to the state's type, and emits the result to all subscribers of this state.

This method allows this state to listen to external source and relay the transformed emitted value to its own subscribers, effectively linking data streams together

@param input: EmitterOrObservable<I> - an emitter, state or observable that provide values to be emitted by this state

@param reducer: (value: I, state: T) => T - A function that converts or transforms the received value from the input type to the type expected by this state. This function takes the value emitted by the input and this state value as parameters, and returns the new state value

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

import {emitter, state} from '@bitfiber/rx';

const source = state<number>(1);

const result = state<string>('', s => s
  // Receives data from a reactive source, converts the value, and emits the result to its subscribers
  .receive(source, value => String(value)));

@method transmit(...outputs): this
Transmits values from the current state to one or more other emitters, states, or subjects. It enables the propagation of data or events across multiple sources, effectively creating a network of interconnected reactive sources

@param ...outputs: (EmitterOrSubject<T> | EmitterOrSubject<void>)[] - One or more emitters, states, or subjects that will receive the transmitted values from this state

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

import {emitter, state} from '@bitfiber/rx';
import {Subject} from 'rxjs';

const receiver1 = emitter<number>();
const receiver2 = state<number>(0);
const receiver3 = new Subject<number>();

const source = state<number>(0, s => s
  // Transmits every emitted value to all reactive sources for further processing or handling
  .transmit(receiver1, receiver2, receiver3));

@method transmit<O>(output, reducer): this
Transmits values from the current state to another state. By using a reducer function, the emitted values can be transformed or customized to match the expected format of another state

@param output: AbstractState<O> - A state that will receive the transmitted values from this state

@param reducer: (value: T, state: O) => O - A function that converts or transforms the emitted value from this state type to the type expected by another state

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

import {emitter, state} from '@bitfiber/rx';

const receiver = state<number>(0);

const source = state<string>(0, s => s
  // Transmits every emitted value to another state for further processing or handling
  .transmit(receiver, (value, state) => state + Number(value)));

@method transmit<O>(output, reducer): this
Transmits values from the current state to another emitter or subject. By using a reducer function, the emitted values can be transformed or customized to match the expected format of the target emitter or subject

@param output: EmitterOrSubject<O> - An emitter or subject that will receive the transmitted values from this state

@param reducer: (value: T) => O - A function that converts or transforms the emitted value from the current state's type to the type expected by the receiving emitter or subject

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

import {emitter, state} from '@bitfiber/rx';

const receiver = emitter<number>();

const source = state<string>(0, s => s
  // Transmits every emitted value to another emitter for further processing or handling
  .transmit(receiver, value => Number(value)));

@method effect(...operators): this
Creates a new stream with a side effect, similar to the RxJS pipe method.

This method allows you to apply a sequence of RxJS operators to the state's stream, performing actions or side effects whenever the state emits a value. This can be particularly useful for tasks like logging, debugging, or triggering external operations in response to emitted values

@param ...operators: OperatorFunction<any, any>[] - A sequence of RxJS operators that define the side effects to be applied to the emitted values

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

import {state} from '@bitfiber/rx';
import {switchMap, of} from 'rxjs';

const openDialog = state<boolean>(false, s => s
  // Performs a effect each time the emitter emits new value
  .effect(
    switchMap(isOpened => !isOpened ? dialog.open() : of(false)),
  ));

@method tap(observer): this
Creates a new stream with a side effect, similar to the RxJS tap operator.

This method allows you to perform actions or side effects whenever the state emits a value, without altering the value itself. It is useful for tasks like logging, debugging, or triggering external operations in response to emitted values

@param observer: Partial<Observer<T>> - a partial observer with lifecycle methods (next, error, complete)

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

import {state} from '@bitfiber/rx';
import {switchMap} from 'rxjs';

const log = state<number>(0, s => s
  // Performs a tap callback each time the state emits new data
  .tap({
    next: id => console.log(id),
    error: error => console.log(error),
  }));

@method tap(next): this
Creates a new stream with a side effect, similar to the RxJS tap operator.

This method allows you to perform actions or side effects whenever the state emits a value, without altering the value itself. It is useful for tasks like logging, debugging, or triggering external operations in response to emitted values

@param next: (value: T) => void - a function that takes the emitted value and performs a side effect

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

import {state} from '@bitfiber/rx';
import {switchMap} from 'rxjs';

const log = state<number>(0, s => s
  // Performs a tap callback each time the state emits new data
  .tap(id => console.log(id)));

@function changeDefaultComparison

Changes the default comparison method that will be used for all states. This can be one of the predefined comparison types: 'equals' for deep comparison, 'strict' for strict equality, or a custom comparison function

@param comparison: Comparison - The comparison method to be set as the default

import {changeDefaultComparison} from '@bitfiber/rx';

changeDefaultComparison('strict');

@type Comparison

Represents a comparison operation, which can be a predefined comparison type or a custom function.

The Comparison type allows for different ways to compare two values:

  • 'equals': A deep comparison using the equals function from the package '@bitfiber/utils',
  • 'strict': A strict equality comparison, using strict equality (===),
  • ((a: any, b: any) => boolean): A custom comparison function that takes two arguments and returns a boolean indicating whether the values are considered equal based on the provided logic

@function namedGroup<I extends Index>

Creates a new NamedGroup instance with the store items from the provided index, where each item is accessible by its unique key.

This function also allows for an optional onInit callback, which can be used to perform additional setup or configuration just before the group initialization

@template I - The type of the index used to access the StoreItem instances in the group

@param index: I - An index that contains StoreItem instances and other data, each associated with a unique key

@param onInit?: (group: NamedGroup<StoreIndex<I>>, sameGroup: NamedGroup<StoreIndex<I>>) => void - An optional callback function that is executed just before initialization

@returns NamedGroup<StoreIndex<I>>

Example:

import {state, emitter, namedGroup} from '@bitfiber/rx';

// Groups all emitters and states for mass initialization and completion
const group = namedGroup({launch: emitter<void>(), data: state<number>(0)}, ({launch}) => {
  launch
    // Performs an effect each time the launch emits new data
    .effect(
      switchMap(page => productsService.get(`api/products?page=${page}`)),
    );
});

// Initializes the group and all items within the group
group.initialize();

// Accesses the 'launch' emitter through the group and emits a new data
group.launch.emit(1);

// Completes the group and all items within the group
group.complete();

@class NamedGroup<I extends Index>

Represents a named group that incorporates store items from the provided index and can manage these items, each accessible by a unique key

@template I - Extends Index that contains StoreItem instances and other data, each associated with a unique key


@method initialize(): this
Initiates the group and all its items.

In most cases, this method will be called automatically by a group or store managing the group, so you generally don't need to call it manually unless you have a specific reason to do so

@returns the instance of the current group, allowing for method chaining

Example:

import {state, emitter, namedGroup} from '@bitfiber/rx';

// Groups all emitters and states for mass initialization and completion
const group = namedGroup({launch: emitter<void>(), data: state<number>(0)});

// Initializes the group and all items within the group
group.initialize();

@method complete(): void
Completes the group and all its items, signaling to all item subscribers that no more values will be emitted.

Once the group is completed, Its items will no longer emit any values, and any subsequent subscriptions will immediately receive an error.

In most cases, this method will be called automatically by a group or store managing the group, so you generally don't need to call it manually unless you have a specific reason to do so

Example:

import {state, emitter, namedGroup} from '@bitfiber/rx';

// Groups all emitters and states for mass initialization and completion
const group = namedGroup({launch: emitter<void>(), data: state<number>(0)});

// Completes the group and all items within the group
group.complete();

@function asyncGroup<L, S, F>

Creates a new AsyncGroup instance that manages the lifecycle of an asynchronous action, including emitters for launching the action, handling its success, dealing with failures, and maintaining the state of the asynchronous action.

This function also allows for an optional onInit callback, which can be used to perform additional setup or configuration just before the group initialization.

The fallback value is used as a default success value in case the asynchronous action fails, ensuring that the success emitter always returns a value

@template L - The type representing the data for the launch emitter
@template S - The type representing the data for the success emitter
@template F - The type representing the error data for the fail emitter

@param onInit?: (group: AsyncGroup<L, S, F>, sameGroup: AsyncGroup<L, S, F>) => void - An optional callback function executed just before the group initialization

@param fallbackValue?: S - An optional fallback value of type S that will be used as the default success value if the asynchronous action fails

@returns AsyncGroup<L, S, F>

Example:

import {state, namedGroup, asyncGroup} from '@bitfiber/rx';

interface Product {
  id: number;
  name: string;
  price: number;
}

interface ProductsState {
  products: Product[];
  isLoading: booleang;
}

// Provides a group of emitters for managing the products loading process
const productsReq = asyncGroup<number, Product[], Error>((group, {launch, fail, finish}) => {
  group
    // Keeps cached data for 120 seconds, with a maximum entry count of 10
    .useCache(120, 10);

  launch
    // Performs an effect each time the launch emits new data
    .effect(
      switchMap(page => productsService.get(`api/products?page=${page}`)
        // 'transmit' operator takes either data or an error and transmits it to the `success`
        // or `fail` emitter of the group, respectively
        .pipe(transmit(group))),
    );

  fail
    // Performs a tap callback each time the request fails
    .tap(error => console.log(error));

  finish
    // Performs a tap callback each time the request either fails or succeeds
    .tap(() => console.log('Request has been finished'));
}, []);

// Provides the final state of the store data
const data = state<ProductsState>({products: [], isLoading: false}, s => s
  // Receives request success data
  .receive(productsReq.success, (products, state) => ({...state, products}))
  // Receives the request state
  .receive(productsReq.state, ({inProgress}, state) => ({...state, isLoading: inProgress}))
);

// Groups all emitters and states for mass initialization and completion
const group = namedGroup({productsReq, data});

// Initializes the group and all items within the group
group.initialize();

// Starts the products loading process
productsReq.launch.emit(1);

// Completes the group and all items within the group
group.complete();

@class AsyncGroup<L, S, F>

Represents an asynchronous group that manages the lifecycle of an asynchronous action, including emitters for launching the action, handling its success, dealing with failures, and maintaining the state of the asynchronous action.

The AsyncGroup class extends AbstractAsyncGroup and is designed to facilitate the management of asynchronous actions. This structure allows for organized and efficient management of complex asynchronous workflows

@template L - The type representing the data for the launch emitter
@template S - The type representing the data for the success emitter
@template F - The type representing the error data for the fail emitter


@property launch: Emitter<L>
An emitter that triggers the start of an asynchronous action. This emitter takes a payload of type L, which contains the necessary data to initiate the action
@readonly


@property success: Emitter<S>
An emitter that triggers when an asynchronous action completes successfully. This emitter takes a payload of type S, which contains the result or data associated with the successful completion of the action
@readonly


@property fail: Emitter<F>
An emitter that triggers when an asynchronous action fails. This emitter takes a payload of type F, which contains the error information or data related to the failure of the action
@readonly


@property finish: Emitter<void>
An emitter that triggers when the asynchronous action's entire lifecycle is completed, whether it ends in success or failure. This emitter does not carry any payload (void), as it simply serves as a notification that the process is fully complete
@readonly


@property state: State<AsyncData> & StateGetter<AsyncData>
The state that tracks the status of an asynchronous action, including counters for successes and failures, as well as flags indicating whether the action is in progress, has completed successfully, or has failed
@readonly


@method initialize(): this
Initiates the group and all its items.

In most cases, this method will be called automatically by a group or store managing the group, so you generally don't need to call it manually unless you have a specific reason to do so

@returns the instance of