Skip to content

Commit

Permalink
Remove debounce functionality (#4)
Browse files Browse the repository at this point in the history
* Remove throttling functionality

* v2.0.0
  • Loading branch information
dpikt authored Aug 24, 2018
1 parent ab18807 commit 0922cb3
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 40 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@ 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.1.0",
"version": "2.0.0",
"description": "Redux action creators for storing session state",
"main": "lib/index.js",
"scripts": {
Expand Down
32 changes: 10 additions & 22 deletions src/enhancer.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,20 @@
import { saveSessionState } from './persistenceHelpers'
import { throttle } from 'lodash'
import { isTestMode } from './utils'
import { loadSessionState, saveSessionState } from './persistenceHelpers'

const DEFAULT_DEBOUNCE_INTERVAL = 500
let CACHED_SESSION_STATE = loadSessionState()

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

This file was deleted.

0 comments on commit 0922cb3

Please sign in to comment.