@effectionx/worker 0.1.1
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/README.md +149 -0
- package/esm/_dnt.shims.d.ts +2 -0
- package/esm/_dnt.shims.d.ts.map +1 -0
- package/esm/_dnt.shims.js +57 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/almost_equals.d.ts +29 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/almost_equals.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/almost_equals.js +46 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/array_includes.d.ts +24 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/array_includes.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/array_includes.js +46 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/assert.d.ts +16 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/assert.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/assert.js +22 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/assertion_error.d.ts +26 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/assertion_error.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/assertion_error.js +30 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/equal.d.ts +17 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/equal.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/equal.js +183 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/equals.d.ts +35 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/equals.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/equals.js +58 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/exists.d.ts +18 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/exists.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/exists.js +27 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/fail.d.ts +15 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/fail.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/fail.js +20 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/false.d.ts +18 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/false.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/false.js +22 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/greater.d.ts +20 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/greater.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/greater.js +29 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/greater_or_equal.d.ts +20 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/greater_or_equal.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/greater_or_equal.js +29 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/instance_of.d.ts +23 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/instance_of.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/instance_of.js +52 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/is_error.d.ts +25 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/is_error.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/is_error.js +53 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/less.d.ts +19 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/less.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/less.js +28 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/less_or_equal.d.ts +20 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/less_or_equal.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/less_or_equal.js +29 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/match.d.ts +18 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/match.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/match.js +26 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/mod.d.ts +44 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/mod.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/mod.js +45 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/not_equals.d.ts +21 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/not_equals.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/not_equals.js +33 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/not_instance_of.d.ts +20 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/not_instance_of.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/not_instance_of.js +29 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/not_match.d.ts +18 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/not_match.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/not_match.js +26 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/not_strict_equals.d.ts +23 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/not_strict_equals.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/not_strict_equals.js +32 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/object_match.d.ts +29 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/object_match.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/object_match.js +167 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/rejects.d.ts +42 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/rejects.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/rejects.js +53 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/strict_equals.d.ts +24 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/strict_equals.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/strict_equals.js +57 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/string_includes.d.ts +18 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/string_includes.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/string_includes.js +26 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/throws.d.ts +45 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/throws.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/throws.js +44 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/unimplemented.d.ts +15 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/unimplemented.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/unimplemented.js +20 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/unreachable.d.ts +15 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/unreachable.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.12/unreachable.js +20 -0
- package/esm/deps/jsr.io/@std/internal/1.0.6/build_message.d.ts +82 -0
- package/esm/deps/jsr.io/@std/internal/1.0.6/build_message.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/internal/1.0.6/build_message.js +110 -0
- package/esm/deps/jsr.io/@std/internal/1.0.6/diff.d.ts +140 -0
- package/esm/deps/jsr.io/@std/internal/1.0.6/diff.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/internal/1.0.6/diff.js +276 -0
- package/esm/deps/jsr.io/@std/internal/1.0.6/diff_str.d.ts +99 -0
- package/esm/deps/jsr.io/@std/internal/1.0.6/diff_str.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/internal/1.0.6/diff_str.js +182 -0
- package/esm/deps/jsr.io/@std/internal/1.0.6/format.d.ts +2 -0
- package/esm/deps/jsr.io/@std/internal/1.0.6/format.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/internal/1.0.6/format.js +37 -0
- package/esm/deps/jsr.io/@std/internal/1.0.6/styles.d.ts +159 -0
- package/esm/deps/jsr.io/@std/internal/1.0.6/styles.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/internal/1.0.6/styles.js +207 -0
- package/esm/deps/jsr.io/@std/internal/1.0.6/types.d.ts +16 -0
- package/esm/deps/jsr.io/@std/internal/1.0.6/types.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/internal/1.0.6/types.js +3 -0
- package/esm/mod.d.ts +2 -0
- package/esm/mod.d.ts.map +1 -0
- package/esm/mod.js +1 -0
- package/esm/package.json +3 -0
- package/esm/worker.d.ts +135 -0
- package/esm/worker.d.ts.map +1 -0
- package/esm/worker.js +237 -0
- package/package.json +29 -0
- package/script/_dnt.shims.d.ts +2 -0
- package/script/_dnt.shims.d.ts.map +1 -0
- package/script/_dnt.shims.js +60 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/almost_equals.d.ts +29 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/almost_equals.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/almost_equals.js +49 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/array_includes.d.ts +24 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/array_includes.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/array_includes.js +49 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/assert.d.ts +16 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/assert.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/assert.js +25 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/assertion_error.d.ts +26 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/assertion_error.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/assertion_error.js +34 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/equal.d.ts +17 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/equal.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/equal.js +209 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/equals.d.ts +35 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/equals.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/equals.js +61 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/exists.d.ts +18 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/exists.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/exists.js +30 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/fail.d.ts +15 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/fail.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/fail.js +23 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/false.d.ts +18 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/false.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/false.js +25 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/greater.d.ts +20 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/greater.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/greater.js +32 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/greater_or_equal.d.ts +20 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/greater_or_equal.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/greater_or_equal.js +32 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/instance_of.d.ts +23 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/instance_of.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/instance_of.js +55 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/is_error.d.ts +25 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/is_error.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/is_error.js +56 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/less.d.ts +19 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/less.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/less.js +31 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/less_or_equal.d.ts +20 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/less_or_equal.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/less_or_equal.js +32 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/match.d.ts +18 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/match.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/match.js +29 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/mod.d.ts +44 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/mod.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/mod.js +61 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/not_equals.d.ts +21 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/not_equals.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/not_equals.js +36 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/not_instance_of.d.ts +20 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/not_instance_of.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/not_instance_of.js +32 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/not_match.d.ts +18 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/not_match.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/not_match.js +29 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/not_strict_equals.d.ts +23 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/not_strict_equals.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/not_strict_equals.js +35 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/object_match.d.ts +29 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/object_match.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/object_match.js +170 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/rejects.d.ts +42 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/rejects.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/rejects.js +56 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/strict_equals.d.ts +24 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/strict_equals.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/strict_equals.js +60 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/string_includes.d.ts +18 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/string_includes.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/string_includes.js +29 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/throws.d.ts +45 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/throws.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/throws.js +47 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/unimplemented.d.ts +15 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/unimplemented.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/unimplemented.js +23 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/unreachable.d.ts +15 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/unreachable.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.12/unreachable.js +23 -0
- package/script/deps/jsr.io/@std/internal/1.0.6/build_message.d.ts +82 -0
- package/script/deps/jsr.io/@std/internal/1.0.6/build_message.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/internal/1.0.6/build_message.js +115 -0
- package/script/deps/jsr.io/@std/internal/1.0.6/diff.d.ts +140 -0
- package/script/deps/jsr.io/@std/internal/1.0.6/diff.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/internal/1.0.6/diff.js +283 -0
- package/script/deps/jsr.io/@std/internal/1.0.6/diff_str.d.ts +99 -0
- package/script/deps/jsr.io/@std/internal/1.0.6/diff_str.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/internal/1.0.6/diff_str.js +188 -0
- package/script/deps/jsr.io/@std/internal/1.0.6/format.d.ts +2 -0
- package/script/deps/jsr.io/@std/internal/1.0.6/format.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/internal/1.0.6/format.js +63 -0
- package/script/deps/jsr.io/@std/internal/1.0.6/styles.d.ts +159 -0
- package/script/deps/jsr.io/@std/internal/1.0.6/styles.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/internal/1.0.6/styles.js +242 -0
- package/script/deps/jsr.io/@std/internal/1.0.6/types.d.ts +16 -0
- package/script/deps/jsr.io/@std/internal/1.0.6/types.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/internal/1.0.6/types.js +4 -0
- package/script/mod.d.ts +2 -0
- package/script/mod.d.ts.map +1 -0
- package/script/mod.js +17 -0
- package/script/package.json +3 -0
- package/script/worker.d.ts +135 -0
- package/script/worker.d.ts.map +1 -0
- package/script/worker.js +241 -0
package/README.md
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
# Web Worker
|
|
2
|
+
|
|
3
|
+
Easily use Web Workers to offload CPU-intensive computations or manage external
|
|
4
|
+
processes. A library for seamlessly integrating [Web Workers][Web Workers] with
|
|
5
|
+
Effection programs.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
This package provides two functions. {@link useWorker} used in the main thread
|
|
10
|
+
to start and establish communication with the worker. {@link workerMain} used in
|
|
11
|
+
the worker script to invoke a worker function and send data back to the main
|
|
12
|
+
thread.
|
|
13
|
+
|
|
14
|
+
## Features
|
|
15
|
+
|
|
16
|
+
- Establishes two-way communication between the main and the worker threads
|
|
17
|
+
- Gracefully shutdowns the worker from the main thread
|
|
18
|
+
- Propagates errors from the worker to the main thread
|
|
19
|
+
- Type-safe message handling with TypeScript
|
|
20
|
+
|
|
21
|
+
## Usage: Get worker's return value
|
|
22
|
+
|
|
23
|
+
The return value of the worker is the return value of the function passed to
|
|
24
|
+
`workerMain`.
|
|
25
|
+
|
|
26
|
+
### Worker thread
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
import { workerMain } from "@effectionx/worker";
|
|
30
|
+
|
|
31
|
+
await workerMain<number, number, number, number>(function* fibonacci({
|
|
32
|
+
data: n, // data sent to the worker from the main thread
|
|
33
|
+
}) {
|
|
34
|
+
if (n <= 1) return n;
|
|
35
|
+
|
|
36
|
+
let a = 0,
|
|
37
|
+
b = 1;
|
|
38
|
+
for (let i = 2; i <= n; i++) {
|
|
39
|
+
let temp = a + b;
|
|
40
|
+
a = b;
|
|
41
|
+
b = temp;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return b;
|
|
45
|
+
});
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Main Thread
|
|
49
|
+
|
|
50
|
+
You can easily retrieve this value from the worker object returned by
|
|
51
|
+
`useWorker` function in the main thread.
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
import { run } from "effection";
|
|
55
|
+
import { useWorker } from "@effectionx/worker";
|
|
56
|
+
|
|
57
|
+
await run(function* () {
|
|
58
|
+
const worker = yield* useWorker<number, number, number, number>(
|
|
59
|
+
"./fibonacci.ts",
|
|
60
|
+
{
|
|
61
|
+
type: "module",
|
|
62
|
+
data: 5, // data is passed to the operation function (can be any serializable value)
|
|
63
|
+
},
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
const result = yield* worker; // wait for the result to receive the result
|
|
67
|
+
|
|
68
|
+
console.log(result); // Output: 5
|
|
69
|
+
});
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Error handling
|
|
73
|
+
|
|
74
|
+
Errors thrown in the function passed to `workerMain` can be captured in the main
|
|
75
|
+
thread by wrapping `yield* worker` in a `try/catch` block;
|
|
76
|
+
|
|
77
|
+
```ts
|
|
78
|
+
try {
|
|
79
|
+
const result = yield * worker;
|
|
80
|
+
|
|
81
|
+
console.log(result);
|
|
82
|
+
} catch (e) {
|
|
83
|
+
console.error(e); // error will be available here
|
|
84
|
+
}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Usage: Sending messages to the worker
|
|
88
|
+
|
|
89
|
+
The worker can respond to incoming messages using `forEach` function provided by
|
|
90
|
+
the `messages` object passed to the `workerMain` function.
|
|
91
|
+
|
|
92
|
+
### Worker Thread
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
import { workerMain } from "../worker.ts";
|
|
96
|
+
|
|
97
|
+
await workerMain<number, number, void, number>(function* ({ messages, data }) {
|
|
98
|
+
let counter = data;
|
|
99
|
+
|
|
100
|
+
yield* messages.forEach(function* (message) {
|
|
101
|
+
counter += message;
|
|
102
|
+
return counter;
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
return counter;
|
|
106
|
+
});
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### Main Thread
|
|
110
|
+
|
|
111
|
+
The main thread can send messages to the worker using the `send` method on the
|
|
112
|
+
object returned by `useWorker`. Effection will wait for the value to be returned
|
|
113
|
+
from the worker before continuing.
|
|
114
|
+
|
|
115
|
+
```ts
|
|
116
|
+
import { run } from "effection";
|
|
117
|
+
import { useWorker } from "@effectionx/worker";
|
|
118
|
+
|
|
119
|
+
await run(function* () {
|
|
120
|
+
const worker = yield* useWorker<number, number, number, number>(
|
|
121
|
+
"./counter-worker.ts",
|
|
122
|
+
{
|
|
123
|
+
type: "module",
|
|
124
|
+
data: 5, // initial value (can be any serializable value)
|
|
125
|
+
},
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
console.log(yield* worker.send(5)); // Output 10
|
|
129
|
+
|
|
130
|
+
console.log(yield* worker.send(10)); // Output: 20
|
|
131
|
+
|
|
132
|
+
console.log(yield* worker.send(-5)); // Output: 15
|
|
133
|
+
});
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### Error Handling
|
|
137
|
+
|
|
138
|
+
You can catch error thrown while computing result for a message by wrapping
|
|
139
|
+
`yield* wrapper.send()` in a `try`/`catch`.
|
|
140
|
+
|
|
141
|
+
```ts
|
|
142
|
+
try {
|
|
143
|
+
console.log(yield * worker.send(5)); // Output 10
|
|
144
|
+
} catch (e) {
|
|
145
|
+
console.error(e); // error will be available here
|
|
146
|
+
}
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
[Web Workers]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_dnt.shims.d.ts","sourceRoot":"","sources":["../src/_dnt.shims.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,aAAa,gCAA2C,CAAC"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
const dntGlobals = {};
|
|
2
|
+
export const dntGlobalThis = createMergeProxy(globalThis, dntGlobals);
|
|
3
|
+
function createMergeProxy(baseObj, extObj) {
|
|
4
|
+
return new Proxy(baseObj, {
|
|
5
|
+
get(_target, prop, _receiver) {
|
|
6
|
+
if (prop in extObj) {
|
|
7
|
+
return extObj[prop];
|
|
8
|
+
}
|
|
9
|
+
else {
|
|
10
|
+
return baseObj[prop];
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
set(_target, prop, value) {
|
|
14
|
+
if (prop in extObj) {
|
|
15
|
+
delete extObj[prop];
|
|
16
|
+
}
|
|
17
|
+
baseObj[prop] = value;
|
|
18
|
+
return true;
|
|
19
|
+
},
|
|
20
|
+
deleteProperty(_target, prop) {
|
|
21
|
+
let success = false;
|
|
22
|
+
if (prop in extObj) {
|
|
23
|
+
delete extObj[prop];
|
|
24
|
+
success = true;
|
|
25
|
+
}
|
|
26
|
+
if (prop in baseObj) {
|
|
27
|
+
delete baseObj[prop];
|
|
28
|
+
success = true;
|
|
29
|
+
}
|
|
30
|
+
return success;
|
|
31
|
+
},
|
|
32
|
+
ownKeys(_target) {
|
|
33
|
+
const baseKeys = Reflect.ownKeys(baseObj);
|
|
34
|
+
const extKeys = Reflect.ownKeys(extObj);
|
|
35
|
+
const extKeysSet = new Set(extKeys);
|
|
36
|
+
return [...baseKeys.filter((k) => !extKeysSet.has(k)), ...extKeys];
|
|
37
|
+
},
|
|
38
|
+
defineProperty(_target, prop, desc) {
|
|
39
|
+
if (prop in extObj) {
|
|
40
|
+
delete extObj[prop];
|
|
41
|
+
}
|
|
42
|
+
Reflect.defineProperty(baseObj, prop, desc);
|
|
43
|
+
return true;
|
|
44
|
+
},
|
|
45
|
+
getOwnPropertyDescriptor(_target, prop) {
|
|
46
|
+
if (prop in extObj) {
|
|
47
|
+
return Reflect.getOwnPropertyDescriptor(extObj, prop);
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
return Reflect.getOwnPropertyDescriptor(baseObj, prop);
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
has(_target, prop) {
|
|
54
|
+
return prop in extObj || prop in baseObj;
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Make an assertion that `actual` and `expected` are almost equal numbers
|
|
3
|
+
* through a given tolerance. It can be used to take into account IEEE-754
|
|
4
|
+
* double-precision floating-point representation limitations. If the values
|
|
5
|
+
* are not almost equal then throw.
|
|
6
|
+
*
|
|
7
|
+
* The default tolerance is one hundred thousandth of a percent of the
|
|
8
|
+
* expected value.
|
|
9
|
+
*
|
|
10
|
+
* @example Usage
|
|
11
|
+
* ```ts ignore
|
|
12
|
+
* import { assertAlmostEquals } from "@std/assert";
|
|
13
|
+
*
|
|
14
|
+
* assertAlmostEquals(0.01, 0.02); // Throws
|
|
15
|
+
* assertAlmostEquals(1e-8, 1e-9); // Throws
|
|
16
|
+
* assertAlmostEquals(1.000000001e-8, 1.000000002e-8); // Doesn't throw
|
|
17
|
+
* assertAlmostEquals(0.01, 0.02, 0.1); // Doesn't throw
|
|
18
|
+
* assertAlmostEquals(0.1 + 0.2, 0.3, 1e-16); // Doesn't throw
|
|
19
|
+
* assertAlmostEquals(0.1 + 0.2, 0.3, 1e-17); // Throws
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @param actual The actual value to compare.
|
|
23
|
+
* @param expected The expected value to compare.
|
|
24
|
+
* @param tolerance The tolerance to consider the values almost equal. The
|
|
25
|
+
* default is one hundred thousandth of a percent of the expected value.
|
|
26
|
+
* @param msg The optional message to include in the error.
|
|
27
|
+
*/
|
|
28
|
+
export declare function assertAlmostEquals(actual: number, expected: number, tolerance?: number, msg?: string): void;
|
|
29
|
+
//# sourceMappingURL=almost_equals.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"almost_equals.d.ts","sourceRoot":"","sources":["../../../../../../src/deps/jsr.io/@std/assert/1.0.12/almost_equals.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,MAAM,EAChB,SAAS,CAAC,EAAE,MAAM,EAClB,GAAG,CAAC,EAAE,MAAM,QAmBb"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// Copyright 2018-2025 the Deno authors. MIT license.
|
|
2
|
+
// This module is browser compatible.
|
|
3
|
+
import { AssertionError } from "./assertion_error.js";
|
|
4
|
+
/**
|
|
5
|
+
* Make an assertion that `actual` and `expected` are almost equal numbers
|
|
6
|
+
* through a given tolerance. It can be used to take into account IEEE-754
|
|
7
|
+
* double-precision floating-point representation limitations. If the values
|
|
8
|
+
* are not almost equal then throw.
|
|
9
|
+
*
|
|
10
|
+
* The default tolerance is one hundred thousandth of a percent of the
|
|
11
|
+
* expected value.
|
|
12
|
+
*
|
|
13
|
+
* @example Usage
|
|
14
|
+
* ```ts ignore
|
|
15
|
+
* import { assertAlmostEquals } from "@std/assert";
|
|
16
|
+
*
|
|
17
|
+
* assertAlmostEquals(0.01, 0.02); // Throws
|
|
18
|
+
* assertAlmostEquals(1e-8, 1e-9); // Throws
|
|
19
|
+
* assertAlmostEquals(1.000000001e-8, 1.000000002e-8); // Doesn't throw
|
|
20
|
+
* assertAlmostEquals(0.01, 0.02, 0.1); // Doesn't throw
|
|
21
|
+
* assertAlmostEquals(0.1 + 0.2, 0.3, 1e-16); // Doesn't throw
|
|
22
|
+
* assertAlmostEquals(0.1 + 0.2, 0.3, 1e-17); // Throws
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @param actual The actual value to compare.
|
|
26
|
+
* @param expected The expected value to compare.
|
|
27
|
+
* @param tolerance The tolerance to consider the values almost equal. The
|
|
28
|
+
* default is one hundred thousandth of a percent of the expected value.
|
|
29
|
+
* @param msg The optional message to include in the error.
|
|
30
|
+
*/
|
|
31
|
+
export function assertAlmostEquals(actual, expected, tolerance, msg) {
|
|
32
|
+
if (Object.is(actual, expected)) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const delta = Math.abs(expected - actual);
|
|
36
|
+
if (tolerance === undefined) {
|
|
37
|
+
tolerance = isFinite(expected) ? Math.abs(expected * 1e-7) : 1e-7;
|
|
38
|
+
}
|
|
39
|
+
if (delta <= tolerance) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
const msgSuffix = msg ? `: ${msg}` : ".";
|
|
43
|
+
const f = (n) => Number.isInteger(n) ? n : n.toExponential();
|
|
44
|
+
throw new AssertionError(`Expected actual: "${f(actual)}" to be close to "${f(expected)}": \
|
|
45
|
+
delta "${f(delta)}" is greater than "${f(tolerance)}"${msgSuffix}`);
|
|
46
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** An array-like object (`Array`, `Uint8Array`, `NodeList`, etc.) that is not a string */
|
|
2
|
+
export type ArrayLikeArg<T> = ArrayLike<T> & object;
|
|
3
|
+
/**
|
|
4
|
+
* Make an assertion that `actual` includes the `expected` values. If not then
|
|
5
|
+
* an error will be thrown.
|
|
6
|
+
*
|
|
7
|
+
* Type parameter can be specified to ensure values under comparison have the
|
|
8
|
+
* same type.
|
|
9
|
+
*
|
|
10
|
+
* @example Usage
|
|
11
|
+
* ```ts ignore
|
|
12
|
+
* import { assertArrayIncludes } from "@std/assert";
|
|
13
|
+
*
|
|
14
|
+
* assertArrayIncludes([1, 2], [2]); // Doesn't throw
|
|
15
|
+
* assertArrayIncludes([1, 2], [3]); // Throws
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* @typeParam T The type of the elements in the array to compare.
|
|
19
|
+
* @param actual The array-like object to check for.
|
|
20
|
+
* @param expected The array-like object to check for.
|
|
21
|
+
* @param msg The optional message to display if the assertion fails.
|
|
22
|
+
*/
|
|
23
|
+
export declare function assertArrayIncludes<T>(actual: ArrayLikeArg<T>, expected: ArrayLikeArg<T>, msg?: string): void;
|
|
24
|
+
//# sourceMappingURL=array_includes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"array_includes.d.ts","sourceRoot":"","sources":["../../../../../../src/deps/jsr.io/@std/assert/1.0.12/array_includes.ts"],"names":[],"mappings":"AAMA,0FAA0F;AAC1F,MAAM,MAAM,YAAY,CAAC,CAAC,IAAI,SAAS,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC;AAEpD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,EACnC,MAAM,EAAE,YAAY,CAAC,CAAC,CAAC,EACvB,QAAQ,EAAE,YAAY,CAAC,CAAC,CAAC,EACzB,GAAG,CAAC,EAAE,MAAM,QAwBb"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// Copyright 2018-2025 the Deno authors. MIT license.
|
|
2
|
+
// This module is browser compatible.
|
|
3
|
+
import { equal } from "./equal.js";
|
|
4
|
+
import { format } from "../../internal/1.0.6/format.js";
|
|
5
|
+
import { AssertionError } from "./assertion_error.js";
|
|
6
|
+
/**
|
|
7
|
+
* Make an assertion that `actual` includes the `expected` values. If not then
|
|
8
|
+
* an error will be thrown.
|
|
9
|
+
*
|
|
10
|
+
* Type parameter can be specified to ensure values under comparison have the
|
|
11
|
+
* same type.
|
|
12
|
+
*
|
|
13
|
+
* @example Usage
|
|
14
|
+
* ```ts ignore
|
|
15
|
+
* import { assertArrayIncludes } from "@std/assert";
|
|
16
|
+
*
|
|
17
|
+
* assertArrayIncludes([1, 2], [2]); // Doesn't throw
|
|
18
|
+
* assertArrayIncludes([1, 2], [3]); // Throws
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* @typeParam T The type of the elements in the array to compare.
|
|
22
|
+
* @param actual The array-like object to check for.
|
|
23
|
+
* @param expected The array-like object to check for.
|
|
24
|
+
* @param msg The optional message to display if the assertion fails.
|
|
25
|
+
*/
|
|
26
|
+
export function assertArrayIncludes(actual, expected, msg) {
|
|
27
|
+
const missing = [];
|
|
28
|
+
for (let i = 0; i < expected.length; i++) {
|
|
29
|
+
let found = false;
|
|
30
|
+
for (let j = 0; j < actual.length; j++) {
|
|
31
|
+
if (equal(expected[i], actual[j])) {
|
|
32
|
+
found = true;
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (!found) {
|
|
37
|
+
missing.push(expected[i]);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
if (missing.length === 0) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const msgSuffix = msg ? `: ${msg}` : ".";
|
|
44
|
+
msg = `Expected actual: "${format(actual)}" to include: "${format(expected)}"${msgSuffix}\nmissing: ${format(missing)}`;
|
|
45
|
+
throw new AssertionError(msg);
|
|
46
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Make an assertion, error will be thrown if `expr` does not have truthy value.
|
|
3
|
+
*
|
|
4
|
+
* @example Usage
|
|
5
|
+
* ```ts ignore
|
|
6
|
+
* import { assert } from "@std/assert";
|
|
7
|
+
*
|
|
8
|
+
* assert("hello".includes("ello")); // Doesn't throw
|
|
9
|
+
* assert("hello".includes("world")); // Throws
|
|
10
|
+
* ```
|
|
11
|
+
*
|
|
12
|
+
* @param expr The expression to test.
|
|
13
|
+
* @param msg The optional message to display if the assertion fails.
|
|
14
|
+
*/
|
|
15
|
+
export declare function assert(expr: unknown, msg?: string): asserts expr;
|
|
16
|
+
//# sourceMappingURL=assert.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"assert.d.ts","sourceRoot":"","sources":["../../../../../../src/deps/jsr.io/@std/assert/1.0.12/assert.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;GAaG;AACH,wBAAgB,MAAM,CAAC,IAAI,EAAE,OAAO,EAAE,GAAG,SAAK,GAAG,OAAO,CAAC,IAAI,CAI5D"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Copyright 2018-2025 the Deno authors. MIT license.
|
|
2
|
+
// This module is browser compatible.
|
|
3
|
+
import { AssertionError } from "./assertion_error.js";
|
|
4
|
+
/**
|
|
5
|
+
* Make an assertion, error will be thrown if `expr` does not have truthy value.
|
|
6
|
+
*
|
|
7
|
+
* @example Usage
|
|
8
|
+
* ```ts ignore
|
|
9
|
+
* import { assert } from "@std/assert";
|
|
10
|
+
*
|
|
11
|
+
* assert("hello".includes("ello")); // Doesn't throw
|
|
12
|
+
* assert("hello".includes("world")); // Throws
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* @param expr The expression to test.
|
|
16
|
+
* @param msg The optional message to display if the assertion fails.
|
|
17
|
+
*/
|
|
18
|
+
export function assert(expr, msg = "") {
|
|
19
|
+
if (!expr) {
|
|
20
|
+
throw new AssertionError(msg);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error thrown when an assertion fails.
|
|
3
|
+
*
|
|
4
|
+
* @example Usage
|
|
5
|
+
* ```ts ignore
|
|
6
|
+
* import { AssertionError } from "@std/assert";
|
|
7
|
+
*
|
|
8
|
+
* try {
|
|
9
|
+
* throw new AssertionError("foo", { cause: "bar" });
|
|
10
|
+
* } catch (error) {
|
|
11
|
+
* if (error instanceof AssertionError) {
|
|
12
|
+
* error.message === "foo"; // true
|
|
13
|
+
* error.cause === "bar"; // true
|
|
14
|
+
* }
|
|
15
|
+
* }
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare class AssertionError extends Error {
|
|
19
|
+
/** Constructs a new instance.
|
|
20
|
+
*
|
|
21
|
+
* @param message The error message.
|
|
22
|
+
* @param options Additional options. This argument is still unstable. It may change in the future release.
|
|
23
|
+
*/
|
|
24
|
+
constructor(message: string, options?: ErrorOptions);
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=assertion_error.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"assertion_error.d.ts","sourceRoot":"","sources":["../../../../../../src/deps/jsr.io/@std/assert/1.0.12/assertion_error.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;GAgBG;AACH,qBAAa,cAAe,SAAQ,KAAK;IACvC;;;;OAIG;gBACS,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY;CAIpD"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// Copyright 2018-2025 the Deno authors. MIT license.
|
|
2
|
+
// This module is browser compatible.
|
|
3
|
+
/**
|
|
4
|
+
* Error thrown when an assertion fails.
|
|
5
|
+
*
|
|
6
|
+
* @example Usage
|
|
7
|
+
* ```ts ignore
|
|
8
|
+
* import { AssertionError } from "@std/assert";
|
|
9
|
+
*
|
|
10
|
+
* try {
|
|
11
|
+
* throw new AssertionError("foo", { cause: "bar" });
|
|
12
|
+
* } catch (error) {
|
|
13
|
+
* if (error instanceof AssertionError) {
|
|
14
|
+
* error.message === "foo"; // true
|
|
15
|
+
* error.cause === "bar"; // true
|
|
16
|
+
* }
|
|
17
|
+
* }
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export class AssertionError extends Error {
|
|
21
|
+
/** Constructs a new instance.
|
|
22
|
+
*
|
|
23
|
+
* @param message The error message.
|
|
24
|
+
* @param options Additional options. This argument is still unstable. It may change in the future release.
|
|
25
|
+
*/
|
|
26
|
+
constructor(message, options) {
|
|
27
|
+
super(message, options);
|
|
28
|
+
this.name = "AssertionError";
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deep equality comparison used in assertions.
|
|
3
|
+
*
|
|
4
|
+
* @param a The actual value
|
|
5
|
+
* @param b The expected value
|
|
6
|
+
* @returns `true` if the values are deeply equal, `false` otherwise
|
|
7
|
+
*
|
|
8
|
+
* @example Usage
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { equal } from "@std/assert/equal";
|
|
11
|
+
*
|
|
12
|
+
* equal({ foo: "bar" }, { foo: "bar" }); // Returns `true`
|
|
13
|
+
* equal({ foo: "bar" }, { foo: "baz" }); // Returns `false`
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export declare function equal(a: unknown, b: unknown): boolean;
|
|
17
|
+
//# sourceMappingURL=equal.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"equal.d.ts","sourceRoot":"","sources":["../../../../../../src/deps/jsr.io/@std/assert/1.0.12/equal.ts"],"names":[],"mappings":"AA2FA;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,KAAK,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,GAAG,OAAO,CAuGrD"}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
// Copyright 2018-2025 the Deno authors. MIT license.
|
|
2
|
+
// This module is browser compatible.
|
|
3
|
+
import * as dntShim from "../../../../../_dnt.shims.js";
|
|
4
|
+
function isKeyedCollection(x) {
|
|
5
|
+
return x instanceof Set || x instanceof Map;
|
|
6
|
+
}
|
|
7
|
+
function prototypesEqual(a, b) {
|
|
8
|
+
const pa = Object.getPrototypeOf(a);
|
|
9
|
+
const pb = Object.getPrototypeOf(b);
|
|
10
|
+
return pa === pb ||
|
|
11
|
+
pa === Object.prototype && pb === null ||
|
|
12
|
+
pa === null && pb === Object.prototype;
|
|
13
|
+
}
|
|
14
|
+
function isBasicObjectOrArray(obj) {
|
|
15
|
+
const proto = Object.getPrototypeOf(obj);
|
|
16
|
+
return proto === null || proto === Object.prototype ||
|
|
17
|
+
proto === Array.prototype;
|
|
18
|
+
}
|
|
19
|
+
// Slightly faster than Reflect.ownKeys in V8 as of 12.9.202.13-rusty (2024-10-28)
|
|
20
|
+
function ownKeys(obj) {
|
|
21
|
+
return [
|
|
22
|
+
...Object.getOwnPropertyNames(obj),
|
|
23
|
+
...Object.getOwnPropertySymbols(obj),
|
|
24
|
+
];
|
|
25
|
+
}
|
|
26
|
+
function getKeysDeep(obj) {
|
|
27
|
+
const keys = new Set();
|
|
28
|
+
while (obj !== Object.prototype && obj !== Array.prototype && obj != null) {
|
|
29
|
+
for (const key of ownKeys(obj)) {
|
|
30
|
+
keys.add(key);
|
|
31
|
+
}
|
|
32
|
+
obj = Object.getPrototypeOf(obj);
|
|
33
|
+
}
|
|
34
|
+
return keys;
|
|
35
|
+
}
|
|
36
|
+
// deno-lint-ignore no-explicit-any
|
|
37
|
+
const Temporal = dntShim.dntGlobalThis.Temporal ??
|
|
38
|
+
new Proxy({}, { get: () => { } });
|
|
39
|
+
/** A non-exhaustive list of prototypes that can be accurately fast-path compared with `String(instance)` */
|
|
40
|
+
const stringComparablePrototypes = new Set([
|
|
41
|
+
Intl.Locale,
|
|
42
|
+
RegExp,
|
|
43
|
+
Temporal.Duration,
|
|
44
|
+
Temporal.Instant,
|
|
45
|
+
Temporal.PlainDate,
|
|
46
|
+
Temporal.PlainDateTime,
|
|
47
|
+
Temporal.PlainTime,
|
|
48
|
+
Temporal.PlainYearMonth,
|
|
49
|
+
Temporal.PlainMonthDay,
|
|
50
|
+
Temporal.ZonedDateTime,
|
|
51
|
+
URL,
|
|
52
|
+
URLSearchParams,
|
|
53
|
+
].filter((x) => x != null).map((x) => x.prototype));
|
|
54
|
+
function isPrimitive(x) {
|
|
55
|
+
return typeof x === "string" ||
|
|
56
|
+
typeof x === "number" ||
|
|
57
|
+
typeof x === "boolean" ||
|
|
58
|
+
typeof x === "bigint" ||
|
|
59
|
+
typeof x === "symbol" ||
|
|
60
|
+
x == null;
|
|
61
|
+
}
|
|
62
|
+
const TypedArray = Object.getPrototypeOf(Uint8Array);
|
|
63
|
+
function compareTypedArrays(a, b) {
|
|
64
|
+
if (a.length !== b.length)
|
|
65
|
+
return false;
|
|
66
|
+
for (let i = 0; i < b.length; i++) {
|
|
67
|
+
if (!sameValueZero(a[i], b[i]))
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
/** Check both strict equality (`0 == -0`) and `Object.is` (`NaN == NaN`) */
|
|
73
|
+
function sameValueZero(a, b) {
|
|
74
|
+
return a === b || Object.is(a, b);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Deep equality comparison used in assertions.
|
|
78
|
+
*
|
|
79
|
+
* @param a The actual value
|
|
80
|
+
* @param b The expected value
|
|
81
|
+
* @returns `true` if the values are deeply equal, `false` otherwise
|
|
82
|
+
*
|
|
83
|
+
* @example Usage
|
|
84
|
+
* ```ts
|
|
85
|
+
* import { equal } from "@std/assert/equal";
|
|
86
|
+
*
|
|
87
|
+
* equal({ foo: "bar" }, { foo: "bar" }); // Returns `true`
|
|
88
|
+
* equal({ foo: "bar" }, { foo: "baz" }); // Returns `false`
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
export function equal(a, b) {
|
|
92
|
+
const seen = new Map();
|
|
93
|
+
return (function compare(a, b) {
|
|
94
|
+
if (sameValueZero(a, b))
|
|
95
|
+
return true;
|
|
96
|
+
if (isPrimitive(a) || isPrimitive(b))
|
|
97
|
+
return false;
|
|
98
|
+
if (a instanceof Date && b instanceof Date) {
|
|
99
|
+
return Object.is(a.getTime(), b.getTime());
|
|
100
|
+
}
|
|
101
|
+
if (a && typeof a === "object" && b && typeof b === "object") {
|
|
102
|
+
if (!prototypesEqual(a, b)) {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
if (a instanceof TypedArray) {
|
|
106
|
+
return compareTypedArrays(a, b);
|
|
107
|
+
}
|
|
108
|
+
if (a instanceof WeakMap) {
|
|
109
|
+
throw new TypeError("cannot compare WeakMap instances");
|
|
110
|
+
}
|
|
111
|
+
if (a instanceof WeakSet) {
|
|
112
|
+
throw new TypeError("cannot compare WeakSet instances");
|
|
113
|
+
}
|
|
114
|
+
if (a instanceof WeakRef) {
|
|
115
|
+
return compare(a.deref(), b.deref());
|
|
116
|
+
}
|
|
117
|
+
if (seen.get(a) === b) {
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
120
|
+
if (Object.keys(a).length !== Object.keys(b).length) {
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
seen.set(a, b);
|
|
124
|
+
if (isKeyedCollection(a) && isKeyedCollection(b)) {
|
|
125
|
+
if (a.size !== b.size) {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
const aKeys = [...a.keys()];
|
|
129
|
+
const primitiveKeysFastPath = aKeys.every(isPrimitive);
|
|
130
|
+
if (primitiveKeysFastPath) {
|
|
131
|
+
if (a instanceof Set) {
|
|
132
|
+
return a.symmetricDifference(b).size === 0;
|
|
133
|
+
}
|
|
134
|
+
for (const key of aKeys) {
|
|
135
|
+
if (!b.has(key) ||
|
|
136
|
+
!compare(a.get(key), b.get(key))) {
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return true;
|
|
141
|
+
}
|
|
142
|
+
let unmatchedEntries = a.size;
|
|
143
|
+
for (const [aKey, aValue] of a.entries()) {
|
|
144
|
+
for (const [bKey, bValue] of b.entries()) {
|
|
145
|
+
/* Given that Map keys can be references, we need
|
|
146
|
+
* to ensure that they are also deeply equal */
|
|
147
|
+
if (!compare(aKey, bKey))
|
|
148
|
+
continue;
|
|
149
|
+
if ((aKey === aValue && bKey === bValue) ||
|
|
150
|
+
(compare(aValue, bValue))) {
|
|
151
|
+
unmatchedEntries--;
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return unmatchedEntries === 0;
|
|
157
|
+
}
|
|
158
|
+
let keys;
|
|
159
|
+
if (isBasicObjectOrArray(a)) {
|
|
160
|
+
// fast path
|
|
161
|
+
keys = ownKeys({ ...a, ...b });
|
|
162
|
+
}
|
|
163
|
+
else if (stringComparablePrototypes.has(Object.getPrototypeOf(a))) {
|
|
164
|
+
// medium path
|
|
165
|
+
return String(a) === String(b);
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
// slow path
|
|
169
|
+
keys = getKeysDeep(a).union(getKeysDeep(b));
|
|
170
|
+
}
|
|
171
|
+
for (const key of keys) {
|
|
172
|
+
if (!compare(a[key], b[key])) {
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
if (((key in a) && (!(key in b))) || ((key in b) && (!(key in a)))) {
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
return true;
|
|
180
|
+
}
|
|
181
|
+
return false;
|
|
182
|
+
})(a, b);
|
|
183
|
+
}
|