Skip to content

Commit

Permalink
feat: add isExportedAttributes to persisted stores for prevent export…
Browse files Browse the repository at this point in the history
… attributes
  • Loading branch information
MatthewPattell committed Jul 1, 2024
1 parent 8bc0c68 commit 4c7fdf2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
17 changes: 15 additions & 2 deletions src/make-exported.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ const makeExported = <T extends object>(
store[exportedPropName] = { ...(shouldExtend ? store?.[exportedPropName] ?? {} : {}), ...props };
};

/**
* Excluded in persistStore level
* @see IPersistOptions
*/
const isPropExcludedInPersist = (store: TAnyStore): boolean => {
return store?.['libStorageOptions']?.isExportedAttributes || false;
};

/**
* Check if store prop is observable exported
*/
Expand All @@ -31,7 +39,12 @@ const isPropSimpleExported = (store: TAnyStore, prop: string): boolean =>
/**
* Check if store prop is excluded from export
*/
const isPropExcludedFromExport = (store: TAnyStore, prop: string): boolean =>
store?.[exportedPropName]?.[prop] === 'excluded';
const isPropExcludedFromExport = (
store: TAnyStore,
prop: string,
withNotExported = false,
): boolean =>
store?.[exportedPropName]?.[prop] === 'excluded' ||
(!withNotExported && isPropExcludedInPersist(store));

export { makeExported, isPropObservableExported, isPropSimpleExported, isPropExcludedFromExport };
15 changes: 8 additions & 7 deletions src/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -571,14 +571,14 @@ class Manager {
/**
* Get store state
*/
public getStoreState(store: TAnyStore): Record<string, any> {
return store.toJSON?.() ?? Manager.getObservableProps(store);
public getStoreState(store: TAnyStore, withNotExported = false): Record<string, any> {
return store.toJSON?.() ?? Manager.getObservableProps(store, withNotExported);
}

/**
* Get store's state
*/
public toJSON(ids?: string[]): Record<string, any> {
public toJSON(ids?: string[], isIncludeExported = false): Record<string, any> {
const result = {};
const stores = Array.isArray(ids)
? ids.reduce((res, id) => {
Expand All @@ -591,7 +591,7 @@ class Manager {
: this.stores;

for (const [storeId, store] of stores.entries()) {
result[storeId] = this.getStoreState(store);
result[storeId] = this.getStoreState(store, isIncludeExported);
}

return result;
Expand All @@ -606,7 +606,7 @@ class Manager {
}

try {
await this.storage.saveStoreData(store, this.getStoreState(store));
await this.storage.saveStoreData(store, this.getStoreState(store, true));

return true;
} catch (e) {
Expand All @@ -619,13 +619,14 @@ class Manager {
/**
* Get observable store props (fields)
*/
public static getObservableProps(store: TAnyStore): Record<string, any> {
public static getObservableProps(store: TAnyStore, withNotExported = false): Record<string, any> {
const props = toJS(store);

return Object.entries(props).reduce(
(res, [prop, value]) => ({
...res,
...((isObservableProp(store, prop) && !isPropExcludedFromExport(store, prop)) ||
...((isObservableProp(store, prop) &&
!isPropExcludedFromExport(store, prop, withNotExported)) ||
isPropSimpleExported(store, prop)
? { [prop]: value }
: {}),
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,7 @@ export interface IPersistOptions {
// first storage => *, by default
[storageId: string]: string[];
};
// disable export all store observable props except props marker with makeExported
// default: false
isExportedAttributes?: boolean;
}

0 comments on commit 4c7fdf2

Please sign in to comment.