Рефакторинг

This commit is contained in:
Book Pauk
2022-10-23 20:51:56 +07:00
parent 723af3ea8b
commit 63dfdaf2c2
2 changed files with 45 additions and 48 deletions

View File

@@ -56,57 +56,54 @@ class DbSearcher {
} }
async calcIntersect(idsArr) { async calcIntersect(idsArr) {
return await this.heavyCalc.run({ return await this.heavyCalc.run(idsArr, (args) => {
args: idsArr, //из utils.intersectSet
fn: (args) => { const intersectSet = (arrSet) => {
//из utils.intersectSet if (!arrSet.length)
const intersectSet = (arrSet) => { return new Set();
if (!arrSet.length)
return new Set();
let min = 0; let min = 0;
let size = arrSet[0].size; let size = arrSet[0].size;
for (let i = 1; i < arrSet.length; i++) { for (let i = 1; i < arrSet.length; i++) {
if (arrSet[i].size < size) { if (arrSet[i].size < size) {
min = i; min = i;
size = arrSet[i].size; size = arrSet[i].size;
}
} }
const result = new Set();
for (const elem of arrSet[min]) {
let inAll = true;
for (let i = 0; i < arrSet.length; i++) {
if (i === min)
continue;
if (!arrSet[i].has(elem)) {
inAll = false;
break;
}
}
if (inAll)
result.add(elem);
}
return result;
};
//считаем пересечение, если надо
let result = [];
if (args.length > 1) {
const arrSet = args.map(ids => new Set(ids));
result = Array.from(intersectSet(arrSet));
} else if (args.length == 1) {
result = args[0];
} }
//сортировка const result = new Set();
result.sort((a, b) => a - b); for (const elem of arrSet[min]) {
let inAll = true;
for (let i = 0; i < arrSet.length; i++) {
if (i === min)
continue;
if (!arrSet[i].has(elem)) {
inAll = false;
break;
}
}
if (inAll)
result.add(elem);
}
return result; return result;
};
//считаем пересечение, если надо
let result = [];
if (args.length > 1) {
const arrSet = args.map(ids => new Set(ids));
result = Array.from(intersectSet(arrSet));
} else if (args.length == 1) {
result = args[0];
} }
//сортировка
result.sort((a, b) => a - b);
return result;
}); });
} }

View File

@@ -66,7 +66,7 @@ class CalcThread {
} }
//async //async
run(params) {//args, fn run(args, fn) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.requestId++; this.requestId++;
@@ -78,7 +78,7 @@ class CalcThread {
}); });
if (this.worker) { if (this.worker) {
this.worker.postMessage({requestId: this.requestId, args: params.args, fn: params.fn.toString()}); this.worker.postMessage({requestId: this.requestId, args, fn: fn.toString()});
} else { } else {
reject(new Error('Worker does not exist')); reject(new Error('Worker does not exist'));
} }
@@ -112,7 +112,7 @@ class HeavyCalc {
} }
} }
async run(params) { async run(args, fn) {
if (this.terminated || !this.workers.length) if (this.terminated || !this.workers.length)
throw new Error('All workers terminated'); throw new Error('All workers terminated');
@@ -125,7 +125,7 @@ class HeavyCalc {
try { try {
this.load[found]++; this.load[found]++;
return await this.workers[found].run(params); return await this.workers[found].run(args, fn);
} finally { } finally {
this.load[found]--; this.load[found]--;
} }