Skip to content

Commit 2d94146

Browse files
authored
fix(runtime-core): set shared loaded status true after calling loadShareSync (#3998)
1 parent 991f57c commit 2d94146

File tree

3 files changed

+50
-44
lines changed

3 files changed

+50
-44
lines changed

.changeset/gold-penguins-dance.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/runtime-core': patch
3+
---
4+
5+
fix(runtime-core): set shared loaded status true after calling loadShareSync

.changeset/short-ducks-dress.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/runtime-core': patch
3+
---
4+
5+
fix(runtime-core): set shared loaded status true after calling loadShareSync

packages/runtime-core/src/shared/index.ts

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -121,18 +121,18 @@ export class SharedHandler {
121121
// 2. Searches globally for a matching share, if found, it uses it directly
122122
// 3. If not found, it retrieves it from the current share and stores the obtained share globally.
123123

124-
const shareInfo = getTargetSharedOptions({
124+
const shareOptions = getTargetSharedOptions({
125125
pkgName,
126126
extraOptions,
127127
shareInfos: host.options.shared,
128128
});
129129

130-
if (shareInfo?.scope) {
130+
if (shareOptions?.scope) {
131131
await Promise.all(
132-
shareInfo.scope.map(async (shareScope) => {
132+
shareOptions.scope.map(async (shareScope) => {
133133
await Promise.all(
134134
this.initializeSharing(shareScope, {
135-
strategy: shareInfo.strategy,
135+
strategy: shareOptions.strategy,
136136
}),
137137
);
138138
return;
@@ -141,24 +141,24 @@ export class SharedHandler {
141141
}
142142
const loadShareRes = await this.hooks.lifecycle.beforeLoadShare.emit({
143143
pkgName,
144-
shareInfo,
144+
shareInfo: shareOptions,
145145
shared: host.options.shared,
146146
origin: host,
147147
});
148148

149-
const { shareInfo: shareInfoRes } = loadShareRes;
149+
const { shareInfo: shareOptionsRes } = loadShareRes;
150150

151151
// Assert that shareInfoRes exists, if not, throw an error
152152
assert(
153-
shareInfoRes,
153+
shareOptionsRes,
154154
`Cannot find ${pkgName} Share in the ${host.options.name}. Please ensure that the ${pkgName} Share parameters have been injected`,
155155
);
156156

157157
// Retrieve from cache
158158
const registeredShared = getRegisteredShare(
159159
this.shareScopeMap,
160160
pkgName,
161-
shareInfoRes,
161+
shareOptionsRes,
162162
this.hooks.lifecycle.resolveShare,
163163
);
164164

@@ -187,20 +187,9 @@ export class SharedHandler {
187187
} else if (registeredShared) {
188188
const asyncLoadProcess = async () => {
189189
const factory = await registeredShared.get();
190-
shareInfoRes.lib = factory;
191-
shareInfoRes.loaded = true;
192-
addUseIn(shareInfoRes);
193-
const gShared = getRegisteredShare(
194-
this.shareScopeMap,
195-
pkgName,
196-
shareInfoRes,
197-
this.hooks.lifecycle.resolveShare,
198-
);
199-
if (gShared) {
200-
gShared.lib = factory;
201-
gShared.loaded = true;
202-
addUseIn(gShared);
203-
}
190+
addUseIn(registeredShared);
191+
registeredShared.loaded = true;
192+
registeredShared.lib = factory;
204193
return factory as () => T;
205194
};
206195
const loading = asyncLoadProcess();
@@ -218,27 +207,28 @@ export class SharedHandler {
218207
return false;
219208
}
220209
const asyncLoadProcess = async () => {
221-
const factory = await shareInfoRes.get();
222-
shareInfoRes.lib = factory;
223-
shareInfoRes.loaded = true;
224-
addUseIn(shareInfoRes);
210+
const factory = await shareOptionsRes.get();
211+
shareOptionsRes.lib = factory;
212+
shareOptionsRes.loaded = true;
213+
addUseIn(shareOptionsRes);
225214
const gShared = getRegisteredShare(
226215
this.shareScopeMap,
227216
pkgName,
228-
shareInfoRes,
217+
shareOptionsRes,
229218
this.hooks.lifecycle.resolveShare,
230219
);
231220
if (gShared) {
232221
gShared.lib = factory;
233222
gShared.loaded = true;
223+
gShared.from = shareOptionsRes.from;
234224
}
235225
return factory as () => T;
236226
};
237227
const loading = asyncLoadProcess();
238228
this.setShared({
239229
pkgName,
240230
loaded: false,
241-
shared: shareInfoRes,
231+
shared: shareOptionsRes,
242232
from: host.options.name,
243233
lib: null,
244234
loading,
@@ -367,21 +357,21 @@ export class SharedHandler {
367357
},
368358
): () => T | never {
369359
const { host } = this;
370-
const shareInfo = getTargetSharedOptions({
360+
const shareOptions = getTargetSharedOptions({
371361
pkgName,
372362
extraOptions,
373363
shareInfos: host.options.shared,
374364
});
375365

376-
if (shareInfo?.scope) {
377-
shareInfo.scope.forEach((shareScope) => {
378-
this.initializeSharing(shareScope, { strategy: shareInfo.strategy });
366+
if (shareOptions?.scope) {
367+
shareOptions.scope.forEach((shareScope) => {
368+
this.initializeSharing(shareScope, { strategy: shareOptions.strategy });
379369
});
380370
}
381371
const registeredShared = getRegisteredShare(
382372
this.shareScopeMap,
383373
pkgName,
384-
shareInfo,
374+
shareOptions,
385375
this.hooks.lifecycle.resolveShare,
386376
);
387377

@@ -398,7 +388,7 @@ export class SharedHandler {
398388
if (!registeredShared.loaded) {
399389
registeredShared.loaded = true;
400390
if (registeredShared.from === host.options.name) {
401-
shareInfo.loaded = true;
391+
shareOptions.loaded = true;
402392
}
403393
}
404394
return registeredShared.lib as () => T;
@@ -419,15 +409,15 @@ export class SharedHandler {
419409
}
420410
}
421411

422-
if (shareInfo.lib) {
423-
if (!shareInfo.loaded) {
424-
shareInfo.loaded = true;
412+
if (shareOptions.lib) {
413+
if (!shareOptions.loaded) {
414+
shareOptions.loaded = true;
425415
}
426-
return shareInfo.lib as () => T;
416+
return shareOptions.lib as () => T;
427417
}
428418

429-
if (shareInfo.get) {
430-
const module = shareInfo.get();
419+
if (shareOptions.get) {
420+
const module = shareOptions.get();
431421

432422
if (module instanceof Promise) {
433423
const errorCode =
@@ -440,16 +430,16 @@ export class SharedHandler {
440430
);
441431
}
442432

443-
shareInfo.lib = module;
433+
shareOptions.lib = module;
444434

445435
this.setShared({
446436
pkgName,
447437
loaded: true,
448438
from: host.options.name,
449-
lib: shareInfo.lib,
450-
shared: shareInfo,
439+
lib: shareOptions.lib,
440+
shared: shareOptions,
451441
});
452-
return shareInfo.lib as () => T;
442+
return shareOptions.lib as () => T;
453443
}
454444

455445
throw new Error(
@@ -522,6 +512,12 @@ export class SharedHandler {
522512
if (loading && !registeredShared.loading) {
523513
registeredShared.loading = loading;
524514
}
515+
if (loaded && !registeredShared.loaded) {
516+
registeredShared.loaded = loaded;
517+
}
518+
if (from && registeredShared.from !== from) {
519+
registeredShared.from = from;
520+
}
525521
});
526522
}
527523

0 commit comments

Comments
 (0)