refactor: replace .forEach()
with for-of
(#39691)
* refactor: replace `.forEach()` with `for-of` * refactor docs/fiddles/features/web-hid/renderer.js
This commit is contained in:
parent
7858921a1f
commit
0b0707145b
32 changed files with 144 additions and 132 deletions
|
@ -57,12 +57,17 @@ function sortTopologically<T> (originalOrder: T[], edgesById: Map<T, T[]>) {
|
|||
marked.add(mark);
|
||||
const edges = edgesById.get(mark);
|
||||
if (edges != null) {
|
||||
edges.forEach(visit);
|
||||
for (const edge of edges) {
|
||||
visit(edge);
|
||||
}
|
||||
}
|
||||
sorted.push(mark);
|
||||
};
|
||||
|
||||
originalOrder.forEach(visit);
|
||||
for (const edge of originalOrder) {
|
||||
visit(edge);
|
||||
}
|
||||
|
||||
return sorted;
|
||||
}
|
||||
|
||||
|
@ -98,24 +103,24 @@ function sortItemsInGroup<T> (group: {before?: T[], after?: T[], id?: T}[]) {
|
|||
const edges = new Map();
|
||||
const idToIndex = new Map(group.map((item, i) => [item.id, i]));
|
||||
|
||||
group.forEach((item, i) => {
|
||||
for (const [i, item] of group.entries()) {
|
||||
if (item.before) {
|
||||
item.before.forEach(toID => {
|
||||
for (const toID of item.before) {
|
||||
const to = idToIndex.get(toID);
|
||||
if (to != null) {
|
||||
pushOntoMultiMap(edges, to, i);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
if (item.after) {
|
||||
item.after.forEach(toID => {
|
||||
for (const toID of item.after) {
|
||||
const to = idToIndex.get(toID);
|
||||
if (to != null) {
|
||||
pushOntoMultiMap(edges, i, to);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const sortedNodes = sortTopologically(originalOrder, edges);
|
||||
return sortedNodes.map(i => group[i]);
|
||||
|
|
|
@ -153,9 +153,9 @@ Menu.prototype.insert = function (pos, item) {
|
|||
|
||||
Menu.prototype._callMenuWillShow = function () {
|
||||
if (this.delegate) this.delegate.menuWillShow(this);
|
||||
this.items.forEach(item => {
|
||||
for (const item of this.items) {
|
||||
if (item.submenu) item.submenu._callMenuWillShow();
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/* Static Methods */
|
||||
|
@ -196,13 +196,13 @@ Menu.buildFromTemplate = function (template) {
|
|||
const filtered = removeExtraSeparators(sorted);
|
||||
|
||||
const menu = new Menu();
|
||||
filtered.forEach(item => {
|
||||
for (const item of filtered) {
|
||||
if (item instanceof MenuItem) {
|
||||
menu.append(item);
|
||||
} else {
|
||||
menu.append(new MenuItem(item));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return menu;
|
||||
};
|
||||
|
@ -280,9 +280,9 @@ function insertItemByType (this: MenuType, item: MenuItem, pos: number) {
|
|||
enumerable: true,
|
||||
get: () => checked.get(item),
|
||||
set: () => {
|
||||
this.groupsMap[item.groupId].forEach(other => {
|
||||
for (const other of this.groupsMap[item.groupId]) {
|
||||
if (other !== item) checked.set(other, false);
|
||||
});
|
||||
}
|
||||
checked.set(item, true);
|
||||
}
|
||||
});
|
||||
|
|
|
@ -70,9 +70,9 @@ class IncomingMessage extends Readable {
|
|||
get rawHeaders () {
|
||||
const rawHeadersArr: string[] = [];
|
||||
const { rawHeaders } = this._responseHead;
|
||||
rawHeaders.forEach(header => {
|
||||
for (const header of rawHeaders) {
|
||||
rawHeadersArr.push(header.key, header.value);
|
||||
});
|
||||
}
|
||||
return rawHeadersArr;
|
||||
}
|
||||
|
||||
|
|
|
@ -98,7 +98,9 @@ export function fetchWithSession (input: RequestInfo, init: (RequestInit & {bypa
|
|||
r.on('response', (resp: IncomingMessage) => {
|
||||
if (locallyAborted) return;
|
||||
const headers = new Headers();
|
||||
for (const [k, v] of Object.entries(resp.headers)) { headers.set(k, Array.isArray(v) ? v.join(', ') : v); }
|
||||
for (const [k, v] of Object.entries(resp.headers)) {
|
||||
headers.set(k, Array.isArray(v) ? v.join(', ') : v);
|
||||
}
|
||||
const nullBodyStatus = [101, 204, 205, 304];
|
||||
const body = nullBodyStatus.includes(resp.statusCode) || req.method === 'HEAD' ? null : Readable.toWeb(resp as unknown as Readable) as ReadableStream;
|
||||
const rResp = new Response(body, {
|
||||
|
|
|
@ -323,13 +323,15 @@ class TouchBar extends EventEmitter implements Electron.TouchBar {
|
|||
this.items.set(item.id, item);
|
||||
item.on('change', this.changeListener);
|
||||
if (item.child instanceof TouchBar) {
|
||||
item.child.orderedItems.forEach(registerItem);
|
||||
for (const child of item.child.orderedItems) {
|
||||
registerItem(child);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let hasOtherItemsProxy = false;
|
||||
const idSet = new Set();
|
||||
items.forEach((item) => {
|
||||
for (const item of items) {
|
||||
if (!(item instanceof TouchBarItem)) {
|
||||
throw new TypeError('Each item must be an instance of TouchBarItem');
|
||||
}
|
||||
|
@ -347,7 +349,7 @@ class TouchBar extends EventEmitter implements Electron.TouchBar {
|
|||
} else {
|
||||
throw new Error('Cannot add a single instance of TouchBarItem multiple times in a TouchBar');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// register in separate loop after all items are validated
|
||||
for (const item of (items as TouchBarItem<any>[])) {
|
||||
|
|
|
@ -140,9 +140,9 @@ const createGuest = function (embedder: Electron.WebContents, embedderFrameId: n
|
|||
|
||||
const makeProps = (eventKey: string, args: any[]) => {
|
||||
const props: Record<string, any> = {};
|
||||
webViewEvents[eventKey].forEach((prop, index) => {
|
||||
for (const [index, prop] of webViewEvents[eventKey].entries()) {
|
||||
props[prop] = args[index];
|
||||
});
|
||||
}
|
||||
return props;
|
||||
};
|
||||
|
||||
|
|
|
@ -80,11 +80,11 @@ export function parseFeatures (features: string) {
|
|||
const parsed = parseCommaSeparatedKeyValue(features);
|
||||
|
||||
const webPreferences: { [K in AllowedWebPreference]?: any } = {};
|
||||
allowedWebPreferences.forEach((key) => {
|
||||
if (parsed[key] === undefined) return;
|
||||
for (const key of allowedWebPreferences) {
|
||||
if (parsed[key] === undefined) continue;
|
||||
webPreferences[key] = parsed[key];
|
||||
delete parsed[key];
|
||||
});
|
||||
}
|
||||
|
||||
if (parsed.left !== undefined) parsed.x = parsed.left;
|
||||
if (parsed.top !== undefined) parsed.y = parsed.top;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue