redux-localstorage
Advanced tools
Comparing version 1.0.0-rc4 to 1.0.0-rc5
@@ -13,3 +13,3 @@ "use strict"; | ||
try { | ||
storage.setItem(key, JSON.stringify(value), callback); | ||
storage.setItem(key, value, callback); | ||
} catch (e) { | ||
@@ -24,3 +24,3 @@ callback(e); | ||
try { | ||
callback(null, JSON.parse(value)); | ||
callback(null, value); | ||
} catch (e) { | ||
@@ -27,0 +27,0 @@ callback(e); |
@@ -7,7 +7,16 @@ 'use strict'; | ||
function _interopRequire(obj) { return obj && obj.__esModule ? obj['default'] : obj; } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
var _transformStateJs = require('../../transformState.js'); | ||
var _transformStateJs2 = _interopRequireDefault(_transformStateJs); | ||
var _adapterJs = require('./adapter.js'); | ||
exports['default'] = _interopRequire(_adapterJs); | ||
var _adapterJs2 = _interopRequireDefault(_adapterJs); | ||
exports['default'] = function (storage) { | ||
return (0, _transformStateJs2['default'])(JSON.stringify, JSON.parse)((0, _adapterJs2['default'])(storage)); | ||
}; | ||
module.exports = exports['default']; |
@@ -13,3 +13,3 @@ "use strict"; | ||
try { | ||
callback(null, storage.setItem(key, JSON.stringify(value))); | ||
callback(null, storage.setItem(key, value)); | ||
} catch (e) { | ||
@@ -22,3 +22,3 @@ callback(e); | ||
try { | ||
callback(null, JSON.parse(storage.getItem(key))); | ||
callback(null, storage.getItem(key)); | ||
} catch (e) { | ||
@@ -25,0 +25,0 @@ callback(e); |
@@ -7,7 +7,16 @@ 'use strict'; | ||
function _interopRequire(obj) { return obj && obj.__esModule ? obj['default'] : obj; } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
var _transformStateJs = require('../../transformState.js'); | ||
var _transformStateJs2 = _interopRequireDefault(_transformStateJs); | ||
var _adapterJs = require('./adapter.js'); | ||
exports['default'] = _interopRequire(_adapterJs); | ||
var _adapterJs2 = _interopRequireDefault(_adapterJs); | ||
exports['default'] = function (storage) { | ||
return (0, _transformStateJs2['default'])(JSON.stringify, JSON.parse)((0, _adapterJs2['default'])(storage)); | ||
}; | ||
module.exports = exports['default']; |
@@ -9,5 +9,5 @@ 'use strict'; | ||
var _localStorageAdapterJs = require('../localStorage/adapter.js'); | ||
var _localStorageIndexJs = require('../localStorage/index.js'); | ||
exports['default'] = _interopRequire(_localStorageAdapterJs); | ||
exports['default'] = _interopRequire(_localStorageIndexJs); | ||
module.exports = exports['default']; |
@@ -21,21 +21,25 @@ 'use strict'; | ||
function bufferActions() { | ||
var type = arguments.length <= 0 || arguments[0] === undefined ? _actionTypesJs2['default'].INIT : arguments[0]; | ||
var buffer = true; | ||
var queue = []; | ||
return function (next) { | ||
return function (action) { | ||
if (!buffer) return next(action); | ||
return function () { | ||
return function (next) { | ||
return function (action) { | ||
if (!buffer) return next(action); | ||
if (action.type === _actionTypesJs2['default'].INIT) { | ||
buffer = false; | ||
next(action); | ||
queue.forEach(function (queuedAction) { | ||
next(queuedAction); | ||
}); | ||
queue = null; | ||
} else { | ||
queue.push(action); | ||
} | ||
if (action.type === type) { | ||
buffer = false; | ||
next(action); | ||
queue.forEach(function (queuedAction) { | ||
next(queuedAction); | ||
}); | ||
queue = null; | ||
} else { | ||
queue.push(action); | ||
} | ||
return action; | ||
return action; | ||
}; | ||
}; | ||
@@ -42,0 +46,0 @@ }; |
@@ -9,2 +9,6 @@ 'use strict'; | ||
var _persistStateMiddlewareJs = require('./persistStateMiddleware.js'); | ||
exports.persistStateMiddleware = _interopRequire(_persistStateMiddlewareJs); | ||
var _mergePersistedStateJs = require('./mergePersistedState.js'); | ||
@@ -14,2 +18,10 @@ | ||
var _transformStateJs = require('./transformState.js'); | ||
exports.transformState = _interopRequire(_transformStateJs); | ||
var _bufferActionsJs = require('./bufferActions.js'); | ||
exports.bufferActions = _interopRequire(_bufferActionsJs); | ||
var _actionTypesJs = require('./actionTypes.js'); | ||
@@ -16,0 +28,0 @@ |
@@ -6,5 +6,2 @@ 'use strict'; | ||
}); | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
exports['default'] = persistState; | ||
@@ -14,2 +11,4 @@ | ||
var _redux = require('redux'); | ||
var _persistStateMiddlewareJs = require('./persistStateMiddleware.js'); | ||
@@ -31,4 +30,6 @@ | ||
var defaultStorage = (0, _adaptersLocalStorage2['default'])(localStorage); | ||
var defaultKey = 'redux-localstorage'; | ||
var getDefaultStorage = function getDefaultStorage() { | ||
(0, _adaptersLocalStorage2['default'])(window.localStorage); | ||
}; | ||
@@ -47,3 +48,3 @@ /** | ||
function persistState(storage, key, callback) { | ||
if (storage === undefined) storage = defaultStorage; | ||
if (storage === undefined) storage = getDefaultStorage(); | ||
if (key === undefined) key = defaultKey; | ||
@@ -63,27 +64,27 @@ | ||
finalKey = storage; | ||
finalStorage = defaultStorage; | ||
finalStorage = getDefaultStorage(); | ||
} else if (typeof storage === 'function') { | ||
finalCallback = storage; | ||
finalStorage = defaultStorage; | ||
finalStorage = getDefaultStorage(); | ||
} | ||
return function (next) { | ||
return function (reducer, initialState) { | ||
// Apply middleware | ||
var store = next(reducer, initialState); | ||
var dispatch = (0, _bufferActionsJs2['default'])()((0, _persistStateMiddlewareJs2['default'])(store, finalStorage, finalKey)(store.dispatch)); | ||
return function (createStore) { | ||
return function () { | ||
var enhancer = (0, _redux.applyMiddleware)((0, _bufferActionsJs2['default'])(), (0, _persistStateMiddlewareJs2['default'])(finalStorage, finalKey)); | ||
var store = enhancer(createStore).apply(undefined, arguments); | ||
// Retrieve and dispatch persisted store state | ||
finalStorage.get(finalKey, function (err, persistedState) { | ||
if (err) console.error('Failed to retrieve persisted state from storage:', err); // eslint-disable-line no-console | ||
dispatch({ | ||
store.dispatch({ | ||
type: _actionTypesJs2['default'].INIT, | ||
payload: persistedState | ||
}); | ||
if (finalCallback) finalCallback(); | ||
}); | ||
return _extends({}, store, { | ||
dispatch: dispatch | ||
}); | ||
return store; | ||
}; | ||
@@ -90,0 +91,0 @@ }; |
@@ -14,20 +14,22 @@ 'use strict'; | ||
function persistStateMiddleware(store, storage) { | ||
var key = arguments.length <= 2 || arguments[2] === undefined ? 'redux-localstorage' : arguments[2]; | ||
function persistStateMiddleware(storage) { | ||
var key = arguments.length <= 1 || arguments[1] === undefined ? 'redux-localstorage' : arguments[1]; | ||
function persistState() { | ||
storage.put(key, store.getState(), function (err) { | ||
if (err) console.error('Unable to persist state to storage:', err); // eslint-disable-line no-console | ||
}); | ||
} | ||
return function (store) { | ||
function persistState() { | ||
storage.put(key, store.getState(), function (err) { | ||
if (err) console.error('Unable to persist state to storage:', err); // eslint-disable-line no-console | ||
}); | ||
} | ||
return function (next) { | ||
return function (action) { | ||
next(action); | ||
return function (next) { | ||
return function (action) { | ||
var result = next(action); | ||
if (action.type !== _actionTypesJs2['default'].INIT) { | ||
persistState(); | ||
} | ||
if (action.type !== _actionTypesJs2['default'].INIT) { | ||
persistState(); | ||
} | ||
return action; | ||
return result; | ||
}; | ||
}; | ||
@@ -34,0 +36,0 @@ }; |
{ | ||
"name": "redux-localstorage", | ||
"version": "1.0.0-rc4", | ||
"version": "1.0.0-rc5", | ||
"description": "Unopinionated store enhancer that persists state changes (locally).", | ||
@@ -11,5 +11,4 @@ "main": "lib", | ||
"files": [ | ||
"lib/", | ||
"package.json", | ||
"README.md" | ||
"lib", | ||
"src" | ||
], | ||
@@ -24,7 +23,8 @@ "repository": { | ||
"Immutable", | ||
"local storage", | ||
"storage", | ||
"localStorage", | ||
"sessionStorage", | ||
"AsyncStorage", | ||
"PouchDB" | ||
"localForage", | ||
"LevelDB" | ||
], | ||
@@ -31,0 +31,0 @@ "author": "Elger Lambert <elgerlambert@gmail.com>", |
@@ -34,7 +34,8 @@ redux-localstorage | ||
const createPersistentStore = compose( | ||
const enhancer = compose( | ||
/* applyMiddleware(...middlewares) */ | ||
persistState(storage, 'my-storage-key') | ||
)(createStore); | ||
); | ||
const store = createPersistentStore(reducer); | ||
const store = createStore(reducer /*, [initialState]*/, enhancer); | ||
``` | ||
@@ -47,3 +48,3 @@ ## API | ||
``` | ||
An object that provides ([enhanced](#enhancers)) methods for data persistence, retrieval and removal as put, get & del. Defaults to adapter(localStorage). | ||
An object that provides ([enhanced](#enhancers)) methods for data persistence, retrieval and removal as put, get & del. Defaults to adapter(window.localStorage). | ||
@@ -81,2 +82,33 @@ #### key | ||
### transformState([down], [up]) | ||
A typical use-case for storage enhancers is to modify the state before it's persisted or retrieved, e.g. serialization, encryption or to filter out a subset. transformState is a storage enhancer that makes it easier to do so. | ||
#### down | ||
```js | ||
type down = Function | Array<Function> | ||
``` | ||
A function or an Array of functions that transform the state before it's persisted. | ||
#### up | ||
```js | ||
type up = Function | Array<Function> | ||
``` | ||
A function or an Array of functions that transform the state directly after it's been retrieved. | ||
```js | ||
import adapter from 'redux-localstorage/lib/adapters/localStorage/adapter.js'; | ||
const storage = compose( | ||
transformState([ | ||
JSON.stringify, | ||
btoa | ||
], [ | ||
atob, | ||
JSON.parse | ||
]) | ||
)(adapter(window.localStorage)) | ||
``` | ||
**Note:** transformState is applied internally to enhance storage adapters that require JSON serialization. If serialization is not the last step before persisting, as is the case in this encryption example, make sure you import the actual adapter.js (as opposed to index.js). | ||
## Rehydration | ||
@@ -109,3 +141,3 @@ The use of `mergePersistedState` is optional. If you prefer to handle rehydration in your own reducer(s), you can. Listen for redux-localstorage's `INIT` action; it includes the persisted state as it's `payload`. For example: | ||
### adapters | ||
Redux-localstorage currently provides adapters for `localStorage`, `sessionStorage` and `AsyncStorage`. An adapter creates a thin wrapper that transforms a storage API so that it conforms to the stated requirements. The original storage object passed to an adapter can be accessed through `adapted[0]`; this provides you access to all the original storage methods when creating a storage enhancer. | ||
Redux-localstorage currently provides adapters for `localStorage`, `localForage`, `sessionStorage`, `AsyncStorage` and `levelDB`. An adapter creates a thin wrapper that transforms a storage API so that it conforms to the stated requirements. The original storage object passed to an adapter can be accessed through `adapted[0]`; this provides you access to all the original storage methods when creating a storage enhancer. | ||
@@ -119,2 +151,3 @@ ```js | ||
``` | ||
**Note:** the adapters for localStorage, sessionStorage, AsyncStorage & levelDB take care of JSON (de)serialization for you. If you just want the adapter without serialization you can access the `adapter.js` within the corresponding storage folder. See [transformState](#transformState) for an example. | ||
@@ -134,5 +167,6 @@ ### enhancers | ||
const createPersistentStore = compose( | ||
const enhancer = compose( | ||
/* applyMiddleware(...middlewares) */ | ||
persistState(storage, 'my-storage-key') | ||
)(createStore); | ||
); | ||
``` | ||
@@ -139,0 +173,0 @@ Check out the wiki for a [list of available storage enhancers](https://github.com/elgerlambert/redux-localstorage/wiki) and don't forget to add your own if you publish any! |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30861
38
618
171
1