Skip to content

Commit 82b50dd

Browse files
Added function to store props from callback-generated update.
1 parent 0f1b299 commit 82b50dd

File tree

2 files changed

+51
-23
lines changed

2 files changed

+51
-23
lines changed

dash/dash-renderer/src/observers/executedCallbacks.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import {ICallback, IStoredCallback} from '../types/callbacks';
3636
import {updateProps, setPaths, handleAsyncError} from '../actions';
3737
import {getPath, computePaths} from '../actions/paths';
3838

39-
import {applyPersistence, prunePersistence} from '../persistence';
39+
import {applyPersistence, prunePersistence, setPersistance} from '../persistence';
4040
import {IStoreObserverDefinition} from '../StoreObserver';
4141

4242
const observer: IStoreObserverDefinition<IStoreState> = {
@@ -65,6 +65,9 @@ const observer: IStoreObserverDefinition<IStoreState> = {
6565
// those components have props to update to persist user edits.
6666
const {props} = applyPersistence({props: updatedProps}, dispatch);
6767

68+
// Save props to storage if persistance is active.
69+
setPersistance(path(itempath, layout), updatedProps, dispatch)
70+
6871
dispatch(
6972
updateProps({
7073
itempath,

dash/dash-renderer/src/persistence.js

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,53 @@ const getProps = layout => {
306306
};
307307
};
308308

309+
export function setPersistance(layout, newProps, dispatch) {
310+
const {
311+
canPersist,
312+
id,
313+
props,
314+
element,
315+
persistence,
316+
persisted_props,
317+
persistence_type
318+
} = getProps(layout);
319+
320+
const getFinal = (propName, prevVal) =>
321+
propName in newProps ? newProps[propName] : prevVal;
322+
const finalPersistence = getFinal('persistence', persistence);
323+
324+
if (!canPersist || !finalPersistence || finalPersistence !== persistence) {
325+
return;
326+
}
327+
328+
forEach(persistedProp => {
329+
const [propName, propPart] = persistedProp.split('.');
330+
if (newProps[propName] !== undefined) {
331+
const storage = getStore(persistence_type, dispatch);
332+
const {extract} = getTransform(element, propName, propPart);
333+
334+
const valsKey = getValsKey(id, persistedProp, persistence);
335+
let originalVal = extract(props[propName]);
336+
const newVal = extract(newProps[propName]);
337+
338+
if (originalVal !== newVal) {
339+
if (storage.hasItem(valsKey)) {
340+
originalVal = storage.getItem(valsKey)[1];
341+
if (newVal !== originalVal) {
342+
storage.setItem(valsKey, [newVal, originalVal], dispatch);
343+
}
344+
else {
345+
storage.removeItem(valsKey)
346+
}
347+
}
348+
else {
349+
storage.setItem(valsKey, [newVal, originalVal], dispatch);
350+
}
351+
}
352+
}
353+
}, persisted_props);
354+
}
355+
309356
export function recordUiEdit(layout, newProps, dispatch) {
310357
const {
311358
canPersist,
@@ -517,28 +564,6 @@ export function prunePersistence(layout, newProps, dispatch) {
517564
filter(notInNewProps, finalPersistedProps)
518565
);
519566
}
520-
521-
// now the main point - clear any edit of a prop that changed
522-
// note that this is independent of the new prop value.
523-
const transforms = element.persistenceTransforms || {};
524-
for (const propName in newProps) {
525-
const propTransforms = transforms[propName];
526-
if (propTransforms) {
527-
for (const propPart in propTransforms) {
528-
finalStorage.removeItem(
529-
getValsKey(
530-
id,
531-
`${propName}.${propPart}`,
532-
finalPersistence
533-
)
534-
);
535-
}
536-
} else {
537-
finalStorage.removeItem(
538-
getValsKey(id, propName, finalPersistence)
539-
);
540-
}
541-
}
542567
}
543568
return persistenceChanged ? mergeRight(newProps, update) : newProps;
544569
}

0 commit comments

Comments
 (0)