Skip to content

Commit bf1b7f0

Browse files
committed
Remove throttling functionality
1 parent ab18807 commit bf1b7f0

4 files changed

Lines changed: 10 additions & 39 deletions

File tree

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,6 @@ The `options` object passed to these action creators may contain the following a
7070
The `redux-sessions` enhancer is what allows the session state to persist across page refreshes. It can receive the following options:
7171

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

7774
### Selectors
7875

src/enhancer.js

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,20 @@
1-
import { saveSessionState } from './persistenceHelpers'
2-
import { throttle } from 'lodash'
3-
import { isTestMode } from './utils'
1+
import { loadSessionState, saveSessionState } from './persistenceHelpers'
42

5-
const DEFAULT_DEBOUNCE_INTERVAL = 500
3+
let CACHED_SESSION_STATE = loadSessionState()
64

75
// Adds store subscription that persists session state in local storage
8-
function enhancer ({
9-
persist=true,
10-
debounce=true,
11-
debounceInTestMode=false,
12-
debounceInterval=DEFAULT_DEBOUNCE_INTERVAL,
13-
}={}) {
6+
function enhancer ({ persist=true }={}) {
147
return function enhance (createStore) {
158
return function newCreateStore (...args) {
169
const store = createStore(...args)
1710
if (!persist) return store
18-
// Define subscription function
19-
function persistState () {
20-
const state = store.getState()
21-
if (!state.sessions) throw new Error('redux-sessions: error when attempting to save state. Did you remember to attach the reducer at key `sessions`?')
22-
return saveSessionState(state.sessions)
23-
}
24-
// Don't debounce in test mode, unless otherwise specified
25-
const doDebounce = isTestMode()
26-
? debounceInTestMode
27-
: debounce
28-
const subscription = doDebounce ? throttle(persistState, debounceInterval) : persistState
29-
store.subscribe(subscription)
11+
store.subscribe(() => {
12+
const sessionState = store.getState().sessions
13+
if (!sessionState) throw new Error('redux-sessions: error when attempting to save state. Did you remember to attach the reducer at key `sessions`?')
14+
if (sessionState === CACHED_SESSION_STATE) return
15+
CACHED_SESSION_STATE = sessionState
16+
return saveSessionState(sessionState)
17+
})
3018
return store
3119
}
3220
}

src/utils/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
export isTestMode from './isTestMode'
21
export * as storage from './storage'

src/utils/isTestMode.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)