Skip to content

Commit

Permalink
Improve PersistOptions merging (#3913)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghsolomon authored Jan 31, 2025
1 parent f8d655b commit 16786f6
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 13 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
### 🐞 Bug Fixes

* Fixed Role grid losing view state on refresh.
* Fixed bug when merging `PersistOptions` with conflicting implicit provider types.
* Fixed bug where explicit `persistGrouping` options were not being respected by `GridModel`.

## v72.0.0 - 2025-01-27

Expand Down
8 changes: 6 additions & 2 deletions cmp/filter/FilterChooserModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,9 @@ export class FilterChooserModel extends HoistModel {
}: FilterChooserPersistOptions) {
if (persistValue) {
const status = {initialized: false},
persistWith = isObject(persistValue) ? persistValue : rootPersistWith;
persistWith = isObject(persistValue)
? PersistenceProvider.mergePersistOptions(rootPersistWith, persistValue)
: rootPersistWith;
PersistenceProvider.create({
persistOptions: {
path: `${path}.value`,
Expand All @@ -432,7 +434,9 @@ export class FilterChooserModel extends HoistModel {
}

if (persistFavorites) {
const persistWith = isObject(persistFavorites) ? persistFavorites : rootPersistWith,
const persistWith = isObject(persistFavorites)
? PersistenceProvider.mergePersistOptions(rootPersistWith, persistFavorites)
: rootPersistWith,
provider = PersistenceProvider.create({
persistOptions: {
path: `${path}.favorites`,
Expand Down
12 changes: 9 additions & 3 deletions cmp/grid/impl/InitPersist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ export function initPersist(
}: GridModelPersistOptions
) {
if (persistColumns) {
const persistWith = isObject(persistColumns) ? persistColumns : rootPersistWith;
const persistWith = isObject(persistColumns)
? PersistenceProvider.mergePersistOptions(rootPersistWith, persistColumns)
: rootPersistWith;
PersistenceProvider.create({
persistOptions: {
path: `${path}.columns`,
Expand All @@ -39,7 +41,9 @@ export function initPersist(
}

if (persistSort) {
const persistWith = isObject(persistSort) ? persistSort : rootPersistWith;
const persistWith = isObject(persistSort)
? PersistenceProvider.mergePersistOptions(rootPersistWith, persistSort)
: rootPersistWith;
PersistenceProvider.create({
persistOptions: {
path: `${path}.sortBy`,
Expand All @@ -55,7 +59,9 @@ export function initPersist(
}

if (persistGrouping) {
const persistWith = isObject(persistSort) ? persistSort : rootPersistWith;
const persistWith = isObject(persistGrouping)
? PersistenceProvider.mergePersistOptions(rootPersistWith, persistGrouping)
: rootPersistWith;
PersistenceProvider.create({
persistOptions: {
path: `${path}.groupBy`,
Expand Down
8 changes: 6 additions & 2 deletions cmp/grouping/GroupingChooserModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,9 @@ export class GroupingChooserModel extends HoistModel {
...rootPersistWith
}: GroupingChooserPersistOptions) {
if (persistValue) {
const persistWith = isObject(persistValue) ? persistValue : rootPersistWith;
const persistWith = isObject(persistValue)
? PersistenceProvider.mergePersistOptions(rootPersistWith, persistValue)
: rootPersistWith;
PersistenceProvider.create({
persistOptions: {
path: `${path}.value`,
Expand All @@ -308,7 +310,9 @@ export class GroupingChooserModel extends HoistModel {
}

if (persistFavorites) {
const persistWith = isObject(persistFavorites) ? persistFavorites : rootPersistWith,
const persistWith = isObject(persistFavorites)
? PersistenceProvider.mergePersistOptions(rootPersistWith, persistFavorites)
: rootPersistWith,
provider = PersistenceProvider.create({
persistOptions: {
path: `${path}.favorites`,
Expand Down
12 changes: 9 additions & 3 deletions cmp/zoneGrid/impl/InitPersist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ export function initPersist(
}: ZoneGridModelPersistOptions
) {
if (persistMappings) {
const persistWith = isObject(persistMappings) ? persistMappings : rootPersistWith;
const persistWith = isObject(persistMappings)
? PersistenceProvider.mergePersistOptions(rootPersistWith, persistMappings)
: rootPersistWith;
PersistenceProvider.create({
persistOptions: {
path: `${path}.mappings`,
Expand All @@ -39,7 +41,9 @@ export function initPersist(
}

if (persistGrouping) {
const persistWith = isObject(persistGrouping) ? persistGrouping : rootPersistWith;
const persistWith = isObject(persistGrouping)
? PersistenceProvider.mergePersistOptions(rootPersistWith, persistGrouping)
: rootPersistWith;
PersistenceProvider.create({
persistOptions: {
path: `${path}.groupBy`,
Expand All @@ -54,7 +58,9 @@ export function initPersist(
}

if (persistSort) {
const persistWith = isObject(persistSort) ? persistSort : rootPersistWith;
const persistWith = isObject(persistSort)
? PersistenceProvider.mergePersistOptions(rootPersistWith, persistSort)
: rootPersistWith;
PersistenceProvider.create({
persistOptions: {
path: `${path}.sortBy`,
Expand Down
3 changes: 1 addition & 2 deletions core/HoistBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,7 @@ export abstract class HoistBase {
PersistenceProvider.create({
persistOptions: {
path: property,
...this.persistWith,
...options
...PersistenceProvider.mergePersistOptions(this.persistWith, options)
},
owner: this,
target: {
Expand Down
5 changes: 4 additions & 1 deletion core/HoistBaseDecorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ function createPersistDescriptor(
// codeValue undefined if no initial in-code value provided, otherwise call to get initial value.
ret = codeValue?.call(this);

const persistOptions = {path: property, ...this.persistWith, ...options};
const persistOptions = {
path: property,
...PersistenceProvider.mergePersistOptions(this.persistWith, options)
};
PersistenceProvider.create({
persistOptions,
owner: this,
Expand Down
31 changes: 31 additions & 0 deletions core/persist/PersistenceProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
import {logDebug, logError, throwIf} from '@xh/hoist/utils/js';
import {
cloneDeep,
compact,
debounce as lodashDebounce,
get,
isEmpty,
isNumber,
isString,
isUndefined,
omit,
set,
toPath
} from 'lodash';
Expand Down Expand Up @@ -91,6 +93,35 @@ export abstract class PersistenceProvider<S = any> {
}
}

/**
* Merge PersistOptions, respecting provider types, with later options overriding earlier ones.
*/
static mergePersistOptions(
defaults: PersistOptions,
...overrides: PersistOptions[]
): PersistOptions {
const TYPE_RELATED_KEYS = [
'type',
'prefKey',
'localStorageKey',
'sessionStorageKey',
'dashViewModel',
'viewManagerModel',
'getData',
'setData'
];
return compact(overrides).reduce(
(ret, override) =>
TYPE_RELATED_KEYS.some(key => override[key])
? {
...omit(ret, ...TYPE_RELATED_KEYS),
...override
}
: {...ret, ...override},
defaults
);
}

/** Read persisted state at this provider's path. */
read(): PersistableState<S> {
const state = get(this.readRaw(), this.path);
Expand Down

0 comments on commit 16786f6

Please sign in to comment.