Skip to content

Commit 71e97d5

Browse files
authored
⚡ 不要 .reduce 写法 (#619)
1 parent 870dd9b commit 71e97d5

3 files changed

Lines changed: 25 additions & 30 deletions

File tree

src/app/service/service_worker/client.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -143,18 +143,15 @@ export class ScriptClient extends Client {
143143
})
144144
// this.do 只会resolve 不会reject
145145
)) as PromiseFulfilledResult<{ success: boolean; msg: string }>[];
146-
const stat = results.reduce(
147-
(obj, result, index) => {
148-
if (result.value.success) {
149-
obj.success++;
150-
} else {
151-
obj.fail++;
152-
obj.msg.push(`#${index + 1}: ${result.value.msg}`);
153-
}
154-
return obj;
155-
},
156-
{ success: 0, fail: 0, msg: [] as string[] }
157-
);
146+
const stat = { success: 0, fail: 0, msg: [] as string[] };
147+
results.forEach(({ value }, index) => {
148+
if (value.success) {
149+
stat.success++;
150+
} else {
151+
stat.fail++;
152+
stat.msg.push(`#${index + 1}: ${value.msg}`);
153+
}
154+
});
158155
return stat;
159156
}
160157

src/app/service/service_worker/runtime.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -409,29 +409,30 @@ export class RuntimeService {
409409
// 匹配当前页面的脚本
410410
const matchScriptUuid = await this.getPageScriptUuidByUrl(chromeSender.url!);
411411

412-
const enableScript = matchScriptUuid.reduce((arr, uuid) => {
412+
const enableScript = [] as ScriptLoadInfo[];
413+
414+
for (const uuid of matchScriptUuid) {
413415
const scriptRes = Object.assign({}, this.scriptMatchCache?.get(uuid));
414416
// 判断脚本是否开启
415417
if (scriptRes.status === SCRIPT_STATUS_DISABLE) {
416-
return arr;
418+
continue;
417419
}
418420
// 判断注入页面类型
419421
if (scriptRes.metadata["run-in"]) {
420422
// 判断插件运行环境
421423
const contextType = chrome.extension.inIncognitoContext ? "incognito-tabs" : "normal-tabs";
422424
if (!scriptRes.metadata["run-in"].includes(contextType)) {
423-
return arr;
425+
continue;
424426
}
425427
}
426428
// 如果是iframe,判断是否允许在iframe里运行
427429
if (chromeSender.frameId) {
428430
if (scriptRes.metadata.noframes) {
429-
return arr;
431+
continue;
430432
}
431433
}
432-
arr.push(scriptRes as ScriptLoadInfo);
433-
return arr;
434-
}, [] as ScriptLoadInfo[]);
434+
enableScript.push(scriptRes as ScriptLoadInfo);
435+
}
435436

436437
await Promise.all([
437438
// 加载value

src/locales/locales.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,14 @@ export async function matchLanguage() {
9191
}
9292
}
9393
// 根据前缀去匹配
94-
const prefixMap = i18n.languages.reduce(
95-
(acc, lng) => {
96-
const prefix = lng.split("-")[0];
97-
if (!acc[prefix]) {
98-
acc[prefix] = [];
99-
}
100-
acc[prefix].push(lng);
101-
return acc;
102-
},
103-
{} as Record<string, string[]>
104-
);
94+
const prefixMap = {} as Partial<Record<string, string[]>>;
95+
for (const lng of i18n.languages) {
96+
const prefix = lng.split("-")[0];
97+
if (!prefixMap[prefix]) {
98+
prefixMap[prefix] = [];
99+
}
100+
prefixMap[prefix].push(lng);
101+
}
105102
for (let i = 0; i < acceptLanguages.length; i += 1) {
106103
const lng = acceptLanguages[i];
107104
const prefix = lng.split("-")[0];

0 commit comments

Comments
 (0)