const makeTree = (root) => {
const tree = {};
const step = (root, branch) => {
root.querySelectorAll(':scope > [class^="block"]').forEach((element, index) => {
const className = [...element.classList.values()].find(
(className) => className.startsWith('block')
) ?? element.className;
const key = `${className}_${index + 1}`;
branch[key] = {};
const control = element.querySelector(':scope > input');
if (control) {
branch[key].value = control.value;
}
step(element, branch[key]);
});
};
step(root, tree);
return tree;
};
const tree = makeTree(document.body);
console.log(tree);
const makeTree = (root) => {
const tree = {};
const step = (root, branch) => {
root.querySelectorAll(':scope > [class^="block"]').forEach((element, index) => {
const className = [...element.classList.values()].find(
(className) => className.startsWith('block')
) ?? element.className;
const key = `${className}_${index + 1}`;
const controls = element.querySelectorAll(':scope > input');
branch[key] = {
values: [...controls].reduce((acc, control) => {
if (control.id) {
acc[control.id] = control.value;
}
return acc;
}, {})
};
step(element, branch[key]);
});
};
step(root, tree);
return tree;
};
const tree = makeTree(document.body);
console.log(tree);
const findWhere = (shelf, book) => {
for (let i = 0; i < shelf.length; i++) {
let flag = true;
for (let [key, value] of Object.entries(book)) {
if (shelf[i][key] !== value) {
flag = false;
break;
}
}
if (flag) {
return shelf[i];
}
}
return null;
};
class Semaphore {
constructor(max = 1) {
if (max < 1) { max = 1; }
this.max = max;
this.count = 0;
this.queue = [];
}
acquire() {
let promise;
if (this.count < this.max) {
promise = Promise.resolve();
} else {
promise = new Promise(resolve => {
this.queue.push(resolve);
});
}
this.count++;
return promise;
}
release() {
if (this.queue.length > 0) {
const resolve = this.queue.shift();
resolve();
}
this.count--;
}
}
const semaphore = new Semaphore(10);
for (const url of urls) {
await semaphore.acquire();
void downloadUrlSynchronized(url, semaphore);
}
async function downloadUrlSynchronized(url, semaphore) {
const resp = await fetch(url);
const blob = await resp.blob();
semaphore.release();
// const name = new URL(url).pathname.slice(1);
// downloadBlob(blob, name, url);
}
fetch
) будет не больше 10 в один момент, что собственно и требовалось.3
:function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
for (const span of h1.children) {
await sleep(500);
span.removeAttribute("hidden");
}
const h1 = document.querySelector("h1");
const chars = "HELLO".split("");
for (const char of chars) {
await sleep(500);
h1.textContent += char;
}
function createTree({
data,
idKey = 'id',
parentKey = 'parentId',
childrenKey = 'children',
}) {
const tree = Object.fromEntries(data.map(n => [ n[idKey], { ...n, [childrenKey]: [] } ]));
return Object.values(tree).filter(n => !tree[n[parentKey]]?.[childrenKey].push(n));
}
const result = createTree({
data: components,
childrenKey: 'components',
});
const map = new Map();
for (const entry of components) {
const parent = map.get(entry.parentId);
if (parent) {
parent.children || (parent.children = []);
parent.children.push(entry);
}
map.set(entry.id, entry);
}
const rootItemId = 1;
console.log(map.get(rootItemId));
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
while (true) {
await sleep(10000);
openBox();
await sleep(3000);
const data = JSON.parse(response);
for (const entry of data) {
entry.btn.click(); // рили?
await sleep(3000);
entry.btnClose.click();
}
await sleep(3000);
btn.click();
}
// element-util.js
export function createElement(options) {
// код функции
}
export function showElement(options) {
// код функции
}
export function hideElement(options) {
// код функции
}
import {createElement} from "./element-util.js";
function Component() {
const heading = createElement({
id: "heading",
content: "Hello World",
});
const message = createElement({
id: "message",
content: "New Article",
});
return {heading, message};
}
[1, 2, 3, 4].reduce((sum, val) => { return sum + val }, 0)
// → 10
['foo', 'bar', 'quux'].reduce((obj, key) => { obj[key] = true; return obj; }, {});
// → { foo: true, bar: true, quux: true }