7
7
RemoteEntryExports ,
8
8
UserOptions ,
9
9
ShareStrategy ,
10
+ InitScope ,
11
+ InitTokens ,
12
+ CallFrom ,
10
13
} from '../type' ;
11
14
import { FederationHost } from '../core' ;
12
15
import {
@@ -55,10 +58,11 @@ export class SharedHandler {
55
58
hostShareScopeMap ?: ShareScopeMap ;
56
59
} > ( 'initContainerShareScopeMap' ) ,
57
60
} ) ;
58
-
61
+ initTokens : InitTokens ;
59
62
constructor ( host : FederationHost ) {
60
63
this . host = host ;
61
64
this . shareScopeMap = { } ;
65
+ this . initTokens = { } ;
62
66
this . _setGlobalShareScopeMap ( host . options ) ;
63
67
}
64
68
@@ -121,7 +125,9 @@ export class SharedHandler {
121
125
await Promise . all (
122
126
shareInfo . scope . map ( async ( shareScope ) => {
123
127
await Promise . all (
124
- this . initializeSharing ( shareScope , shareInfo . strategy ) ,
128
+ this . initializeSharing ( shareScope , {
129
+ strategy : shareInfo . strategy ,
130
+ } ) ,
125
131
) ;
126
132
return ;
127
133
} ) ,
@@ -242,9 +248,26 @@ export class SharedHandler {
242
248
// eslint-disable-next-line @typescript-eslint/member-ordering
243
249
initializeSharing (
244
250
shareScopeName = DEFAULT_SCOPE ,
245
- strategy ?: ShareStrategy ,
251
+ extraOptions ?: {
252
+ initScope ?: InitScope ;
253
+ from ?: CallFrom ;
254
+ strategy ?: ShareStrategy ;
255
+ } ,
246
256
) : Array < Promise < void > > {
247
257
const { host } = this ;
258
+ const from = extraOptions ?. from ;
259
+ const strategy = extraOptions ?. strategy ;
260
+ let initScope = extraOptions ?. initScope ;
261
+ const promises : Promise < any > [ ] = [ ] ;
262
+ if ( from !== 'build' ) {
263
+ const { initTokens } = this ;
264
+ if ( ! initScope ) initScope = [ ] ;
265
+ let initToken = initTokens [ shareScopeName ] ;
266
+ if ( ! initToken )
267
+ initToken = initTokens [ shareScopeName ] = { from : this . host . name } ;
268
+ if ( initScope . indexOf ( initToken ) >= 0 ) return promises ;
269
+ initScope . push ( initToken ) ;
270
+ }
248
271
249
272
const shareScope = this . shareScopeMap ;
250
273
const hostName = host . options . name ;
@@ -274,9 +297,8 @@ export class SharedHandler {
274
297
versions [ version ] = shared ;
275
298
}
276
299
} ;
277
- const promises : Promise < any > [ ] = [ ] ;
278
300
const initFn = ( mod : RemoteEntryExports ) =>
279
- mod && mod . init && mod . init ( shareScope [ shareScopeName ] ) ;
301
+ mod && mod . init && mod . init ( shareScope [ shareScopeName ] , initScope ) ;
280
302
281
303
const initRemoteModule = async ( key : string ) : Promise < void > => {
282
304
const { module } = await host . remoteHandler . getRemoteModuleAndOptions ( {
@@ -285,7 +307,7 @@ export class SharedHandler {
285
307
if ( module . getEntry ) {
286
308
const entry = await module . getEntry ( ) ;
287
309
if ( ! module . inited ) {
288
- initFn ( entry ) ;
310
+ await initFn ( entry ) ;
289
311
module . inited = true ;
290
312
}
291
313
}
@@ -333,7 +355,7 @@ export class SharedHandler {
333
355
334
356
if ( shareInfo ?. scope ) {
335
357
shareInfo . scope . forEach ( ( shareScope ) => {
336
- this . initializeSharing ( shareScope , shareInfo . strategy ) ;
358
+ this . initializeSharing ( shareScope , { strategy : shareInfo . strategy } ) ;
337
359
} ) ;
338
360
}
339
361
const registeredShared = getRegisteredShare (
0 commit comments