forked from iou90/redux-persist-transform-expire
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
af81b0a
commit bd5f6aa
Showing
3 changed files
with
4,209 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
'use strict'; | ||
|
||
import { reduxPersist, createTransform } from 'redux-persist'; | ||
|
||
const PERSIST_EXPIRE_DEFAULT_KEY = 'persistExpiresAt'; | ||
|
||
function dateToUnix(date) { | ||
return +(date.getTime() / 1000).toFixed(0); | ||
} | ||
|
||
export default function createExpirationTransform(expireDatas) { | ||
expireDatas = expireDatas || {}; | ||
let stateMap = new Map(); | ||
const inbound = (state, key) => { | ||
if ((state || typeof state === 'object') && expireDatas.hasOwnProperty(key)) { | ||
if (stateMap.has(key)) { | ||
if (typeof state.toJS === 'function') { | ||
let newState = state.toJS(); | ||
newState.expireDate = new Date(new Date().getTime() + expireDatas[key].expireSpan); | ||
return newState; | ||
} else { | ||
state.expireDate = new Date(new Date().getTime() + expireDatas[key].expireSpan); | ||
} | ||
} else { | ||
stateMap.set(key, true); | ||
} | ||
} | ||
return state; | ||
}; | ||
const outbound = (state, key) => { | ||
if ((state || typeof state === 'object') && expireDatas.hasOwnProperty(key)) { | ||
if (state.expireDate) { | ||
const data = expireDatas[key]; | ||
if (dateToUnix(new Date(state.expireDate)) < dateToUnix(new Date())) { | ||
state = data.default; | ||
} | ||
} | ||
} | ||
return state; | ||
}; | ||
return createTransform(inbound, outbound); | ||
} |
Oops, something went wrong.