Skip to content

Commit

Permalink
add demand version and last access to localStorage state
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirk Lüth committed Jan 21, 2017
1 parent 1e22093 commit 749c1af
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions src/singleton/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var singletonCache = (function(JSON) {
STORAGE_SUFFIX_VALUE = '[value]',
regexMatchState = new RegExp('^' + functionEscapeRegex(STORAGE_PREFIX) + '\\[(.+?)\\]' + functionEscapeRegex(STORAGE_SUFFIX_STATE) + '$'),
supportsLocalStorage = (function() { try { return 'localStorage' in global && global.localStorage; } catch(exception) { return FALSE; } }()),
localStorage = supportsLocalStorage ? global.localStorage : NULL,
supportsRemainingSpace = supportsLocalStorage && 'remainingSpace' in localStorage,
storage = {},
cache;
Expand Down Expand Up @@ -59,7 +60,24 @@ var singletonCache = (function(JSON) {
});

return match ? match.state : FALSE;

}

function getKey(key) {
return localStorage.getItem(key);
}

function setKey(key, value) {
localStorage[value ? 'setItem' : 'removeItem'](key, value);
}

function getState(key) {
return JSON.parse(getKey(key));
}

function setState(key, state) {
state.access = functionGetTimestamp();

setKey(key, JSON.stringify(state));
}

function emit(event, dependency, state) {
Expand All @@ -78,10 +96,14 @@ var singletonCache = (function(JSON) {

if(enabled(dependency)) {
id = STORAGE_PREFIX + '[' + dependency.id + ']';
state = JSON.parse(localStorage.getItem(id + STORAGE_SUFFIX_STATE));
state = getState(id + STORAGE_SUFFIX_STATE);

if(state && state.version === dependency.version && ((!state.expires && !dependency.lifetime) || state.expires > functionGetTimestamp())) {
dependency.source = localStorage.getItem(id + STORAGE_SUFFIX_VALUE);
dependency.source = getKey(id + STORAGE_SUFFIX_VALUE);

functionDefer(function() {
setState(id + STORAGE_SUFFIX_STATE, state);
});

return TRUE;
}
Expand Down Expand Up @@ -114,16 +136,16 @@ var singletonCache = (function(JSON) {
var state, id, spaceBefore;

if(enabled(dependency)) {
state = { version: dependency.version, expires: dependency.lifetime ? functionGetTimestamp() + dependency.lifetime : dependency.lifetime };
state = { version: dependency.version, demand: demand.version, expires: dependency.lifetime ? functionGetTimestamp() + dependency.lifetime : dependency.lifetime };
id = STORAGE_PREFIX + '[' + dependency.id + ']';

emit(EVENT_PRE_CACHE, dependency, state);

try {
spaceBefore = supportsRemainingSpace ? localStorage.remainingSpace : NULL;

localStorage.setItem(id + STORAGE_SUFFIX_VALUE, dependency.source);
localStorage.setItem(id + STORAGE_SUFFIX_STATE, JSON.stringify(state));
setKey(id + STORAGE_SUFFIX_VALUE, dependency.source);
setState(id + STORAGE_SUFFIX_STATE, state);

// strict equality check with "===" is required due to spaceBefore might be "0"
if(spaceBefore !== NULL && localStorage.remainingSpace === spaceBefore) {
Expand All @@ -147,9 +169,9 @@ var singletonCache = (function(JSON) {
var id = functionResolveId(path),
key = STORAGE_PREFIX + '[' + id + ']';

if(localStorage[key + STORAGE_SUFFIX_STATE]) {
localStorage.removeItem(key + STORAGE_SUFFIX_STATE);
localStorage.removeItem(key + STORAGE_SUFFIX_VALUE);
if(getKey(key + STORAGE_SUFFIX_STATE)) {
setKey(key + STORAGE_SUFFIX_STATE);
setKey(key + STORAGE_SUFFIX_VALUE);

emit(EVENT_CACHE_CLEAR, ClassDependency.get(id) || new ClassDependency(id, NULL, FALSE));
}
Expand Down Expand Up @@ -183,7 +205,7 @@ var singletonCache = (function(JSON) {
match = property.match(regexMatchState);

if(match) {
state = JSON.parse(localStorage.getItem(STORAGE_PREFIX + '[' + match[1] + ']' + STORAGE_SUFFIX_STATE));
state = getState(STORAGE_PREFIX + '[' + match[1] + ']' + STORAGE_SUFFIX_STATE);

if(state && state.expires > 0 && state.expires <= functionGetTimestamp()) {
self.path(match[1]);
Expand Down

0 comments on commit 749c1af

Please sign in to comment.