Skip to content
This repository was archived by the owner on Jun 16, 2021. It is now read-only.

Latest commit

 

History

History
424 lines (244 loc) · 13.4 KB

baseservice.md

File metadata and controls

424 lines (244 loc) · 13.4 KB

redux-data-service > BaseService

Class: 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 IServices. abstract:

class: BaseService

implements: IService

Type parameters

S

Hierarchy

BaseService

DataService

Implements

Index

Properties

Accessors

Methods


Properties

<Protected>``<Optional> internalActions

● internalActions: IActionCreators

Defined in Services/BaseService.ts:45


<Protected>``<Optional> internalEpics

● internalEpics: IActionEpic[]

Defined in Services/BaseService.ts:44


<Protected>``<Optional> internalReducers

● internalReducers: IReducers<S>

Defined in Services/BaseService.ts:43


<Protected>``<Optional> internalSelectors

● internalSelectors: ISelectors

Defined in Services/BaseService.ts:46


<Protected> internalTypes

● internalTypes: IActionTypes

Defined in Services/BaseService.ts:47


<Abstract> name

● name: string

Implementation of IService.name

Defined in Services/BaseService.ts:39


<Static>``<Protected> dispatch

● dispatch: function

Defined in Services/BaseService.ts:41

Type declaration

▸(action: IAction): void

Parameters:

Name Type
action IAction

Returns: void


<Static>``<Protected> state$

● state$: any

Defined in Services/BaseService.ts:42


Accessors

actions

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


epics

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[]


reducer

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>


<Protected> reducers

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>


selectors

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


types

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


Methods

<Protected> createActions

createActions(): IActionCreators

Defined in Services/BaseService.ts:119

Children classes should extend this method to dispatch new Actions

Returns: IActionCreators


<Protected> createEpics

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[]


<Protected> createReducers

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>


<Protected> createSelectors

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


<Protected> createTypes

createTypes(): IActionTypes

Defined in Services/BaseService.ts:109

Children classes should extend this method to create new IActionTypes

Returns: IActionTypes


<Abstract> getDefaultState

getDefaultState(): S

Defined in Services/BaseService.ts:55

The default Redux state of the IService abstract:

Returns: S


makeActionCreator

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:

T : any

M

Parameters:

Name Type Description
type string -
Optional defaultMeta any -

Returns: IActionCreator


makeActionType

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


<Static> getStateObservable

getStateObservable(): Observable<any>

Defined in Services/BaseService.ts:82

Returns: Observable<any>


<Static> registerDispatch

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


<Static> setStateObservable

setStateObservable(state$: Observable<any>): void

Defined in Services/BaseService.ts:78

Parameters:

Name Type
state$ Observable<any>

Returns: void