Skip to content

feat(AP-2173): Add persistence to context #147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

90 changes: 83 additions & 7 deletions src/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ function EvolvContext(store) {
let uid;
let remoteContext;
let localContext;
let localStorageKey;
let remoteStorageKey;

const persistedKeys = [];
const sessionPersistedKeys = [];

let initialized = false;

/**
Expand Down Expand Up @@ -50,13 +56,48 @@ function EvolvContext(store) {
}
}

function mergePersistedContext(contextKey, context, storage) {
const persistedContext = storage.getItem(contextKey);
if (persistedContext) {
const parsedContext = JSON.parse(persistedContext);
if (context) {
return objects.deepMerge(context, parsedContext);
}

return parsedContext;
}

return context || {};
}

function mergeAllPersistedStorage(contextKey, context) {
if (window === undefined || !window.localStorage) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

javascript-sdk currently has no references to session or local storage. Is this check to prevent errors if we're running on node?

A warning here that persisting is doing nothing would be good -- but should this capability live in asset-manager?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'll add a warning.

return context || {};
}

const localStorageContext = mergePersistedContext(contextKey, context, window.localStorage);
return mergePersistedContext(contextKey, localStorageContext, window.sessionStorage);
}

function persistValue(key, value, local, sessionOnly) {
const storage = sessionOnly ? window.sessionStorage : window.localStorage;
const persistedContext = storage.getItem(local ? localStorageKey : remoteStorageKey);
const context = persistedContext ? JSON.parse(persistedContext) : {};
objects.setKeyToValue(key, value, context);
storage.setItem(local ? localStorageKey : remoteStorageKey, JSON.stringify(context));
}

this.initialize = function(_uid, _remoteContext, _localContext) {
if (initialized) {
throw new Error('Evolv: The context is already initialized');
}
uid = _uid;
remoteContext = _remoteContext ? objects.deepClone(_remoteContext) : {};
localContext = _localContext ? objects.deepClone(_localContext) : {};

localStorageKey = 'evolv_' + this.uid + '_local_context';
remoteStorageKey = 'evolv_' + this.uid + '_remote_context';

localContext = mergeAllPersistedStorage(localStorageKey, _localContext);
remoteContext = mergeAllPersistedStorage(remoteStorageKey, _remoteContext);
initialized = true;
emit(this, CONTEXT_INITIALIZED, this.resolve());
};
Expand All @@ -77,6 +118,16 @@ function EvolvContext(store) {
return objects.deepClone(mutableResolve());
};

/**
* Returns true if a context key is persisted.
*
* @param {String} key The key to associate the value to.
*/

this.isPersisted = function(key) {
return persistedKeys.indexOf(key) >= 0 || sessionPersistedKeys.indexOf(key) >= 0;
}

/**
* Sets a value in the current context.
*
Expand All @@ -96,6 +147,9 @@ function EvolvContext(store) {
}

objects.setKeyToValue(key, value, context);
if (this.isPersisted(key)) {
persistValue(key, value, local, sessionPersistedKeys.indexOf(key) >= 0);
}

const updated = this.resolve();
if (typeof before === 'undefined') {
Expand All @@ -107,6 +161,31 @@ function EvolvContext(store) {
return true;
};

/**
* Persist context key to sessionStorage.
*
* @param {String} key The key to persist.
* @param {boolean} [sessionOnly] If true, the value will only be persisted to sessionStorage.
* Default: true
*/
this.persist = function(key, sessionOnly) {
ensureInitialized();

if (window === undefined || !window.localStorage) {
console.log('Evolv: Unable to persist context key. LocalStorage is not available.');
return;
}

const keys = sessionOnly === false ? persistedKeys : sessionPersistedKeys;
if (!keys.indexOf(key)) {
keys.push(key);
}

const local = objects.hasKey(key, localContext);
const value = objects.getValueForKey(key, local ? localContext : remoteContext);
persistValue(key, value, local, sessionOnly);
}

/**
* Merge the specified object into the current context.
*
Expand Down Expand Up @@ -188,11 +267,8 @@ function EvolvContext(store) {
' and "contaminations" is deprecated. Please use "experiments.confirmations" and "experiments.contaminations" instead.');
}

const valueFromRemote = objects.getValueForKey(key, remoteContext);

return objects.hasKey(key, remoteContext)
? valueFromRemote
: objects.getValueForKey(key, localContext);
return objects.getValueForKey(key,
objects.hasKey(key, remoteContext)? remoteContext : localContext);
};

/**
Expand Down