包详细信息

ts-action-operators

cartant51.7kMIT9.1.2

TypeScript action operators for NgRx and redux-observable

action, ngrx, redux, redux-observable

自述文件

ts-action-operators

GitHub License NPM version Build status Greenkeeper badge

What is it?

The ts-action-operators package contains RxJS operators for action observables.

Why might you need it?

I created the ts-action package because I wanted a mechanism for declaring and consuming actions that involved writing as little boilerplate as possible. And I created this package so that I could apply the TypeScript narrowing mechanisms in ts-action to the composition of NgRx effects and redux-observable epics.

If you, too, want less cruft in your effects or epics, you might find this package useful.

For an in-depth look at TypeScript, Redux and ts-action, have a look at: How to Reduce Action Boilerplate.

Install

Install the package using npm:

npm install ts-action-operators --save

Usage

The package includes operators for filtering and narrowing actions and for selecting strongly typed payloads. The operators can be used in NgRx effects or redux-observable epics, like this:

import { ofType, toPayload } from "ts-action-operators";
const epic = actions => actions.pipe(
  ofType(foo),
  toPayload(),
  tap(payload => console.log(payload.foo)),
  ...
);

Using pipe is recommended; however, if you use a version of RxJS that does not include pipe, you can use let instead:

import { ofType, toPayload } from "ts-action-operators";
import "rxjs/add/operator/let";
const epic = actions => actions
  .let(ofType(foo))
  .let(toPayload())
  .do(payload => console.log(payload.foo))
  ...

API

act

The act operator is a convenience operator that facilitates the mapping of an input action to an output action with as little boilerplate as possible and with some sensible defaults. It also ensures that errors are handled and that catchError is called in the correct location.

act can be passed a project function and error selector, like this:

.pipe(
  ofType(thingRequested),
  act(
    ({ id }) => things.get(id).pipe(
      map(thing => thingFulfilled(thing))
    ),
    (error, { id }) => thingRejected(id, error)
  )
)

Which is equivalent to:

.pipe(
  ofType(thingRequested),
  concatMap(
    ({ id }) => things.get(id).pipe(
      map(thing => thingFulfilled(thing)),
      catchError(error => thingRejected(id, error))
    )
  )
)

act can also be passed a config object that includes optional complete, operator and unsubscribe properties:

.pipe(
  ofType(thingRequested),
  act({
    ({ id }) => things.get(id).pipe(
      map(thing => thingFulfilled(thing))
    ),
    error: (error, { id }) => thingRejected(id, error),
    unsubscribe: (_ , { id }) => thingCancelled(id),
    operator: switchMap
  })
)

The unsubscribe callback is called if the observable returned from project is unsubscribed before a complete or error notification is emitted. The complete and unsubscribe callbacks are passed the number of actions emitted by the observable returned from project and the input action.

ofType

The ofType operator can be passed ts-action-declared action creators. The operator will remove unwanted actions from the observable stream.

If only a single action creator is specified, the action's type will be narrowed. For example:

.pipe(
  ofType(foo),
  tap(action => {
    // Here, TypeScript has narrowed the type, so the action is strongly typed
    // and individual properties can be accessed in a type-safe manner.
  })
)

If multiple action creators are specified - in an array literal - the action's type will be narrowed to a union:

.pipe(
  ofType([foo, bar]),
  tap(action => {
    // Here, the action has been narrowed to either a FOO or a BAR action.
    // Common properties will be accessible, other will require further narrowing.
    if (isType(action, foo)) {
      // Here, the action has been narrowed to a FOO action.
    } else if (isType(action, bar)) {
      // Here, the action has been narrowed to a BAR action.
    }
  })
)

toPayload

The toPayload operator takes no parameters. It can be applied to an obserable stream that emits narrowed actions that contain payloads. For example:

.pipe(
  ofType(foo),
  toPayload()
  tap(payload => {
    // Here, TypeScript has narrowed the type, so the payload is strongly typed
    // and individual properties can be accessed in a type-safe manner.
  })
)

更新日志

9.1.2 (2021-05-08)

Changes

  • Upgrade to RxJS version 7 and widen peer dependency range for rxjs. (2aba46a)

9.1.1 (2019-10-26)

Changes

  • Widen peer dependency range for ts-action.

9.1.0 (2019-05-06)

Features

  • Add act convenience operator. (b33eb9b)

9.0.1 (2019-04-19)

Changes

9.0.0 (2019-04-06)

Breaking Changes

  • Expressing multiple actions using array or object literals is no longer supported. Pass multiple actions as separate arguments instead.

    That is, instead of this:

      ofType({ foo, bar }); // or ofType([foo, bar]);

    Do this:

      ofType(foo, bar);

8.1.0 (2019-04-04)

Features

  • Support using array literals to represent mulitple action types. (f2264cc)

Changes

  • Deprecate using object literals to represent multiple action types.

8.0.0 (2018-11-03)

Breaking Changes

  • Upgrade to ts-action version 8.

7.0.1 (2018-09-29)

Changes

  • Update dependencies to allow for multiple versions of ts-action. (1f20efa)

7.0.0 (2018-04-25)

Breaking Changes

  • Upgrade to RxJS version 6.

6.0.0 (2018-02-03)

Breaking Changes

  • ofType: To support matching an arbitrary number of types, ofType can now be passed either a single action creator or an object literal of action creators, so calls like ofType(action, Foo, Bar) should be replaced with calls like ofType(action, { Foo, Bar }). Calls that specify a single action creator - like ofType(action, Foo) - do not need to be changed. (f4b3d7b)

5.0.1 (2018-02-03)

Changes

  • Allow a version 5.0.0 ts-action peer.

5.0.0 (2018-02-02)

Breaking Changes

  • Removed the prototype-patching operators; use pipe or let instead. Patching Observable.prototype with ofType is pointless, as ofType will be on the prototype of the framework's actions observable.

4.0.1 (2018-01-31)

Fixes

  • Use the correct semver for the ts-action peer dependency.

4.0.0 (2018-01-31)

Breaking Changes

  • Use ts-action 4.0.0.
  • The package is now distributed with CommonJS, ES5 and ES2015 files (see the Angular Package Format). The ES5 and ES2015 files use ES modules. The entry points in the package.json are:
    • main - CommonJS
    • module - ES5 with ES modules (in the esm5 directory)
    • es2015 - ES2015 (in the esm2015 directory)

3.2.0 (2018-01-29)

Features

  • ofType: Support narrowing when passing multiple action creators. (e862519)

3.1.1 (2018-01-27)

Fixes

  • Fix types filename in package.json.

3.1.0 (2017-11-10)

Changes

  • Use ts-action 3.1.0.

3.0.1 (2017-11-10)

Changes

  • The distribution now includes .d.ts files rather than .ts files. (b9ca7ac)

3.0.0 (2017-11-09)

Fixes

ofType: Enforce the passing of at least one action creator. (d9753aa)

Breaking Changes

  • Use ts-action 3.0.0.

2.0.2 (2017-11-07)

Changes

  • Remove tslib.

2.0.1 (2017-11-07)

Fixes

  • Remove redundant type parameters from ofType signatures. (a5a8a6e)

2.0.0 (2017-11-06)

Breaking Changes

  • Use ts-action 2.0.0.