Skip to content

Commit

Permalink
feat: Add the possibility to specify action types to invoke the reduc…
Browse files Browse the repository at this point in the history
…er in the storage sync helper
  • Loading branch information
Abhishek Buragohain committed Mar 3, 2024
1 parent f703128 commit 97a95a0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
2 changes: 2 additions & 0 deletions packages/@o3r/store-sync/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@angular-devkit/schematics": "~16.2.0",
"@ngrx/entity": "~16.3.0",
"@ngrx/store": "~16.3.0",
"@ngrx/store-devtools": "~16.3.0",
"@o3r/core": "workspace:^",
"@o3r/dev-tools": "workspace:^",
"@o3r/logger": "workspace:^",
Expand Down Expand Up @@ -57,6 +58,7 @@
"@angular/platform-browser-dynamic": "~16.2.0",
"@ngrx/entity": "~16.3.0",
"@ngrx/store": "~16.3.0",
"@ngrx/store-devtools": "~16.3.0",
"@nx/eslint-plugin": "~16.10.0",
"@nx/js": "~16.10.0",
"@nx/linter": "~16.10.0",
Expand Down
14 changes: 13 additions & 1 deletion packages/@o3r/store-sync/src/sync-storage/storage-sync.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { INIT, UPDATE } from '@ngrx/store';
import type { RECOMPUTE } from '@ngrx/store-devtools';
import { deepFill } from '@o3r/core';
import type { Logger } from '@o3r/core';
import type { StorageKeyConfiguration, StorageKeys, SyncStorageConfig, SyncStorageSyncOptions } from './interfaces';

/** The recompute action type of ngrx store devtools */
const RECOMPUTE_ACTION: typeof RECOMPUTE = '@ngrx/store-devtools/recompute';

/**
* Reviver the date from a JSON field if the string is matching iso format. Return the same value otherwise
*
Expand Down Expand Up @@ -319,6 +323,12 @@ export const syncStorage = (config: SyncStorageConfig) => (reducer: any) => {
const logger = config.logger || console;

const stateKeys = validateStateKeys(config.keys);
const syncForFeature = stateKeys.some((key) => {
if (typeof key === 'object') {
const currentKey = Object.keys(key)[0];
return !!(key?.[currentKey] as SyncStorageSyncOptions)?.syncForFeature;
}
});
const rehydratedState = config.rehydrate
? rehydrateApplicationState(stateKeys, config.storage, config.storageKeySerializer, config.restoreDates, logger)
: undefined;
Expand All @@ -328,7 +338,9 @@ export const syncStorage = (config: SyncStorageConfig) => (reducer: any) => {

// If state arrives undefined, we need to let it through the supplied reducer
// in order to get a complete state as defined by user
if (action.type === INIT && !state) {
const checkForUpdateAndRecompute = syncForFeature && (action.type === UPDATE || action.type === RECOMPUTE_ACTION);

if ((action.type === INIT || checkForUpdateAndRecompute) && !state) {
nextState = reducer(state, action);
} else {
nextState = { ...state };
Expand Down
2 changes: 2 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8621,6 +8621,7 @@ __metadata:
"@angular/platform-browser-dynamic": "npm:~16.2.0"
"@ngrx/entity": "npm:~16.3.0"
"@ngrx/store": "npm:~16.3.0"
"@ngrx/store-devtools": "npm:~16.3.0"
"@nx/eslint-plugin": "npm:~16.10.0"
"@nx/js": "npm:~16.10.0"
"@nx/linter": "npm:~16.10.0"
Expand Down Expand Up @@ -8659,6 +8660,7 @@ __metadata:
"@angular-devkit/schematics": ~16.2.0
"@ngrx/entity": ~16.3.0
"@ngrx/store": ~16.3.0
"@ngrx/store-devtools": ~16.3.0
"@o3r/core": "workspace:^"
"@o3r/dev-tools": "workspace:^"
"@o3r/logger": "workspace:^"
Expand Down

0 comments on commit 97a95a0

Please sign in to comment.