From 5bf8b48efcf727768032fcb8b3ba3ea20359a8b3 Mon Sep 17 00:00:00 2001 From: Benno Kohrs Date: Sat, 17 Apr 2021 16:05:19 +0200 Subject: [PATCH 01/10] optimized default Storage key positon by setting it into a config object instead of holding the whole default Storage reference --- .../src/collection/collection.persistent.ts | 4 +- packages/core/src/state/state.persistent.ts | 2 +- packages/core/src/storages/index.ts | 50 ++++++++++---- packages/core/src/storages/persistent.ts | 68 ++++++++++++++----- 4 files changed, 91 insertions(+), 33 deletions(-) diff --git a/packages/core/src/collection/collection.persistent.ts b/packages/core/src/collection/collection.persistent.ts index fed0825c..68020942 100644 --- a/packages/core/src/collection/collection.persistent.ts +++ b/packages/core/src/collection/collection.persistent.ts @@ -106,7 +106,7 @@ export class CollectionPersistent extends Persistent { // Check if Collection is Persisted const isPersisted = await this.agileInstance().storages.get( _key, - this.defaultStorageKey + this.config.defaultStorageKey as any ); if (!isPersisted) return false; @@ -137,7 +137,7 @@ export class CollectionPersistent extends Persistent { // Get Storage Value const storageValue = await this.agileInstance().storages.get( itemStorageKey, - this.defaultStorageKey + this.config.defaultStorageKey as any ); if (!storageValue) continue; diff --git a/packages/core/src/state/state.persistent.ts b/packages/core/src/state/state.persistent.ts index 8cd0336b..052dac01 100644 --- a/packages/core/src/state/state.persistent.ts +++ b/packages/core/src/state/state.persistent.ts @@ -97,7 +97,7 @@ export class StatePersistent extends Persistent { // Load Value from default Storage const loadedValue = await this.agileInstance().storages.get( _key, - this.defaultStorageKey + this.config.defaultStorageKey as any ); if (!loadedValue) return false; diff --git a/packages/core/src/storages/index.ts b/packages/core/src/storages/index.ts index 4d372c2c..c220a548 100644 --- a/packages/core/src/storages/index.ts +++ b/packages/core/src/storages/index.ts @@ -11,7 +11,7 @@ import { export class Storages { public agileInstance: () => Agile; - public defaultStorage?: Storage; + public config: StoragesConfigInterface; public storages: { [key: string]: Storage } = {}; // All registered Storages public persistentInstances: Set = new Set(); @@ -21,11 +21,16 @@ export class Storages { * @param agileInstance - An Instance of Agile * @param config - Config */ - constructor(agileInstance: Agile, config: StoragesConfigInterface = {}) { + constructor( + agileInstance: Agile, + config: CreateStoragesConfigInterface = {} + ) { this.agileInstance = () => agileInstance; config = defineConfig(config, { localStorage: false, + defaultStorageKey: null, }); + this.config = { defaultStorageKey: config.defaultStorageKey as any }; if (config.localStorage) this.instantiateLocalStorage(); } @@ -91,11 +96,11 @@ export class Storages { // Register Storage this.storages[storage.key] = storage; - if (config.default) this.defaultStorage = storage; + if (config.default) this.config.defaultStorageKey = storage.key; this.persistentInstances.forEach((persistent) => { // If Persistent isn't ready and has no default StorageKey.. reassignStorageKeys and try to load it - if (!persistent.defaultStorageKey) { + if (!persistent.ready) { persistent.assignStorageKeys(); const isValid = persistent.validatePersistent(); if (isValid) persistent.initialLoading(); @@ -116,20 +121,27 @@ export class Storages { /** * @internal * Get Storage at Key/Name - * @param key - Key/Name of Storage + * @param storageKey - Key/Name of Storage */ - public getStorage(key: StorageKey): Storage | undefined { - const storage = this.storages[key]; + public getStorage( + storageKey: StorageKey | undefined | null + ): Storage | undefined { + if (!storageKey) return undefined; + const storage = this.storages[storageKey]; // Check if Storage exists if (!storage) { - Agile.logger.error(`Storage with the key/name '${key}' doesn't exist`); + Agile.logger.error( + `Storage with the key/name '${storageKey}' doesn't exist!` + ); return undefined; } // Check if Storage is ready if (!storage.ready) { - Agile.logger.error(`Storage with the key/name '${key}' isn't ready`); + Agile.logger.error( + `Storage with the key/name '${storageKey}' isn't ready yet!` + ); return undefined; } @@ -163,7 +175,8 @@ export class Storages { } // Call get Method in default Storage - return this.defaultStorage?.get(key) || Promise.resolve(undefined); + const defaultStorage = this.getStorage(this.config.defaultStorageKey); + return defaultStorage?.get(key) || Promise.resolve(undefined); } //========================================================================================================= @@ -196,7 +209,8 @@ export class Storages { } // Call set Method in default Storage - this.defaultStorage?.set(key, value); + const defaultStorage = this.getStorage(this.config.defaultStorageKey); + defaultStorage?.set(key, value); } //========================================================================================================= @@ -224,7 +238,8 @@ export class Storages { } // Call remove Method in default Storage - this.defaultStorage?.remove(key); + const defaultStorage = this.getStorage(this.config.defaultStorageKey); + defaultStorage?.remove(key); } //========================================================================================================= @@ -258,9 +273,18 @@ export class Storages { /** * @param localStorage - If Local Storage should be instantiated + * @param defaultStorage - Default Storage Key */ -export interface StoragesConfigInterface { +export interface CreateStoragesConfigInterface { localStorage?: boolean; + defaultStorageKey?: StorageKey; +} + +/** + * @param defaultStorage - Default Storage Key + */ +export interface StoragesConfigInterface { + defaultStorageKey: StorageKey | null; } /** diff --git a/packages/core/src/storages/persistent.ts b/packages/core/src/storages/persistent.ts index c3702f70..b28970fd 100644 --- a/packages/core/src/storages/persistent.ts +++ b/packages/core/src/storages/persistent.ts @@ -1,9 +1,10 @@ -import { Agile, defineConfig, StorageKey } from '../internal'; +import { Agile, copy, defineConfig, StorageKey } from '../internal'; export class Persistent { public agileInstance: () => Agile; public static placeHolderKey = '__THIS_IS_A_PLACEHOLDER__'; + public config: PersistentConfigInterface; public _key: PersistentKey; public ready = false; @@ -12,7 +13,6 @@ export class Persistent { // StorageKeys of Storages in that the Persisted Value gets saved public storageKeys: StorageKey[] = []; - public defaultStorageKey: StorageKey | undefined; /** * @internal @@ -30,13 +30,19 @@ export class Persistent { config = defineConfig(config, { instantiate: true, storageKeys: [], + defaultStorageKey: null, }); this.agileInstance().storages.persistentInstances.add(this); - if (config.instantiate) + this.config = { defaultStorageKey: config.defaultStorageKey as any }; + + // Instantiate Persistent + if (config.instantiate) { this.instantiatePersistent({ storageKeys: config.storageKeys, key: config.key, + defaultStorageKey: config.defaultStorageKey, }); + } } /** @@ -77,9 +83,12 @@ export class Persistent { * have to define some stuff before being able to instantiate the parent (this) * @param config - Config */ - public instantiatePersistent(config: PersistentConfigInterface = {}) { + public instantiatePersistent( + config: InstantiatePersistentConfigInterface = {} + ) { this._key = this.formatKey(config.key) || Persistent.placeHolderKey; - this.assignStorageKeys(config.storageKeys); + console.log(config.storageKeys, config.defaultStorageKey); + this.assignStorageKeys(config.storageKeys, config.defaultStorageKey); this.validatePersistent(); } @@ -102,7 +111,7 @@ export class Persistent { } // Validate StorageKeys - if (!this.defaultStorageKey || this.storageKeys.length <= 0) { + if (!this.config.defaultStorageKey || this.storageKeys.length <= 0) { Agile.logger.error( 'No persist Storage Key found! Please provide at least one Storage Key.' ); @@ -120,23 +129,37 @@ export class Persistent { * @internal * Assign new StorageKeys to Persistent and overwrite the old ones * @param storageKeys - New Storage Keys + * @param defaultStorageKey - Key of default Storage */ - public assignStorageKeys(storageKeys: StorageKey[] = []) { + public assignStorageKeys( + storageKeys: StorageKey[] = [], + defaultStorageKey?: StorageKey + ) { const storages = this.agileInstance().storages; + const _storageKeys = copy(storageKeys); + + // Print warning if default StorageKey passed, but it isn't in stoargeKeys + if (defaultStorageKey && _storageKeys.includes(defaultStorageKey)) { + Agile.logger.warn( + `Default Storage Key '${defaultStorageKey}' isn't contained in storageKeys!`, + _storageKeys + ); + _storageKeys.push(defaultStorageKey); + } - // Set default Agile Storage to defaultStorage if no storageKey provided - if (storageKeys.length <= 0) { - this.storageKeys = []; - if (storages.defaultStorage) { - const key = storages.defaultStorage.key; - this.defaultStorageKey = key; - this.storageKeys.push(key); + // Add default Storage of AgileTs if no storageKey provided + if (_storageKeys.length <= 0) { + const _defaultStorageKey = + defaultStorageKey || storages.config.defaultStorageKey; + if (_defaultStorageKey) { + this.config.defaultStorageKey = _defaultStorageKey; + this.storageKeys.push(_defaultStorageKey); } return; } - this.storageKeys = storageKeys; - this.defaultStorageKey = storageKeys[0]; + this.storageKeys = _storageKeys; + this.config.defaultStorageKey = defaultStorageKey || _storageKeys[0]; } //========================================================================================================= @@ -215,19 +238,30 @@ export type PersistentKey = string | number; /** * @param key - Key/Name of Persistent * @param storageKeys - Keys of Storages in that the persisted Value gets saved + * @param defaultStorage - Default Storage Key * @param instantiate - If Persistent gets Instantiated immediately */ export interface CreatePersistentConfigInterface { key?: PersistentKey; storageKeys?: StorageKey[]; + defaultStorageKey?: StorageKey; instantiate?: boolean; } +/** + * @param defaultStorage - Default Storage Key + */ +export interface PersistentConfigInterface { + defaultStorageKey: StorageKey | null; +} + /** * @param key - Key/Name of Persistent * @param storageKeys - Keys of Storages in that the persisted Value gets saved + * @param defaultStorage - Default Storage Key */ -export interface PersistentConfigInterface { +export interface InstantiatePersistentConfigInterface { key?: PersistentKey; storageKeys?: StorageKey[]; + defaultStorageKey?: StorageKey; } From 791398a3ddc1d364219f2cafb4735d6b365c340c Mon Sep 17 00:00:00 2001 From: Benno Kohrs Date: Sat, 17 Apr 2021 16:05:42 +0200 Subject: [PATCH 02/10] fixed storage tests --- .../tests/unit/collection/collection.test.ts | 2 +- .../tests/unit/storages/persistent.test.ts | 56 ++++++++++++++----- .../core/tests/unit/storages/storages.test.ts | 33 +++++++---- 3 files changed, 66 insertions(+), 25 deletions(-) diff --git a/packages/core/tests/unit/collection/collection.test.ts b/packages/core/tests/unit/collection/collection.test.ts index d9344955..8c1f219d 100644 --- a/packages/core/tests/unit/collection/collection.test.ts +++ b/packages/core/tests/unit/collection/collection.test.ts @@ -1032,7 +1032,7 @@ describe('Collection Tests', () => { expect(ComputedTracker.tracked).not.toHaveBeenCalled(); }); - it('should return and track existing placeholder Group (config.notExisting = true)', () => { + it('should return and track existing placeholder Group (config.notExisting = true)', () => { dummyGroup.isPlaceholder = true; const response = collection.getGroup('dummyGroup', { diff --git a/packages/core/tests/unit/storages/persistent.test.ts b/packages/core/tests/unit/storages/persistent.test.ts index 2baf0c61..94ee3f93 100644 --- a/packages/core/tests/unit/storages/persistent.test.ts +++ b/packages/core/tests/unit/storages/persistent.test.ts @@ -25,6 +25,7 @@ describe('Persistent Tests', () => { expect(persistent.instantiatePersistent).toHaveBeenCalledWith({ storageKeys: [], key: undefined, + defaultStorageKey: null, }); expect( dummyAgile.storages.persistentInstances.has(persistent) @@ -35,7 +36,7 @@ describe('Persistent Tests', () => { expect(persistent.isPersisted).toBeFalsy(); expect(persistent.onLoad).toBeUndefined(); expect(persistent.storageKeys).toStrictEqual([]); - expect(persistent.defaultStorageKey).toBeUndefined(); + expect(persistent.config).toStrictEqual({ defaultStorageKey: null }); }); it('should create Persistent (specific config)', () => { @@ -47,12 +48,14 @@ describe('Persistent Tests', () => { const persistent = new Persistent(dummyAgile, { storageKeys: ['test1', 'test2'], key: 'persistentKey', + defaultStorageKey: 'test1', }); expect(persistent).toBeInstanceOf(Persistent); expect(persistent.instantiatePersistent).toHaveBeenCalledWith({ storageKeys: ['test1', 'test2'], key: 'persistentKey', + defaultStorageKey: 'test1', }); expect( dummyAgile.storages.persistentInstances.has(persistent) @@ -63,7 +66,7 @@ describe('Persistent Tests', () => { expect(persistent.isPersisted).toBeFalsy(); expect(persistent.onLoad).toBeUndefined(); expect(persistent.storageKeys).toStrictEqual([]); - expect(persistent.defaultStorageKey).toBeUndefined(); + expect(persistent.config).toStrictEqual({ defaultStorageKey: 'test1' }); }); it('should create Persistent (config.instantiate = false)', () => { @@ -85,7 +88,7 @@ describe('Persistent Tests', () => { expect(persistent.isPersisted).toBeFalsy(); expect(persistent.onLoad).toBeUndefined(); expect(persistent.storageKeys).toStrictEqual([]); - expect(persistent.defaultStorageKey).toBeUndefined(); + expect(persistent.config).toStrictEqual({ defaultStorageKey: null }); }); describe('Persistent Function Tests', () => { @@ -122,15 +125,15 @@ describe('Persistent Tests', () => { persistent.instantiatePersistent({ key: 'persistentKey', storageKeys: ['myName', 'is', 'jeff'], + defaultStorageKey: 'jeff', }); expect(persistent._key).toBe('persistentKey'); expect(persistent.formatKey).toHaveBeenCalledWith('persistentKey'); - expect(persistent.assignStorageKeys).toHaveBeenCalledWith([ - 'myName', - 'is', - 'jeff', - ]); + expect(persistent.assignStorageKeys).toHaveBeenCalledWith( + ['myName', 'is', 'jeff'], + 'jeff' + ); expect(persistent.validatePersistent).toHaveBeenCalled(); }); }); @@ -138,7 +141,7 @@ describe('Persistent Tests', () => { describe('validatePersistent function tests', () => { beforeEach(() => { persistent.key = Persistent.placeHolderKey; - persistent.defaultStorageKey = undefined; + persistent.config.defaultStorageKey = null; persistent.storageKeys = []; persistent.ready = undefined as any; }); @@ -168,7 +171,7 @@ describe('Persistent Tests', () => { }); it('should return false if no set key and set StorageKeys', () => { - persistent.defaultStorageKey = 'test'; + persistent.config.defaultStorageKey = 'test'; persistent.storageKeys = ['test']; const isValid = persistent.validatePersistent(); @@ -183,7 +186,7 @@ describe('Persistent Tests', () => { it('should return true if set key and set StorageKeys', () => { persistent._key = 'persistentKey'; - persistent.defaultStorageKey = 'test'; + persistent.config.defaultStorageKey = 'test'; persistent.storageKeys = ['test']; const isValid = persistent.validatePersistent(); @@ -202,7 +205,33 @@ describe('Persistent Tests', () => { 'test2', 'test3', ]); - expect(persistent.defaultStorageKey).toBe('test1'); + expect(persistent.config.defaultStorageKey).toBe('test1'); + expect(console.warn).not.toHaveBeenCalled(); + }); + + it('should assign passed StorageKeys and set defaultStorageKey as default StorageKey', () => { + persistent.assignStorageKeys(['test1', 'test2', 'test3'], 'test3'); + + expect(persistent.storageKeys).toStrictEqual([ + 'test1', + 'test2', + 'test3', + ]); + expect(persistent.config.defaultStorageKey).toBe('test3'); + expect(console.warn).not.toHaveBeenCalled(); + }); + + it('should assign passed StorageKeys and set not existing defaultStorageKey as default StorageKey, push it into storageKeys and print warning', () => { + persistent.assignStorageKeys(['test1', 'test2', 'test3'], 'test4'); + + expect(persistent.storageKeys).toStrictEqual([ + 'test1', + 'test2', + 'test3', + 'test4', + ]); + expect(persistent.config.defaultStorageKey).toBe('test4'); + expect(console.warn).toHaveBeenCalledWith('todo'); }); it('should try to get default StorageKey from Agile if no StorageKeys passed', () => { @@ -227,7 +256,8 @@ describe('Persistent Tests', () => { persistent.assignStorageKeys(); expect(persistent.storageKeys).toStrictEqual(['storage1']); - expect(persistent.defaultStorageKey).toBe('storage1'); + expect(persistent.config.defaultStorageKey).toBe('storage1'); + expect(console.warn).not.toHaveBeenCalled(); }); }); diff --git a/packages/core/tests/unit/storages/storages.test.ts b/packages/core/tests/unit/storages/storages.test.ts index 6f125f9f..407c7a01 100644 --- a/packages/core/tests/unit/storages/storages.test.ts +++ b/packages/core/tests/unit/storages/storages.test.ts @@ -16,16 +16,19 @@ describe('Storages Tests', () => { it('should create Storages (default config)', () => { const storages = new Storages(dummyAgile); - expect(storages.defaultStorage).toBeUndefined(); + expect(storages.config).toStrictEqual({ defaultStorage: null }); expect(storages.storages).toStrictEqual({}); expect(storages.persistentInstances.size).toBe(0); expect(storages.instantiateLocalStorage).not.toHaveBeenCalled(); }); - it('should create Storages and should get a warning (config.localStorage = true)', () => { - const storages = new Storages(dummyAgile, { localStorage: true }); + it('should create Storages (specific config)', () => { + const storages = new Storages(dummyAgile, { + defaultStorageKey: 'jeff', + localStorage: true, + }); - expect(storages.defaultStorage).toBeUndefined(); + expect(storages.config).toStrictEqual({ defaultStorage: 'jeff' }); expect(storages.storages).toStrictEqual({}); expect(storages.persistentInstances.size).toBe(0); expect(storages.instantiateLocalStorage).toHaveBeenCalled(); @@ -101,7 +104,7 @@ describe('Storages Tests', () => { expect(storages.storages).toHaveProperty('storage1'); expect(storages.storages['storage1']).toBe(dummyStorage1); - expect(storages.defaultStorage).toBe(dummyStorage1); + expect(storages.config.defaultStorageKey).toBe('storage1'); expect(response).toBeTruthy(); }); @@ -114,7 +117,7 @@ describe('Storages Tests', () => { expect(storages.storages).toHaveProperty('storage1'); expect(storages.storages['storage1']).toBe(dummyStorage1); - expect(storages.defaultStorage).toBe(dummyStorage1); + expect(storages.config.defaultStorageKey).toBe('storage1'); expect(response).toBeTruthy(); }); @@ -125,7 +128,7 @@ describe('Storages Tests', () => { expect(storages.storages).toHaveProperty('storage2'); expect(storages.storages['storage2']).toBe(dummyStorage2); - expect(storages.defaultStorage).toBe(dummyStorage1); + expect(storages.config.defaultStorageKey).toBe('storage1'); expect(response).toBeTruthy(); }); @@ -136,7 +139,7 @@ describe('Storages Tests', () => { expect(storages.storages).toHaveProperty('storage2'); expect(storages.storages['storage2']).toBe(dummyStorage2); - expect(storages.defaultStorage).toBe(dummyStorage2); + expect(storages.config.defaultStorageKey).toBe('storage2'); expect(response).toBeTruthy(); }); @@ -212,6 +215,14 @@ describe('Storages Tests', () => { const response = storages.getStorage('storage1'); expect(response).toBe(dummyStorage1); + expect(console.error).not.toHaveBeenCalled(); + }); + + it("shouldn't get Storage with undefined key", () => { + const response = storages.getStorage(null); + + expect(response).toBeUndefined(); + expect(console.error).not.toHaveBeenCalled(); }); it("shouldn't get not existing Storage", () => { @@ -219,7 +230,7 @@ describe('Storages Tests', () => { expect(response).toBeUndefined(); expect(console.error).toHaveBeenCalledWith( - "Agile Error: Storage with the key/name 'notExistingStorage' doesn't exist" + "Agile Error: Storage with the key/name 'notExistingStorage' doesn't exist!" ); }); @@ -230,7 +241,7 @@ describe('Storages Tests', () => { expect(response).toBeUndefined(); expect(console.error).toHaveBeenCalledWith( - "Agile Error: Storage with the key/name 'storage1' isn't ready" + "Agile Error: Storage with the key/name 'storage1' isn't ready yet!" ); }); }); @@ -266,7 +277,7 @@ describe('Storages Tests', () => { expect(response).toBe('dummyStorage1Response'); expect(dummyStorage1.get).toHaveBeenCalledWith('value1'); expect(console.error).toHaveBeenCalledWith( - "Agile Error: Storage with the key/name 'notExistingStorage' doesn't exist" + "Agile Error: Storage with the key/name 'notExistingStorage' doesn't exist!" ); }); From 81c14761f68b8ab65501c4dfaa243942a9272b9d Mon Sep 17 00:00:00 2001 From: Benno Kohrs Date: Sat, 17 Apr 2021 17:20:31 +0200 Subject: [PATCH 03/10] fixed some tests --- .../src/collection/collection.persistent.ts | 2 + packages/core/src/state/state.persistent.ts | 2 + packages/core/src/storages/persistent.ts | 19 +++----- .../collection/collection.persistent.test.ts | 45 +++++++++++-------- .../tests/unit/state/state.persistent.test.ts | 15 ++++--- .../tests/unit/storages/persistent.test.ts | 13 ++++-- .../core/tests/unit/storages/storages.test.ts | 8 ++-- 7 files changed, 60 insertions(+), 44 deletions(-) diff --git a/packages/core/src/collection/collection.persistent.ts b/packages/core/src/collection/collection.persistent.ts index 68020942..e561e640 100644 --- a/packages/core/src/collection/collection.persistent.ts +++ b/packages/core/src/collection/collection.persistent.ts @@ -35,11 +35,13 @@ export class CollectionPersistent extends Persistent { config = defineConfig(config, { instantiate: true, storageKeys: [], + defaultStorageKey: null, }); this.collection = () => collection; this.instantiatePersistent({ key: config.key, storageKeys: config.storageKeys, + defaultStorageKey: config.defaultStorageKey, }); // Load/Store persisted Value/s for the first Time diff --git a/packages/core/src/state/state.persistent.ts b/packages/core/src/state/state.persistent.ts index 052dac01..91f56c03 100644 --- a/packages/core/src/state/state.persistent.ts +++ b/packages/core/src/state/state.persistent.ts @@ -27,11 +27,13 @@ export class StatePersistent extends Persistent { config = defineConfig(config, { instantiate: true, storageKeys: [], + defaultStorageKey: null, }); this.state = () => state; this.instantiatePersistent({ key: config.key, storageKeys: config.storageKeys, + defaultStorageKey: config.defaultStorageKey, }); // Load/Store persisted Value for the first Time diff --git a/packages/core/src/storages/persistent.ts b/packages/core/src/storages/persistent.ts index b28970fd..84f2158a 100644 --- a/packages/core/src/storages/persistent.ts +++ b/packages/core/src/storages/persistent.ts @@ -87,7 +87,6 @@ export class Persistent { config: InstantiatePersistentConfigInterface = {} ) { this._key = this.formatKey(config.key) || Persistent.placeHolderKey; - console.log(config.storageKeys, config.defaultStorageKey); this.assignStorageKeys(config.storageKeys, config.defaultStorageKey); this.validatePersistent(); } @@ -134,12 +133,12 @@ export class Persistent { public assignStorageKeys( storageKeys: StorageKey[] = [], defaultStorageKey?: StorageKey - ) { + ): void { const storages = this.agileInstance().storages; const _storageKeys = copy(storageKeys); // Print warning if default StorageKey passed, but it isn't in stoargeKeys - if (defaultStorageKey && _storageKeys.includes(defaultStorageKey)) { + if (defaultStorageKey && !_storageKeys.includes(defaultStorageKey)) { Agile.logger.warn( `Default Storage Key '${defaultStorageKey}' isn't contained in storageKeys!`, _storageKeys @@ -149,17 +148,13 @@ export class Persistent { // Add default Storage of AgileTs if no storageKey provided if (_storageKeys.length <= 0) { - const _defaultStorageKey = - defaultStorageKey || storages.config.defaultStorageKey; - if (_defaultStorageKey) { - this.config.defaultStorageKey = _defaultStorageKey; - this.storageKeys.push(_defaultStorageKey); - } - return; + this.config.defaultStorageKey = storages.config.defaultStorageKey as any; + _storageKeys.push(storages.config.defaultStorageKey as any); + } else { + this.config.defaultStorageKey = defaultStorageKey || _storageKeys[0]; } this.storageKeys = _storageKeys; - this.config.defaultStorageKey = defaultStorageKey || _storageKeys[0]; } //========================================================================================================= @@ -169,7 +164,7 @@ export class Persistent { * @internal * Loads/Saves Storage Value for the first Time */ - public async initialLoading() { + public async initialLoading(): Promise { const success = await this.loadPersistedValue(); if (this.onLoad) this.onLoad(success); if (!success) await this.persistValue(); diff --git a/packages/core/tests/unit/collection/collection.persistent.test.ts b/packages/core/tests/unit/collection/collection.persistent.test.ts index 6ad34561..1b91a032 100644 --- a/packages/core/tests/unit/collection/collection.persistent.test.ts +++ b/packages/core/tests/unit/collection/collection.persistent.test.ts @@ -48,6 +48,7 @@ describe('CollectionPersistent Tests', () => { expect(collectionPersistent.instantiatePersistent).toHaveBeenCalledWith({ key: undefined, storageKeys: [], + defaultStorageKey: null, }); expect(collectionPersistent.initialLoading).not.toHaveBeenCalled(); @@ -56,7 +57,9 @@ describe('CollectionPersistent Tests', () => { expect(collectionPersistent.isPersisted).toBeFalsy(); expect(collectionPersistent.onLoad).toBeUndefined(); expect(collectionPersistent.storageKeys).toStrictEqual([]); - expect(collectionPersistent.defaultStorageKey).toBeUndefined(); + expect(collectionPersistent.config).toStrictEqual({ + defaultStorageKey: null, + }); }); it("should create CollectionPersistent and shouldn't call initialLoading if Persistent isn't ready (specific config)", () => { @@ -71,12 +74,14 @@ describe('CollectionPersistent Tests', () => { const collectionPersistent = new CollectionPersistent(dummyCollection, { key: 'collectionPersistentKey', storageKeys: ['test1', 'test2'], + defaultStorageKey: 'test2', }); expect(collectionPersistent).toBeInstanceOf(CollectionPersistent); expect(collectionPersistent.instantiatePersistent).toHaveBeenCalledWith({ key: 'collectionPersistentKey', storageKeys: ['test1', 'test2'], + defaultStorageKey: 'test2', }); expect(collectionPersistent.initialLoading).not.toHaveBeenCalled(); @@ -85,7 +90,9 @@ describe('CollectionPersistent Tests', () => { expect(collectionPersistent.isPersisted).toBeFalsy(); expect(collectionPersistent.onLoad).toBeUndefined(); expect(collectionPersistent.storageKeys).toStrictEqual([]); - expect(collectionPersistent.defaultStorageKey).toBeUndefined(); + expect(collectionPersistent.config).toStrictEqual({ + defaultStorageKey: null, // gets set in 'instantiatePersistent' which is mocked + }); }); it('should create CollectionPersistent and should call initialLoading if Persistent is ready (default config)', () => { @@ -258,7 +265,7 @@ describe('CollectionPersistent Tests', () => { let dummyDefaultGroup: Group; beforeEach(() => { - collectionPersistent.defaultStorageKey = 'test'; + collectionPersistent.config.defaultStorageKey = 'test'; dummyDefaultGroup = new Group(dummyCollection, ['1', '2', '3']); dummyDefaultGroup.persistent = new StatePersistent(dummyDefaultGroup); @@ -294,28 +301,28 @@ describe('CollectionPersistent Tests', () => { expect(dummyAgile.storages.get).toHaveBeenCalledWith( collectionPersistent._key, - collectionPersistent.defaultStorageKey + collectionPersistent.config.defaultStorageKey ); expect(dummyAgile.storages.get).toHaveBeenCalledWith( CollectionPersistent.getItemStorageKey( '1', collectionPersistent._key ), - collectionPersistent.defaultStorageKey + collectionPersistent.config.defaultStorageKey ); expect(dummyAgile.storages.get).toHaveBeenCalledWith( CollectionPersistent.getItemStorageKey( '2', collectionPersistent._key ), - collectionPersistent.defaultStorageKey + collectionPersistent.config.defaultStorageKey ); expect(dummyAgile.storages.get).toHaveBeenCalledWith( CollectionPersistent.getItemStorageKey( '3', collectionPersistent._key ), - collectionPersistent.defaultStorageKey + collectionPersistent.config.defaultStorageKey ); expect(dummyDefaultGroup.persist).toHaveBeenCalledWith({ @@ -355,28 +362,28 @@ describe('CollectionPersistent Tests', () => { expect(dummyAgile.storages.get).toHaveBeenCalledWith( collectionPersistent._key, - collectionPersistent.defaultStorageKey + collectionPersistent.config.defaultStorageKey ); expect(dummyAgile.storages.get).not.toHaveBeenCalledWith( CollectionPersistent.getItemStorageKey( '1', collectionPersistent._key ), - collectionPersistent.defaultStorageKey + collectionPersistent.config.defaultStorageKey ); expect(dummyAgile.storages.get).not.toHaveBeenCalledWith( CollectionPersistent.getItemStorageKey( '2', collectionPersistent._key ), - collectionPersistent.defaultStorageKey + collectionPersistent.config.defaultStorageKey ); expect(dummyAgile.storages.get).not.toHaveBeenCalledWith( CollectionPersistent.getItemStorageKey( '3', collectionPersistent._key ), - collectionPersistent.defaultStorageKey + collectionPersistent.config.defaultStorageKey ); expect(dummyDefaultGroup.persist).not.toHaveBeenCalled(); @@ -412,19 +419,19 @@ describe('CollectionPersistent Tests', () => { expect(dummyAgile.storages.get).toHaveBeenCalledWith( 'dummyKey', - collectionPersistent.defaultStorageKey + collectionPersistent.config.defaultStorageKey ); expect(dummyAgile.storages.get).toHaveBeenCalledWith( CollectionPersistent.getItemStorageKey('1', 'dummyKey'), - collectionPersistent.defaultStorageKey + collectionPersistent.config.defaultStorageKey ); expect(dummyAgile.storages.get).toHaveBeenCalledWith( CollectionPersistent.getItemStorageKey('2', 'dummyKey'), - collectionPersistent.defaultStorageKey + collectionPersistent.config.defaultStorageKey ); expect(dummyAgile.storages.get).toHaveBeenCalledWith( CollectionPersistent.getItemStorageKey('3', 'dummyKey'), - collectionPersistent.defaultStorageKey + collectionPersistent.config.defaultStorageKey ); expect(dummyDefaultGroup.persist).toHaveBeenCalledWith({ @@ -493,28 +500,28 @@ describe('CollectionPersistent Tests', () => { expect(dummyAgile.storages.get).toHaveBeenCalledWith( collectionPersistent._key, - collectionPersistent.defaultStorageKey + collectionPersistent.config.defaultStorageKey ); expect(dummyAgile.storages.get).not.toHaveBeenCalledWith( CollectionPersistent.getItemStorageKey( '1', collectionPersistent._key ), - collectionPersistent.defaultStorageKey + collectionPersistent.config.defaultStorageKey ); expect(dummyAgile.storages.get).not.toHaveBeenCalledWith( CollectionPersistent.getItemStorageKey( '2', collectionPersistent._key ), - collectionPersistent.defaultStorageKey + collectionPersistent.config.defaultStorageKey ); expect(dummyAgile.storages.get).not.toHaveBeenCalledWith( CollectionPersistent.getItemStorageKey( '3', collectionPersistent._key ), - collectionPersistent.defaultStorageKey + collectionPersistent.config.defaultStorageKey ); expect(dummyDefaultGroup.persist).not.toHaveBeenCalled(); diff --git a/packages/core/tests/unit/state/state.persistent.test.ts b/packages/core/tests/unit/state/state.persistent.test.ts index 9fbfa647..ad918ee0 100644 --- a/packages/core/tests/unit/state/state.persistent.test.ts +++ b/packages/core/tests/unit/state/state.persistent.test.ts @@ -38,6 +38,7 @@ describe('StatePersistent Tests', () => { expect(statePersistent.instantiatePersistent).toHaveBeenCalledWith({ key: undefined, storageKeys: [], + defaultStorageKey: null, }); expect(statePersistent.initialLoading).not.toHaveBeenCalled(); @@ -46,7 +47,7 @@ describe('StatePersistent Tests', () => { expect(statePersistent.isPersisted).toBeFalsy(); expect(statePersistent.onLoad).toBeUndefined(); expect(statePersistent.storageKeys).toStrictEqual([]); - expect(statePersistent.defaultStorageKey).toBeUndefined(); + expect(statePersistent.config).toStrictEqual({ defaultStorageKey: null }); }); it("should create StatePersistent and shouldn't call initialLoading if Persistent isn't ready (specific config)", () => { @@ -61,12 +62,14 @@ describe('StatePersistent Tests', () => { const statePersistent = new StatePersistent(dummyState, { key: 'statePersistentKey', storageKeys: ['test1', 'test2'], + defaultStorageKey: 'test2', }); expect(statePersistent).toBeInstanceOf(StatePersistent); expect(statePersistent.instantiatePersistent).toHaveBeenCalledWith({ key: 'statePersistentKey', storageKeys: ['test1', 'test2'], + defaultStorageKey: 'test2', }); expect(statePersistent.initialLoading).not.toHaveBeenCalled(); @@ -75,7 +78,9 @@ describe('StatePersistent Tests', () => { expect(statePersistent.isPersisted).toBeFalsy(); expect(statePersistent.onLoad).toBeUndefined(); expect(statePersistent.storageKeys).toStrictEqual([]); - expect(statePersistent.defaultStorageKey).toBeUndefined(); + expect(statePersistent.config).toStrictEqual({ + defaultStorageKey: null, // gets set in 'instantiatePersistent' which is mocked + }); }); it('should create StatePersistent and should call initialLoading if Persistent is ready (default config)', () => { @@ -225,7 +230,7 @@ describe('StatePersistent Tests', () => { expect(response).toBeTruthy(); expect(dummyAgile.storages.get).toHaveBeenCalledWith( statePersistent._key, - statePersistent.defaultStorageKey + statePersistent.config.defaultStorageKey ); expect(dummyState.set).toHaveBeenCalledWith('dummyValue', { storage: false, @@ -246,7 +251,7 @@ describe('StatePersistent Tests', () => { expect(response).toBeFalsy(); expect(dummyAgile.storages.get).toHaveBeenCalledWith( statePersistent._key, - statePersistent.defaultStorageKey + statePersistent.config.defaultStorageKey ); expect(dummyState.set).not.toHaveBeenCalled(); expect(statePersistent.persistValue).not.toHaveBeenCalled(); @@ -263,7 +268,7 @@ describe('StatePersistent Tests', () => { expect(response).toBeTruthy(); expect(dummyAgile.storages.get).toHaveBeenCalledWith( 'coolKey', - statePersistent.defaultStorageKey + statePersistent.config.defaultStorageKey ); expect(dummyState.set).toHaveBeenCalledWith('dummyValue', { storage: false, diff --git a/packages/core/tests/unit/storages/persistent.test.ts b/packages/core/tests/unit/storages/persistent.test.ts index 94ee3f93..4594d358 100644 --- a/packages/core/tests/unit/storages/persistent.test.ts +++ b/packages/core/tests/unit/storages/persistent.test.ts @@ -197,7 +197,7 @@ describe('Persistent Tests', () => { }); describe('assignStorageKeys function tests', () => { - it('should assign passed StorageKeys and set first one as default StorageKey', () => { + it('should assign passed StorageKeys and set first one as default StorageKey if no default Storage Key passed', () => { persistent.assignStorageKeys(['test1', 'test2', 'test3']); expect(persistent.storageKeys).toStrictEqual([ @@ -209,7 +209,7 @@ describe('Persistent Tests', () => { expect(console.warn).not.toHaveBeenCalled(); }); - it('should assign passed StorageKeys and set defaultStorageKey as default StorageKey', () => { + it('should assign passed StorageKeys and set passed defaultStorageKey as default StorageKey', () => { persistent.assignStorageKeys(['test1', 'test2', 'test3'], 'test3'); expect(persistent.storageKeys).toStrictEqual([ @@ -231,10 +231,15 @@ describe('Persistent Tests', () => { 'test4', ]); expect(persistent.config.defaultStorageKey).toBe('test4'); - expect(console.warn).toHaveBeenCalledWith('todo'); + expect( + console.warn + ).toHaveBeenCalledWith( + "Agile Warn: Default Storage Key 'test4' isn't contained in storageKeys!", + ['test1', 'test2', 'test3', 'test4'] + ); }); - it('should try to get default StorageKey from Agile if no StorageKeys passed', () => { + it('should try to get default StorageKey from Agile if no StorageKey got passed', () => { dummyAgile.storages.register( new Storage({ key: 'storage1', diff --git a/packages/core/tests/unit/storages/storages.test.ts b/packages/core/tests/unit/storages/storages.test.ts index 407c7a01..38c0f18b 100644 --- a/packages/core/tests/unit/storages/storages.test.ts +++ b/packages/core/tests/unit/storages/storages.test.ts @@ -16,7 +16,7 @@ describe('Storages Tests', () => { it('should create Storages (default config)', () => { const storages = new Storages(dummyAgile); - expect(storages.config).toStrictEqual({ defaultStorage: null }); + expect(storages.config).toStrictEqual({ defaultStorageKey: null }); expect(storages.storages).toStrictEqual({}); expect(storages.persistentInstances.size).toBe(0); expect(storages.instantiateLocalStorage).not.toHaveBeenCalled(); @@ -28,7 +28,7 @@ describe('Storages Tests', () => { localStorage: true, }); - expect(storages.config).toStrictEqual({ defaultStorage: 'jeff' }); + expect(storages.config).toStrictEqual({ defaultStorageKey: 'jeff' }); expect(storages.storages).toStrictEqual({}); expect(storages.persistentInstances.size).toBe(0); expect(storages.instantiateLocalStorage).toHaveBeenCalled(); @@ -180,9 +180,9 @@ describe('Storages Tests', () => { it('should reassignStorageKeys, revalidate and initialLoad Persistents that have no defined defaultStorage', () => { const dummyPersistent1 = new Persistent(dummyAgile); - dummyPersistent1.defaultStorageKey = undefined; + dummyPersistent1.config = { defaultStorageKey: null }; const dummyPersistent2 = new Persistent(dummyAgile); - dummyPersistent2.defaultStorageKey = 'unknown'; + dummyPersistent2.config = { defaultStorageKey: 'unknown' }; jest.spyOn(dummyPersistent1, 'assignStorageKeys'); jest .spyOn(dummyPersistent1, 'validatePersistent') From 49233e771dea1085d9c9d8286ebaebd3503165b1 Mon Sep 17 00:00:00 2001 From: Benno Kohrs Date: Sat, 17 Apr 2021 17:41:48 +0200 Subject: [PATCH 04/10] fixed some tests --- packages/core/src/storages/index.ts | 2 +- packages/core/tests/unit/storages/storages.test.ts | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/packages/core/src/storages/index.ts b/packages/core/src/storages/index.ts index c220a548..2feedc89 100644 --- a/packages/core/src/storages/index.ts +++ b/packages/core/src/storages/index.ts @@ -99,7 +99,7 @@ export class Storages { if (config.default) this.config.defaultStorageKey = storage.key; this.persistentInstances.forEach((persistent) => { - // If Persistent isn't ready and has no default StorageKey.. reassignStorageKeys and try to load it + // If Persistent isn't ready (try to reassign StorageKeys) if (!persistent.ready) { persistent.assignStorageKeys(); const isValid = persistent.validatePersistent(); diff --git a/packages/core/tests/unit/storages/storages.test.ts b/packages/core/tests/unit/storages/storages.test.ts index 38c0f18b..1c77d498 100644 --- a/packages/core/tests/unit/storages/storages.test.ts +++ b/packages/core/tests/unit/storages/storages.test.ts @@ -164,9 +164,11 @@ describe('Storages Tests', () => { it('should call updateValue method on all persistent Instances that have the newly registered StorageKey', () => { const dummyPersistent1 = new Persistent(dummyAgile, { storageKeys: ['storage1'], + key: 'dummyPersistent1', }); const dummyPersistent2 = new Persistent(dummyAgile, { storageKeys: ['notExistingStorage'], + key: 'dummyPersistent2', }); jest.spyOn(dummyPersistent1, 'persistValue'); jest.spyOn(dummyPersistent2, 'persistValue'); @@ -179,10 +181,14 @@ describe('Storages Tests', () => { }); it('should reassignStorageKeys, revalidate and initialLoad Persistents that have no defined defaultStorage', () => { - const dummyPersistent1 = new Persistent(dummyAgile); - dummyPersistent1.config = { defaultStorageKey: null }; - const dummyPersistent2 = new Persistent(dummyAgile); - dummyPersistent2.config = { defaultStorageKey: 'unknown' }; + const dummyPersistent1 = new Persistent(dummyAgile, { + key: 'dummyPersistent1', + }); + const dummyPersistent2 = new Persistent(dummyAgile, { + storageKeys: ['dummy'], + defaultStorageKey: 'dummy', + key: 'dummyPersistent2', + }); jest.spyOn(dummyPersistent1, 'assignStorageKeys'); jest .spyOn(dummyPersistent1, 'validatePersistent') From f294f0a1a2a94c86d48d7726f41b1dd0fa1f76f6 Mon Sep 17 00:00:00 2001 From: Benno Kohrs Date: Sat, 17 Apr 2021 17:53:53 +0200 Subject: [PATCH 05/10] clarified loadValue naming --- .../core/src/collection/collection.persistent.ts | 2 +- packages/core/src/collection/group.ts | 4 ++-- packages/core/src/collection/index.ts | 8 ++++---- packages/core/src/state/index.ts | 8 ++++---- .../collection/collection.persistent.test.ts | 4 ++-- .../tests/unit/collection/collection.test.ts | 4 ++-- .../core/tests/unit/collection/group.test.ts | 16 ++++++++-------- packages/core/tests/unit/state/state.test.ts | 4 ++-- 8 files changed, 25 insertions(+), 25 deletions(-) diff --git a/packages/core/src/collection/collection.persistent.ts b/packages/core/src/collection/collection.persistent.ts index e561e640..4a4c8c19 100644 --- a/packages/core/src/collection/collection.persistent.ts +++ b/packages/core/src/collection/collection.persistent.ts @@ -121,7 +121,7 @@ export class CollectionPersistent extends Persistent { // Persist Default Group and load its Value manually to be 100% sure it got loaded defaultGroup.persist({ - instantiate: false, + loadValue: false, followCollectionPersistKeyPattern: true, }); if (defaultGroup.persistent?.ready) { diff --git a/packages/core/src/collection/group.ts b/packages/core/src/collection/group.ts index 831249dc..7e7b35e6 100644 --- a/packages/core/src/collection/group.ts +++ b/packages/core/src/collection/group.ts @@ -270,7 +270,7 @@ export class Group extends State> { } _config = defineConfig(_config, { - instantiate: true, + loadValue: true, followCollectionPattern: false, storageKeys: [], }); @@ -283,7 +283,7 @@ export class Group extends State> { } super.persist(key, { - instantiate: _config.instantiate, + loadValue: _config.loadValue, storageKeys: _config.storageKeys, }); diff --git a/packages/core/src/collection/index.ts b/packages/core/src/collection/index.ts index 426935f0..b1452af4 100644 --- a/packages/core/src/collection/index.ts +++ b/packages/core/src/collection/index.ts @@ -799,7 +799,7 @@ export class Collection { } _config = defineConfig(_config, { - instantiate: true, + loadValue: true, storageKeys: [], }); @@ -810,7 +810,7 @@ export class Collection { // Create persistent -> Persist Value this.persistent = new CollectionPersistent(this, { - instantiate: _config.instantiate, + instantiate: _config.loadValue, storageKeys: _config.storageKeys, key: key, }); @@ -1279,11 +1279,11 @@ export interface HasConfigInterface { } /** - * @param instantiate - If Persistent gets instantiated + * @param loadValue - If Persistent loads the persisted value into the Collection * @param storageKeys - Key/Name of Storages which gets used to persist the Collection Value (NOTE: If not passed the default Storage will be used) */ export interface CollectionPersistentConfigInterface { - instantiate?: boolean; + loadValue?: boolean; storageKeys?: StorageKey[]; } diff --git a/packages/core/src/state/index.ts b/packages/core/src/state/index.ts index 53ebe94f..dcaecd50 100644 --- a/packages/core/src/state/index.ts +++ b/packages/core/src/state/index.ts @@ -397,7 +397,7 @@ export class State { } _config = defineConfig(_config, { - instantiate: true, + loadValue: true, storageKeys: [], }); @@ -408,7 +408,7 @@ export class State { // Create persistent -> Persist Value this.persistent = new StatePersistent(this, { - instantiate: _config.instantiate, + instantiate: _config.loadValue, storageKeys: _config.storageKeys, key: key, }); @@ -657,11 +657,11 @@ export interface PatchConfigInterface extends StateIngestConfigInterface { } /** - * @param instantiate - If Persistent gets instantiated + * @param loadValue - If Persistent loads the persisted value into the State * @param storageKeys - Key/Name of Storages which gets used to persist the State Value (NOTE: If not passed the default Storage will be used) */ export interface StatePersistentConfigInterface { - instantiate?: boolean; + loadValue?: boolean; storageKeys?: StorageKey[]; } diff --git a/packages/core/tests/unit/collection/collection.persistent.test.ts b/packages/core/tests/unit/collection/collection.persistent.test.ts index 1b91a032..a91d4e8e 100644 --- a/packages/core/tests/unit/collection/collection.persistent.test.ts +++ b/packages/core/tests/unit/collection/collection.persistent.test.ts @@ -326,7 +326,7 @@ describe('CollectionPersistent Tests', () => { ); expect(dummyDefaultGroup.persist).toHaveBeenCalledWith({ - instantiate: false, + loadValue: false, followCollectionPersistKeyPattern: true, }); expect(dummyDefaultGroup.persistent?.initialLoading).toHaveBeenCalled(); @@ -435,7 +435,7 @@ describe('CollectionPersistent Tests', () => { ); expect(dummyDefaultGroup.persist).toHaveBeenCalledWith({ - instantiate: false, + loadValue: false, followCollectionPersistKeyPattern: true, }); expect(dummyDefaultGroup.persistent?.initialLoading).toHaveBeenCalled(); diff --git a/packages/core/tests/unit/collection/collection.test.ts b/packages/core/tests/unit/collection/collection.test.ts index 8c1f219d..7e28b282 100644 --- a/packages/core/tests/unit/collection/collection.test.ts +++ b/packages/core/tests/unit/collection/collection.test.ts @@ -1594,7 +1594,7 @@ describe('Collection Tests', () => { it('should create persistent with CollectionKey (specific config)', () => { collection.persist({ storageKeys: ['test1', 'test2'], - instantiate: false, + loadValue: false, }); expect(collection.persistent).toBeInstanceOf(CollectionPersistent); @@ -1619,7 +1619,7 @@ describe('Collection Tests', () => { it('should create persistent with passed Key (specific config)', () => { collection.persist('passedKey', { storageKeys: ['test1', 'test2'], - instantiate: false, + loadValue: false, }); expect(collection.persistent).toBeInstanceOf(CollectionPersistent); diff --git a/packages/core/tests/unit/collection/group.test.ts b/packages/core/tests/unit/collection/group.test.ts index 92a9fca4..60f3b52c 100644 --- a/packages/core/tests/unit/collection/group.test.ts +++ b/packages/core/tests/unit/collection/group.test.ts @@ -434,16 +434,16 @@ describe('Group Tests', () => { group.persist(); expect(State.prototype.persist).toHaveBeenCalledWith(group._key, { - instantiate: true, + loadValue: true, storageKeys: [], }); }); it('should persist Group with GroupKey (specific config)', () => { - group.persist({ instantiate: false, storageKeys: ['test1', 'test2'] }); + group.persist({ loadValue: false, storageKeys: ['test1', 'test2'] }); expect(State.prototype.persist).toHaveBeenCalledWith(group._key, { - instantiate: false, + loadValue: false, storageKeys: ['test1', 'test2'], }); }); @@ -452,19 +452,19 @@ describe('Group Tests', () => { group.persist('dummyKey'); expect(State.prototype.persist).toHaveBeenCalledWith('dummyKey', { - instantiate: true, + loadValue: true, storageKeys: [], }); }); it('should persist Group with passed Key (specific config)', () => { group.persist('dummyKey', { - instantiate: false, + loadValue: false, storageKeys: ['test1', 'test2'], }); expect(State.prototype.persist).toHaveBeenCalledWith('dummyKey', { - instantiate: false, + loadValue: false, storageKeys: ['test1', 'test2'], }); }); @@ -478,7 +478,7 @@ describe('Group Tests', () => { dummyCollection._key ), { - instantiate: true, + loadValue: true, storageKeys: [], } ); @@ -493,7 +493,7 @@ describe('Group Tests', () => { dummyCollection._key ), { - instantiate: true, + loadValue: true, storageKeys: [], } ); diff --git a/packages/core/tests/unit/state/state.test.ts b/packages/core/tests/unit/state/state.test.ts index 378f384c..726c01c3 100644 --- a/packages/core/tests/unit/state/state.test.ts +++ b/packages/core/tests/unit/state/state.test.ts @@ -600,7 +600,7 @@ describe('State Tests', () => { it('should create persistent with StateKey (specific config)', () => { numberState.persist({ storageKeys: ['test1', 'test2'], - instantiate: false, + loadValue: false, }); expect(numberState.persistent).toBeInstanceOf(StatePersistent); @@ -625,7 +625,7 @@ describe('State Tests', () => { it('should create persistent with passed Key (specific config)', () => { numberState.persist('passedKey', { storageKeys: ['test1', 'test2'], - instantiate: false, + loadValue: false, }); expect(numberState.persistent).toBeInstanceOf(StatePersistent); From b2d64dd87affc7ecc78ea2f2129e6e29517dbcc0 Mon Sep 17 00:00:00 2001 From: Benno Kohrs Date: Sun, 18 Apr 2021 07:11:26 +0200 Subject: [PATCH 06/10] optimized naming --- .../src/collection/collection.persistent.ts | 36 ++++++++++-------- packages/core/src/state/state.persistent.ts | 37 +++++++++++-------- packages/core/src/storages/index.ts | 18 ++++----- packages/core/src/storages/persistent.ts | 14 ++++++- .../tests/unit/storages/persistent.test.ts | 33 ++++++++++++++++- .../core/tests/unit/storages/storages.test.ts | 10 ++--- 6 files changed, 99 insertions(+), 49 deletions(-) diff --git a/packages/core/src/collection/collection.persistent.ts b/packages/core/src/collection/collection.persistent.ts index 4a4c8c19..3fe9758d 100644 --- a/packages/core/src/collection/collection.persistent.ts +++ b/packages/core/src/collection/collection.persistent.ts @@ -98,16 +98,18 @@ export class CollectionPersistent extends Persistent { /** * @internal * Loads Collection from Storage - * @param key - Prefix Key of Persisted Instances (default PersistentKey) + * @param storageKey - Prefix Key of Persisted Instances (default PersistentKey) * @return Success? */ - public async loadPersistedValue(key?: PersistentKey): Promise { + public async loadPersistedValue( + storageKey?: PersistentKey + ): Promise { if (!this.ready) return false; - const _key = key || this._key; + const _storageKey = storageKey || this._key; // Check if Collection is Persisted const isPersisted = await this.agileInstance().storages.get( - _key, + _storageKey, this.config.defaultStorageKey as any ); if (!isPersisted) return false; @@ -133,7 +135,7 @@ export class CollectionPersistent extends Persistent { for (const itemKey of defaultGroup._value) { const itemStorageKey = CollectionPersistent.getItemStorageKey( itemKey, - _key + _storageKey ); // Get Storage Value @@ -151,7 +153,7 @@ export class CollectionPersistent extends Persistent { const success = await loadValuesIntoCollection(); // Persist Collection, so that the Storage Value updates dynamically if the Collection updates - if (success) await this.persistValue(_key); + if (success) await this.persistValue(_storageKey); return success; } @@ -162,19 +164,19 @@ export class CollectionPersistent extends Persistent { /** * @internal * Sets everything up so that the Collection gets saved in the Storage - * @param key - Prefix Key of Persisted Instances (default PersistentKey) + * @param storageKey - Prefix Key of Persisted Instances (default PersistentKey) * @return Success? */ - public async persistValue(key?: PersistentKey): Promise { + public async persistValue(storageKey?: PersistentKey): Promise { if (!this.ready) return false; - const _key = key || this._key; + const _storageKey = storageKey || this._key; const defaultGroup = this.collection().getGroup( this.collection().config.defaultGroupKey ); if (!defaultGroup) return false; // Set Collection to Persisted (in Storage) - this.agileInstance().storages.set(_key, true, this.storageKeys); + this.agileInstance().storages.set(_storageKey, true, this.storageKeys); // Persist default Group if (!defaultGroup.isPersisted) @@ -183,7 +185,7 @@ export class CollectionPersistent extends Persistent { // Add sideEffect to default Group which adds and removes Items from the Storage depending on the Group Value defaultGroup.addSideEffect( CollectionPersistent.defaultGroupSideEffectKey, - () => this.rebuildStorageSideEffect(defaultGroup, _key), + () => this.rebuildStorageSideEffect(defaultGroup, _storageKey), { weight: 0 } ); @@ -192,7 +194,7 @@ export class CollectionPersistent extends Persistent { const item = this.collection().getItem(itemKey); const itemStorageKey = CollectionPersistent.getItemStorageKey( itemKey, - _key + _storageKey ); item?.persist(itemStorageKey); } @@ -207,19 +209,21 @@ export class CollectionPersistent extends Persistent { /** * @internal * Removes Collection from the Storage - * @param key - Prefix Key of Persisted Instances (default PersistentKey) + * @param storageKey - Prefix Key of Persisted Instances (default PersistentKey) * @return Success? */ - public async removePersistedValue(key?: PersistentKey): Promise { + public async removePersistedValue( + storageKey?: PersistentKey + ): Promise { if (!this.ready) return false; - const _key = key || this._key; + const _storageKey = storageKey || this._key; const defaultGroup = this.collection().getGroup( this.collection().config.defaultGroupKey ); if (!defaultGroup) return false; // Set Collection to not Persisted - this.agileInstance().storages.remove(_key, this.storageKeys); + this.agileInstance().storages.remove(_storageKey, this.storageKeys); // Remove default Group from Storage defaultGroup.persistent?.removePersistedValue(); diff --git a/packages/core/src/state/state.persistent.ts b/packages/core/src/state/state.persistent.ts index 91f56c03..5e51ea79 100644 --- a/packages/core/src/state/state.persistent.ts +++ b/packages/core/src/state/state.persistent.ts @@ -90,15 +90,18 @@ export class StatePersistent extends Persistent { /** * @internal * Loads State Value from the Storage + * @param storageKey - Prefix Key of Persisted Instances (default PersistentKey) * @return Success? */ - public async loadPersistedValue(key?: PersistentKey): Promise { + public async loadPersistedValue( + storageKey?: PersistentKey + ): Promise { if (!this.ready) return false; - const _key = key || this._key; + const _storageKey = storageKey || this._key; // Load Value from default Storage const loadedValue = await this.agileInstance().storages.get( - _key, + _storageKey, this.config.defaultStorageKey as any ); if (!loadedValue) return false; @@ -107,7 +110,7 @@ export class StatePersistent extends Persistent { this.state().set(loadedValue, { storage: false }); // Persist State, so that the Storage Value updates dynamically if the State updates - await this.persistValue(_key); + await this.persistValue(_storageKey); return true; } @@ -118,23 +121,24 @@ export class StatePersistent extends Persistent { /** * @internal * Sets everything up so that the State gets saved in the Storage on every Value change + * @param storageKey - Prefix Key of Persisted Instances (default PersistentKey) * @return Success? */ - public async persistValue(key?: PersistentKey): Promise { + public async persistValue(storageKey?: PersistentKey): Promise { if (!this.ready) return false; - const _key = key || this._key; + const _storageKey = storageKey || this._key; // Add SideEffect to State, that updates the saved State Value depending on the current State Value this.state().addSideEffect( StatePersistent.storeValueSideEffectKey, (instance, config) => { - this.rebuildStorageSideEffect(this.state(), _key, config); + this.rebuildStorageSideEffect(this.state(), _storageKey, config); }, { weight: 0 } ); - // Rebuild Storage for saving State Value in the Storage - this.rebuildStorageSideEffect(this.state(), _key); + // Initial rebuild Storage for saving State Value in the Storage + this.rebuildStorageSideEffect(this.state(), _storageKey); this.isPersisted = true; return true; @@ -146,17 +150,20 @@ export class StatePersistent extends Persistent { /** * @internal * Removes State Value form the Storage + * @param storageKey - Prefix Key of Persisted Instances (default PersistentKey) * @return Success? */ - public async removePersistedValue(key?: PersistentKey): Promise { + public async removePersistedValue( + storageKey?: PersistentKey + ): Promise { if (!this.ready) return false; - const _key = key || this._key; + const _storageKey = storageKey || this._key; // Remove SideEffect this.state().removeSideEffect(StatePersistent.storeValueSideEffectKey); // Remove Value from Storage - this.agileInstance().storages.remove(_key, this.storageKeys); + this.agileInstance().storages.remove(_storageKey, this.storageKeys); this.isPersisted = false; return true; @@ -191,18 +198,18 @@ export class StatePersistent extends Persistent { * @internal * Rebuilds Storage depending on the State Value (Saves current State Value into the Storage) * @param state - State that holds the new Value - * @param key - Key/Name of Persistent + * @param storageKey - StorageKey where value should be persisted * @param config - Config */ public rebuildStorageSideEffect( state: State, - key: PersistentKey, + storageKey: PersistentKey, config: { [key: string]: any } = {} ) { if (config.storage !== undefined && !config.storage) return; this.agileInstance().storages.set( - key, + storageKey, this.state().getPersistableValue(), this.storageKeys ); diff --git a/packages/core/src/storages/index.ts b/packages/core/src/storages/index.ts index 2feedc89..7de55531 100644 --- a/packages/core/src/storages/index.ts +++ b/packages/core/src/storages/index.ts @@ -99,17 +99,17 @@ export class Storages { if (config.default) this.config.defaultStorageKey = storage.key; this.persistentInstances.forEach((persistent) => { - // If Persistent isn't ready (try to reassign StorageKeys) - if (!persistent.ready) { - persistent.assignStorageKeys(); - const isValid = persistent.validatePersistent(); - if (isValid) persistent.initialLoading(); - return; - } + if (persistent.storageKeys.includes(storage.key)) { + // If Persistent isn't ready (try to revalidate and load it) + if (!persistent.ready) { + const isValid = persistent.validatePersistent(); + if (isValid) persistent.initialLoading(); + return; + } - // Add Value of Persistent to newly registered Storage - if (persistent.storageKeys.includes(storage.key)) + // Add Value of Persistent to newly registered Storage persistent.persistValue(); + } }); return true; diff --git a/packages/core/src/storages/persistent.ts b/packages/core/src/storages/persistent.ts index 84f2158a..cc8f8c37 100644 --- a/packages/core/src/storages/persistent.ts +++ b/packages/core/src/storages/persistent.ts @@ -117,6 +117,16 @@ export class Persistent { isValid = false; } + // Check if Storages exist + this.storageKeys.map((key) => { + if (!this.agileInstance().storages.storages[key]) { + Agile.logger.error( + `Storage '${key}' doesn't exist yet. Please provide only existing StorageKeys!` + ); + isValid = false; + } + }); + this.ready = isValid; return isValid; } @@ -137,7 +147,7 @@ export class Persistent { const storages = this.agileInstance().storages; const _storageKeys = copy(storageKeys); - // Print warning if default StorageKey passed, but it isn't in stoargeKeys + // Print warning if default StorageKey passed, but it isn't in storageKeys if (defaultStorageKey && !_storageKeys.includes(defaultStorageKey)) { Agile.logger.warn( `Default Storage Key '${defaultStorageKey}' isn't contained in storageKeys!`, @@ -146,7 +156,7 @@ export class Persistent { _storageKeys.push(defaultStorageKey); } - // Add default Storage of AgileTs if no storageKey provided + // Add default Storage of AgileTs to storageKeys if no storageKey provided if (_storageKeys.length <= 0) { this.config.defaultStorageKey = storages.config.defaultStorageKey as any; _storageKeys.push(storages.config.defaultStorageKey as any); diff --git a/packages/core/tests/unit/storages/persistent.test.ts b/packages/core/tests/unit/storages/persistent.test.ts index 4594d358..6b685a87 100644 --- a/packages/core/tests/unit/storages/persistent.test.ts +++ b/packages/core/tests/unit/storages/persistent.test.ts @@ -170,7 +170,7 @@ describe('Persistent Tests', () => { ); }); - it('should return false if no set key and set StorageKeys', () => { + it('should return false and print error if no set key and set StorageKeys', () => { persistent.config.defaultStorageKey = 'test'; persistent.storageKeys = ['test']; @@ -184,7 +184,38 @@ describe('Persistent Tests', () => { ); }); + it('should return false and print error if set key and set StorageKeys but no existing Storage at storageKeys', () => { + persistent.config.defaultStorageKey = 'test'; + persistent.storageKeys = ['test']; + + const isValid = persistent.validatePersistent(); + + expect(isValid).toBeFalsy(); + expect(persistent.ready).toBeFalsy(); + + expect(console.error).toHaveBeenCalledWith( + "Agile Error: Storage 'test' doesn't exist yet. Please provide only existing StorageKeys!" + ); + }); + it('should return true if set key and set StorageKeys', () => { + dummyAgile.storages.register( + dummyAgile.createStorage({ + key: 'test', + methods: { + get: () => { + /* empty */ + }, + set: () => { + /* empty */ + }, + remove: () => { + /* empty */ + }, + }, + }) + ); + persistent._key = 'persistentKey'; persistent.config.defaultStorageKey = 'test'; persistent.storageKeys = ['test']; diff --git a/packages/core/tests/unit/storages/storages.test.ts b/packages/core/tests/unit/storages/storages.test.ts index 1c77d498..1d0ec768 100644 --- a/packages/core/tests/unit/storages/storages.test.ts +++ b/packages/core/tests/unit/storages/storages.test.ts @@ -143,7 +143,7 @@ describe('Storages Tests', () => { expect(response).toBeTruthy(); }); - it("shouldn't register Storage with the same key twice", () => { + it("shouldn't register Storage with the same key twice and print error", () => { const dummyStorage = new Storage({ key: 'storage1', methods: dummyStorageMethods, @@ -173,6 +173,8 @@ describe('Storages Tests', () => { jest.spyOn(dummyPersistent1, 'persistValue'); jest.spyOn(dummyPersistent2, 'persistValue'); + console.log(dummyPersistent1, dummyPersistent2); + const response = storages.register(dummyStorage1); expect(dummyPersistent1.persistValue).toHaveBeenCalled(); @@ -180,7 +182,7 @@ describe('Storages Tests', () => { expect(response).toBeTruthy(); }); - it('should reassignStorageKeys, revalidate and initialLoad Persistents that have no defined defaultStorage', () => { + it('should revalidate and initialLoad Persistents that have no defined defaultStorage', () => { const dummyPersistent1 = new Persistent(dummyAgile, { key: 'dummyPersistent1', }); @@ -189,22 +191,18 @@ describe('Storages Tests', () => { defaultStorageKey: 'dummy', key: 'dummyPersistent2', }); - jest.spyOn(dummyPersistent1, 'assignStorageKeys'); jest .spyOn(dummyPersistent1, 'validatePersistent') .mockReturnValue(true); jest.spyOn(dummyPersistent1, 'initialLoading'); - jest.spyOn(dummyPersistent2, 'assignStorageKeys'); jest.spyOn(dummyPersistent2, 'validatePersistent'); jest.spyOn(dummyPersistent2, 'initialLoading'); const response = storages.register(dummyStorage1); - expect(dummyPersistent1.assignStorageKeys).toHaveBeenCalled(); expect(dummyPersistent1.validatePersistent).toHaveBeenCalled(); expect(dummyPersistent1.initialLoading).toHaveBeenCalled(); - expect(dummyPersistent2.assignStorageKeys).not.toHaveBeenCalled(); expect(dummyPersistent2.validatePersistent).not.toHaveBeenCalled(); expect(dummyPersistent2.initialLoading).not.toHaveBeenCalled(); From 1986e19116a76fc2c441e7c585780c96b98b4612 Mon Sep 17 00:00:00 2001 From: Benno Kohrs Date: Sun, 18 Apr 2021 07:24:15 +0200 Subject: [PATCH 07/10] fixed tests --- packages/core/src/storages/index.ts | 18 ++++++++------- .../core/tests/unit/storages/storages.test.ts | 22 ++++++++++++------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/packages/core/src/storages/index.ts b/packages/core/src/storages/index.ts index 7de55531..826269b1 100644 --- a/packages/core/src/storages/index.ts +++ b/packages/core/src/storages/index.ts @@ -99,16 +99,18 @@ export class Storages { if (config.default) this.config.defaultStorageKey = storage.key; this.persistentInstances.forEach((persistent) => { + // Revalidate Persistent that includes the newly registered StorageKey if (persistent.storageKeys.includes(storage.key)) { - // If Persistent isn't ready (try to revalidate and load it) - if (!persistent.ready) { - const isValid = persistent.validatePersistent(); - if (isValid) persistent.initialLoading(); - return; - } + const isValid = persistent.validatePersistent(); + if (isValid) persistent.initialLoading(); + return; + } - // Add Value of Persistent to newly registered Storage - persistent.persistValue(); + // If persistent has no default StorageKey (reassign StorageKeys since this registered Storage might be tagged as default Storage) + if (!persistent.config.defaultStorageKey) { + persistent.assignStorageKeys(); + const isValid = persistent.validatePersistent(); + if (isValid) persistent.initialLoading(); } }); diff --git a/packages/core/tests/unit/storages/storages.test.ts b/packages/core/tests/unit/storages/storages.test.ts index 1d0ec768..6c146bb9 100644 --- a/packages/core/tests/unit/storages/storages.test.ts +++ b/packages/core/tests/unit/storages/storages.test.ts @@ -161,7 +161,7 @@ describe('Storages Tests', () => { expect(response).toBeFalsy(); }); - it('should call updateValue method on all persistent Instances that have the newly registered StorageKey', () => { + it('should revalidate and initialLoad value on all persistent Instances that have the newly registered StorageKey', () => { const dummyPersistent1 = new Persistent(dummyAgile, { storageKeys: ['storage1'], key: 'dummyPersistent1', @@ -170,19 +170,21 @@ describe('Storages Tests', () => { storageKeys: ['notExistingStorage'], key: 'dummyPersistent2', }); - jest.spyOn(dummyPersistent1, 'persistValue'); - jest.spyOn(dummyPersistent2, 'persistValue'); - - console.log(dummyPersistent1, dummyPersistent2); + jest.spyOn(dummyPersistent1, 'validatePersistent'); + jest.spyOn(dummyPersistent1, 'initialLoading'); + jest.spyOn(dummyPersistent2, 'validatePersistent'); + jest.spyOn(dummyPersistent2, 'initialLoading'); const response = storages.register(dummyStorage1); - expect(dummyPersistent1.persistValue).toHaveBeenCalled(); - expect(dummyPersistent2.persistValue).not.toHaveBeenCalled(); + expect(dummyPersistent1.validatePersistent).toHaveBeenCalled(); + expect(dummyPersistent1.initialLoading).toHaveBeenCalled(); + expect(dummyPersistent2.validatePersistent).not.toHaveBeenCalled(); + expect(dummyPersistent2.initialLoading).not.toHaveBeenCalled(); expect(response).toBeTruthy(); }); - it('should revalidate and initialLoad Persistents that have no defined defaultStorage', () => { + it('should revalidate and initial load Persistents that have no defined defaultStorage', () => { const dummyPersistent1 = new Persistent(dummyAgile, { key: 'dummyPersistent1', }); @@ -191,18 +193,22 @@ describe('Storages Tests', () => { defaultStorageKey: 'dummy', key: 'dummyPersistent2', }); + jest.spyOn(dummyPersistent1, 'assignStorageKeys'); jest .spyOn(dummyPersistent1, 'validatePersistent') .mockReturnValue(true); jest.spyOn(dummyPersistent1, 'initialLoading'); + jest.spyOn(dummyPersistent2, 'assignStorageKeys'); jest.spyOn(dummyPersistent2, 'validatePersistent'); jest.spyOn(dummyPersistent2, 'initialLoading'); const response = storages.register(dummyStorage1); + expect(dummyPersistent1.assignStorageKeys).toHaveBeenCalled(); expect(dummyPersistent1.validatePersistent).toHaveBeenCalled(); expect(dummyPersistent1.initialLoading).toHaveBeenCalled(); + expect(dummyPersistent2.assignStorageKeys).not.toHaveBeenCalled(); expect(dummyPersistent2.validatePersistent).not.toHaveBeenCalled(); expect(dummyPersistent2.initialLoading).not.toHaveBeenCalled(); From 67fdf485b4d8c1205ee1e688304e709d1de7c12d Mon Sep 17 00:00:00 2001 From: Benno Kohrs Date: Sun, 18 Apr 2021 07:41:34 +0200 Subject: [PATCH 08/10] fixed typo --- packages/core/src/storages/persistent.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/core/src/storages/persistent.ts b/packages/core/src/storages/persistent.ts index cc8f8c37..313e1d65 100644 --- a/packages/core/src/storages/persistent.ts +++ b/packages/core/src/storages/persistent.ts @@ -254,7 +254,7 @@ export interface CreatePersistentConfigInterface { } /** - * @param defaultStorage - Default Storage Key + * @param defaultStorageKey - Default Storage Key */ export interface PersistentConfigInterface { defaultStorageKey: StorageKey | null; @@ -263,7 +263,7 @@ export interface PersistentConfigInterface { /** * @param key - Key/Name of Persistent * @param storageKeys - Keys of Storages in that the persisted Value gets saved - * @param defaultStorage - Default Storage Key + * @param defaultStorageKey - Default Storage Key */ export interface InstantiatePersistentConfigInterface { key?: PersistentKey; From 7b7faee1ab31523e2870b4119c8603627f1439ce Mon Sep 17 00:00:00 2001 From: Benno Kohrs Date: Sun, 18 Apr 2021 08:11:43 +0200 Subject: [PATCH 09/10] added default storageKey to persist methods and added custom Storage in functional component example --- .../functional-component-ts/src/core/index.ts | 29 ++++++++++++++++++- packages/core/src/collection/group.ts | 2 ++ packages/core/src/collection/index.ts | 4 +++ packages/core/src/state/index.ts | 4 +++ packages/core/src/storages/persistent.ts | 2 +- 5 files changed, 39 insertions(+), 2 deletions(-) diff --git a/examples/react/functional-component-ts/src/core/index.ts b/examples/react/functional-component-ts/src/core/index.ts index c3fa7e91..705736db 100644 --- a/examples/react/functional-component-ts/src/core/index.ts +++ b/examples/react/functional-component-ts/src/core/index.ts @@ -2,14 +2,41 @@ import { Agile, clone, Logger } from '@agile-ts/core'; import API from '@agile-ts/api'; import Event from '@agile-ts/event'; +export const myStorage: any = {}; + export const App = new Agile({ logConfig: { level: Logger.level.DEBUG }, + localStorage: true, }); +// Register custom second Storage +App.registerStorage( + App.createStorage({ + key: 'myStorage', + methods: { + get: (key: string) => { + console.log(`GET '${key}'`); + return myStorage[key]; + }, + set: (key: string, value: any) => { + console.log(`SET '${key}'`, value); + myStorage[key] = value; + }, + remove: (key: string) => { + console.log(`DELETE '${key}'`); + delete myStorage[key]; + }, + }, + }) +); + export const MY_STATE = App.createState('MyState', { key: 'my-state' }); //.persist(); export const MY_STATE_2 = App.createState('MyState2', { key: 'my-state2', -}).persist(); +}).persist({ + storageKeys: ['myStorage', 'localStorage'], + defaultStorageKey: 'localStorage', // where the persisted value gets loaded from (saved is it in all provided Storages (storageKeys)) +}); MY_STATE_2.onLoad(() => { console.log('On Load MY_STATE_2'); }); diff --git a/packages/core/src/collection/group.ts b/packages/core/src/collection/group.ts index 7e7b35e6..984c71ea 100644 --- a/packages/core/src/collection/group.ts +++ b/packages/core/src/collection/group.ts @@ -273,6 +273,7 @@ export class Group extends State> { loadValue: true, followCollectionPattern: false, storageKeys: [], + defaultStorageKey: null, }); if (_config.followCollectionPersistKeyPattern) { @@ -285,6 +286,7 @@ export class Group extends State> { super.persist(key, { loadValue: _config.loadValue, storageKeys: _config.storageKeys, + defaultStorageKey: _config.defaultStorageKey, }); return this; diff --git a/packages/core/src/collection/index.ts b/packages/core/src/collection/index.ts index b1452af4..5ca05bad 100644 --- a/packages/core/src/collection/index.ts +++ b/packages/core/src/collection/index.ts @@ -801,6 +801,7 @@ export class Collection { _config = defineConfig(_config, { loadValue: true, storageKeys: [], + defaultStorageKey: null, }); if (this.persistent) @@ -813,6 +814,7 @@ export class Collection { instantiate: _config.loadValue, storageKeys: _config.storageKeys, key: key, + defaultStorageKey: _config.defaultStorageKey, }); return this; @@ -1281,10 +1283,12 @@ export interface HasConfigInterface { /** * @param loadValue - If Persistent loads the persisted value into the Collection * @param storageKeys - Key/Name of Storages which gets used to persist the Collection Value (NOTE: If not passed the default Storage will be used) + * @param defaultStorageKey - Default Storage Key (if not provided it takes the first index of storageKeys or the AgileTs default Storage) */ export interface CollectionPersistentConfigInterface { loadValue?: boolean; storageKeys?: StorageKey[]; + defaultStorageKey?: StorageKey; } /** diff --git a/packages/core/src/state/index.ts b/packages/core/src/state/index.ts index dcaecd50..6b9cb056 100644 --- a/packages/core/src/state/index.ts +++ b/packages/core/src/state/index.ts @@ -399,6 +399,7 @@ export class State { _config = defineConfig(_config, { loadValue: true, storageKeys: [], + defaultStorageKey: null, }); if (this.persistent) @@ -411,6 +412,7 @@ export class State { instantiate: _config.loadValue, storageKeys: _config.storageKeys, key: key, + defaultStorageKey: _config.defaultStorageKey, }); return this; @@ -659,10 +661,12 @@ export interface PatchConfigInterface extends StateIngestConfigInterface { /** * @param loadValue - If Persistent loads the persisted value into the State * @param storageKeys - Key/Name of Storages which gets used to persist the State Value (NOTE: If not passed the default Storage will be used) + * @param defaultStorageKey - Default Storage Key (if not provided it takes the first index of storageKeys or the AgileTs default Storage) */ export interface StatePersistentConfigInterface { loadValue?: boolean; storageKeys?: StorageKey[]; + defaultStorageKey?: StorageKey; } export type StateWatcherCallback = (value: T, key: string) => void; diff --git a/packages/core/src/storages/persistent.ts b/packages/core/src/storages/persistent.ts index 313e1d65..00269b89 100644 --- a/packages/core/src/storages/persistent.ts +++ b/packages/core/src/storages/persistent.ts @@ -263,7 +263,7 @@ export interface PersistentConfigInterface { /** * @param key - Key/Name of Persistent * @param storageKeys - Keys of Storages in that the persisted Value gets saved - * @param defaultStorageKey - Default Storage Key + * @param defaultStorageKey - Default Storage Key (if not provided it takes the first index of storageKeys or the AgileTs default Storage) */ export interface InstantiatePersistentConfigInterface { key?: PersistentKey; From 98417a6809483ae94e07c1c7a12d3155ebc9f2e8 Mon Sep 17 00:00:00 2001 From: Benno Kohrs Date: Sun, 18 Apr 2021 08:23:54 +0200 Subject: [PATCH 10/10] fixed tests --- .../core/tests/unit/collection/collection.test.ts | 7 +++++++ packages/core/tests/unit/collection/group.test.ts | 13 ++++++++++++- packages/core/tests/unit/state/state.test.ts | 7 +++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/packages/core/tests/unit/collection/collection.test.ts b/packages/core/tests/unit/collection/collection.test.ts index 7e28b282..edee560a 100644 --- a/packages/core/tests/unit/collection/collection.test.ts +++ b/packages/core/tests/unit/collection/collection.test.ts @@ -1588,6 +1588,7 @@ describe('Collection Tests', () => { instantiate: true, storageKeys: [], key: collection._key, + defaultStorageKey: null, }); }); @@ -1595,6 +1596,7 @@ describe('Collection Tests', () => { collection.persist({ storageKeys: ['test1', 'test2'], loadValue: false, + defaultStorageKey: 'test1', }); expect(collection.persistent).toBeInstanceOf(CollectionPersistent); @@ -1602,6 +1604,7 @@ describe('Collection Tests', () => { instantiate: false, storageKeys: ['test1', 'test2'], key: collection._key, + defaultStorageKey: 'test1', }); }); @@ -1613,6 +1616,7 @@ describe('Collection Tests', () => { instantiate: true, storageKeys: [], key: 'passedKey', + defaultStorageKey: null, }); }); @@ -1620,6 +1624,7 @@ describe('Collection Tests', () => { collection.persist('passedKey', { storageKeys: ['test1', 'test2'], loadValue: false, + defaultStorageKey: 'test1', }); expect(collection.persistent).toBeInstanceOf(CollectionPersistent); @@ -1627,6 +1632,7 @@ describe('Collection Tests', () => { instantiate: false, storageKeys: ['test1', 'test2'], key: 'passedKey', + defaultStorageKey: 'test1', }); }); @@ -1641,6 +1647,7 @@ describe('Collection Tests', () => { instantiate: true, storageKeys: [], key: 'newPersistentKey', + defaultStorageKey: null, }); expect(console.warn).toHaveBeenCalledWith( `Agile Warn: By persisting the Collection '${collection._key}' twice you overwrite the old Persistent Instance!` diff --git a/packages/core/tests/unit/collection/group.test.ts b/packages/core/tests/unit/collection/group.test.ts index 60f3b52c..711c3e00 100644 --- a/packages/core/tests/unit/collection/group.test.ts +++ b/packages/core/tests/unit/collection/group.test.ts @@ -436,15 +436,21 @@ describe('Group Tests', () => { expect(State.prototype.persist).toHaveBeenCalledWith(group._key, { loadValue: true, storageKeys: [], + defaultStorageKey: null, }); }); it('should persist Group with GroupKey (specific config)', () => { - group.persist({ loadValue: false, storageKeys: ['test1', 'test2'] }); + group.persist({ + loadValue: false, + storageKeys: ['test1', 'test2'], + defaultStorageKey: 'test1', + }); expect(State.prototype.persist).toHaveBeenCalledWith(group._key, { loadValue: false, storageKeys: ['test1', 'test2'], + defaultStorageKey: 'test1', }); }); @@ -454,6 +460,7 @@ describe('Group Tests', () => { expect(State.prototype.persist).toHaveBeenCalledWith('dummyKey', { loadValue: true, storageKeys: [], + defaultStorageKey: null, }); }); @@ -461,11 +468,13 @@ describe('Group Tests', () => { group.persist('dummyKey', { loadValue: false, storageKeys: ['test1', 'test2'], + defaultStorageKey: 'test1', }); expect(State.prototype.persist).toHaveBeenCalledWith('dummyKey', { loadValue: false, storageKeys: ['test1', 'test2'], + defaultStorageKey: 'test1', }); }); @@ -480,6 +489,7 @@ describe('Group Tests', () => { { loadValue: true, storageKeys: [], + defaultStorageKey: null, } ); }); @@ -495,6 +505,7 @@ describe('Group Tests', () => { { loadValue: true, storageKeys: [], + defaultStorageKey: null, } ); }); diff --git a/packages/core/tests/unit/state/state.test.ts b/packages/core/tests/unit/state/state.test.ts index 726c01c3..86d89205 100644 --- a/packages/core/tests/unit/state/state.test.ts +++ b/packages/core/tests/unit/state/state.test.ts @@ -594,6 +594,7 @@ describe('State Tests', () => { instantiate: true, storageKeys: [], key: numberState._key, + defaultStorageKey: null, }); }); @@ -601,6 +602,7 @@ describe('State Tests', () => { numberState.persist({ storageKeys: ['test1', 'test2'], loadValue: false, + defaultStorageKey: 'test1', }); expect(numberState.persistent).toBeInstanceOf(StatePersistent); @@ -608,6 +610,7 @@ describe('State Tests', () => { instantiate: false, storageKeys: ['test1', 'test2'], key: numberState._key, + defaultStorageKey: 'test1', }); }); @@ -619,6 +622,7 @@ describe('State Tests', () => { instantiate: true, storageKeys: [], key: 'passedKey', + defaultStorageKey: null, }); }); @@ -626,6 +630,7 @@ describe('State Tests', () => { numberState.persist('passedKey', { storageKeys: ['test1', 'test2'], loadValue: false, + defaultStorageKey: 'test1', }); expect(numberState.persistent).toBeInstanceOf(StatePersistent); @@ -633,6 +638,7 @@ describe('State Tests', () => { instantiate: false, storageKeys: ['test1', 'test2'], key: 'passedKey', + defaultStorageKey: 'test1', }); }); @@ -647,6 +653,7 @@ describe('State Tests', () => { instantiate: true, storageKeys: [], key: 'newPersistentKey', + defaultStorageKey: null, }); expect(console.warn).toHaveBeenCalledWith( `Agile Warn: By persisting the State '${numberState._key}' twice you overwrite the old Persistent Instance!`