@yorozu/utils 1.0.25 → 1.0.52
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/async/bitmap-work-queue.d.ts +6 -4
- package/async/priority-work-queue.d.ts +3 -0
- package/index.js +74 -33
- package/package.json +1 -1
|
@@ -12,6 +12,11 @@ export type BitmapWorkQueueStats = {
|
|
|
12
12
|
active: number;
|
|
13
13
|
queued: number;
|
|
14
14
|
};
|
|
15
|
+
export type BitmapWorkQueueOptions = {
|
|
16
|
+
concurrency?: number;
|
|
17
|
+
idle?: boolean;
|
|
18
|
+
onError?: (error: Error, id: string) => void;
|
|
19
|
+
};
|
|
15
20
|
export type BitmapWorkQueue = {
|
|
16
21
|
enqueue(job: BitmapWorkJob): boolean;
|
|
17
22
|
cancel(id: string): boolean;
|
|
@@ -19,7 +24,4 @@ export type BitmapWorkQueue = {
|
|
|
19
24
|
resume(): void;
|
|
20
25
|
readonly stats: BitmapWorkQueueStats;
|
|
21
26
|
};
|
|
22
|
-
export declare function createBitmapWorkQueue(opts?:
|
|
23
|
-
concurrency?: number;
|
|
24
|
-
idle?: boolean;
|
|
25
|
-
}): BitmapWorkQueue;
|
|
27
|
+
export declare function createBitmapWorkQueue(opts?: BitmapWorkQueueOptions): BitmapWorkQueue;
|
|
@@ -19,7 +19,10 @@ export type PriorityWorkQueue = {
|
|
|
19
19
|
enqueue(job: PriorityWorkJob): boolean;
|
|
20
20
|
cancel(id: string): boolean;
|
|
21
21
|
cancelAll(): void;
|
|
22
|
+
pause(): void;
|
|
23
|
+
resume(): void;
|
|
22
24
|
isBusy(id: string): boolean;
|
|
25
|
+
isRunning(id: string): boolean;
|
|
23
26
|
get stats(): PriorityWorkQueueStats;
|
|
24
27
|
};
|
|
25
28
|
export declare function createPriorityWorkQueue(opts?: PriorityWorkQueueOptions): PriorityWorkQueue;
|
package/index.js
CHANGED
|
@@ -1737,6 +1737,28 @@ var ConditionVariable = class {
|
|
|
1737
1737
|
}
|
|
1738
1738
|
};
|
|
1739
1739
|
//#endregion
|
|
1740
|
+
//#region src/types/error.ts
|
|
1741
|
+
function unknownToError(err) {
|
|
1742
|
+
if (err instanceof Error) return err;
|
|
1743
|
+
if (typeof err === "string") return new Error(err);
|
|
1744
|
+
let error = "";
|
|
1745
|
+
try {
|
|
1746
|
+
error = JSON.stringify(err);
|
|
1747
|
+
} catch (_) {}
|
|
1748
|
+
return new Error(error, { cause: err });
|
|
1749
|
+
}
|
|
1750
|
+
var NotImplementedError = class extends Error {
|
|
1751
|
+
constructor(message, options = {}) {
|
|
1752
|
+
super(`Not implemented${message != null ? `: ${message}` : ""}`, options);
|
|
1753
|
+
}
|
|
1754
|
+
};
|
|
1755
|
+
function throwNotImplemented(message, options) {
|
|
1756
|
+
throw new NotImplementedError(message, options);
|
|
1757
|
+
}
|
|
1758
|
+
function throwUnreachable() {
|
|
1759
|
+
throw new Error("Unreachable");
|
|
1760
|
+
}
|
|
1761
|
+
//#endregion
|
|
1740
1762
|
//#region src/async/idle.ts
|
|
1741
1763
|
function requestIdle(fn, opts) {
|
|
1742
1764
|
let g = globalThis;
|
|
@@ -1766,6 +1788,9 @@ function requestIdle(fn, opts) {
|
|
|
1766
1788
|
//#endregion
|
|
1767
1789
|
//#region src/async/bitmap-work-queue.ts
|
|
1768
1790
|
var PRI_ORDER$1 = ["visible", "preload"];
|
|
1791
|
+
function isAbortError$1(err) {
|
|
1792
|
+
return err instanceof Error && err.name === "AbortError";
|
|
1793
|
+
}
|
|
1769
1794
|
function resolveConcurrency$1(value) {
|
|
1770
1795
|
if (value === void 0) return 1;
|
|
1771
1796
|
let n = Math.floor(value);
|
|
@@ -1833,27 +1858,49 @@ function createBitmapWorkQueue(opts) {
|
|
|
1833
1858
|
}
|
|
1834
1859
|
function startJob(job) {
|
|
1835
1860
|
let controller = new AbortController();
|
|
1836
|
-
|
|
1861
|
+
let token = {
|
|
1837
1862
|
id: job.id,
|
|
1838
1863
|
controller
|
|
1839
|
-
}
|
|
1864
|
+
};
|
|
1865
|
+
active.set(job.id, token);
|
|
1840
1866
|
(async () => {
|
|
1867
|
+
let bitmap;
|
|
1868
|
+
let reported;
|
|
1841
1869
|
try {
|
|
1842
1870
|
let signal = controller.signal;
|
|
1843
1871
|
if (signal.aborted) return;
|
|
1844
|
-
|
|
1845
|
-
if (signal.aborted) {
|
|
1872
|
+
bitmap = await createImageBitmap(job.source, { signal });
|
|
1873
|
+
if (active.get(job.id) !== token || signal.aborted) {
|
|
1846
1874
|
bitmap.close();
|
|
1875
|
+
bitmap = void 0;
|
|
1847
1876
|
return;
|
|
1848
1877
|
}
|
|
1849
|
-
if (job.run)
|
|
1850
|
-
|
|
1851
|
-
bitmap
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1878
|
+
if (job.run) {
|
|
1879
|
+
let owned = bitmap;
|
|
1880
|
+
bitmap = void 0;
|
|
1881
|
+
await job.run({
|
|
1882
|
+
signal,
|
|
1883
|
+
bitmap: owned
|
|
1884
|
+
});
|
|
1885
|
+
} else {
|
|
1886
|
+
bitmap.close();
|
|
1887
|
+
bitmap = void 0;
|
|
1888
|
+
}
|
|
1889
|
+
} catch (err) {
|
|
1890
|
+
if (bitmap) {
|
|
1891
|
+
try {
|
|
1892
|
+
bitmap.close();
|
|
1893
|
+
} catch {}
|
|
1894
|
+
bitmap = void 0;
|
|
1895
|
+
}
|
|
1896
|
+
if (!isAbortError$1(err) && !controller.signal.aborted) reported = unknownToError(err);
|
|
1897
|
+
} finally {
|
|
1898
|
+
if (active.get(job.id) === token) active.delete(job.id);
|
|
1899
|
+
else reported = void 0;
|
|
1856
1900
|
}
|
|
1901
|
+
if (reported) try {
|
|
1902
|
+
opts?.onError?.(reported, job.id);
|
|
1903
|
+
} catch {}
|
|
1857
1904
|
pump();
|
|
1858
1905
|
})();
|
|
1859
1906
|
}
|
|
@@ -1879,12 +1926,15 @@ function createBitmapWorkQueue(opts) {
|
|
|
1879
1926
|
let running = active.get(id);
|
|
1880
1927
|
if (!running) return false;
|
|
1881
1928
|
running.controller.abort();
|
|
1929
|
+
active.delete(id);
|
|
1930
|
+
pump();
|
|
1882
1931
|
return true;
|
|
1883
1932
|
},
|
|
1884
1933
|
pause() {
|
|
1885
1934
|
paused = true;
|
|
1886
1935
|
},
|
|
1887
1936
|
resume() {
|
|
1937
|
+
if (!paused) return;
|
|
1888
1938
|
paused = false;
|
|
1889
1939
|
pump();
|
|
1890
1940
|
},
|
|
@@ -1963,28 +2013,6 @@ async function parallelMap(iterable, executor, options = {}) {
|
|
|
1963
2013
|
return result;
|
|
1964
2014
|
}
|
|
1965
2015
|
//#endregion
|
|
1966
|
-
//#region src/types/error.ts
|
|
1967
|
-
function unknownToError(err) {
|
|
1968
|
-
if (err instanceof Error) return err;
|
|
1969
|
-
if (typeof err === "string") return new Error(err);
|
|
1970
|
-
let error = "";
|
|
1971
|
-
try {
|
|
1972
|
-
error = JSON.stringify(err);
|
|
1973
|
-
} catch (_) {}
|
|
1974
|
-
return new Error(error, { cause: err });
|
|
1975
|
-
}
|
|
1976
|
-
var NotImplementedError = class extends Error {
|
|
1977
|
-
constructor(message, options = {}) {
|
|
1978
|
-
super(`Not implemented${message != null ? `: ${message}` : ""}`, options);
|
|
1979
|
-
}
|
|
1980
|
-
};
|
|
1981
|
-
function throwNotImplemented(message, options) {
|
|
1982
|
-
throw new NotImplementedError(message, options);
|
|
1983
|
-
}
|
|
1984
|
-
function throwUnreachable() {
|
|
1985
|
-
throw new Error("Unreachable");
|
|
1986
|
-
}
|
|
1987
|
-
//#endregion
|
|
1988
2016
|
//#region src/async/priority-work-queue.ts
|
|
1989
2017
|
var PRI_ORDER = [
|
|
1990
2018
|
"visible",
|
|
@@ -2013,6 +2041,7 @@ function createPriorityWorkQueue(opts) {
|
|
|
2013
2041
|
let queuedPri = /* @__PURE__ */ new Map();
|
|
2014
2042
|
let active = /* @__PURE__ */ new Map();
|
|
2015
2043
|
let maxActive = 0;
|
|
2044
|
+
let paused = false;
|
|
2016
2045
|
function queuedCount() {
|
|
2017
2046
|
return queuedPri.size;
|
|
2018
2047
|
}
|
|
@@ -2037,6 +2066,7 @@ function createPriorityWorkQueue(opts) {
|
|
|
2037
2066
|
}
|
|
2038
2067
|
}
|
|
2039
2068
|
function pump() {
|
|
2069
|
+
if (paused) return;
|
|
2040
2070
|
while (active.size < concurrency) {
|
|
2041
2071
|
let job = pickNext();
|
|
2042
2072
|
if (!job) return;
|
|
@@ -2090,9 +2120,20 @@ function createPriorityWorkQueue(opts) {
|
|
|
2090
2120
|
queuedPri.clear();
|
|
2091
2121
|
for (let running of active.values()) running.controller.abort();
|
|
2092
2122
|
},
|
|
2123
|
+
pause() {
|
|
2124
|
+
paused = true;
|
|
2125
|
+
},
|
|
2126
|
+
resume() {
|
|
2127
|
+
if (!paused) return;
|
|
2128
|
+
paused = false;
|
|
2129
|
+
pump();
|
|
2130
|
+
},
|
|
2093
2131
|
isBusy(id) {
|
|
2094
2132
|
return active.has(id) || queuedPri.has(id);
|
|
2095
2133
|
},
|
|
2134
|
+
isRunning(id) {
|
|
2135
|
+
return active.has(id);
|
|
2136
|
+
},
|
|
2096
2137
|
get stats() {
|
|
2097
2138
|
return {
|
|
2098
2139
|
active: active.size,
|