Skip to content

Commit

Permalink
Documents impact of partial actions
Browse files Browse the repository at this point in the history
  • Loading branch information
rjz committed Sep 5, 2017
1 parent c97ee82 commit 68127d0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/actions/index.ts
Expand Up @@ -3,6 +3,8 @@ import * as redux from 'redux'
import { api } from '../api'
import * as state from '../reducers/index'

// API actions contain details on the initial request and an eventual error or
// response
type APIAction<Q, S> = {
request?: Q
response?: S
Expand All @@ -14,7 +16,8 @@ export type Action =
{ type: 'INCREMENT_COUNTER', delta: number }
| { type: 'RESET_COUNTER' }

// API Requests
// API Requests implemented as partial actions
// See: https://goo.gl/FYWGpr
| ({ type: 'SAVE_COUNT' } & APIAction<{ value: number }, {}>)
| ({ type: 'LOAD_COUNT' } & APIAction<undefined, { value: number }>)

Expand Down
5 changes: 5 additions & 0 deletions src/reducers/index.ts
Expand Up @@ -13,6 +13,9 @@ export type All = {

function isSaving (state: boolean = false, action: Action): boolean {
if (action.type === 'SAVE_COUNT') {
// `SAVE_COUNT` is a partial action. We'll check its payload to determine
// whether this instance describes its resolution.
// See: https://goo.gl/FYWGpr
return !action.response && !action.error
}
return state
Expand Down Expand Up @@ -51,6 +54,8 @@ function counter (state: Counter = initialState, action: Action): Counter {
case 'LOAD_COUNT': {
const { response } = action
if (response) {
// If `response` is set, `LOAD_COUNT` is "resolved"
// See: https://goo.gl/FYWGpr
return { value: response.value }
}
}
Expand Down

0 comments on commit 68127d0

Please sign in to comment.