use shared key for saved user state

This commit is contained in:
Akinwale Ariwodola 2019-10-02 10:27:09 +01:00
parent f2d46c359a
commit 13ae65bd46
2 changed files with 46 additions and 51 deletions
dist
src/redux/actions

45
dist/bundle.es.js vendored
View file

@ -3742,11 +3742,12 @@ function has(obj, path) {
var _extends$5 = 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; }; var _extends$5 = 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; };
const stateCache = {}; let oldShared;
const sharedPreferenceKey = 'shared';
function extractUserState(rawObj) { function extractUserState(rawObj) {
if (rawObj && rawObj.version === '0.1' && rawObj.shared) { if (rawObj && rawObj.version === '0.1' && rawObj.value) {
const { subscriptions, tags } = rawObj.shared; const { subscriptions, tags } = rawObj.value;
return _extends$5({}, subscriptions ? { subscriptions } : {}, tags ? { tags } : {}); return _extends$5({}, subscriptions ? { subscriptions } : {}, tags ? { tags } : {});
} }
@ -3761,7 +3762,10 @@ function doPopulateSharedUserState(settings) {
}; };
} }
function sharedStateSubscriber(state, filters, accountId, walletId) { function sharedStateSubscriber(state, filters, version, accountId, walletId) {
// the shared object that will be saved
const shared = {};
Object.keys(filters).forEach(key => { Object.keys(filters).forEach(key => {
const filter = filters[key]; const filter = filters[key];
const { source, property, transform } = filter; const { source, property, transform } = filter;
@ -3770,19 +3774,14 @@ function sharedStateSubscriber(state, filters, accountId, walletId) {
value = transform(value); value = transform(value);
} }
let cacheKey = key; shared[key] = value;
if (accountId) {
cacheKey = `${cacheKey}_${accountId}`;
}
if (walletId) {
cacheKey = `${cacheKey}_${walletId}`;
}
if (!isEqual(stateCache[cacheKey], value)) {
// only update if the preference changed from last call in the same session
doPreferenceSet(key, value, accountId, walletId);
}
}); });
if (!isEqual(oldShared, shared)) {
// only update if the preference changed from last call in the same session
oldShared = shared;
doPreferenceSet(sharedPreferenceKey, shared, version, accountId, walletId);
}
} }
function doPreferenceSet(key, value, version, accountId, walletId, success, fail) { function doPreferenceSet(key, value, version, accountId, walletId, success, fail) {
@ -3793,7 +3792,7 @@ function doPreferenceSet(key, value, version, accountId, walletId, success, fail
}; };
lbryProxy.preference_set({ key, value: JSON.stringify(preference), account_id: accountId, wallet_id: walletId }).then(() => { lbryProxy.preference_set({ key, value: JSON.stringify(preference), account_id: accountId, wallet_id: walletId }).then(() => {
success(value); success(preference);
}).catch(() => { }).catch(() => {
if (fail) { if (fail) {
fail(); fail();
@ -3804,16 +3803,14 @@ function doPreferenceSet(key, value, version, accountId, walletId, success, fail
function doPreferenceGet(key, accountId, walletId, success, fail) { function doPreferenceGet(key, accountId, walletId, success, fail) {
lbryProxy.preference_get({ key, account_id: accountId, wallet_id: walletId }).then(result => { lbryProxy.preference_get({ key, account_id: accountId, wallet_id: walletId }).then(result => {
if (result) { if (result) {
const preference = JSON.parse(result); const preference = result[key];
const { value, version } = preference; return success(preference);
return success(value, version);
} }
// Or fail instead? return success(null);
return success(null, null); }).catch(err => {
}).catch(() => {
if (fail) { if (fail) {
fail(); fail(err);
} }
}); });
} }

View file

@ -3,19 +3,20 @@ import * as ACTIONS from 'constants/action_types';
import Lbry from 'lbry'; import Lbry from 'lbry';
import isEqual from 'util/deep-equal'; import isEqual from 'util/deep-equal';
type v0Data = { type sharedData = {
version: '0.1', version: '0.1',
shared: { value: {
subscriptions?: Array<string>, subscriptions?: Array<string>,
tags?: Array<string>, tags?: Array<string>,
}, },
}; };
const stateCache = {}; let oldShared;
const sharedPreferenceKey = 'shared';
function extractUserState(rawObj: v0Data) { function extractUserState(rawObj: sharedData) {
if (rawObj && rawObj.version === '0.1' && rawObj.shared) { if (rawObj && rawObj.version === '0.1' && rawObj.value) {
const { subscriptions, tags } = rawObj.shared; const { subscriptions, tags } = rawObj.value;
return { return {
...(subscriptions ? { subscriptions } : {}), ...(subscriptions ? { subscriptions } : {}),
@ -40,6 +41,9 @@ export function sharedStateSubscriber(
accountId: string, accountId: string,
walletId: string) walletId: string)
{ {
// the shared object that will be saved
const shared = {};
Object.keys(filters).forEach(key => { Object.keys(filters).forEach(key => {
const filter = filters[key]; const filter = filters[key];
const { source, property, transform } = filter; const { source, property, transform } = filter;
@ -48,19 +52,15 @@ export function sharedStateSubscriber(
value = transform(value); value = transform(value);
} }
let cacheKey = key; shared[key] = value;
if (accountId) {
cacheKey = `${cacheKey}_${accountId}`;
}
if (walletId) {
cacheKey = `${cacheKey}_${walletId}`;
}
if (!isEqual(stateCache[cacheKey], value)) {
// only update if the preference changed from last call in the same session
doPreferenceSet(key, value, version, accountId, walletId);
}
}); });
if (!isEqual(oldShared, shared)) {
// only update if the preference changed from last call in the same session
oldShared = shared;
doPreferenceSet(sharedPreferenceKey, shared, version, accountId, walletId);
}
} }
export function doPreferenceSet( export function doPreferenceSet(
@ -75,11 +75,11 @@ export function doPreferenceSet(
const preference = { const preference = {
type: (typeof value), type: (typeof value),
version, version,
value value,
}; };
Lbry.preference_set({ key, value: JSON.stringify(preference), account_id: accountId, wallet_id: walletId }).then(() => { Lbry.preference_set({ key, value: JSON.stringify(preference), account_id: accountId, wallet_id: walletId }).then(() => {
success(value); success(preference);
}).catch(() => { }).catch(() => {
if (fail) { fail(); } if (fail) { fail(); }
}); });
@ -94,14 +94,12 @@ export function doPreferenceGet(
) { ) {
Lbry.preference_get({ key, account_id: accountId, wallet_id: walletId }).then(result => { Lbry.preference_get({ key, account_id: accountId, wallet_id: walletId }).then(result => {
if (result) { if (result) {
const preference = JSON.parse(result); const preference = result[key];
const { value, version } = preference; return success(preference);
return success(value, version);
} }
// Or fail instead? return success(null);
return success(null, null); }).catch(err => {
}).catch(() => { if (fail) { fail(err); }
if (fail) { fail(); }
}); });
} }