Skip to content

Commit b0f9318

Browse files
committed
把并行控制的代码移动至 concurrency-control.ts
1 parent 05e54f7 commit b0f9318

2 files changed

Lines changed: 58 additions & 54 deletions

File tree

src/app/service/service_worker/resource.ts

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -14,63 +14,10 @@ import { isBase64, parseUrlSRI, type TUrlSRIInfo } from "./utils";
1414
import { stackAsyncTask } from "@App/pkg/utils/async_queue";
1515
import { blobToUint8Array } from "@App/pkg/utils/datatype";
1616
import { readBlobContent } from "@App/pkg/utils/encoding";
17-
18-
class Semaphore {
19-
private active = 0;
20-
private readonly queue: Array<() => void> = [];
21-
22-
constructor(readonly limit: number) {
23-
if (limit < 1) throw new Error("limit must be >= 1");
24-
}
25-
26-
async acquire() {
27-
if (this.active >= this.limit) {
28-
await new Promise<void>((resolve) => this.queue.push(resolve));
29-
}
30-
this.active++;
31-
}
32-
33-
release() {
34-
if (this.active > 0) {
35-
this.active--;
36-
this.queue.shift()?.();
37-
} else {
38-
console.warn("Semaphore double release detected");
39-
}
40-
}
41-
}
17+
import { Semaphore, withTimeoutNotify } from "@App/pkg/utils/concurrency-control";
4218

4319
const fetchSemaphore = new Semaphore(5);
4420

45-
type TWithTimeoutNotifyResult<T> = {
46-
timeouted: boolean;
47-
result: T | undefined;
48-
done: boolean;
49-
err: undefined | Error;
50-
};
51-
const withTimeoutNotify = <T>(promise: Promise<T>, time: number, fn: (res: TWithTimeoutNotifyResult<T>) => any) => {
52-
const res: TWithTimeoutNotifyResult<T> = { timeouted: false, result: undefined, done: false, err: undefined };
53-
const cid = setTimeout(() => {
54-
res.timeouted = true;
55-
fn(res);
56-
}, time);
57-
return promise
58-
.then((result: T) => {
59-
clearTimeout(cid);
60-
res.result = result;
61-
res.done = true;
62-
fn(res);
63-
return res;
64-
})
65-
.catch((e) => {
66-
clearTimeout(cid);
67-
res.err = e;
68-
res.done = true;
69-
fn(res);
70-
return res;
71-
});
72-
};
73-
7421
export class ResourceService {
7522
logger: Logger;
7623
resourceDAO: ResourceDAO = new ResourceDAO();
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
export class Semaphore {
2+
private active = 0;
3+
private readonly queue: Array<() => void> = [];
4+
5+
constructor(readonly limit: number) {
6+
if (limit < 1) throw new Error("limit must be >= 1");
7+
}
8+
9+
async acquire() {
10+
if (this.active >= this.limit) {
11+
await new Promise<void>((resolve) => this.queue.push(resolve));
12+
}
13+
this.active++;
14+
}
15+
16+
release() {
17+
if (this.active > 0) {
18+
this.active--;
19+
this.queue.shift()?.();
20+
} else {
21+
console.warn("Semaphore double release detected");
22+
}
23+
}
24+
}
25+
26+
type TWithTimeoutNotifyResult<T> = {
27+
timeouted: boolean;
28+
result: T | undefined;
29+
done: boolean;
30+
err: undefined | Error;
31+
};
32+
export const withTimeoutNotify = <T>(
33+
promise: Promise<T>,
34+
time: number,
35+
fn: (res: TWithTimeoutNotifyResult<T>) => any
36+
) => {
37+
const res: TWithTimeoutNotifyResult<T> = { timeouted: false, result: undefined, done: false, err: undefined };
38+
const cid = setTimeout(() => {
39+
res.timeouted = true;
40+
fn(res);
41+
}, time);
42+
return promise
43+
.then((result: T) => {
44+
clearTimeout(cid);
45+
res.result = result;
46+
res.done = true;
47+
fn(res);
48+
return res;
49+
})
50+
.catch((e) => {
51+
clearTimeout(cid);
52+
res.err = e;
53+
res.done = true;
54+
fn(res);
55+
return res;
56+
});
57+
};

0 commit comments

Comments
 (0)