@effectionx/worker 0.1.2 → 0.3.0
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/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.16/almost_equals.d.ts +29 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/almost_equals.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/almost_equals.js +46 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/array_includes.d.ts +24 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/array_includes.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/array_includes.js +46 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/assert.d.ts +16 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/assert.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/assert.js +22 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/assertion_error.d.ts +26 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/assertion_error.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/assertion_error.js +30 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/equal.d.ts +17 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/equal.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/equal.js +186 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/equals.d.ts +35 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/equals.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/equals.js +59 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/exists.d.ts +18 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/exists.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/exists.js +27 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/fail.d.ts +15 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/fail.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/fail.js +20 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/false.d.ts +18 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/false.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/false.js +22 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/greater.d.ts +20 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/greater.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/greater.js +29 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/greater_or_equal.d.ts +20 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/greater_or_equal.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/greater_or_equal.js +29 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/instance_of.d.ts +23 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/instance_of.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/instance_of.js +52 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/is_error.d.ts +25 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/is_error.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/is_error.js +53 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/less.d.ts +19 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/less.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/less.js +28 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/less_or_equal.d.ts +20 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/less_or_equal.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/less_or_equal.js +29 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/match.d.ts +18 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/match.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/match.js +26 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/mod.d.ts +44 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/mod.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/mod.js +45 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/not_equals.d.ts +21 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/not_equals.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/not_equals.js +33 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/not_instance_of.d.ts +20 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/not_instance_of.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/not_instance_of.js +29 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/not_match.d.ts +18 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/not_match.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/not_match.js +26 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/not_strict_equals.d.ts +23 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/not_strict_equals.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/not_strict_equals.js +32 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/object_match.d.ts +29 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/object_match.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/object_match.js +167 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/rejects.d.ts +42 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/rejects.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/rejects.js +53 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/strict_equals.d.ts +24 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/strict_equals.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/strict_equals.js +58 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/string_includes.d.ts +18 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/string_includes.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/string_includes.js +26 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/throws.d.ts +45 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/throws.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/throws.js +44 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/unimplemented.d.ts +15 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/unimplemented.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/unimplemented.js +20 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/unreachable.d.ts +15 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/unreachable.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/assert/1.0.16/unreachable.js +20 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/build_message.d.ts +80 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/build_message.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/build_message.js +116 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/diff.d.ts +140 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/diff.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/diff.js +276 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/diff_str.d.ts +99 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/diff_str.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/diff_str.js +182 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/format.d.ts +29 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/format.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/format.js +70 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/styles.d.ts +159 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/styles.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/styles.js +207 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/types.d.ts +25 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/types.d.ts.map +1 -0
- package/esm/deps/jsr.io/@std/internal/1.0.12/types.js +3 -0
- package/esm/message-channel.d.ts +3 -0
- package/esm/message-channel.d.ts.map +1 -0
- package/esm/message-channel.js +13 -0
- package/esm/mod.d.ts +1 -0
- package/esm/mod.d.ts.map +1 -1
- package/esm/mod.js +1 -0
- package/esm/types.d.ts +33 -0
- package/esm/types.d.ts.map +1 -0
- package/esm/types.js +1 -0
- package/esm/worker-main.d.ts +84 -0
- package/esm/worker-main.d.ts.map +1 -0
- package/esm/worker-main.js +186 -0
- package/esm/worker.d.ts +2 -74
- package/esm/worker.d.ts.map +1 -1
- package/esm/worker.js +4 -120
- package/package.json +6 -1
- 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.16/almost_equals.d.ts +29 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/almost_equals.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/almost_equals.js +49 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/array_includes.d.ts +24 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/array_includes.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/array_includes.js +49 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/assert.d.ts +16 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/assert.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/assert.js +25 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/assertion_error.d.ts +26 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/assertion_error.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/assertion_error.js +34 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/equal.d.ts +17 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/equal.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/equal.js +222 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/equals.d.ts +35 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/equals.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/equals.js +62 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/exists.d.ts +18 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/exists.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/exists.js +30 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/fail.d.ts +15 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/fail.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/fail.js +23 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/false.d.ts +18 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/false.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/false.js +25 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/greater.d.ts +20 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/greater.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/greater.js +32 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/greater_or_equal.d.ts +20 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/greater_or_equal.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/greater_or_equal.js +32 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/instance_of.d.ts +23 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/instance_of.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/instance_of.js +55 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/is_error.d.ts +25 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/is_error.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/is_error.js +56 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/less.d.ts +19 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/less.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/less.js +31 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/less_or_equal.d.ts +20 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/less_or_equal.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/less_or_equal.js +32 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/match.d.ts +18 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/match.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/match.js +29 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/mod.d.ts +44 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/mod.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/mod.js +61 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/not_equals.d.ts +21 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/not_equals.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/not_equals.js +36 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/not_instance_of.d.ts +20 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/not_instance_of.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/not_instance_of.js +32 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/not_match.d.ts +18 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/not_match.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/not_match.js +29 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/not_strict_equals.d.ts +23 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/not_strict_equals.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/not_strict_equals.js +35 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/object_match.d.ts +29 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/object_match.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/object_match.js +170 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/rejects.d.ts +42 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/rejects.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/rejects.js +56 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/strict_equals.d.ts +24 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/strict_equals.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/strict_equals.js +61 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/string_includes.d.ts +18 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/string_includes.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/string_includes.js +29 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/throws.d.ts +45 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/throws.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/throws.js +47 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/unimplemented.d.ts +15 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/unimplemented.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/unimplemented.js +23 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/unreachable.d.ts +15 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/unreachable.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/assert/1.0.16/unreachable.js +23 -0
- package/script/deps/jsr.io/@std/internal/1.0.12/build_message.d.ts +80 -0
- package/script/deps/jsr.io/@std/internal/1.0.12/build_message.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/internal/1.0.12/build_message.js +121 -0
- package/script/deps/jsr.io/@std/internal/1.0.12/diff.d.ts +140 -0
- package/script/deps/jsr.io/@std/internal/1.0.12/diff.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/internal/1.0.12/diff.js +283 -0
- package/script/deps/jsr.io/@std/internal/1.0.12/diff_str.d.ts +99 -0
- package/script/deps/jsr.io/@std/internal/1.0.12/diff_str.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/internal/1.0.12/diff_str.js +188 -0
- package/script/deps/jsr.io/@std/internal/1.0.12/format.d.ts +29 -0
- package/script/deps/jsr.io/@std/internal/1.0.12/format.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/internal/1.0.12/format.js +106 -0
- package/script/deps/jsr.io/@std/internal/1.0.12/styles.d.ts +159 -0
- package/script/deps/jsr.io/@std/internal/1.0.12/styles.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/internal/1.0.12/styles.js +252 -0
- package/script/deps/jsr.io/@std/internal/1.0.12/types.d.ts +25 -0
- package/script/deps/jsr.io/@std/internal/1.0.12/types.d.ts.map +1 -0
- package/script/deps/jsr.io/@std/internal/1.0.12/types.js +4 -0
- package/script/message-channel.d.ts +3 -0
- package/script/message-channel.d.ts.map +1 -0
- package/script/message-channel.js +16 -0
- package/script/mod.d.ts +1 -0
- package/script/mod.d.ts.map +1 -1
- package/script/mod.js +1 -0
- package/script/types.d.ts +33 -0
- package/script/types.d.ts.map +1 -0
- package/script/types.js +2 -0
- package/script/worker-main.d.ts +84 -0
- package/script/worker-main.d.ts.map +1 -0
- package/script/worker-main.js +190 -0
- package/script/worker.d.ts +2 -74
- package/script/worker.d.ts.map +1 -1
- package/script/worker.js +5 -122
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { createSignal, each, Err, main, Ok, on, resource, spawn, } from "effection";
|
|
2
|
+
/**
|
|
3
|
+
* Entrypoint used in the worker that estaliblishes communication
|
|
4
|
+
* with the main thread. It can be used to return a value,
|
|
5
|
+
* respond to messages or both.
|
|
6
|
+
*
|
|
7
|
+
* @example Returning a value
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { workerMain } from "../worker.ts";
|
|
10
|
+
*
|
|
11
|
+
* await workerMain(function* ({ data }) {
|
|
12
|
+
* return data;
|
|
13
|
+
* });
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* @example Responding to messages
|
|
17
|
+
* ```ts
|
|
18
|
+
* import { workerMain } from "../worker.ts";
|
|
19
|
+
*
|
|
20
|
+
* await workerMain(function* ({ messages }) {
|
|
21
|
+
* yield* messages.forEach(function* (message) {
|
|
22
|
+
* return message;
|
|
23
|
+
* });
|
|
24
|
+
* });
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @example Responding to messages and return a value
|
|
28
|
+
* ```ts
|
|
29
|
+
* import { workerMain } from "../worker.ts";
|
|
30
|
+
*
|
|
31
|
+
* await workerMain<number, number, number, number>(
|
|
32
|
+
* function* ({ messages, data: initial }) {
|
|
33
|
+
* let counter = initial;
|
|
34
|
+
*
|
|
35
|
+
* yield* messages.forEach(function* (message) {
|
|
36
|
+
* counter += message;
|
|
37
|
+
* return counter; // returns a value after each message
|
|
38
|
+
* });
|
|
39
|
+
*
|
|
40
|
+
* return counter; // returns the final value
|
|
41
|
+
* },
|
|
42
|
+
* );
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* @template TSend - value main thread will send to the worker
|
|
46
|
+
* @template TRecv - value main thread will receive from the worker
|
|
47
|
+
* @template TReturn - worker operation return value
|
|
48
|
+
* @template TData - data passed from the main thread to the worker during initialization
|
|
49
|
+
* @param {(options: WorkerMainOptions<TSend, TRecv, TData>) => Operation<TReturn>} body
|
|
50
|
+
* @returns {Promise<void>}
|
|
51
|
+
*/
|
|
52
|
+
export async function workerMain(body) {
|
|
53
|
+
await main(function* () {
|
|
54
|
+
let sent = createSignal();
|
|
55
|
+
let worker = yield* createWorkerStatesSignal();
|
|
56
|
+
yield* spawn(function* () {
|
|
57
|
+
for (const message of yield* each(on(self, "message"))) {
|
|
58
|
+
const control = message.data;
|
|
59
|
+
switch (control.type) {
|
|
60
|
+
case "init": {
|
|
61
|
+
worker.start(yield* spawn(function* () {
|
|
62
|
+
try {
|
|
63
|
+
let value = yield* body({
|
|
64
|
+
data: control.data,
|
|
65
|
+
messages: {
|
|
66
|
+
*forEach(fn) {
|
|
67
|
+
for (let { value, response } of yield* each(sent)) {
|
|
68
|
+
yield* spawn(function* () {
|
|
69
|
+
try {
|
|
70
|
+
let result = yield* fn(value);
|
|
71
|
+
response.postMessage(Ok(result));
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
response.postMessage(Err(error));
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
yield* each.next();
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
worker.complete(value);
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
worker.crash(error);
|
|
86
|
+
}
|
|
87
|
+
}));
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
case "send": {
|
|
91
|
+
let { value, response } = control;
|
|
92
|
+
sent.send({ value, response });
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
case "close": {
|
|
96
|
+
const current = worker.valueOf();
|
|
97
|
+
if (current.type === "running") {
|
|
98
|
+
yield* current.task.halt();
|
|
99
|
+
worker.interrupt();
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
yield* each.next();
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
for (const state of yield* each(worker)) {
|
|
107
|
+
if (state.type === "new") {
|
|
108
|
+
postMessage({ type: "open" });
|
|
109
|
+
}
|
|
110
|
+
else if (state.type === "interrupted" || state.type === "error") {
|
|
111
|
+
postMessage({ type: "close", result: Err(state.error) });
|
|
112
|
+
}
|
|
113
|
+
else if (state.type === "complete") {
|
|
114
|
+
postMessage({ type: "close", result: Ok(state.value) });
|
|
115
|
+
}
|
|
116
|
+
yield* each.next();
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
export function createWorkerStatesSignal() {
|
|
121
|
+
return resource(function* (provide) {
|
|
122
|
+
let ref = {
|
|
123
|
+
current: { type: "new" },
|
|
124
|
+
};
|
|
125
|
+
const signal = createSignal();
|
|
126
|
+
const set = (value) => {
|
|
127
|
+
signal.send(ref.current = value);
|
|
128
|
+
return value;
|
|
129
|
+
};
|
|
130
|
+
const update = (updater) => set(updater(ref.current));
|
|
131
|
+
const interrupt = () => {
|
|
132
|
+
let next = {
|
|
133
|
+
type: "interrupted",
|
|
134
|
+
error: new Error(`worker terminated`),
|
|
135
|
+
};
|
|
136
|
+
set(next);
|
|
137
|
+
return next;
|
|
138
|
+
};
|
|
139
|
+
try {
|
|
140
|
+
yield* provide({
|
|
141
|
+
*[Symbol.iterator]() {
|
|
142
|
+
let subscription = yield* signal;
|
|
143
|
+
signal.send(ref.current);
|
|
144
|
+
return subscription;
|
|
145
|
+
},
|
|
146
|
+
get state() {
|
|
147
|
+
return ref.current.type;
|
|
148
|
+
},
|
|
149
|
+
set,
|
|
150
|
+
update,
|
|
151
|
+
valueOf() {
|
|
152
|
+
return ref.current;
|
|
153
|
+
},
|
|
154
|
+
start(task) {
|
|
155
|
+
if (ref.current.type === "complete") {
|
|
156
|
+
return ref.current;
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
const next = { type: "running", task };
|
|
160
|
+
set(next);
|
|
161
|
+
return next;
|
|
162
|
+
}
|
|
163
|
+
},
|
|
164
|
+
complete(value) {
|
|
165
|
+
let next = { type: "complete", value };
|
|
166
|
+
set(next);
|
|
167
|
+
return next;
|
|
168
|
+
},
|
|
169
|
+
crash(error) {
|
|
170
|
+
let next = { type: "error", error };
|
|
171
|
+
set(next);
|
|
172
|
+
return next;
|
|
173
|
+
},
|
|
174
|
+
interrupt: interrupt,
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
finally {
|
|
178
|
+
if (ref.current.type === "running") {
|
|
179
|
+
interrupt();
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
function postMessage(message) {
|
|
185
|
+
self.postMessage(message);
|
|
186
|
+
}
|
package/esm/worker.d.ts
CHANGED
|
@@ -1,18 +1,4 @@
|
|
|
1
1
|
import { type Operation } from "effection";
|
|
2
|
-
export interface WorkerResource<TSend, TRecv, TReturn> extends Operation<TReturn> {
|
|
3
|
-
send(data: TSend): Operation<TRecv>;
|
|
4
|
-
}
|
|
5
|
-
/**
|
|
6
|
-
* Object that represents messages the main thread
|
|
7
|
-
* sends to the worker. It provides function for
|
|
8
|
-
* handling messages.
|
|
9
|
-
*
|
|
10
|
-
* @template TSend - value main thread will send to the worker
|
|
11
|
-
* @template TRecv - value main thread will receive from the worker
|
|
12
|
-
*/
|
|
13
|
-
export interface WorkerMessages<TSend, TRecv> {
|
|
14
|
-
forEach(fn: (message: TSend) => Operation<TRecv>): Operation<void>;
|
|
15
|
-
}
|
|
16
2
|
/**
|
|
17
3
|
* Argument received by workerMain function
|
|
18
4
|
*
|
|
@@ -20,67 +6,9 @@ export interface WorkerMessages<TSend, TRecv> {
|
|
|
20
6
|
* @template TRecv - value main thread will receive from the worker
|
|
21
7
|
* @template TData - data passed from the main thread to the worker during initialization
|
|
22
8
|
*/
|
|
23
|
-
export interface
|
|
24
|
-
|
|
25
|
-
* Namespace that provides APIs for working with incoming messages
|
|
26
|
-
*/
|
|
27
|
-
messages: WorkerMessages<TSend, TRecv>;
|
|
28
|
-
/**
|
|
29
|
-
* Initial data received by the worker from the main thread used for initialization.
|
|
30
|
-
*/
|
|
31
|
-
data: TData;
|
|
9
|
+
export interface WorkerResource<TSend, TRecv, TReturn> extends Operation<TReturn> {
|
|
10
|
+
send(data: TSend): Operation<TRecv>;
|
|
32
11
|
}
|
|
33
|
-
/**
|
|
34
|
-
* Entrypoint used in the worker that estaliblishes communication
|
|
35
|
-
* with the main thread. It can be used to return a value,
|
|
36
|
-
* respond to messages or both.
|
|
37
|
-
*
|
|
38
|
-
* @example Returning a value
|
|
39
|
-
* ```ts
|
|
40
|
-
* import { workerMain } from "../worker.ts";
|
|
41
|
-
*
|
|
42
|
-
* await workerMain(function* ({ data }) {
|
|
43
|
-
* return data;
|
|
44
|
-
* });
|
|
45
|
-
* ```
|
|
46
|
-
*
|
|
47
|
-
* @example Responding to messages
|
|
48
|
-
* ```ts
|
|
49
|
-
* import { workerMain } from "../worker.ts";
|
|
50
|
-
*
|
|
51
|
-
* await workerMain(function* ({ messages }) {
|
|
52
|
-
* yield* messages.forEach(function* (message) {
|
|
53
|
-
* return message;
|
|
54
|
-
* });
|
|
55
|
-
* });
|
|
56
|
-
* ```
|
|
57
|
-
*
|
|
58
|
-
* @example Responding to messages and return a value
|
|
59
|
-
* ```ts
|
|
60
|
-
* import { workerMain } from "../worker.ts";
|
|
61
|
-
*
|
|
62
|
-
* await workerMain<number, number, number, number>(
|
|
63
|
-
* function* ({ messages, data: initial }) {
|
|
64
|
-
* let counter = initial;
|
|
65
|
-
*
|
|
66
|
-
* yield* messages.forEach(function* (message) {
|
|
67
|
-
* counter += message;
|
|
68
|
-
* return counter; // returns a value after each message
|
|
69
|
-
* });
|
|
70
|
-
*
|
|
71
|
-
* return counter; // returns the final value
|
|
72
|
-
* },
|
|
73
|
-
* );
|
|
74
|
-
* ```
|
|
75
|
-
*
|
|
76
|
-
* @template TSend - value main thread will send to the worker
|
|
77
|
-
* @template TRecv - value main thread will receive from the worker
|
|
78
|
-
* @template TReturn - worker operation return value
|
|
79
|
-
* @template TData - data passed from the main thread to the worker during initialization
|
|
80
|
-
* @param {(options: WorkerMainOptions<TSend, TRecv, TData>) => Operation<TReturn>} body
|
|
81
|
-
* @returns {Promise<void>}
|
|
82
|
-
*/
|
|
83
|
-
export declare function workerMain<TSend, TRecv, TReturn, TData>(body: (options: WorkerMainOptions<TSend, TRecv, TData>) => Operation<TReturn>): Promise<void>;
|
|
84
12
|
/**
|
|
85
13
|
* Use on the main thread to create and exeecute a well behaved web worker.
|
|
86
14
|
*
|
package/esm/worker.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"worker.d.ts","sourceRoot":"","sources":["../src/worker.ts"],"names":[],"mappings":"AACA,OAAO,
|
|
1
|
+
{"version":3,"file":"worker.d.ts","sourceRoot":"","sources":["../src/worker.ts"],"names":[],"mappings":"AACA,OAAO,EAKL,KAAK,SAAS,EAKf,MAAM,WAAW,CAAC;AAInB;;;;;;GAMG;AACH,MAAM,WAAW,cAAc,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,CACnD,SAAQ,SAAS,CAAC,OAAO,CAAC;IAC1B,IAAI,CAAC,IAAI,EAAE,KAAK,GAAG,SAAS,CAAC,KAAK,CAAC,CAAC;CACrC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+CG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,KAAK,EACpD,GAAG,EAAE,MAAM,GAAG,GAAG,EACjB,OAAO,CAAC,EAAE,aAAa,GAAG;IAAE,IAAI,CAAC,EAAE,KAAK,CAAA;CAAE,GACzC,SAAS,CAAC,cAAc,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC,CAgElD"}
|
package/esm/worker.js
CHANGED
|
@@ -1,110 +1,6 @@
|
|
|
1
|
-
import { assert } from "
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
* Entrypoint used in the worker that estaliblishes communication
|
|
5
|
-
* with the main thread. It can be used to return a value,
|
|
6
|
-
* respond to messages or both.
|
|
7
|
-
*
|
|
8
|
-
* @example Returning a value
|
|
9
|
-
* ```ts
|
|
10
|
-
* import { workerMain } from "../worker.ts";
|
|
11
|
-
*
|
|
12
|
-
* await workerMain(function* ({ data }) {
|
|
13
|
-
* return data;
|
|
14
|
-
* });
|
|
15
|
-
* ```
|
|
16
|
-
*
|
|
17
|
-
* @example Responding to messages
|
|
18
|
-
* ```ts
|
|
19
|
-
* import { workerMain } from "../worker.ts";
|
|
20
|
-
*
|
|
21
|
-
* await workerMain(function* ({ messages }) {
|
|
22
|
-
* yield* messages.forEach(function* (message) {
|
|
23
|
-
* return message;
|
|
24
|
-
* });
|
|
25
|
-
* });
|
|
26
|
-
* ```
|
|
27
|
-
*
|
|
28
|
-
* @example Responding to messages and return a value
|
|
29
|
-
* ```ts
|
|
30
|
-
* import { workerMain } from "../worker.ts";
|
|
31
|
-
*
|
|
32
|
-
* await workerMain<number, number, number, number>(
|
|
33
|
-
* function* ({ messages, data: initial }) {
|
|
34
|
-
* let counter = initial;
|
|
35
|
-
*
|
|
36
|
-
* yield* messages.forEach(function* (message) {
|
|
37
|
-
* counter += message;
|
|
38
|
-
* return counter; // returns a value after each message
|
|
39
|
-
* });
|
|
40
|
-
*
|
|
41
|
-
* return counter; // returns the final value
|
|
42
|
-
* },
|
|
43
|
-
* );
|
|
44
|
-
* ```
|
|
45
|
-
*
|
|
46
|
-
* @template TSend - value main thread will send to the worker
|
|
47
|
-
* @template TRecv - value main thread will receive from the worker
|
|
48
|
-
* @template TReturn - worker operation return value
|
|
49
|
-
* @template TData - data passed from the main thread to the worker during initialization
|
|
50
|
-
* @param {(options: WorkerMainOptions<TSend, TRecv, TData>) => Operation<TReturn>} body
|
|
51
|
-
* @returns {Promise<void>}
|
|
52
|
-
*/
|
|
53
|
-
export async function workerMain(body) {
|
|
54
|
-
await main(function* () {
|
|
55
|
-
let sent = createSignal();
|
|
56
|
-
let controls = yield* on(self, "message");
|
|
57
|
-
let outcome = withResolvers();
|
|
58
|
-
self.postMessage({ type: "open" });
|
|
59
|
-
let result = yield* scoped(function* () {
|
|
60
|
-
yield* spawn(function* () {
|
|
61
|
-
let next = yield* controls.next();
|
|
62
|
-
while (true) {
|
|
63
|
-
let control = next.value.data;
|
|
64
|
-
if (control.type === "init") {
|
|
65
|
-
yield* spawn(function* () {
|
|
66
|
-
try {
|
|
67
|
-
let value = yield* body({
|
|
68
|
-
data: control.data,
|
|
69
|
-
messages: {
|
|
70
|
-
*forEach(fn) {
|
|
71
|
-
for (let { value, response } of yield* each(sent)) {
|
|
72
|
-
yield* spawn(function* () {
|
|
73
|
-
try {
|
|
74
|
-
let result = yield* fn(value);
|
|
75
|
-
response.postMessage(Ok(result));
|
|
76
|
-
}
|
|
77
|
-
catch (error) {
|
|
78
|
-
response.postMessage(Err(error));
|
|
79
|
-
}
|
|
80
|
-
});
|
|
81
|
-
yield* each.next();
|
|
82
|
-
}
|
|
83
|
-
},
|
|
84
|
-
},
|
|
85
|
-
});
|
|
86
|
-
outcome.resolve(Ok(value));
|
|
87
|
-
}
|
|
88
|
-
catch (error) {
|
|
89
|
-
outcome.resolve(Err(error));
|
|
90
|
-
}
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
else if (control.type === "send") {
|
|
94
|
-
let { value, response } = control;
|
|
95
|
-
sent.send({ value, response });
|
|
96
|
-
}
|
|
97
|
-
else if (control.type === "close") {
|
|
98
|
-
outcome.resolve(Err(new Error(`worker terminated`)));
|
|
99
|
-
}
|
|
100
|
-
next = yield* controls.next();
|
|
101
|
-
}
|
|
102
|
-
});
|
|
103
|
-
return yield* outcome.operation;
|
|
104
|
-
});
|
|
105
|
-
self.postMessage({ type: "close", result });
|
|
106
|
-
});
|
|
107
|
-
}
|
|
1
|
+
import { assert } from "./deps/jsr.io/@std/assert/1.0.16/mod.js";
|
|
2
|
+
import { Err, Ok, on, once, resource, spawn, withResolvers, } from "effection";
|
|
3
|
+
import { useMessageChannel } from "./message-channel.js";
|
|
108
4
|
/**
|
|
109
5
|
* Use on the main thread to create and exeecute a well behaved web worker.
|
|
110
6
|
*
|
|
@@ -210,24 +106,12 @@ export function useWorker(url, options) {
|
|
|
210
106
|
}
|
|
211
107
|
});
|
|
212
108
|
}
|
|
213
|
-
function useMessageChannel() {
|
|
214
|
-
return resource(function* (provide) {
|
|
215
|
-
let channel = new MessageChannel();
|
|
216
|
-
try {
|
|
217
|
-
yield* provide(channel);
|
|
218
|
-
}
|
|
219
|
-
finally {
|
|
220
|
-
channel.port1.close();
|
|
221
|
-
channel.port2.close();
|
|
222
|
-
}
|
|
223
|
-
});
|
|
224
|
-
}
|
|
225
109
|
function settled(operation) {
|
|
226
110
|
return {
|
|
227
111
|
*[Symbol.iterator]() {
|
|
228
112
|
try {
|
|
229
113
|
yield* operation;
|
|
230
|
-
return Ok();
|
|
114
|
+
return Ok(void 0);
|
|
231
115
|
}
|
|
232
116
|
catch (error) {
|
|
233
117
|
return Err(error);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@effectionx/worker",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"author": "engineering@frontside.com",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -18,9 +18,14 @@
|
|
|
18
18
|
"require": "./script/mod.js"
|
|
19
19
|
}
|
|
20
20
|
},
|
|
21
|
+
"scripts": {},
|
|
21
22
|
"engines": {
|
|
22
23
|
"node": ">= 16"
|
|
23
24
|
},
|
|
24
25
|
"sideEffects": false,
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"@effectionx/signals": "0.3.0",
|
|
28
|
+
"effection": "^3 || ^4.0.0-0"
|
|
29
|
+
},
|
|
25
30
|
"_generatedBy": "dnt@dev"
|
|
26
31
|
}
|
|
@@ -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,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.dntGlobalThis = void 0;
|
|
4
|
+
const dntGlobals = {};
|
|
5
|
+
exports.dntGlobalThis = createMergeProxy(globalThis, dntGlobals);
|
|
6
|
+
function createMergeProxy(baseObj, extObj) {
|
|
7
|
+
return new Proxy(baseObj, {
|
|
8
|
+
get(_target, prop, _receiver) {
|
|
9
|
+
if (prop in extObj) {
|
|
10
|
+
return extObj[prop];
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
return baseObj[prop];
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
set(_target, prop, value) {
|
|
17
|
+
if (prop in extObj) {
|
|
18
|
+
delete extObj[prop];
|
|
19
|
+
}
|
|
20
|
+
baseObj[prop] = value;
|
|
21
|
+
return true;
|
|
22
|
+
},
|
|
23
|
+
deleteProperty(_target, prop) {
|
|
24
|
+
let success = false;
|
|
25
|
+
if (prop in extObj) {
|
|
26
|
+
delete extObj[prop];
|
|
27
|
+
success = true;
|
|
28
|
+
}
|
|
29
|
+
if (prop in baseObj) {
|
|
30
|
+
delete baseObj[prop];
|
|
31
|
+
success = true;
|
|
32
|
+
}
|
|
33
|
+
return success;
|
|
34
|
+
},
|
|
35
|
+
ownKeys(_target) {
|
|
36
|
+
const baseKeys = Reflect.ownKeys(baseObj);
|
|
37
|
+
const extKeys = Reflect.ownKeys(extObj);
|
|
38
|
+
const extKeysSet = new Set(extKeys);
|
|
39
|
+
return [...baseKeys.filter((k) => !extKeysSet.has(k)), ...extKeys];
|
|
40
|
+
},
|
|
41
|
+
defineProperty(_target, prop, desc) {
|
|
42
|
+
if (prop in extObj) {
|
|
43
|
+
delete extObj[prop];
|
|
44
|
+
}
|
|
45
|
+
Reflect.defineProperty(baseObj, prop, desc);
|
|
46
|
+
return true;
|
|
47
|
+
},
|
|
48
|
+
getOwnPropertyDescriptor(_target, prop) {
|
|
49
|
+
if (prop in extObj) {
|
|
50
|
+
return Reflect.getOwnPropertyDescriptor(extObj, prop);
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
return Reflect.getOwnPropertyDescriptor(baseObj, prop);
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
has(_target, prop) {
|
|
57
|
+
return prop in extObj || prop in baseObj;
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
}
|
|
@@ -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.16/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,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.assertAlmostEquals = assertAlmostEquals;
|
|
4
|
+
// Copyright 2018-2025 the Deno authors. MIT license.
|
|
5
|
+
// This module is browser compatible.
|
|
6
|
+
const assertion_error_js_1 = require("./assertion_error.js");
|
|
7
|
+
/**
|
|
8
|
+
* Make an assertion that `actual` and `expected` are almost equal numbers
|
|
9
|
+
* through a given tolerance. It can be used to take into account IEEE-754
|
|
10
|
+
* double-precision floating-point representation limitations. If the values
|
|
11
|
+
* are not almost equal then throw.
|
|
12
|
+
*
|
|
13
|
+
* The default tolerance is one hundred thousandth of a percent of the
|
|
14
|
+
* expected value.
|
|
15
|
+
*
|
|
16
|
+
* @example Usage
|
|
17
|
+
* ```ts ignore
|
|
18
|
+
* import { assertAlmostEquals } from "@std/assert";
|
|
19
|
+
*
|
|
20
|
+
* assertAlmostEquals(0.01, 0.02); // Throws
|
|
21
|
+
* assertAlmostEquals(1e-8, 1e-9); // Throws
|
|
22
|
+
* assertAlmostEquals(1.000000001e-8, 1.000000002e-8); // Doesn't throw
|
|
23
|
+
* assertAlmostEquals(0.01, 0.02, 0.1); // Doesn't throw
|
|
24
|
+
* assertAlmostEquals(0.1 + 0.2, 0.3, 1e-16); // Doesn't throw
|
|
25
|
+
* assertAlmostEquals(0.1 + 0.2, 0.3, 1e-17); // Throws
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @param actual The actual value to compare.
|
|
29
|
+
* @param expected The expected value to compare.
|
|
30
|
+
* @param tolerance The tolerance to consider the values almost equal. The
|
|
31
|
+
* default is one hundred thousandth of a percent of the expected value.
|
|
32
|
+
* @param msg The optional message to include in the error.
|
|
33
|
+
*/
|
|
34
|
+
function assertAlmostEquals(actual, expected, tolerance, msg) {
|
|
35
|
+
if (Object.is(actual, expected)) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
const delta = Math.abs(expected - actual);
|
|
39
|
+
if (tolerance === undefined) {
|
|
40
|
+
tolerance = isFinite(expected) ? Math.abs(expected * 1e-7) : 1e-7;
|
|
41
|
+
}
|
|
42
|
+
if (delta <= tolerance) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
const msgSuffix = msg ? `: ${msg}` : ".";
|
|
46
|
+
const f = (n) => Number.isInteger(n) ? n : n.toExponential();
|
|
47
|
+
throw new assertion_error_js_1.AssertionError(`Expected actual: "${f(actual)}" to be close to "${f(expected)}": \
|
|
48
|
+
delta "${f(delta)}" is greater than "${f(tolerance)}"${msgSuffix}`);
|
|
49
|
+
}
|
|
@@ -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.16/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,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.assertArrayIncludes = assertArrayIncludes;
|
|
4
|
+
// Copyright 2018-2025 the Deno authors. MIT license.
|
|
5
|
+
// This module is browser compatible.
|
|
6
|
+
const equal_js_1 = require("./equal.js");
|
|
7
|
+
const format_js_1 = require("../../internal/1.0.12/format.js");
|
|
8
|
+
const assertion_error_js_1 = require("./assertion_error.js");
|
|
9
|
+
/**
|
|
10
|
+
* Make an assertion that `actual` includes the `expected` values. If not then
|
|
11
|
+
* an error will be thrown.
|
|
12
|
+
*
|
|
13
|
+
* Type parameter can be specified to ensure values under comparison have the
|
|
14
|
+
* same type.
|
|
15
|
+
*
|
|
16
|
+
* @example Usage
|
|
17
|
+
* ```ts ignore
|
|
18
|
+
* import { assertArrayIncludes } from "@std/assert";
|
|
19
|
+
*
|
|
20
|
+
* assertArrayIncludes([1, 2], [2]); // Doesn't throw
|
|
21
|
+
* assertArrayIncludes([1, 2], [3]); // Throws
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @typeParam T The type of the elements in the array to compare.
|
|
25
|
+
* @param actual The array-like object to check for.
|
|
26
|
+
* @param expected The array-like object to check for.
|
|
27
|
+
* @param msg The optional message to display if the assertion fails.
|
|
28
|
+
*/
|
|
29
|
+
function assertArrayIncludes(actual, expected, msg) {
|
|
30
|
+
const missing = [];
|
|
31
|
+
for (let i = 0; i < expected.length; i++) {
|
|
32
|
+
let found = false;
|
|
33
|
+
for (let j = 0; j < actual.length; j++) {
|
|
34
|
+
if ((0, equal_js_1.equal)(expected[i], actual[j])) {
|
|
35
|
+
found = true;
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (!found) {
|
|
40
|
+
missing.push(expected[i]);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (missing.length === 0) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
const msgSuffix = msg ? `: ${msg}` : ".";
|
|
47
|
+
msg = `Expected actual: "${(0, format_js_1.format)(actual)}" to include: "${(0, format_js_1.format)(expected)}"${msgSuffix}\nmissing: ${(0, format_js_1.format)(missing)}`;
|
|
48
|
+
throw new assertion_error_js_1.AssertionError(msg);
|
|
49
|
+
}
|