redux-data-service > BaseService
IService
takes an Object-Oriented approach to following the ducks-modular-redux proposal. This enables us to organize actions
, selectors
, and epics
together with their related reducer
. The goal is to create a cleaner project structure that is hopefully more extensible and DRY.
Children classes should extend createTypes
, createActions
, createSelectors
, createEpics
and createReducers
as needed to add functionality. The output from these methods will be lazy loaded and used to export the types
, actions
, selectors
, epics
and reducer
, respectively, as public readonly fields on the service. As such, when overriding those methods you will likely want to build upon and return what was outputted from the parent method.
The reducers
should map an ActionType
to a "micro" reducer. A single, final reducer
that is exported will fire the "micro" reducer associated to the given action type, in lieu of creating one large hairy switch statement.
IActionTypes
are namespaced to the given service name
provided to the constructor. This is to avoid naming collisions with other IService
s.
abstract:
class: BaseService
implements: IService
BaseService
- IService<
S
>
- createActions
- createEpics
- createReducers
- createSelectors
- createTypes
- getDefaultState
- makeActionCreator
- makeActionType
- getStateObservable
- registerDispatch
- setStateObservable
● internalActions: IActionCreators
Defined in Services/BaseService.ts:45
● internalEpics: IActionEpic[]
Defined in Services/BaseService.ts:44
● internalReducers: IReducers<S
>
Defined in Services/BaseService.ts:43
● internalSelectors: ISelectors
Defined in Services/BaseService.ts:46
● internalTypes: IActionTypes
Defined in Services/BaseService.ts:47
● name: string
Implementation of IService.name
Defined in Services/BaseService.ts:39
● dispatch: function
Defined in Services/BaseService.ts:41
▸(action: IAction): void
Parameters:
Name | Type |
---|---|
action | IAction |
Returns: void
● state$: any
Defined in Services/BaseService.ts:42
getactions(): IActionCreators
Defined in Services/BaseService.ts:205
Returns a map of IActionCreators, which when dispatched to Redux, one or many Reducers or epics may act on that IAction.
Returns: IActionCreators
getepics(): IActionEpic[]
Defined in Services/BaseService.ts:248
Returns an array of RxJS Observeable Epics from redux-observable, which are observers that are always listening for a given ActionType. They are useful for triggering side effects (such as loading data asynchronously) in response to an IAction via chainable, asynchronous "streams".
They will usually emit one or many Actions to pass data into Redux (via a IReducer) or to trigger other Epics. Not only can they be daisy-chained in this manner, RxJS also supports a variety of other common use-cases such as throttling/debouncing and retrying failed promises.
Notes:
- When an IAction is dispatched to Redux, it hits the reducers BEFORE the epics.
- Before you can use something from RxJS (such as an operator), you must import it first.
Returns: IActionEpic[]
getreducer(): IReducer<S
>
Defined in Services/BaseService.ts:170
Returns a single IReducer function which triggers the methods mapped in the internal reducers
object to the given IAction type.
This is the function that is actually injected into and later triggered by Redux.
Returns: IReducer<S
>
getreducers(): IReducers<S
>
Defined in Services/BaseService.ts:156
Return a map of functions which are triggered by the IReducer for a given ActionType. They are used to update the Redux state in response to a given IAction.
Like a reducer, they are given the Redux state
object and the action
that was triggered and should return a new copy of the immutable state. However, these are not individually added to Redux, but rather through the single reducer function returned for this IService.
Returns: IReducers<S
>
getselectors(): ISelectors
Defined in Services/BaseService.ts:224
Returns an object of selectors using Reselect. ISelectors are useful for efficiently filtering data from the Redux state.
ISelectors are composable: a selector may be built from other selectors
ISelectors are memoized: the output from each selector is cached, so future requests will not require a recompute unless its inputs change
Returns: ISelectors
gettypes(): IActionTypes
Defined in Services/BaseService.ts:191
Returns a map of IActionTypes, which are string "constants" that represent the type of an IAction. When an IAction is dispatched to Redux via an IActionCreator, one or many reducers or epics may act on that IAction
Returns: IActionTypes
▸ createActions(): IActionCreators
Defined in Services/BaseService.ts:119
Children classes should extend this method to dispatch new Actions
Returns: IActionCreators
▸ createEpics(): IActionEpic[]
Defined in Services/BaseService.ts:134
Children classes should extend this method to perform new side effects (such as loading data) in response to a given IAction.
Returns: IActionEpic[]
▸ createReducers(): IReducers<S
>
Defined in Services/BaseService.ts:126
Children classes should extend this method to handle new IAction types in the reducer.
Returns: IReducers<S
>
▸ createSelectors(): ISelectors
Defined in Services/BaseService.ts:142
Children classes should extend this method to efficiently slice data from the Redux state in a composable manner.
Returns: ISelectors
▸ createTypes(): IActionTypes
Defined in Services/BaseService.ts:109
Children classes should extend this method to create new IActionTypes
Returns: IActionTypes
▸ getDefaultState(): S
Defined in Services/BaseService.ts:55
The default Redux state of the IService abstract:
Returns: S
▸ makeActionCreator<T
,M
>(type: string
, defaultMeta?: any
): IActionCreator
Defined in Services/BaseService.ts:95
Creates an IActionCreator function for triggering an IAction with the given type.
Type parameters:
Parameters:
Name | Type | Description |
---|---|---|
type | string |
- |
Optional defaultMeta |
any |
- |
Returns: IActionCreator
▸ makeActionType(type: string
): string
Defined in Services/BaseService.ts:64
Returns a namespaced IAction type in the form <name>/<type>
. For example: student/FETCH_ALL
Parameters:
Name | Type | Description |
---|---|---|
type | string |
- |
Returns: string
▸ getStateObservable(): Observable
<any
>
Defined in Services/BaseService.ts:82
Returns: Observable
<any
>
▸ registerDispatch(dispatch: any
): void
Defined in Services/BaseService.ts:74
Registers the dispatch function that is passed in from the middleware.
Parameters:
Name | Type | Description |
---|---|---|
dispatch | any |
- |
Returns: void
void
▸ setStateObservable(state$: Observable
<any
>): void
Defined in Services/BaseService.ts:78
Parameters:
Name | Type |
---|---|
state$ | Observable <any > |
Returns: void