|
|
|
|
|
|
|
|
|
|
|
|
|
|
const debug = require( 'debug' )( 'notifications:note' ); |
|
|
|
|
|
function getItem( key ) { |
|
|
let item; |
|
|
try { |
|
|
item = window.localStorage.getItem( key ); |
|
|
return JSON.parse( item ); |
|
|
} catch ( e ) { |
|
|
if ( e instanceof SyntaxError ) { |
|
|
return item; |
|
|
} |
|
|
|
|
|
debug( 'couldnt get localStorage item for: %s', key ); |
|
|
} |
|
|
|
|
|
return null; |
|
|
} |
|
|
|
|
|
function setItem( key, value ) { |
|
|
try { |
|
|
window.localStorage.setItem( key, JSON.stringify( value ) ); |
|
|
} catch ( e ) { |
|
|
debug( 'couldnt set localStorage item for: %s', key ); |
|
|
} |
|
|
} |
|
|
|
|
|
function removeItem( key ) { |
|
|
try { |
|
|
window.localStorage.removeItem( key ); |
|
|
} catch ( e ) { |
|
|
debug( 'couldnt remove item from localStorage for: %s', key ); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function cleanup() { |
|
|
const keysToRemove = []; |
|
|
|
|
|
try { |
|
|
for ( let i = 0; i < window.localStorage.length; i++ ) { |
|
|
const storedReplyKey = window.localStorage.key( i ); |
|
|
|
|
|
|
|
|
if ( 'reply_' === window.localStorage.key( i ).substring( 0, 6 ) ) { |
|
|
const storedReply = getItem( storedReplyKey ); |
|
|
|
|
|
if ( storedReply && Date.now() - storedReply[ 1 ] >= 24 * 60 * 60 * 1000 ) { |
|
|
keysToRemove.push( storedReplyKey ); |
|
|
} |
|
|
} |
|
|
} |
|
|
} catch ( e ) { |
|
|
debug( 'couldnt cleanup cache' ); |
|
|
} |
|
|
|
|
|
keysToRemove.forEach( removeItem ); |
|
|
} |
|
|
|
|
|
export default { |
|
|
cleanup, |
|
|
getItem, |
|
|
setItem, |
|
|
removeItem, |
|
|
}; |
|
|
|