@@ -18,6 +18,29 @@ type HasStoreData = GConstructor<{ _storeData: { items: Array<Link> } }>;
18
18
19
19
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
20
20
function HasItems < TBase extends HasStoreData > ( Base : TBase , apiActions : ApiActions , config : InternalConfig , reloadUri ?: string , reloadProperty ?: string ) {
21
+ /**
22
+ * Returns a promise that resolves to items, once all items have been loaded
23
+ */
24
+ function itemLoader ( array : Array < Link > ) : Promise < Array < Resource > > {
25
+ if ( ! containsUnknownEntityReference ( array ) ) {
26
+ return Promise . resolve ( replaceEntityReferences ( array ) )
27
+ }
28
+
29
+ // eager loading of 'fetchAllUri' (e.g. parent for embedded collections)
30
+ if ( config . avoidNPlusOneRequests ) {
31
+ return apiActions . reload ( { _meta : { reload : { uri : reloadUri || '' , property : reloadProperty || '' } } } )
32
+ . then ( ( ) => replaceEntityReferences ( array ) )
33
+
34
+ // no eager loading: replace each reference (Link) with a StoreValue (Resource)
35
+ } else {
36
+ const arrayWithReplacedReferences = replaceEntityReferences ( array )
37
+
38
+ return Promise . all (
39
+ arrayWithReplacedReferences . map ( entry => entry . _meta . load )
40
+ )
41
+ }
42
+ }
43
+
21
44
/**
22
45
* Filter out items that are marked as deleting (eager removal)
23
46
*/
@@ -35,26 +58,19 @@ function HasItems<TBase extends HasStoreData> (Base: TBase, apiActions: ApiActio
35
58
* @returns array the new array with replaced items, or a LoadingStoreCollection if any of the array
36
59
* elements is still loading.
37
60
*/
38
- function mapArrayOfEntityReferences ( array : Array < Link > , fetchAllUri : string , fetchAllProperty : string ) : Array < Resource > {
61
+ function mapArrayOfEntityReferences ( array : Array < Link > ) : Array < Resource > {
39
62
if ( ! containsUnknownEntityReference ( array ) ) {
40
63
return replaceEntityReferences ( array )
41
64
}
42
65
66
+ const itemsLoaded = itemLoader ( array )
67
+
43
68
// eager loading of 'fetchAllUri' (e.g. parent for embedded collections)
44
69
if ( config . avoidNPlusOneRequests ) {
45
- const completelyLoaded = apiActions . reload ( { _meta : { reload : { uri : fetchAllUri , property : fetchAllProperty } } } )
46
- . then ( ( ) => replaceEntityReferences ( array ) )
47
- return LoadingStoreCollection . create ( completelyLoaded )
48
-
70
+ return LoadingStoreCollection . create ( itemsLoaded )
49
71
// no eager loading: replace each reference (Link) with a StoreValue (Resource)
50
72
} else {
51
- const arrayWithReplacedReferences = replaceEntityReferences ( array )
52
-
53
- const arrayCompletelyLoaded = Promise . all (
54
- arrayWithReplacedReferences . map ( entry => entry . _meta . load )
55
- )
56
-
57
- return LoadingStoreCollection . create ( arrayCompletelyLoaded , arrayWithReplacedReferences )
73
+ return LoadingStoreCollection . create ( itemsLoaded , replaceEntityReferences ( array ) )
58
74
}
59
75
}
60
76
@@ -75,23 +91,27 @@ function HasItems<TBase extends HasStoreData> (Base: TBase, apiActions: ApiActio
75
91
}
76
92
77
93
const HasItems = class extends Base {
78
- fetchAllUri = reloadUri || ''
79
- fetchAllProperty = reloadProperty || ''
80
-
81
94
/**
82
95
* Get items excluding ones marked as 'deleting' (eager remove)
83
96
* The items property should always be a getter, in order to make the call to mapArrayOfEntityReferences
84
97
* lazy, since that potentially fetches a large number of entities from the API.
85
98
*/
86
99
public get items ( ) : Array < Resource > {
87
- return filterDeleting ( mapArrayOfEntityReferences ( this . _storeData . items , this . fetchAllUri , this . fetchAllProperty ) )
100
+ return filterDeleting ( mapArrayOfEntityReferences ( this . _storeData . items ) )
88
101
}
89
102
90
103
/**
91
104
* Get all items including ones marked as 'deleting' (lazy remove)
92
105
*/
93
106
public get allItems ( ) : Array < Resource > {
94
- return mapArrayOfEntityReferences ( this . _storeData . items , this . fetchAllUri , this . fetchAllProperty )
107
+ return mapArrayOfEntityReferences ( this . _storeData . items )
108
+ }
109
+
110
+ /**
111
+ * Returns a promise that resolves to items, once all items have been loaded
112
+ */
113
+ public $loadItems ( ) :Promise < Array < Resource > > {
114
+ return itemLoader ( this . _storeData . items )
95
115
}
96
116
}
97
117
0 commit comments