Skip to content

Commit d6e03bd

Browse files
authored
fix: cache resources are returned by macro tasks (#603)
1 parent 3d12ee8 commit d6e03bd

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

packages/loader/src/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import {
66
} from '@garfish/hooks';
77
import {
88
error,
9+
macroTask,
910
__LOADER_FLAG__,
1011
isJsType,
1112
isCssType,
1213
isHtmlType,
13-
parseContentType,
1414
} from '@garfish/utils';
1515
import { StyleManager } from './managers/style';
1616
import { ModuleManager } from './managers/module';
@@ -166,7 +166,7 @@ export class Loader {
166166
// 当最后一个子应用被卸载时,其它子应用也将一起被卸载
167167

168168
if (res && Object.keys(res).includes(scope)) {
169-
return res[scope]
169+
return res[scope];
170170
}
171171

172172
let appCacheContainer = cacheStore[scope];
@@ -177,7 +177,7 @@ export class Loader {
177177
}
178178

179179
if (appCacheContainer.has(url)) {
180-
return Promise.resolve(copyResult(appCacheContainer.get(url)));
180+
return macroTask(copyResult(appCacheContainer.get(url)));
181181
} else {
182182
// If other containers have cache
183183
for (const key in cacheStore) {
@@ -187,7 +187,7 @@ export class Loader {
187187
const result = container.get(url);
188188
cachedDataSet.add(result);
189189
appCacheContainer.set(url, result, result.fileType);
190-
return Promise.resolve(copyResult(result));
190+
return macroTask(copyResult(result));
191191
}
192192
}
193193
}
@@ -268,7 +268,9 @@ export class Loader {
268268
// loadingList[url][scope] = null
269269
});
270270

271-
loadingList[url] ? loadingList[url][scope] = loadRes : loadingList[url] = { [scope]: loadRes };
271+
loadingList[url]
272+
? (loadingList[url][scope] = loadRes)
273+
: (loadingList[url] = { [scope]: loadRes });
272274
return loadRes;
273275
}
274276
}

packages/utils/src/utils.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export const noop = () => { };
1+
export const noop = () => {};
22

33
export const objectToString = Object.prototype.toString;
44

@@ -113,11 +113,11 @@ export function error(error: string | Error) {
113113
export function validURL(str) {
114114
const pattern = new RegExp(
115115
'^(https?:\\/\\/)?' + // protocol
116-
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name
117-
'((\\d{1,3}\\.){3}\\d{1,3}))' + // OR ip (v4) address
118-
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + // port and path
119-
'(\\?[;&a-z\\d%_.~+=-]*)?' + // query string
120-
'(\\#[-a-z\\d_]*)?$',
116+
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name
117+
'((\\d{1,3}\\.){3}\\d{1,3}))' + // OR ip (v4) address
118+
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + // port and path
119+
'(\\?[;&a-z\\d%_.~+=-]*)?' + // query string
120+
'(\\#[-a-z\\d_]*)?$',
121121
'i',
122122
); // fragment locator
123123
return !!pattern.test(str);
@@ -164,7 +164,10 @@ export function evalWithEnv(
164164
}
165165
}
166166

167-
export function safeWrapper(callback: (...args: Array<any>) => any, disableWarn?: boolean) {
167+
export function safeWrapper(
168+
callback: (...args: Array<any>) => any,
169+
disableWarn?: boolean,
170+
) {
168171
try {
169172
callback();
170173
} catch (e) {
@@ -407,9 +410,10 @@ export function setDocCurrentScript(
407410
el.textContent = code;
408411
}
409412

410-
originScript && originScript.getAttributeNames().forEach((attribute) => {
411-
el.setAttribute(attribute, originScript.getAttribute(attribute) || '');
412-
});
413+
originScript &&
414+
originScript.getAttributeNames().forEach((attribute) => {
415+
el.setAttribute(attribute, originScript.getAttribute(attribute) || '');
416+
});
413417

414418
if (async) {
415419
el.setAttribute('async', 'true');
@@ -583,3 +587,9 @@ export function getSourceURL(url: string | URL | Request): string {
583587
if (url instanceof Request) return url.url;
584588
return url.startsWith('/') ? `${location.origin}${url}` : url;
585589
}
590+
591+
export function macroTask<T extends any>(value: T) {
592+
return new Promise<T>((resolve) => {
593+
setTimeout(() => resolve(value));
594+
});
595+
}

0 commit comments

Comments
 (0)