Skip to content
This repository was archived by the owner on Oct 16, 2024. It is now read-only.

Commit 5cd5f89

Browse files
authored
Merge pull request #214 from agile-ts/add_migration_callback
add migration callback
2 parents aa7c184 + 844a680 commit 5cd5f89

File tree

13 files changed

+320
-610
lines changed

13 files changed

+320
-610
lines changed

packages/core/src/collection/collection.persistent.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ export class CollectionPersistent<
101101
// Persist default Group and load its value manually to be 100% sure
102102
// that it was loaded completely
103103
defaultGroup.loadedInitialValue = false;
104-
defaultGroup.persist(defaultGroupStorageKey, {
104+
defaultGroup.persist({
105+
key: defaultGroupStorageKey,
105106
loadValue: false,
106107
defaultStorageKey: this.config.defaultStorageKey || undefined,
107108
storageKeys: this.storageKeys,
@@ -121,7 +122,8 @@ export class CollectionPersistent<
121122
// Persist an already present Item and load its value manually to be 100% sure
122123
// that it was loaded completely
123124
if (item != null) {
124-
item.persist(itemStorageKey, {
125+
item.persist({
126+
key: itemStorageKey,
125127
loadValue: false,
126128
defaultStorageKey: this.config.defaultStorageKey || undefined,
127129
storageKeys: this.storageKeys,
@@ -135,17 +137,18 @@ export class CollectionPersistent<
135137
// that it was loaded completely.
136138
// After a successful loading assign the now valid Item to the Collection.
137139
else {
138-
const placeholderItem =
139-
this.collection().getItemWithReference(itemKey);
140-
placeholderItem?.persist(itemStorageKey, {
140+
const placeholderItem = this.collection().getItemWithReference(
141+
itemKey
142+
);
143+
placeholderItem?.persist({
144+
key: itemStorageKey,
141145
loadValue: false,
142146
defaultStorageKey: this.config.defaultStorageKey as any,
143147
storageKeys: this.storageKeys,
144148
followCollectionPersistKeyPattern: false, // Because of the dynamic 'storageItemKey', the key is already formatted above
145149
});
146150
if (placeholderItem?.persistent?.ready) {
147-
const loadedPersistedValueIntoItem =
148-
await placeholderItem.persistent.loadPersistedValue();
151+
const loadedPersistedValueIntoItem = await placeholderItem.persistent.loadPersistedValue();
149152

150153
// If successfully loaded Item value, assign Item to Collection
151154
if (loadedPersistedValueIntoItem) {
@@ -202,7 +205,8 @@ export class CollectionPersistent<
202205
getSharedStorageManager()?.set(_storageItemKey, true, this.storageKeys);
203206

204207
// Persist default Group
205-
defaultGroup.persist(defaultGroupStorageKey, {
208+
defaultGroup.persist({
209+
key: defaultGroupStorageKey,
206210
defaultStorageKey: this.config.defaultStorageKey || undefined,
207211
storageKeys: this.storageKeys,
208212
followCollectionPersistKeyPattern: false, // Because of the dynamic 'storageItemKey', the key is already formatted above
@@ -215,7 +219,8 @@ export class CollectionPersistent<
215219
itemKey,
216220
_storageItemKey
217221
);
218-
item?.persist(itemStorageKey, {
222+
item?.persist({
223+
key: itemStorageKey,
219224
defaultStorageKey: this.config.defaultStorageKey || undefined,
220225
storageKeys: this.storageKeys,
221226
followCollectionPersistKeyPattern: false, // Because of the dynamic 'storageItemKey', the key is already formatted above
@@ -348,7 +353,8 @@ export class CollectionPersistent<
348353
_storageItemKey
349354
);
350355
if (item != null && !item.isPersisted)
351-
item.persist(itemStorageKey, {
356+
item.persist({
357+
key: itemStorageKey,
352358
defaultStorageKey: this.config.defaultStorageKey || undefined,
353359
storageKeys: this.storageKeys,
354360
followCollectionPersistKeyPattern: false, // Because of the dynamic 'storageItemKey', the key is already formatted above

packages/core/src/collection/collection.ts

Lines changed: 15 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
TrackedChangeMethod,
2121
} from './group';
2222
import { GroupIngestConfigInterface } from './group/group.observer';
23-
import { StorageKey } from '../storages';
23+
import { CreatePersistentConfigInterface, StorageKey } from '../storages';
2424
import { CollectionPersistent } from './collection.persistent';
2525

2626
export class Collection<DataType extends DefaultItem = DefaultItem> {
@@ -936,53 +936,16 @@ export class Collection<DataType extends DefaultItem = DefaultItem> {
936936
* @public
937937
* @param config - Configuration object
938938
*/
939-
public persist(config?: CollectionPersistentConfigInterface): this;
940-
/**
941-
* Preserves the Collection `value` in the corresponding external Storage.
942-
*
943-
* The specified key is used as the unique identifier for the Persistent.
944-
*
945-
* [Learn more..](https://agile-ts.org/docs/core/collection/methods/#persist)
946-
*
947-
* @public
948-
* @param key - Key/Name identifier of Persistent.
949-
* @param config - Configuration object
950-
*/
951-
public persist(
952-
key?: StorageKey,
953-
config?: CollectionPersistentConfigInterface
954-
): this;
955-
public persist(
956-
keyOrConfig: StorageKey | CollectionPersistentConfigInterface = {},
957-
config: CollectionPersistentConfigInterface = {}
958-
): this {
959-
let _config: CollectionPersistentConfigInterface;
960-
let key: StorageKey | undefined;
961-
962-
if (isValidObject(keyOrConfig)) {
963-
_config = keyOrConfig as CollectionPersistentConfigInterface;
964-
key = this._key;
965-
} else {
966-
_config = config || {};
967-
key = keyOrConfig as StorageKey;
968-
}
969-
970-
_config = defineConfig(_config, {
971-
loadValue: true,
972-
storageKeys: [],
973-
defaultStorageKey: null as any,
939+
public persist(config: CreatePersistentConfigInterface = {}): this {
940+
config = defineConfig(config, {
941+
key: this.key,
974942
});
975943

976944
// Check if Collection is already persisted
977945
if (this.persistent != null && this.isPersisted) return this;
978946

979947
// Create Persistent (-> persist value)
980-
this.persistent = new CollectionPersistent<DataType>(this, {
981-
loadValue: _config.loadValue,
982-
storageKeys: _config.storageKeys,
983-
key: key,
984-
defaultStorageKey: _config.defaultStorageKey,
985-
});
948+
this.persistent = new CollectionPersistent<DataType>(this, config);
986949

987950
return this;
988951
}
@@ -1218,7 +1181,9 @@ export class Collection<DataType extends DefaultItem = DefaultItem> {
12181181
* @public
12191182
* @param itemKeys - Item/s with identifier/s to be removed.
12201183
*/
1221-
public remove(itemKeys: ItemKey | Array<ItemKey>): {
1184+
public remove(
1185+
itemKeys: ItemKey | Array<ItemKey>
1186+
): {
12221187
fromGroups: (groups: Array<ItemKey> | ItemKey) => Collection<DataType>;
12231188
everywhere: (config?: RemoveItemsConfigInterface) => Collection<DataType>;
12241189
} {
@@ -1553,12 +1518,13 @@ export interface CreateCollectionConfigImpl<
15531518
initialData?: Array<DataType>;
15541519
}
15551520

1556-
export type CreateCollectionConfig<DataType extends DefaultItem = DefaultItem> =
1557-
1558-
| CreateCollectionConfigImpl<DataType>
1559-
| ((
1560-
collection: Collection<DataType>
1561-
) => CreateCollectionConfigImpl<DataType>);
1521+
export type CreateCollectionConfig<
1522+
DataType extends DefaultItem = DefaultItem
1523+
> =
1524+
| CreateCollectionConfigImpl<DataType>
1525+
| ((
1526+
collection: Collection<DataType>
1527+
) => CreateCollectionConfigImpl<DataType>);
15621528

15631529
export interface CollectionConfigInterface {
15641530
/**
@@ -1636,31 +1602,6 @@ export interface HasConfigInterface {
16361602
notExisting?: boolean;
16371603
}
16381604

1639-
export interface CollectionPersistentConfigInterface {
1640-
/**
1641-
* Whether the Persistent should automatically load
1642-
* the persisted value into the Collection after its instantiation.
1643-
* @default true
1644-
*/
1645-
loadValue?: boolean;
1646-
/**
1647-
* Key/Name identifier of Storages
1648-
* in which the Collection value should be or is persisted.
1649-
* @default [`defaultStorageKey`]
1650-
*/
1651-
storageKeys?: StorageKey[];
1652-
/**
1653-
* Key/Name identifier of the default Storage of the specified Storage keys.
1654-
*
1655-
* The Collection value is loaded from the default Storage by default
1656-
* and is only loaded from the remaining Storages (`storageKeys`)
1657-
* if the loading from the default Storage failed.
1658-
*
1659-
* @default first index of the specified Storage keys or the AgileTs default Storage key
1660-
*/
1661-
defaultStorageKey?: StorageKey;
1662-
}
1663-
16641605
export interface RemoveItemsConfigInterface {
16651606
/**
16661607
* Whether to remove not officially existing Items (such as placeholder Items).

packages/core/src/collection/group/index.ts

Lines changed: 11 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,20 @@
1-
import {
2-
copy,
3-
defineConfig,
4-
isValidObject,
5-
normalizeArray,
6-
} from '@agile-ts/utils';
1+
import { copy, defineConfig, normalizeArray } from '@agile-ts/utils';
72
import { logCodeManager } from '../../logCodeManager';
83
import {
4+
CreateStatePersistentConfigInterface,
95
EnhancedState,
106
StateIngestConfigInterface,
117
StateObserver,
128
StateObserversInterface,
13-
StatePersistentConfigInterface,
149
} from '../../state';
1510
import { Collection, DefaultItem, ItemKey } from '../collection';
1611
import { GroupIngestConfigInterface, GroupObserver } from './group.observer';
1712
import { ComputedTracker } from '../../computed';
1813
import { Item } from '../item';
19-
import { PersistentKey } from '../../storages';
2014
import { CollectionPersistent } from '../collection.persistent';
2115

2216
export class Group<
23-
DataType extends DefaultItem = DefaultItem,
24-
ValueType = Array<ItemKey> // To extract the Group Type Value in Integration methods like 'useAgile()'
17+
DataType extends DefaultItem = DefaultItem
2518
> extends EnhancedState<Array<ItemKey>> {
2619
// Collection the Group belongs to
2720
collection: () => Collection<DataType>;
@@ -322,58 +315,22 @@ export class Group<
322315
* @public
323316
* @param config - Configuration object
324317
*/
325-
public persist(config?: GroupPersistConfigInterface): this;
326-
/**
327-
* Preserves the Group `value` in the corresponding external Storage.
328-
*
329-
* The specified key is used as the unique identifier for the Persistent.
330-
*
331-
* [Learn more..](https://agile-ts.org/docs/core/state/methods/#persist)
332-
*
333-
* @public
334-
* @param key - Key/Name identifier of Persistent.
335-
* @param config - Configuration object
336-
*/
337-
public persist(
338-
key?: PersistentKey,
339-
config?: GroupPersistConfigInterface
340-
): this;
341-
public persist(
342-
keyOrConfig: PersistentKey | GroupPersistConfigInterface = {},
343-
config: GroupPersistConfigInterface = {}
344-
): this {
345-
let _config: GroupPersistConfigInterface;
346-
let key: PersistentKey | undefined;
347-
348-
if (isValidObject(keyOrConfig)) {
349-
_config = keyOrConfig as GroupPersistConfigInterface;
350-
key = this._key;
351-
} else {
352-
_config = config || {};
353-
key = keyOrConfig as PersistentKey;
354-
}
355-
356-
_config = defineConfig(_config, {
357-
loadValue: true,
318+
public persist(config: GroupPersistConfigInterface = {}): this {
319+
config = defineConfig(config, {
320+
key: this._key,
358321
followCollectionPersistKeyPattern: true,
359-
storageKeys: [],
360-
defaultStorageKey: null as any,
361322
});
362323

363324
// Create storageItemKey based on Collection key/name identifier
364-
if (_config.followCollectionPersistKeyPattern) {
365-
key = CollectionPersistent.getGroupStorageKey(
366-
key || this._key,
325+
if (config.followCollectionPersistKeyPattern) {
326+
config.key = CollectionPersistent.getGroupStorageKey(
327+
config.key || this._key,
367328
this.collection()._key
368329
);
369330
}
370331

371332
// Persist Group
372-
super.persist(key, {
373-
loadValue: _config.loadValue,
374-
storageKeys: _config.storageKeys,
375-
defaultStorageKey: _config.defaultStorageKey,
376-
});
333+
super.persist(config);
377334

378335
return this;
379336
}
@@ -524,7 +481,7 @@ export interface GroupConfigInterface {
524481
}
525482

526483
export interface GroupPersistConfigInterface
527-
extends StatePersistentConfigInterface {
484+
extends CreateStatePersistentConfigInterface {
528485
/**
529486
* Whether to format the specified Storage key following the Collection Group Storage key pattern.
530487
* `_${collectionKey}_group_${groupKey}`

packages/core/src/collection/item.ts

Lines changed: 10 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { defineConfig, isValidObject } from '@agile-ts/utils';
1+
import { defineConfig } from '@agile-ts/utils';
22
import {
3+
CreateStatePersistentConfigInterface,
34
EnhancedState,
45
StateKey,
5-
StatePersistentConfigInterface,
66
StateRuntimeJobConfigInterface,
77
} from '../state';
88
import { Collection, DefaultItem } from './collection';
@@ -96,59 +96,21 @@ export class Item<
9696
* @public
9797
* @param config - Configuration object
9898
*/
99-
public persist(config?: ItemPersistConfigInterface): this;
100-
/**
101-
* Preserves the Item `value` in the corresponding external Storage.
102-
*
103-
* The specified key is used as the unique identifier for the Persistent.
104-
*
105-
* [Learn more..](https://agile-ts.org/docs/core/state/methods/#persist)
106-
*
107-
* @public
108-
* @param key - Key/Name identifier of Persistent.
109-
* @param config - Configuration object
110-
*/
111-
public persist(
112-
key?: PersistentKey,
113-
config?: ItemPersistConfigInterface
114-
): this;
115-
public persist(
116-
keyOrConfig: PersistentKey | ItemPersistConfigInterface = {},
117-
config: ItemPersistConfigInterface = {}
118-
): this {
119-
let _config: ItemPersistConfigInterface;
120-
let key: PersistentKey | undefined;
121-
122-
if (isValidObject(keyOrConfig)) {
123-
_config = keyOrConfig as ItemPersistConfigInterface;
124-
key = this._key;
125-
} else {
126-
_config = config || {};
127-
key = keyOrConfig as PersistentKey;
128-
}
129-
130-
_config = defineConfig(_config, {
131-
loadValue: true,
99+
public persist(config: ItemPersistConfigInterface = {}): this {
100+
config = defineConfig(config, {
101+
key: this._key,
132102
followCollectionPersistKeyPattern: true,
133-
storageKeys: [],
134-
defaultStorageKey: null as any,
135103
});
136104

137105
// Create storageItemKey based on Collection key/name identifier
138-
if (_config.followCollectionPersistKeyPattern) {
139-
key = CollectionPersistent.getItemStorageKey(
140-
key || this._key,
106+
if (config.followCollectionPersistKeyPattern) {
107+
config.key = CollectionPersistent.getItemStorageKey(
108+
config.key || this._key,
141109
this.collection()._key
142110
);
143111
}
144112

145-
// Persist Item
146-
super.persist(key, {
147-
loadValue: _config.loadValue,
148-
storageKeys: _config.storageKeys,
149-
defaultStorageKey: _config.defaultStorageKey,
150-
});
151-
113+
super.persist(config);
152114
return this;
153115
}
154116

@@ -181,7 +143,7 @@ export interface ItemConfigInterface {
181143
}
182144

183145
export interface ItemPersistConfigInterface
184-
extends StatePersistentConfigInterface {
146+
extends CreateStatePersistentConfigInterface {
185147
/**
186148
* Whether to format the specified Storage key following the Collection Item Storage key pattern.
187149
* `_${collectionKey}_item_${itemKey}`

0 commit comments

Comments
 (0)