-
Notifications
You must be signed in to change notification settings - Fork 405
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(storage-plugin): allow providing feature states
This commit introduces the ability to specify states for persistence within lazy modules. Previously, it was only possible to specify a list of states for persistence when providing the storage plugin at the root level.
- Loading branch information
Showing
10 changed files
with
329 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
export * from './symbols'; | ||
export * from './final-options'; | ||
export * from './storage-key'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { Injectable, Injector, inject } from '@angular/core'; | ||
import { | ||
STORAGE_ENGINE, | ||
StorageEngine, | ||
StorageKey, | ||
ɵextractStringKey, | ||
ɵisKeyWithExplicitEngine, | ||
ɵNGXS_STORAGE_PLUGIN_OPTIONS | ||
} from '@ngxs/storage-plugin/internals'; | ||
|
||
interface KeyWithEngine { | ||
key: string; | ||
engine: StorageEngine; | ||
} | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
export class ɵNgxsStoragePluginKeysManager { | ||
/** Store keys separately in a set so we're able to check if the key already exists. */ | ||
private readonly _keys = new Set<string>(); | ||
|
||
private readonly _injector = inject(Injector); | ||
|
||
private readonly _keysWithEngines: KeyWithEngine[] = []; | ||
|
||
constructor() { | ||
const { keys } = inject(ɵNGXS_STORAGE_PLUGIN_OPTIONS); | ||
this.addKeys(keys); | ||
} | ||
|
||
getKeysWithEngines() { | ||
// Spread to prevent external code from directly modifying the internal state. | ||
return [...this._keysWithEngines]; | ||
} | ||
|
||
addKeys(storageKeys: StorageKey[]): void { | ||
for (const storageKey of storageKeys) { | ||
const key = ɵextractStringKey(storageKey); | ||
|
||
// The user may call `withStorageFeature` with the same state multiple times. | ||
// Let's prevent duplicating state names in the `keysWithEngines` list. | ||
// Please note that calling provideStates multiple times with the same state is | ||
// acceptable behavior. This may occur because the state could be necessary at the | ||
// feature level, and different parts of the application might require its registration. | ||
// Consequently, `withStorageFeature` may also be called multiple times. | ||
if (this._keys.has(key)) { | ||
continue; | ||
} | ||
|
||
this._keys.add(key); | ||
|
||
const engine = ɵisKeyWithExplicitEngine(storageKey) | ||
? this._injector.get(storageKey.engine) | ||
: this._injector.get(STORAGE_ENGINE); | ||
|
||
this._keysWithEngines.push({ key, engine }); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.