🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

@ngbites/reactive-enum

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ngbites/reactive-enum

A tool to automatically generate a typed reactive enum with Rx

0.0.2
latest
Source
npm
Version published
Weekly downloads
1
-75%
Maintainers
1
Weekly downloads
 
Created
Source

Reactive Enum

A tool to automatically generate a typed reactive enum with Rx

Not yet production ready.

Install

npm i @ngbites/reactive-enum

Usage

This utility is particularly useful with framework such as Angular to automatically generate a set of reactive streams, so that you can use them in your template effortlessly.

This basically replaces using a BehaviorSubject property followed by auxiliary methods such as:

import { BehaviorSubject } from 'rxjs';

class MyComponent {
  private readonly status$ = new BehaviorSubject<Status>(Status.Initial);
  
  private readonly initial$ = this.status$.pipe(map(status => status === 
    Status.Initial));
  
  private readonly pending$ = this.status$.pipe(map(status => status ===
    Status.Pending));
}

Basic Usage

// declare an enum
enum Status {
  Initial,
  Pending,
  Success,
  Error
}

// pass your enum to "reactiveEnum"
const status = reactiveEnum(Status);

// "status" has now autmatically generated a method for each value of the enum
status.initial$.subscribe(); // false ... // true
status.pending$.subscribe(); // false
status.success$.subscribe(); // false
status.error$.subscribe(); // false

status.set(Status.Initial);

Passing an initial value

const status = reactiveEnum(Status, {
  initialValue: Status.Initial,
});

status.value$.subscribe(console.log); // Status.Initial

Updating the value

const status = reactiveEnum(Status);

status.value$.subscribe(console.log); // Status.Success

status.set(Status.Success);

Resetting to the original value

const status = reactiveEnum(Status, {
  initialValue: Status.Initial
});

status.value$.subscribe(console.log); 
// 1. Status.Initial
// 2. Status.Success
// 3. Status.Initial

status.set(Status.Success);
status.reset();

FAQs

Package last updated on 14 Jun 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts