@@ -20,113 +20,6 @@ export class EntityMapper extends React.Component<
20
20
21
21
lastAssuredProps : EntityMapperInnerProps | null = null ;
22
22
23
- /**
24
- * This gets a component from some mapper data.
25
- * This can also be a promise resolving to a component.
26
- */
27
- static getComponentOrPromiseFromMapper (
28
- props : EntityMapperInnerProps ,
29
- ) :
30
- | { type : 'component' ; value : undefined | React . ReactType }
31
- | { type : 'promise' ; value : undefined | ( ( ) => ImportReturn ) } {
32
- // This gets the entity from the site, based on the uuid.
33
- const entity = props . site . getData ( props . uuid ) ;
34
-
35
- // This should give back a bundle string, that is used in the mapper.
36
- const bundle = EntityMapper . getBundle ( entity ) ;
37
-
38
- // If it's not an async mapper, we can just use the 'mapper' prop.
39
- if ( ! props . asyncMapper ) {
40
- return {
41
- type : 'component' ,
42
- value :
43
- typeof props . mapper === 'function'
44
- ? props . mapper ( entity , bundle )
45
- : props . mapper [ bundle ] ,
46
- } ;
47
- }
48
-
49
- // We check which style of the mapper it is.
50
- const entityComponentMapper =
51
- props . asyncMapper === true ? props . mapper : props . asyncMapper ;
52
- const entityComponentResult =
53
- typeof entityComponentMapper === 'function'
54
- ? entityComponentMapper ( entity , bundle )
55
- : entityComponentMapper [ bundle ] ;
56
-
57
- return {
58
- type : 'promise' ,
59
- value : entityComponentResult ,
60
- } ;
61
- }
62
-
63
- /**
64
- * This makes sure the data for this url is ready to be rendered.
65
- */
66
- static async assureComponent ( props : EntityMapperInnerProps ) {
67
- const component = this . getComponentOrPromiseFromMapper ( props ) ;
68
-
69
- // If it's not a promise, return.
70
- // Nothing to assure.
71
- if ( component . type === 'component' ) {
72
- return ;
73
- }
74
-
75
- // If no value was returned from the mapper, we can't assure the component.
76
- if ( ! component . value ) {
77
- return ;
78
- }
79
-
80
- // Check if we already cached this component.
81
- if ( entityCache . has ( component . value ) ) {
82
- return ;
83
- }
84
-
85
- // If not, we get this entity now.
86
- const entityComponentResult = await component . value ( ) ;
87
-
88
- // We save the entity in the cache.
89
- if (
90
- typeof entityComponentResult === 'object' &&
91
- typeof entityComponentResult . default !== 'undefined'
92
- ) {
93
- entityCache . set ( component . value , entityComponentResult . default ) ;
94
- } else {
95
- entityCache . set (
96
- component . value ,
97
- entityComponentResult as React . ReactType ,
98
- ) ;
99
- }
100
- }
101
-
102
- /**
103
- * Returns the component if it is availible right now.
104
- * Returns null if the component isn't available, and never will be.
105
- * Returns undefined if the component isn't available, but maybe will be in the future.
106
- */
107
- static getComponentFromMapper (
108
- props : EntityMapperInnerProps ,
109
- ) : React . ReactType | null | undefined {
110
- const entityComponent = EntityMapper . getComponentOrPromiseFromMapper ( props ) ;
111
-
112
- if (
113
- entityComponent . type === 'promise' &&
114
- typeof entityComponent . value !== 'undefined'
115
- ) {
116
- return entityCache . get ( entityComponent . value ) ;
117
- }
118
- if ( entityComponent . type === 'component' ) {
119
- return entityComponent . value || null ;
120
- }
121
- return null ;
122
- }
123
-
124
- static getBundle = entity =>
125
- getNested (
126
- ( ) => `${ entity . __hn . entity . type } __${ entity . __hn . entity . bundle } ` ,
127
- '_fallback' ,
128
- ) ;
129
-
130
23
/**
131
24
* If this component exists in a tree that is invoked with the waitForHnData function, this function is invoked.
132
25
* Only after the promise is resolved, the component will be mounted. To keep the data fetched here, we assign the
@@ -135,7 +28,7 @@ export class EntityMapper extends React.Component<
135
28
*/
136
29
async bootstrap ( ) {
137
30
// Make sure that if this is an async component, we have it saved in the entity cache.
138
- return EntityMapper . assureComponent ( this . props ) ;
31
+ return assureComponent ( this . props ) ;
139
32
}
140
33
141
34
lastRequest : symbol | undefined ;
@@ -157,7 +50,7 @@ export class EntityMapper extends React.Component<
157
50
}
158
51
159
52
( async ( ) => {
160
- await EntityMapper . assureComponent ( this . props ) ;
53
+ await assureComponent ( this . props ) ;
161
54
162
55
if ( lastRequest !== this . lastRequest ) {
163
56
return ;
@@ -181,7 +74,7 @@ export class EntityMapper extends React.Component<
181
74
}
182
75
183
76
isReady ( props = this . props ) {
184
- return typeof EntityMapper . getComponentFromMapper ( props ) !== 'undefined' ;
77
+ return typeof getComponentFromMapper ( props ) !== 'undefined' ;
185
78
}
186
79
187
80
render ( ) {
@@ -201,7 +94,7 @@ export class EntityMapper extends React.Component<
201
94
return null ;
202
95
}
203
96
204
- const EntityComponent = EntityMapper . getComponentFromMapper ( props ) ;
97
+ const EntityComponent = getComponentFromMapper ( props ) ;
205
98
206
99
if ( EntityComponent === null ) {
207
100
return null ;
@@ -218,7 +111,7 @@ export class EntityMapper extends React.Component<
218
111
219
112
return (
220
113
< EntityComponent
221
- bundle = { EntityMapper . getBundle ( data ) }
114
+ bundle = { getBundle ( data ) }
222
115
paragraph = { data }
223
116
entity = { data }
224
117
{ ...props . entityProps }
@@ -309,6 +202,109 @@ const EntityMapperWrapper = React.forwardRef<EntityMapper, EntityMapperProps>(
309
202
) ,
310
203
) ;
311
204
312
- export const assureComponent = EntityMapper . assureComponent ;
313
-
314
205
export default EntityMapperWrapper ;
206
+
207
+ /**
208
+ * This gets a component from some mapper data.
209
+ * This can also be a promise resolving to a component.
210
+ */
211
+ function getComponentOrPromiseFromMapper (
212
+ props : EntityMapperInnerProps ,
213
+ ) :
214
+ | { type : 'component' ; value : undefined | React . ReactType }
215
+ | { type : 'promise' ; value : undefined | ( ( ) => ImportReturn ) } {
216
+ // This gets the entity from the site, based on the uuid.
217
+ const entity = props . site . getData ( props . uuid ) ;
218
+
219
+ // This should give back a bundle string, that is used in the mapper.
220
+ const bundle = getBundle ( entity ) ;
221
+
222
+ // If it's not an async mapper, we can just use the 'mapper' prop.
223
+ if ( ! props . asyncMapper ) {
224
+ return {
225
+ type : 'component' ,
226
+ value :
227
+ typeof props . mapper === 'function'
228
+ ? props . mapper ( entity , bundle )
229
+ : props . mapper [ bundle ] ,
230
+ } ;
231
+ }
232
+
233
+ // We check which style of the mapper it is.
234
+ const entityComponentMapper =
235
+ props . asyncMapper === true ? props . mapper : props . asyncMapper ;
236
+ const entityComponentResult =
237
+ typeof entityComponentMapper === 'function'
238
+ ? entityComponentMapper ( entity , bundle )
239
+ : entityComponentMapper [ bundle ] ;
240
+
241
+ return {
242
+ type : 'promise' ,
243
+ value : entityComponentResult ,
244
+ } ;
245
+ }
246
+
247
+ /**
248
+ * This makes sure the data for this url is ready to be rendered.
249
+ */
250
+ export async function assureComponent ( props : EntityMapperInnerProps ) {
251
+ const component = getComponentOrPromiseFromMapper ( props ) ;
252
+
253
+ // If it's not a promise, return.
254
+ // Nothing to assure.
255
+ if ( component . type === 'component' ) {
256
+ return ;
257
+ }
258
+
259
+ // If no value was returned from the mapper, we can't assure the component.
260
+ if ( ! component . value ) {
261
+ return ;
262
+ }
263
+
264
+ // Check if we already cached this component.
265
+ if ( entityCache . has ( component . value ) ) {
266
+ return ;
267
+ }
268
+
269
+ // If not, we get this entity now.
270
+ const entityComponentResult = await component . value ( ) ;
271
+
272
+ // We save the entity in the cache.
273
+ if (
274
+ typeof entityComponentResult === 'object' &&
275
+ typeof entityComponentResult . default !== 'undefined'
276
+ ) {
277
+ entityCache . set ( component . value , entityComponentResult . default ) ;
278
+ } else {
279
+ entityCache . set ( component . value , entityComponentResult as React . ReactType ) ;
280
+ }
281
+ }
282
+
283
+ /**
284
+ * Returns the component if it is availible right now.
285
+ * Returns null if the component isn't available, and never will be.
286
+ * Returns undefined if the component isn't available, but maybe will be in the future.
287
+ */
288
+ export function getComponentFromMapper (
289
+ props : EntityMapperInnerProps ,
290
+ ) : React . ReactType | null | undefined {
291
+ const entityComponent = getComponentOrPromiseFromMapper ( props ) ;
292
+
293
+ if (
294
+ entityComponent . type === 'promise' &&
295
+ typeof entityComponent . value !== 'undefined'
296
+ ) {
297
+ return entityCache . get ( entityComponent . value ) ;
298
+ }
299
+ if ( entityComponent . type === 'component' ) {
300
+ return entityComponent . value || null ;
301
+ }
302
+ return null ;
303
+ }
304
+
305
+ function getBundle ( entity ) {
306
+ return getNested (
307
+ ( ) => `${ entity . __hn . entity . type } __${ entity . __hn . entity . bundle } ` ,
308
+ '_fallback' ,
309
+ ) ;
310
+ }
0 commit comments