Roundtrips
Installation
npm install ngrx-roundtrips
Description
Roundtrips
is a tiny library that unifies the way we handle remote operations using the Redux pattern. It does that by
introducing a concept of Loadable
that can be, well, loaded, and action/query roundtrip
.
Loadable
is a combination of data, and it's status.
;
Such definition enables you to know whether you've loaded the last value or not (to remove the ambiguity if the result
of your asynchronous operation was undefined
), whether a value is currently loading (to show the spinner and prevent
duplicate requests should you want to), and the last received response.
Roundtrip
is a collection of Redux actions related to a single asynchronous operation
Asynchronous operations divide into actions and queries. Actions do not return any result apart from whether it has
completed successfully, but queries do. Regardless of whether it's a query or an action, each roundtrip has three
Redux actions: the initiator action (that starts the action/query), the success action (suffixed with " (Success)"
),
and the error action (suffixed with " (Error)"
).
Actions roundtrip:
You create an action roundtrip with createActionRoundtrip
function, specifying the type of arguments (if any), and
NgRx action name. In return, you receive a group of Redux action creators that looks like this:
; ;;
Using action roundtrip looks like this:
/** Create action definitions grouped into a single object */; /** In component or a service */this.store.dispatchcreatePost.request; /** In effect */onCreatePost$ = createActionRoundtripEffect this.actions$, // actions observable injected into Effect classes createPost, // action roundtrip to handle this.blogService.createPostargs // Angular service that does a HTTP call and returns Observable;
Query roundtrip:
You create a query roundtrip with createQueryRoundtrip
function, specifying the type of arguments (if any), type of
the result and NgRx action name. In return, you receive a group of Redux action creators that looks like this:
; ;;
Using query roundtrip looks like this:
/** Create action definitions grouped into a single object */; /** In component or a service */this.store.dispatchloadPosts.request; /** In reducer */; /** In effect */onLoadPosts$ = createQueryRoundtripEffect this.actions$, // actions observable injected into Effect classes loadPosts, // action roundtrip to handle this.blogService.loadPostsargs // Angular service that does a HTTP call and returns Observable<Post[]>;