|
1 | 1 | import { FederationRuntimePlugin } from '@module-federation/runtime/types'; |
2 | 2 | import { fetchWithRetry } from './fetch-retry'; |
3 | | -import { defaultRetries, defaultRetryDelay } from './constant'; |
4 | 3 | import type { RetryPluginParams } from './types'; |
| 4 | +import { scriptCommonRetry } from './util'; |
5 | 5 |
|
6 | 6 | const RetryPlugin: (params: RetryPluginParams) => FederationRuntimePlugin = ({ |
7 | 7 | fetch: fetchOption, |
@@ -36,39 +36,39 @@ const RetryPlugin: (params: RetryPluginParams) => FederationRuntimePlugin = ({ |
36 | 36 | } |
37 | 37 | return fetch(url, options); |
38 | 38 | }, |
39 | | - async getModuleFactory({ remoteEntryExports, expose, moduleInfo }) { |
40 | | - let moduleFactory; |
41 | | - const { retryTimes = defaultRetries, retryDelay = defaultRetryDelay } = |
42 | | - scriptOption || {}; |
43 | | - |
44 | | - if ( |
45 | | - (scriptOption?.moduleName && |
46 | | - scriptOption?.moduleName.some( |
47 | | - (m) => moduleInfo.name === m || (moduleInfo as any)?.alias === m, |
48 | | - )) || |
49 | | - scriptOption?.moduleName === undefined |
50 | | - ) { |
51 | | - let attempts = 0; |
52 | 39 |
|
53 | | - while (attempts - 1 < retryTimes) { |
54 | | - try { |
55 | | - moduleFactory = await remoteEntryExports.get(expose); |
56 | | - break; |
57 | | - } catch (error) { |
58 | | - attempts++; |
59 | | - if (attempts - 1 >= retryTimes) { |
60 | | - scriptOption?.cb && |
61 | | - (await new Promise( |
62 | | - (resolve) => |
63 | | - scriptOption?.cb && scriptOption?.cb(resolve, error), |
64 | | - )); |
65 | | - throw error; |
66 | | - } |
67 | | - await new Promise((resolve) => setTimeout(resolve, retryDelay)); |
68 | | - } |
69 | | - } |
70 | | - } |
71 | | - return moduleFactory; |
| 40 | + async loadEntryError({ |
| 41 | + getRemoteEntry, |
| 42 | + origin, |
| 43 | + remoteInfo, |
| 44 | + remoteEntryExports, |
| 45 | + globalLoading, |
| 46 | + uniqueKey, |
| 47 | + }) { |
| 48 | + if (!scriptOption) return; |
| 49 | + const retryFn = getRemoteEntry; |
| 50 | + const beforeExecuteRetry = () => delete globalLoading[uniqueKey]; |
| 51 | + const getRemoteEntryRetry = scriptCommonRetry({ |
| 52 | + scriptOption, |
| 53 | + moduleInfo: remoteInfo, |
| 54 | + retryFn, |
| 55 | + beforeExecuteRetry, |
| 56 | + }); |
| 57 | + return getRemoteEntryRetry({ |
| 58 | + origin, |
| 59 | + remoteInfo, |
| 60 | + remoteEntryExports, |
| 61 | + }); |
| 62 | + }, |
| 63 | + async getModuleFactory({ remoteEntryExports, expose, moduleInfo }) { |
| 64 | + if (!scriptOption) return; |
| 65 | + const retryFn = remoteEntryExports.get; |
| 66 | + const getRemoteEntryRetry = scriptCommonRetry({ |
| 67 | + scriptOption, |
| 68 | + moduleInfo, |
| 69 | + retryFn, |
| 70 | + }); |
| 71 | + return getRemoteEntryRetry(expose); |
72 | 72 | }, |
73 | 73 | }); |
74 | 74 |
|
|
0 commit comments