LogoLogo
  • Introduction
  • Intro
    • 🚀Getting Started
    • Key Characteristics
    • Fundamentals
    • Motivation
  • Fundamentals
    • 💾Units
      • BoolUnit
      • NumUnit
      • StringUnit
      • DictUnit
      • ListUnit
      • GenericUnit
    • 🤝Systems
      • AsyncSystem
      • Custom AsyncSystem
    • 🤜Action
    • 📦Cluster
  • 🔨Utilities
    • Stream
    • Selection
  • Integrations
    • Angular
    • React
      • useObservable Hook
      • useUnit Hook
  • 📖Guides
    • Configuration
    • Nesting
    • Events
    • Typings
    • Caching
    • Persistence
    • Immutability
    • Freeze and Mute
    • Development Environment
    • General Guidelines
  • More
    • 👀Examples
    • ✍️Articles
Powered by GitBook
On this page
  • Initializing an Action:
  • Accessing the value:
  • Dispatching a value:
  • Creating a stream:
  • Action vs Units vs BehaviorSubject
  • Configuration Options

Was this helpful?

  1. Fundamentals

Action

PreviousCustom AsyncSystemNextCluster

Last updated 4 years ago

Was this helpful?

Actions are meant to represent unique reactive events. Action is an RxJS like construct. Actions don't replay the last value when subscribed to, unlike , and . Actions help trigger customized operations in a reactive fashion.

You can call an Action a simplified form of a Unit as they share the same base API, although configured in a slightly different way.

  • It has a dispatch method, just like a .

  • It extends the RxJS Observable class, hence it's an Observable.

  • By default, It doesn't replay when subscribed to, unlike a Unit.

  • It doesn't require an initial value, yet provides direct access to the last emitted value, just like a Unit.

  • It can only cache one value at a time, unlike a Unit.

  • Unlike Units, it doesn't perform any checks on dispatch.

See for more details.

Initializing an Action:

// create an Action, that accepts number as its value
const multiplyAction = new Action<number>();

Action is an Observable but not a Subject.

multiplyAction instanceof Subject // false 
multiplyAction instanceof Observable // true

Accessing the value:

// access the value
console.log(multiplyAction.value()) // logs undefined

// dynamically access the value
multiplyAction.subscribe(value => {
  console.log(value) // will log future values
  // do something with the value here
});

Dispatching a value:

// dispatch a value
multiplyAction.dispatch(3);

// replay the value
multiplyAction.replay(); // multiplyAction emits 3 again

The above example shows the basic usage of an Action, it's similar to an RxJS Subject with slight differences.

Creating a stream:

The real-life usage would most probably be to create streams using the Action.

// create a NumUnit to apply the multiplyAction on it
const numUnit = new NumUnit({initialValue: 10})


// create a stream using the earlier Action and above Unit
const subscription = multiplyAction.pipe(
  tap(multiplier => {
    // multiply the numUnit's value and update it
    numUnit.dispatch(currValue => currValue * multiplier)
  })
).subscribe();


// or simply use the built-in createStream method
const stream = multiplyAction.createStream(action$ => {
  return action$.pipe(
    tap(multiplier => {
      // multiply the numUnit's value and update it
      numUnit.dispatch(currValue => currValue * multiplier)
    })
  )
})
// stop the multiplication stream
stream.unsubscribe();

// re-start the multiplication stream
stream.resubscribe();

Action vs Units vs BehaviorSubject

Action

Unit

BehaviorSubject

Replays value on subscription (by default)

❌

✅

✅

Can be configured to replay value on subscription

✅

✅

❌

Allows to listen to only future values

(useful when configured to replay on subscription)

✅

✅

❌

Accepts an initial-value

✅

✅

✅

Can cache more than one value

❌

✅

❌

Validates dispatched values

❌

✅

❌

Can re-emit/replay last value manually

✅

✅

❌

✅

✅

❌

Configuration Options

The advantage of using the createStream method is that we get a instance. So we can stop and re-start the multiplication stream.

Can be part of a

The configuration options can be passed at the time of instantiation. All the configuration options are optional. Some options can also be set globally. See for more details.

🤜
Subject
Units
Systems
Clusters
Unit
API reference
Stream
Configuration
Cluster