Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

This TypeScript library provides factories for creating fully typed FSA-compliant Actions, reducers and RxJS utility operators.

License

NotificationsYou must be signed in to change notification settings

patrimart/rx-fsa

Repository files navigation

This TypeScript library provides:

  • Factories for creating fully-typed FSA-compliant Actions.
  • Composable reducers and Action filters.
  • RxJS operators for easier async Effects set-up.
  • @ngrx/store lazy MemoizedSelector
exporttypeMeta=Readonly<object>&{readonly[key:string]:any};exportinterfaceAction<P,MextendsMeta=Meta>{readonlytype:string;readonlypayload:P;readonlymeta:M;readonlyerror:boolean;}

Installation

npm i rx-fsa

/ngrx and/rxjs libs are compatible withAngular 6 andRxJS 6.

Usage

Basic Empty and Syncronous Actions

import{actionCreatorFactory}from"rx-fsa/actions";constcounterActionFactory=actionCreatorFactory("COUNTER");constresetCounterFactory=counterActionFactory("RESET");constresetActionAction=resetCounterFactory(undefined);constincrementCounterFactory=counterActionFactory<number>("INCREMENT");constincrementOneAction=incrementCounterFactory(1);constdecrementCounterFactory=counterActionFactory<number>("INCREMENT");constdecrementAction=(count:number)=>decrementCounterFactory(count);

Async Action Creators

interfaceState{readonlycount?:number;}constsaveCountFactory=counterActionFactory.async<State,void,Error>("SAVE");constsaveCountStartedAction=saveCountFactory.started({count:100});constsaveCountDoneAction=saveCountFactory.done({result:100,params:{count:100}});constsaveCountFailedAction=saveCountFactory.failed({error:newError("File not saved."),params:{count:100}});

Reducers

import{caseFn,Re,reducerDefaultFn}from"rx-fsa/reducers";constINIT_STATE:State={count:0};constresetHandler:Re<State,void>=state=>INIT_STATE;constincrementHandler:Re<State,number>=(state,add)=>({count:state.count+add,});constdecrementHandler:Re<State,number>=(state,sub)=>({count:state.count-sub,});exportconstreducers=reducerDefaultFn(INIT_STATE)(caseFn(incrementCounterFactory,incrementHandler),caseFn(decrementCounterFactory,decrementHandler),caseFn(resetCounterFactory,resetHandler),);

RxJS Operators

The following operators are provided underrx-fsa/rxjs:

  • filterConcatAsync
  • filterExhaustAsync
  • filterMergeAsync
  • filterSwitchAsync
  • filterConcat
  • filterExhaust
  • filterMerge
  • filterSwitch
  • filterActions

Example with NgRx Effects:

import{filterExhaustAsync}from"rx-fsa/rxjs";@Effect()publicsaveCount$=this.action$.pipe(filterExhaustAsync(saveCountFactory,params=>this.httpSvc.save(params)),);

The above is equivalent to:

@Effect()publicsaveCount$=this.action$.pipe(filter(saveCountFactory.started.match),mergeMap((result:R)=>this.httpSvc.save(params).pipe(map(result=saveCountFactory.done({ result, params})),catchError(error=>saveCountFactory.failed({ error, params}),),);

Lazy Selector

Create aMemoizedSelector that will emit a store slice or, if unavailable, dispatch action(s) to set the store slice and then emit.

// selectors.tsimport{createSelector}from"@ngrx/store";import{composeLazySelector,createLazySelector}from"rx-fsa/ngrx";constselectFeature=(state:AppState)=>state.feature;constselectFeatureCount=createSelector(selectFeature,state=>state.counter);exportconstlazySelectFeatureCount=createLazySelector(selectFeatureCount,loadFeatureAction.started(undefined));exportconstlazySelectCountPlusN=composeLazySelector(lazySelectFeatureCount,lazySelectN)((count,n)=>count+n);
// app.component.tsimport{lazySelectFeatureCount}from'./selectors';@Component({ ...})classMyAppComponent{count:Observable<number>;constructor(privatestore$:Store<AppState>){constselectFeatureCount=lazySelectFeatureCount(this.store$);this.count=this.store$.pipe(selectFeatureCount());}}

API

API Documentation is coming soon. The library is small enough to scour through in GitHub to learn more.

Credits

This library was inspired by:

About

This TypeScript library provides factories for creating fully typed FSA-compliant Actions, reducers and RxJS utility operators.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp