|
3 | 3 | CollectionKey, |
4 | 4 | CreatePersistentConfigInterface, |
5 | 5 | DefaultItem, |
| 6 | + defineConfig, |
6 | 7 | Group, |
7 | 8 | GroupKey, |
8 | 9 | ItemKey, |
@@ -36,12 +37,11 @@ export class CollectionPersistent< |
36 | 37 | super(collection.agileInstance(), { |
37 | 38 | instantiate: false, |
38 | 39 | }); |
39 | | - config = { |
| 40 | + config = defineConfig(config, { |
40 | 41 | instantiate: true, |
41 | 42 | storageKeys: [], |
42 | 43 | defaultStorageKey: null as any, |
43 | | - ...config, |
44 | | - }; |
| 44 | + }); |
45 | 45 | this.collection = () => collection; |
46 | 46 | this.instantiatePersistent({ |
47 | 47 | key: config.key, |
@@ -128,35 +128,49 @@ export class CollectionPersistent< |
128 | 128 | _storageItemKey |
129 | 129 | ); |
130 | 130 |
|
131 | | - // Persist and therefore load already present Item |
| 131 | + // Persist an already present Item and load its value manually to be 100% sure |
| 132 | + // that it was loaded completely |
132 | 133 | if (item != null) { |
133 | 134 | item.persist(itemStorageKey, { |
| 135 | + loadValue: false, |
134 | 136 | defaultStorageKey: this.config.defaultStorageKey || undefined, |
135 | 137 | storageKeys: this.storageKeys, |
136 | 138 | followCollectionPersistKeyPattern: false, // Because of the dynamic 'storageItemKey', the key is already formatted above |
137 | 139 | }); |
| 140 | + if (item?.persistent?.ready) await item.persistent.initialLoading(); |
138 | 141 | } |
139 | | - // Persist and therefore load not present Item |
| 142 | + // Persist an already present placeholder Item |
| 143 | + // or create a new placeholder Item to load the value in |
| 144 | + // and load its value manually to be 100% sure |
| 145 | + // that it was loaded completely. |
| 146 | + // After a successful loading assign the now valid Item to the Collection. |
140 | 147 | else { |
141 | | - // Create temporary placeholder Item in which the Item value will be loaded |
142 | | - const dummyItem = this.collection().createPlaceholderItem(itemKey); |
143 | | - |
144 | | - // Persist dummy Item and load its value manually to be 100% sure |
145 | | - // that it was loaded completely and exists at all |
146 | | - dummyItem?.persist(itemStorageKey, { |
| 148 | + const placeholderItem = this.collection().getItemWithReference( |
| 149 | + itemKey |
| 150 | + ); |
| 151 | + placeholderItem?.persist(itemStorageKey, { |
147 | 152 | loadValue: false, |
148 | 153 | defaultStorageKey: this.config.defaultStorageKey as any, |
149 | 154 | storageKeys: this.storageKeys, |
150 | 155 | followCollectionPersistKeyPattern: false, // Because of the dynamic 'storageItemKey', the key is already formatted above |
151 | 156 | }); |
152 | | - if (dummyItem?.persistent?.ready) { |
153 | | - const loadedPersistedValueIntoItem = await dummyItem.persistent.loadPersistedValue( |
154 | | - itemStorageKey |
155 | | - ); // TODO FIRST GROUP REBUILD (by assigning loaded value to Item) |
| 157 | + if (placeholderItem?.persistent?.ready) { |
| 158 | + const loadedPersistedValueIntoItem = await placeholderItem.persistent.loadPersistedValue(); // TODO FIRST GROUP REBUILD (by assigning loaded value to Item) |
156 | 159 |
|
157 | 160 | // If successfully loaded Item value, assign Item to Collection |
158 | | - if (loadedPersistedValueIntoItem) |
159 | | - this.collection().assignItem(dummyItem, { overwrite: false }); // TODO SECOND GROUP REBUILD (by calling rebuildGroupThatInclude() in the assignItem() method) |
| 161 | + if (loadedPersistedValueIntoItem) { |
| 162 | + this.collection().assignItem(placeholderItem, { |
| 163 | + overwrite: true, // Overwrite to overwrite the existing placeholder Item, if one exists |
| 164 | + }); // TODO SECOND GROUP REBUILD (by calling rebuildGroupThatInclude() in the assignItem() method) |
| 165 | + |
| 166 | + placeholderItem.isPersisted = true; |
| 167 | + |
| 168 | + // Manually increase Collection size, |
| 169 | + // since these Items already exist in the Collection (because of 'getItemWithReference()') |
| 170 | + // but were placeholder before the persisted value got loaded |
| 171 | + // -> Collection size wasn't increased in 'assignItem()' |
| 172 | + this.collection().size += 1; |
| 173 | + } |
160 | 174 | } |
161 | 175 | } |
162 | 176 | } |
|
0 commit comments