Skip to content

Commit

Permalink
Add debounce and debounceInTestMode options to enhancer (#3)
Browse files Browse the repository at this point in the history
* Add `debounce` and `debounceInTestMode` options to enhancer

* v1.1.0
  • Loading branch information
dpikt authored Aug 24, 2018
1 parent 210009f commit ab18807
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ The `options` object passed to these action creators may contain the following a
The `redux-sessions` enhancer is what allows the session state to persist across page refreshes. It can receive the following options:

- `persist (default=true)`: A flag indicating whether or not to persist session state.
- `debounce (default=true)`: A flag indicating whether or not to debounce writing to `localStorage`.
- `debounceInTestMode (default=false)`: A flag indicating whether or not to debounce writing to `localStorage` when `NODE_ENV === 'test'`.
- `debounceInterval (default=500)`: The debounce interval used when writing session state to `localStorage` (ms).

### Selectors
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "redux-sessions",
"version": "1.0.1",
"version": "1.1.0",
"description": "Redux action creators for storing session state",
"main": "lib/index.js",
"scripts": {
Expand Down
15 changes: 13 additions & 2 deletions src/enhancer.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { saveSessionState } from './persistenceHelpers'
import { throttle } from 'lodash'
import { isTestMode } from './utils'

const DEFAULT_DEBOUNCE_INTERVAL = 500

// Adds store subscription that persists session state in local storage
function enhancer ({ persist=true, debounceInterval=DEFAULT_DEBOUNCE_INTERVAL }={}) {
function enhancer ({
persist=true,
debounce=true,
debounceInTestMode=false,
debounceInterval=DEFAULT_DEBOUNCE_INTERVAL,
}={}) {
return function enhance (createStore) {
return function newCreateStore (...args) {
const store = createStore(...args)
Expand All @@ -15,7 +21,12 @@ function enhancer ({ persist=true, debounceInterval=DEFAULT_DEBOUNCE_INTERVAL }=
if (!state.sessions) throw new Error('redux-sessions: error when attempting to save state. Did you remember to attach the reducer at key `sessions`?')
return saveSessionState(state.sessions)
}
store.subscribe(throttle(persistState, debounceInterval))
// Don't debounce in test mode, unless otherwise specified
const doDebounce = isTestMode()
? debounceInTestMode
: debounce
const subscription = doDebounce ? throttle(persistState, debounceInterval) : persistState
store.subscribe(subscription)
return store
}
}
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export isTestMode from './isTestMode'
export * as storage from './storage'
13 changes: 13 additions & 0 deletions src/utils/isTestMode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { get } from 'lodash/fp'

function getEnv () {
return (typeof process === 'undefined')
? null
: get('env.NODE_ENV', process)
}

function isTestMode () {
return getEnv() === 'test'
}

export default isTestMode
2 changes: 0 additions & 2 deletions test/enhancer.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { enhancer } from '../src'
// Mock debounce: https://github.com/facebook/jest/issues/3465
jest.mock('lodash/debounce', () => func => func)

function mockCreateStore (initialState) {
const mockStore = {}
Expand Down

0 comments on commit ab18807

Please sign in to comment.