@aztec/foundation 0.7.2 → 0.7.3
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/.tsbuildinfo +1 -0
- package/dest/abi/abi.d.ts +232 -0
- package/dest/abi/abi.d.ts.map +1 -0
- package/dest/abi/abi.js +37 -0
- package/dest/abi/abi_coder.d.ts +8 -0
- package/dest/abi/abi_coder.d.ts.map +1 -0
- package/dest/abi/abi_coder.js +24 -0
- package/dest/abi/decoder.d.ts +57 -0
- package/dest/abi/decoder.d.ts.map +1 -0
- package/dest/abi/decoder.js +149 -0
- package/dest/abi/decoder.test.d.ts +2 -0
- package/dest/abi/decoder.test.d.ts.map +1 -0
- package/dest/abi/decoder.test.js +70 -0
- package/dest/abi/encoder.d.ts +10 -0
- package/dest/abi/encoder.d.ts.map +1 -0
- package/dest/abi/encoder.js +89 -0
- package/dest/abi/encoder.test.d.ts +2 -0
- package/dest/abi/encoder.test.d.ts.map +1 -0
- package/dest/abi/encoder.test.js +74 -0
- package/dest/abi/function_selector.d.ts +73 -0
- package/dest/abi/function_selector.d.ts.map +1 -0
- package/dest/abi/function_selector.js +104 -0
- package/dest/abi/index.d.ts +6 -0
- package/dest/abi/index.d.ts.map +1 -0
- package/dest/abi/index.js +6 -0
- package/dest/async-map/async_map.test.d.ts +2 -0
- package/dest/async-map/async_map.test.d.ts.map +1 -0
- package/dest/async-map/async_map.test.js +9 -0
- package/dest/async-map/index.d.ts +13 -0
- package/dest/async-map/index.d.ts.map +1 -0
- package/dest/async-map/index.js +19 -0
- package/dest/aztec-address/index.d.ts +114 -0
- package/dest/aztec-address/index.d.ts.map +1 -0
- package/dest/aztec-address/index.js +148 -0
- package/dest/bigint-buffer/bigint-buffer.test.d.ts +2 -0
- package/dest/bigint-buffer/bigint-buffer.test.d.ts.map +1 -0
- package/dest/bigint-buffer/bigint-buffer.test.js +18 -0
- package/dest/bigint-buffer/index.d.ts +35 -0
- package/dest/bigint-buffer/index.d.ts.map +1 -0
- package/dest/bigint-buffer/index.js +68 -0
- package/dest/collection/array.d.ts +30 -0
- package/dest/collection/array.d.ts.map +1 -0
- package/dest/collection/array.js +47 -0
- package/dest/collection/index.d.ts +2 -0
- package/dest/collection/index.d.ts.map +1 -0
- package/dest/collection/index.js +2 -0
- package/dest/committable/committable.d.ts +28 -0
- package/dest/committable/committable.d.ts.map +1 -0
- package/dest/committable/committable.js +41 -0
- package/dest/committable/committable.test.d.ts +2 -0
- package/dest/committable/committable.test.d.ts.map +1 -0
- package/dest/committable/committable.test.js +26 -0
- package/dest/committable/index.d.ts +2 -0
- package/dest/committable/index.d.ts.map +1 -0
- package/dest/committable/index.js +2 -0
- package/dest/crypto/index.d.ts +4 -0
- package/dest/crypto/index.d.ts.map +1 -0
- package/dest/crypto/index.js +4 -0
- package/dest/crypto/keccak/index.d.ts +20 -0
- package/dest/crypto/keccak/index.d.ts.map +1 -0
- package/dest/crypto/keccak/index.js +31 -0
- package/dest/crypto/random/index.d.ts +3 -0
- package/dest/crypto/random/index.d.ts.map +1 -0
- package/dest/crypto/random/index.js +36 -0
- package/dest/crypto/random/index.test.d.ts +2 -0
- package/dest/crypto/random/index.test.d.ts.map +1 -0
- package/dest/crypto/random/index.test.js +13 -0
- package/dest/crypto/sha256/index.d.ts +11 -0
- package/dest/crypto/sha256/index.d.ts.map +1 -0
- package/dest/crypto/sha256/index.js +14 -0
- package/dest/crypto/sha256/index.test.d.ts +2 -0
- package/dest/crypto/sha256/index.test.d.ts.map +1 -0
- package/dest/crypto/sha256/index.test.js +11 -0
- package/dest/errors/index.d.ts +8 -0
- package/dest/errors/index.d.ts.map +1 -0
- package/dest/errors/index.js +8 -0
- package/dest/eth-address/eth_address.test.d.ts +2 -0
- package/dest/eth-address/eth_address.test.d.ts.map +1 -0
- package/dest/eth-address/eth_address.test.js +95 -0
- package/dest/eth-address/index.d.ts +144 -0
- package/dest/eth-address/index.d.ts.map +1 -0
- package/dest/eth-address/index.js +228 -0
- package/dest/fields/coordinate.d.ts +67 -0
- package/dest/fields/coordinate.d.ts.map +1 -0
- package/dest/fields/coordinate.js +95 -0
- package/dest/fields/coordinate.test.d.ts +2 -0
- package/dest/fields/coordinate.test.d.ts.map +1 -0
- package/dest/fields/coordinate.test.js +22 -0
- package/dest/fields/fields.d.ts +177 -0
- package/dest/fields/fields.d.ts.map +1 -0
- package/dest/fields/fields.js +219 -0
- package/dest/fields/grumpkin_scalar.d.ts +96 -0
- package/dest/fields/grumpkin_scalar.d.ts.map +1 -0
- package/dest/fields/grumpkin_scalar.js +125 -0
- package/dest/fields/grumpkin_scalar.test.d.ts +2 -0
- package/dest/fields/grumpkin_scalar.test.d.ts.map +1 -0
- package/dest/fields/grumpkin_scalar.test.js +45 -0
- package/dest/fields/index.d.ts +5 -0
- package/dest/fields/index.d.ts.map +1 -0
- package/dest/fields/index.js +5 -0
- package/dest/fields/point.d.ts +105 -0
- package/dest/fields/point.d.ts.map +1 -0
- package/dest/fields/point.js +125 -0
- package/dest/fifo/bounded_serial_queue.d.ts +56 -0
- package/dest/fifo/bounded_serial_queue.d.ts.map +1 -0
- package/dest/fifo/bounded_serial_queue.js +94 -0
- package/dest/fifo/index.d.ts +5 -0
- package/dest/fifo/index.d.ts.map +1 -0
- package/dest/fifo/index.js +5 -0
- package/dest/fifo/memory_fifo.d.ts +55 -0
- package/dest/fifo/memory_fifo.d.ts.map +1 -0
- package/dest/fifo/memory_fifo.js +109 -0
- package/dest/fifo/semaphore.d.ts +23 -0
- package/dest/fifo/semaphore.d.ts.map +1 -0
- package/dest/fifo/semaphore.js +30 -0
- package/dest/fifo/serial_queue.d.ts +48 -0
- package/dest/fifo/serial_queue.d.ts.map +1 -0
- package/dest/fifo/serial_queue.js +74 -0
- package/dest/json-rpc/class_converter.d.ts +144 -0
- package/dest/json-rpc/class_converter.d.ts.map +1 -0
- package/dest/json-rpc/class_converter.js +100 -0
- package/dest/json-rpc/client/index.d.ts +2 -0
- package/dest/json-rpc/client/index.d.ts.map +1 -0
- package/dest/json-rpc/client/index.js +2 -0
- package/dest/json-rpc/client/json_rpc_client.d.ts +29 -0
- package/dest/json-rpc/client/json_rpc_client.d.ts.map +1 -0
- package/dest/json-rpc/client/json_rpc_client.js +104 -0
- package/dest/json-rpc/client/json_rpc_client.test.d.ts +2 -0
- package/dest/json-rpc/client/json_rpc_client.test.d.ts.map +1 -0
- package/dest/json-rpc/client/json_rpc_client.test.js +20 -0
- package/dest/json-rpc/convert.d.ts +28 -0
- package/dest/json-rpc/convert.d.ts.map +1 -0
- package/dest/json-rpc/convert.js +139 -0
- package/dest/json-rpc/convert.test.d.ts +2 -0
- package/dest/json-rpc/convert.test.d.ts.map +1 -0
- package/dest/json-rpc/convert.test.js +68 -0
- package/dest/json-rpc/fixtures/class_a.d.ts +11 -0
- package/dest/json-rpc/fixtures/class_a.d.ts.map +1 -0
- package/dest/json-rpc/fixtures/class_a.js +17 -0
- package/dest/json-rpc/fixtures/class_b.d.ts +11 -0
- package/dest/json-rpc/fixtures/class_b.d.ts.map +1 -0
- package/dest/json-rpc/fixtures/class_b.js +17 -0
- package/dest/json-rpc/fixtures/test_state.d.ts +47 -0
- package/dest/json-rpc/fixtures/test_state.d.ts.map +1 -0
- package/dest/json-rpc/fixtures/test_state.js +62 -0
- package/dest/json-rpc/index.d.ts +3 -0
- package/dest/json-rpc/index.d.ts.map +1 -0
- package/dest/json-rpc/index.js +3 -0
- package/dest/json-rpc/js_utils.d.ts +13 -0
- package/dest/json-rpc/js_utils.d.ts.map +1 -0
- package/dest/json-rpc/js_utils.js +18 -0
- package/dest/json-rpc/server/index.d.ts +3 -0
- package/dest/json-rpc/server/index.d.ts.map +1 -0
- package/dest/json-rpc/server/index.js +3 -0
- package/dest/json-rpc/server/json_proxy.d.ts +18 -0
- package/dest/json-rpc/server/json_proxy.d.ts.map +1 -0
- package/dest/json-rpc/server/json_proxy.js +36 -0
- package/dest/json-rpc/server/json_rpc_server.d.ts +34 -0
- package/dest/json-rpc/server/json_rpc_server.d.ts.map +1 -0
- package/dest/json-rpc/server/json_rpc_server.js +133 -0
- package/dest/json-rpc/server/json_rpc_server.test.d.ts +2 -0
- package/dest/json-rpc/server/json_rpc_server.test.d.ts.map +1 -0
- package/dest/json-rpc/server/json_rpc_server.test.js +22 -0
- package/dest/log/console.d.ts +11 -0
- package/dest/log/console.d.ts.map +1 -0
- package/dest/log/console.js +38 -0
- package/dest/log/debug.d.ts +43 -0
- package/dest/log/debug.d.ts.map +1 -0
- package/dest/log/debug.js +75 -0
- package/dest/log/index.d.ts +9 -0
- package/dest/log/index.d.ts.map +1 -0
- package/dest/log/index.js +5 -0
- package/dest/log/log_history.d.ts +31 -0
- package/dest/log/log_history.d.ts.map +1 -0
- package/dest/log/log_history.js +42 -0
- package/dest/log/log_history.test.d.ts +2 -0
- package/dest/log/log_history.test.d.ts.map +1 -0
- package/dest/log/log_history.test.js +78 -0
- package/dest/log/logger.d.ts +34 -0
- package/dest/log/logger.d.ts.map +1 -0
- package/dest/log/logger.js +78 -0
- package/dest/mutex/index.d.ts +53 -0
- package/dest/mutex/index.d.ts.map +1 -0
- package/dest/mutex/index.js +74 -0
- package/dest/mutex/mutex.test.d.ts +9 -0
- package/dest/mutex/mutex.test.d.ts.map +1 -0
- package/dest/mutex/mutex.test.js +58 -0
- package/dest/mutex/mutex_database.d.ts +10 -0
- package/dest/mutex/mutex_database.d.ts.map +1 -0
- package/dest/mutex/mutex_database.js +2 -0
- package/dest/retry/index.d.ts +44 -0
- package/dest/retry/index.d.ts.map +1 -0
- package/dest/retry/index.js +89 -0
- package/dest/running-promise/index.d.ts +35 -0
- package/dest/running-promise/index.d.ts.map +1 -0
- package/dest/running-promise/index.js +59 -0
- package/dest/serialize/buffer_reader.d.ts +188 -0
- package/dest/serialize/buffer_reader.d.ts.map +1 -0
- package/dest/serialize/buffer_reader.js +229 -0
- package/dest/serialize/buffer_reader.test.d.ts +2 -0
- package/dest/serialize/buffer_reader.test.d.ts.map +1 -0
- package/dest/serialize/buffer_reader.test.js +156 -0
- package/dest/serialize/deserializer.d.ts +134 -0
- package/dest/serialize/deserializer.d.ts.map +1 -0
- package/dest/serialize/deserializer.js +145 -0
- package/dest/serialize/free_funcs.d.ts +203 -0
- package/dest/serialize/free_funcs.d.ts.map +1 -0
- package/dest/serialize/free_funcs.js +250 -0
- package/dest/serialize/index.d.ts +6 -0
- package/dest/serialize/index.d.ts.map +1 -0
- package/dest/serialize/index.js +6 -0
- package/dest/serialize/serialize.test.d.ts +2 -0
- package/dest/serialize/serialize.test.d.ts.map +1 -0
- package/dest/serialize/serialize.test.js +68 -0
- package/dest/serialize/serializer.d.ts +89 -0
- package/dest/serialize/serializer.d.ts.map +1 -0
- package/dest/serialize/serializer.js +111 -0
- package/dest/serialize/types.d.ts +33 -0
- package/dest/serialize/types.d.ts.map +1 -0
- package/dest/serialize/types.js +22 -0
- package/dest/sleep/index.d.ts +52 -0
- package/dest/sleep/index.d.ts.map +1 -0
- package/dest/sleep/index.js +70 -0
- package/dest/timer/index.d.ts +3 -0
- package/dest/timer/index.d.ts.map +1 -0
- package/dest/timer/index.js +3 -0
- package/dest/timer/timeout.d.ts +41 -0
- package/dest/timer/timeout.d.ts.map +1 -0
- package/dest/timer/timeout.js +62 -0
- package/dest/timer/timer.d.ts +33 -0
- package/dest/timer/timer.d.ts.map +1 -0
- package/dest/timer/timer.js +38 -0
- package/dest/transport/browser/index.d.ts +5 -0
- package/dest/transport/browser/index.d.ts.map +1 -0
- package/dest/transport/browser/index.js +5 -0
- package/dest/transport/browser/message_port_socket.d.ts +37 -0
- package/dest/transport/browser/message_port_socket.d.ts.map +1 -0
- package/dest/transport/browser/message_port_socket.js +46 -0
- package/dest/transport/browser/shared_worker_connector.d.ts +19 -0
- package/dest/transport/browser/shared_worker_connector.d.ts.map +1 -0
- package/dest/transport/browser/shared_worker_connector.js +21 -0
- package/dest/transport/browser/shared_worker_listener.d.ts +38 -0
- package/dest/transport/browser/shared_worker_listener.d.ts.map +1 -0
- package/dest/transport/browser/shared_worker_listener.js +37 -0
- package/dest/transport/browser/worker_connector.d.ts +26 -0
- package/dest/transport/browser/worker_connector.d.ts.map +1 -0
- package/dest/transport/browser/worker_connector.js +30 -0
- package/dest/transport/browser/worker_listener.d.ts +39 -0
- package/dest/transport/browser/worker_listener.d.ts.map +1 -0
- package/dest/transport/browser/worker_listener.js +39 -0
- package/dest/transport/dispatch/create_dispatch_fn.d.ts +25 -0
- package/dest/transport/dispatch/create_dispatch_fn.d.ts.map +1 -0
- package/dest/transport/dispatch/create_dispatch_fn.js +18 -0
- package/dest/transport/dispatch/create_dispatch_proxy.d.ts +102 -0
- package/dest/transport/dispatch/create_dispatch_proxy.d.ts.map +1 -0
- package/dest/transport/dispatch/create_dispatch_proxy.js +56 -0
- package/dest/transport/dispatch/messages.d.ts +52 -0
- package/dest/transport/dispatch/messages.d.ts.map +1 -0
- package/dest/transport/dispatch/messages.js +12 -0
- package/dest/transport/index.d.ts +12 -0
- package/dest/transport/index.d.ts.map +1 -0
- package/dest/transport/index.js +12 -0
- package/dest/transport/interface/connector.d.ts +8 -0
- package/dest/transport/interface/connector.d.ts.map +1 -0
- package/dest/transport/interface/connector.js +2 -0
- package/dest/transport/interface/listener.d.ts +13 -0
- package/dest/transport/interface/listener.d.ts.map +1 -0
- package/dest/transport/interface/listener.js +2 -0
- package/dest/transport/interface/socket.d.ts +13 -0
- package/dest/transport/interface/socket.d.ts.map +1 -0
- package/dest/transport/interface/socket.js +2 -0
- package/dest/transport/interface/transferable.d.ts +68 -0
- package/dest/transport/interface/transferable.d.ts.map +1 -0
- package/dest/transport/interface/transferable.js +63 -0
- package/dest/transport/node/index.d.ts +3 -0
- package/dest/transport/node/index.d.ts.map +1 -0
- package/dest/transport/node/index.js +3 -0
- package/dest/transport/node/node_connector.d.ts +28 -0
- package/dest/transport/node/node_connector.d.ts.map +1 -0
- package/dest/transport/node/node_connector.js +28 -0
- package/dest/transport/node/node_connector_socket.d.ts +42 -0
- package/dest/transport/node/node_connector_socket.d.ts.map +1 -0
- package/dest/transport/node/node_connector_socket.js +48 -0
- package/dest/transport/node/node_listener.d.ts +26 -0
- package/dest/transport/node/node_listener.d.ts.map +1 -0
- package/dest/transport/node/node_listener.js +30 -0
- package/dest/transport/node/node_listener_socket.d.ts +37 -0
- package/dest/transport/node/node_listener_socket.d.ts.map +1 -0
- package/dest/transport/node/node_listener_socket.js +44 -0
- package/dest/transport/transport_client.d.ts +61 -0
- package/dest/transport/transport_client.d.ts.map +1 -0
- package/dest/transport/transport_client.js +94 -0
- package/dest/transport/transport_server.d.ts +56 -0
- package/dest/transport/transport_server.d.ts.map +1 -0
- package/dest/transport/transport_server.js +101 -0
- package/dest/types/index.d.ts +7 -0
- package/dest/types/index.d.ts.map +1 -0
- package/dest/types/index.js +2 -0
- package/dest/url/index.d.ts +9 -0
- package/dest/url/index.d.ts.map +1 -0
- package/dest/url/index.js +66 -0
- package/dest/wasm/empty_wasi_sdk.d.ts +130 -0
- package/dest/wasm/empty_wasi_sdk.d.ts.map +1 -0
- package/dest/wasm/empty_wasi_sdk.js +166 -0
- package/dest/wasm/index.d.ts +2 -0
- package/dest/wasm/index.d.ts.map +1 -0
- package/dest/wasm/index.js +2 -0
- package/dest/wasm/wasm_module.d.ts +135 -0
- package/dest/wasm/wasm_module.d.ts.map +1 -0
- package/dest/wasm/wasm_module.js +205 -0
- package/dest/wasm/wasm_module.test.d.ts +2 -0
- package/dest/wasm/wasm_module.test.d.ts.map +1 -0
- package/dest/wasm/wasm_module.test.js +24 -0
- package/dest/worker/browser/index.d.ts +3 -0
- package/dest/worker/browser/index.d.ts.map +1 -0
- package/dest/worker/browser/index.js +3 -0
- package/dest/worker/browser/start_web_module.d.ts +7 -0
- package/dest/worker/browser/start_web_module.d.ts.map +1 -0
- package/dest/worker/browser/start_web_module.js +22 -0
- package/dest/worker/browser/web_data_store.d.ts +23 -0
- package/dest/worker/browser/web_data_store.d.ts.map +1 -0
- package/dest/worker/browser/web_data_store.js +32 -0
- package/dest/worker/browser/web_worker.d.ts +10 -0
- package/dest/worker/browser/web_worker.d.ts.map +1 -0
- package/dest/worker/browser/web_worker.js +23 -0
- package/dest/worker/data_store.d.ts +20 -0
- package/dest/worker/data_store.d.ts.map +1 -0
- package/dest/worker/data_store.js +2 -0
- package/dest/worker/index.d.ts +3 -0
- package/dest/worker/index.d.ts.map +1 -0
- package/dest/worker/index.js +2 -0
- package/dest/worker/node/index.d.ts +3 -0
- package/dest/worker/node/index.d.ts.map +1 -0
- package/dest/worker/node/index.js +3 -0
- package/dest/worker/node/node_data_store.d.ts +12 -0
- package/dest/worker/node/node_data_store.d.ts.map +1 -0
- package/dest/worker/node/node_data_store.js +21 -0
- package/dest/worker/node/node_worker.d.ts +6 -0
- package/dest/worker/node/node_worker.d.ts.map +1 -0
- package/dest/worker/node/node_worker.js +20 -0
- package/dest/worker/node/start_node_module.d.ts +7 -0
- package/dest/worker/node/start_node_module.d.ts.map +1 -0
- package/dest/worker/node/start_node_module.js +26 -0
- package/dest/worker/wasm_worker.d.ts +9 -0
- package/dest/worker/wasm_worker.d.ts.map +1 -0
- package/dest/worker/wasm_worker.js +2 -0
- package/dest/worker/worker_pool.d.ts +40 -0
- package/dest/worker/worker_pool.d.ts.map +1 -0
- package/dest/worker/worker_pool.js +62 -0
- package/package.json +1 -1
- package/Dockerfile +0 -15
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* InterruptableSleep is a utility class that allows you to create an interruptible sleep function.
|
|
3
|
+
* The sleep function can be interrupted at any time by calling the `interrupt` method, which can
|
|
4
|
+
* also specify whether the sleep should throw an error or just return. This is useful when you need
|
|
5
|
+
* to terminate long-running processes or perform cleanup tasks in response to external events.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* const sleeper = new InterruptableSleep();
|
|
9
|
+
*
|
|
10
|
+
* async function longRunningTask() \{
|
|
11
|
+
* try \{
|
|
12
|
+
* await sleeper.sleep(3000);
|
|
13
|
+
* console.log('Task completed after 3 seconds');
|
|
14
|
+
* \} catch (e) \{
|
|
15
|
+
* console.log('Task was interrupted');
|
|
16
|
+
* \}
|
|
17
|
+
* \}
|
|
18
|
+
*
|
|
19
|
+
* setTimeout(() =\> sleeper.interrupt(true), 1500); // Interrupt the sleep after 1.5 seconds
|
|
20
|
+
*/
|
|
21
|
+
export declare class InterruptableSleep {
|
|
22
|
+
private interruptResolve;
|
|
23
|
+
private interruptPromise;
|
|
24
|
+
private timeouts;
|
|
25
|
+
/**
|
|
26
|
+
* Sleep for a specified amount of time in milliseconds.
|
|
27
|
+
* The sleep function will pause the execution of the current async function
|
|
28
|
+
* for the given time period, allowing other tasks to run before resuming.
|
|
29
|
+
*
|
|
30
|
+
* @param ms - The number of milliseconds to sleep.
|
|
31
|
+
* @returns A Promise that resolves after the specified time has passed.
|
|
32
|
+
*/
|
|
33
|
+
sleep(ms: number): Promise<void>;
|
|
34
|
+
/**
|
|
35
|
+
* Interrupts the current sleep operation and optionally throws an error if specified.
|
|
36
|
+
* By default, when interrupted, the sleep operation will resolve without throwing.
|
|
37
|
+
* If 'sleepShouldThrow' is set to true, the sleep operation will throw an InterruptError instead.
|
|
38
|
+
*
|
|
39
|
+
* @param sleepShouldThrow - A boolean value indicating whether the sleep operation should throw an error when interrupted. Default is false.
|
|
40
|
+
*/
|
|
41
|
+
interrupt(sleepShouldThrow?: boolean): void;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Puts the current execution context to sleep for a specified duration.
|
|
45
|
+
* This simulates a blocking sleep operation by using an asynchronous function and a Promise that resolves after the given duration.
|
|
46
|
+
* The sleep function can be interrupted by the 'interrupt' method of the InterruptableSleep class.
|
|
47
|
+
*
|
|
48
|
+
* @param ms - The duration in milliseconds for which the sleep operation should last.
|
|
49
|
+
* @returns A Promise that resolves after the specified duration, allowing the use of 'await' to pause execution.
|
|
50
|
+
*/
|
|
51
|
+
export declare function sleep(ms: number): Promise<unknown>;
|
|
52
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/sleep/index.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,qBAAa,kBAAkB;IAC7B,OAAO,CAAC,gBAAgB,CAA4C;IACpE,OAAO,CAAC,gBAAgB,CAAsE;IAC9F,OAAO,CAAC,QAAQ,CAAwB;IAExC;;;;;;;OAOG;IACU,KAAK,CAAC,EAAE,EAAE,MAAM;IAY7B;;;;;;OAMG;IACI,SAAS,CAAC,gBAAgB,UAAQ;CAI1C;AAED;;;;;;;GAOG;AACH,wBAAgB,KAAK,CAAC,EAAE,EAAE,MAAM,oBAE/B"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { InterruptError } from '../errors/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* InterruptableSleep is a utility class that allows you to create an interruptible sleep function.
|
|
4
|
+
* The sleep function can be interrupted at any time by calling the `interrupt` method, which can
|
|
5
|
+
* also specify whether the sleep should throw an error or just return. This is useful when you need
|
|
6
|
+
* to terminate long-running processes or perform cleanup tasks in response to external events.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const sleeper = new InterruptableSleep();
|
|
10
|
+
*
|
|
11
|
+
* async function longRunningTask() \{
|
|
12
|
+
* try \{
|
|
13
|
+
* await sleeper.sleep(3000);
|
|
14
|
+
* console.log('Task completed after 3 seconds');
|
|
15
|
+
* \} catch (e) \{
|
|
16
|
+
* console.log('Task was interrupted');
|
|
17
|
+
* \}
|
|
18
|
+
* \}
|
|
19
|
+
*
|
|
20
|
+
* setTimeout(() =\> sleeper.interrupt(true), 1500); // Interrupt the sleep after 1.5 seconds
|
|
21
|
+
*/
|
|
22
|
+
export class InterruptableSleep {
|
|
23
|
+
constructor() {
|
|
24
|
+
this.interruptResolve = () => { };
|
|
25
|
+
this.interruptPromise = new Promise(resolve => (this.interruptResolve = resolve));
|
|
26
|
+
this.timeouts = [];
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Sleep for a specified amount of time in milliseconds.
|
|
30
|
+
* The sleep function will pause the execution of the current async function
|
|
31
|
+
* for the given time period, allowing other tasks to run before resuming.
|
|
32
|
+
*
|
|
33
|
+
* @param ms - The number of milliseconds to sleep.
|
|
34
|
+
* @returns A Promise that resolves after the specified time has passed.
|
|
35
|
+
*/
|
|
36
|
+
async sleep(ms) {
|
|
37
|
+
let timeout;
|
|
38
|
+
const promise = new Promise(resolve => (timeout = setTimeout(() => resolve(false), ms)));
|
|
39
|
+
this.timeouts.push(timeout);
|
|
40
|
+
const shouldThrow = await Promise.race([promise, this.interruptPromise]);
|
|
41
|
+
clearTimeout(timeout);
|
|
42
|
+
this.timeouts.splice(this.timeouts.indexOf(timeout), 1);
|
|
43
|
+
if (shouldThrow) {
|
|
44
|
+
throw new InterruptError('Interrupted.');
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Interrupts the current sleep operation and optionally throws an error if specified.
|
|
49
|
+
* By default, when interrupted, the sleep operation will resolve without throwing.
|
|
50
|
+
* If 'sleepShouldThrow' is set to true, the sleep operation will throw an InterruptError instead.
|
|
51
|
+
*
|
|
52
|
+
* @param sleepShouldThrow - A boolean value indicating whether the sleep operation should throw an error when interrupted. Default is false.
|
|
53
|
+
*/
|
|
54
|
+
interrupt(sleepShouldThrow = false) {
|
|
55
|
+
this.interruptResolve(sleepShouldThrow);
|
|
56
|
+
this.interruptPromise = new Promise(resolve => (this.interruptResolve = resolve));
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Puts the current execution context to sleep for a specified duration.
|
|
61
|
+
* This simulates a blocking sleep operation by using an asynchronous function and a Promise that resolves after the given duration.
|
|
62
|
+
* The sleep function can be interrupted by the 'interrupt' method of the InterruptableSleep class.
|
|
63
|
+
*
|
|
64
|
+
* @param ms - The duration in milliseconds for which the sleep operation should last.
|
|
65
|
+
* @returns A Promise that resolves after the specified duration, allowing the use of 'await' to pause execution.
|
|
66
|
+
*/
|
|
67
|
+
export function sleep(ms) {
|
|
68
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvc2xlZXAvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLGNBQWMsRUFBRSxNQUFNLG9CQUFvQixDQUFDO0FBRXBEOzs7Ozs7Ozs7Ozs7Ozs7Ozs7O0dBbUJHO0FBQ0gsTUFBTSxPQUFPLGtCQUFrQjtJQUEvQjtRQUNVLHFCQUFnQixHQUFtQyxHQUFHLEVBQUUsR0FBRSxDQUFDLENBQUM7UUFDNUQscUJBQWdCLEdBQUcsSUFBSSxPQUFPLENBQVUsT0FBTyxDQUFDLEVBQUUsQ0FBQyxDQUFDLElBQUksQ0FBQyxnQkFBZ0IsR0FBRyxPQUFPLENBQUMsQ0FBQyxDQUFDO1FBQ3RGLGFBQVEsR0FBcUIsRUFBRSxDQUFDO0lBaUMxQyxDQUFDO0lBL0JDOzs7Ozs7O09BT0c7SUFDSSxLQUFLLENBQUMsS0FBSyxDQUFDLEVBQVU7UUFDM0IsSUFBSSxPQUF3QixDQUFDO1FBQzdCLE1BQU0sT0FBTyxHQUFHLElBQUksT0FBTyxDQUFVLE9BQU8sQ0FBQyxFQUFFLENBQUMsQ0FBQyxPQUFPLEdBQUcsVUFBVSxDQUFDLEdBQUcsRUFBRSxDQUFDLE9BQU8sQ0FBQyxLQUFLLENBQUMsRUFBRSxFQUFFLENBQUMsQ0FBQyxDQUFDLENBQUM7UUFDbEcsSUFBSSxDQUFDLFFBQVEsQ0FBQyxJQUFJLENBQUMsT0FBTyxDQUFDLENBQUM7UUFDNUIsTUFBTSxXQUFXLEdBQUcsTUFBTSxPQUFPLENBQUMsSUFBSSxDQUFDLENBQUMsT0FBTyxFQUFFLElBQUksQ0FBQyxnQkFBZ0IsQ0FBQyxDQUFDLENBQUM7UUFDekUsWUFBWSxDQUFDLE9BQU8sQ0FBQyxDQUFDO1FBQ3RCLElBQUksQ0FBQyxRQUFRLENBQUMsTUFBTSxDQUFDLElBQUksQ0FBQyxRQUFRLENBQUMsT0FBTyxDQUFDLE9BQU8sQ0FBQyxFQUFFLENBQUMsQ0FBQyxDQUFDO1FBQ3hELElBQUksV0FBVyxFQUFFO1lBQ2YsTUFBTSxJQUFJLGNBQWMsQ0FBQyxjQUFjLENBQUMsQ0FBQztTQUMxQztJQUNILENBQUM7SUFFRDs7Ozs7O09BTUc7SUFDSSxTQUFTLENBQUMsZ0JBQWdCLEdBQUcsS0FBSztRQUN2QyxJQUFJLENBQUMsZ0JBQWdCLENBQUMsZ0JBQWdCLENBQUMsQ0FBQztRQUN4QyxJQUFJLENBQUMsZ0JBQWdCLEdBQUcsSUFBSSxPQUFPLENBQUMsT0FBTyxDQUFDLEVBQUUsQ0FBQyxDQUFDLElBQUksQ0FBQyxnQkFBZ0IsR0FBRyxPQUFPLENBQUMsQ0FBQyxDQUFDO0lBQ3BGLENBQUM7Q0FDRjtBQUVEOzs7Ozs7O0dBT0c7QUFDSCxNQUFNLFVBQVUsS0FBSyxDQUFDLEVBQVU7SUFDOUIsT0FBTyxJQUFJLE9BQU8sQ0FBQyxPQUFPLENBQUMsRUFBRSxDQUFDLFVBQVUsQ0FBQyxPQUFPLEVBQUUsRUFBRSxDQUFDLENBQUMsQ0FBQztBQUN6RCxDQUFDIn0=
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/timer/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { TimeoutTask } from './timeout.js';
|
|
2
|
+
export { Timer } from './timer.js';
|
|
3
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvdGltZXIvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLFdBQVcsRUFBRSxNQUFNLGNBQWMsQ0FBQztBQUMzQyxPQUFPLEVBQUUsS0FBSyxFQUFFLE1BQU0sWUFBWSxDQUFDIn0=
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TimeoutTask class creates an instance for managing and executing a given asynchronous function with a specified timeout duration.
|
|
3
|
+
* The task will be automatically interrupted if it exceeds the given timeout duration, and will throw a custom error message.
|
|
4
|
+
* Additional information such as execution time can be retrieved using getTime method after the task has been executed.
|
|
5
|
+
*
|
|
6
|
+
* @typeparam T - The return type of the asynchronous function to be executed.
|
|
7
|
+
*/
|
|
8
|
+
export declare class TimeoutTask<T> {
|
|
9
|
+
private fn;
|
|
10
|
+
private timeout;
|
|
11
|
+
private interruptPromise;
|
|
12
|
+
private interrupt;
|
|
13
|
+
private totalTime;
|
|
14
|
+
constructor(fn: () => Promise<T>, timeout?: number, fnName?: string);
|
|
15
|
+
/**
|
|
16
|
+
* Executes the given function with a specified timeout.
|
|
17
|
+
* If the function takes longer than the timeout, it will be interrupted and an error will be thrown.
|
|
18
|
+
* The total execution time of the function will be stored in the totalTime property.
|
|
19
|
+
*
|
|
20
|
+
* @returns The result of the executed function if completed within the timeout.
|
|
21
|
+
* @throws An error with a message indicating the function was interrupted due to exceeding the specified timeout.
|
|
22
|
+
*/
|
|
23
|
+
exec(): Promise<T>;
|
|
24
|
+
/**
|
|
25
|
+
* Returns the interrupt promise associated with the TimeoutTask instance.
|
|
26
|
+
* The interrupt promise is used internally to reject the task when a timeout occurs.
|
|
27
|
+
* This method can be helpful when debugging or tracking the state of the task.
|
|
28
|
+
*
|
|
29
|
+
* @returns The interrupt promise associated with the task.
|
|
30
|
+
*/
|
|
31
|
+
getInterruptPromise(): Promise<any>;
|
|
32
|
+
/**
|
|
33
|
+
* Get the total time spent on the most recent execution of the wrapped function.
|
|
34
|
+
* This method provides the duration from the start to the end of the function execution, whether it completed or timed out.
|
|
35
|
+
*
|
|
36
|
+
* @returns The total time in milliseconds spent on the most recent function execution.
|
|
37
|
+
*/
|
|
38
|
+
getTime(): number;
|
|
39
|
+
}
|
|
40
|
+
export declare const executeTimeout: <T>(fn: () => Promise<T>, timeout?: number, fnName?: string) => Promise<T>;
|
|
41
|
+
//# sourceMappingURL=timeout.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"timeout.d.ts","sourceRoot":"","sources":["../../src/timer/timeout.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,qBAAa,WAAW,CAAC,CAAC;IAKZ,OAAO,CAAC,EAAE;IAAoB,OAAO,CAAC,OAAO;IAJzD,OAAO,CAAC,gBAAgB,CAAgB;IACxC,OAAO,CAAC,SAAS,CAAY;IAC7B,OAAO,CAAC,SAAS,CAAK;gBAEF,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EAAU,OAAO,SAAI,EAAE,MAAM,SAAK;IAM1E;;;;;;;OAOG;IACU,IAAI;IAYjB;;;;;;OAMG;IACI,mBAAmB;IAI1B;;;;;OAKG;IACI,OAAO;CAGf;AAED,eAAO,MAAM,cAAc,4EAG1B,CAAC"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TimeoutTask class creates an instance for managing and executing a given asynchronous function with a specified timeout duration.
|
|
3
|
+
* The task will be automatically interrupted if it exceeds the given timeout duration, and will throw a custom error message.
|
|
4
|
+
* Additional information such as execution time can be retrieved using getTime method after the task has been executed.
|
|
5
|
+
*
|
|
6
|
+
* @typeparam T - The return type of the asynchronous function to be executed.
|
|
7
|
+
*/
|
|
8
|
+
export class TimeoutTask {
|
|
9
|
+
constructor(fn, timeout = 0, fnName = '') {
|
|
10
|
+
this.fn = fn;
|
|
11
|
+
this.timeout = timeout;
|
|
12
|
+
this.interrupt = () => { };
|
|
13
|
+
this.totalTime = 0;
|
|
14
|
+
this.interruptPromise = new Promise((_, reject) => {
|
|
15
|
+
this.interrupt = () => reject(new Error(`Timeout${fnName ? ` running ${fnName}` : ''} after ${timeout}ms.`));
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Executes the given function with a specified timeout.
|
|
20
|
+
* If the function takes longer than the timeout, it will be interrupted and an error will be thrown.
|
|
21
|
+
* The total execution time of the function will be stored in the totalTime property.
|
|
22
|
+
*
|
|
23
|
+
* @returns The result of the executed function if completed within the timeout.
|
|
24
|
+
* @throws An error with a message indicating the function was interrupted due to exceeding the specified timeout.
|
|
25
|
+
*/
|
|
26
|
+
async exec() {
|
|
27
|
+
const interruptTimeout = !this.timeout ? 0 : setTimeout(this.interrupt, this.timeout);
|
|
28
|
+
try {
|
|
29
|
+
const start = Date.now();
|
|
30
|
+
const result = await Promise.race([this.fn(), this.interruptPromise]);
|
|
31
|
+
this.totalTime = Date.now() - start;
|
|
32
|
+
return result;
|
|
33
|
+
}
|
|
34
|
+
finally {
|
|
35
|
+
clearTimeout(interruptTimeout);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Returns the interrupt promise associated with the TimeoutTask instance.
|
|
40
|
+
* The interrupt promise is used internally to reject the task when a timeout occurs.
|
|
41
|
+
* This method can be helpful when debugging or tracking the state of the task.
|
|
42
|
+
*
|
|
43
|
+
* @returns The interrupt promise associated with the task.
|
|
44
|
+
*/
|
|
45
|
+
getInterruptPromise() {
|
|
46
|
+
return this.interruptPromise;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Get the total time spent on the most recent execution of the wrapped function.
|
|
50
|
+
* This method provides the duration from the start to the end of the function execution, whether it completed or timed out.
|
|
51
|
+
*
|
|
52
|
+
* @returns The total time in milliseconds spent on the most recent function execution.
|
|
53
|
+
*/
|
|
54
|
+
getTime() {
|
|
55
|
+
return this.totalTime;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
export const executeTimeout = async (fn, timeout = 0, fnName = '') => {
|
|
59
|
+
const task = new TimeoutTask(fn, timeout, fnName);
|
|
60
|
+
return await task.exec();
|
|
61
|
+
};
|
|
62
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGltZW91dC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy90aW1lci90aW1lb3V0LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBOzs7Ozs7R0FNRztBQUNILE1BQU0sT0FBTyxXQUFXO0lBS3RCLFlBQW9CLEVBQW9CLEVBQVUsVUFBVSxDQUFDLEVBQUUsTUFBTSxHQUFHLEVBQUU7UUFBdEQsT0FBRSxHQUFGLEVBQUUsQ0FBa0I7UUFBVSxZQUFPLEdBQVAsT0FBTyxDQUFJO1FBSHJELGNBQVMsR0FBRyxHQUFHLEVBQUUsR0FBRSxDQUFDLENBQUM7UUFDckIsY0FBUyxHQUFHLENBQUMsQ0FBQztRQUdwQixJQUFJLENBQUMsZ0JBQWdCLEdBQUcsSUFBSSxPQUFPLENBQUksQ0FBQyxDQUFDLEVBQUUsTUFBTSxFQUFFLEVBQUU7WUFDbkQsSUFBSSxDQUFDLFNBQVMsR0FBRyxHQUFHLEVBQUUsQ0FBQyxNQUFNLENBQUMsSUFBSSxLQUFLLENBQUMsVUFBVSxNQUFNLENBQUMsQ0FBQyxDQUFDLFlBQVksTUFBTSxFQUFFLENBQUMsQ0FBQyxDQUFDLEVBQUUsVUFBVSxPQUFPLEtBQUssQ0FBQyxDQUFDLENBQUM7UUFDL0csQ0FBQyxDQUFDLENBQUM7SUFDTCxDQUFDO0lBRUQ7Ozs7Ozs7T0FPRztJQUNJLEtBQUssQ0FBQyxJQUFJO1FBQ2YsTUFBTSxnQkFBZ0IsR0FBRyxDQUFDLElBQUksQ0FBQyxPQUFPLENBQUMsQ0FBQyxDQUFDLENBQUMsQ0FBQyxDQUFDLENBQUMsVUFBVSxDQUFDLElBQUksQ0FBQyxTQUFTLEVBQUUsSUFBSSxDQUFDLE9BQU8sQ0FBQyxDQUFDO1FBQ3RGLElBQUk7WUFDRixNQUFNLEtBQUssR0FBRyxJQUFJLENBQUMsR0FBRyxFQUFFLENBQUM7WUFDekIsTUFBTSxNQUFNLEdBQUcsTUFBTSxPQUFPLENBQUMsSUFBSSxDQUFJLENBQUMsSUFBSSxDQUFDLEVBQUUsRUFBRSxFQUFFLElBQUksQ0FBQyxnQkFBZ0IsQ0FBQyxDQUFDLENBQUM7WUFDekUsSUFBSSxDQUFDLFNBQVMsR0FBRyxJQUFJLENBQUMsR0FBRyxFQUFFLEdBQUcsS0FBSyxDQUFDO1lBQ3BDLE9BQU8sTUFBTSxDQUFDO1NBQ2Y7Z0JBQVM7WUFDUixZQUFZLENBQUMsZ0JBQWdCLENBQUMsQ0FBQztTQUNoQztJQUNILENBQUM7SUFFRDs7Ozs7O09BTUc7SUFDSSxtQkFBbUI7UUFDeEIsT0FBTyxJQUFJLENBQUMsZ0JBQWdCLENBQUM7SUFDL0IsQ0FBQztJQUVEOzs7OztPQUtHO0lBQ0ksT0FBTztRQUNaLE9BQU8sSUFBSSxDQUFDLFNBQVMsQ0FBQztJQUN4QixDQUFDO0NBQ0Y7QUFFRCxNQUFNLENBQUMsTUFBTSxjQUFjLEdBQUcsS0FBSyxFQUFLLEVBQW9CLEVBQUUsT0FBTyxHQUFHLENBQUMsRUFBRSxNQUFNLEdBQUcsRUFBRSxFQUFFLEVBQUU7SUFDeEYsTUFBTSxJQUFJLEdBQUcsSUFBSSxXQUFXLENBQUMsRUFBRSxFQUFFLE9BQU8sRUFBRSxNQUFNLENBQUMsQ0FBQztJQUNsRCxPQUFPLE1BQU0sSUFBSSxDQUFDLElBQUksRUFBRSxDQUFDO0FBQzNCLENBQUMsQ0FBQyJ9
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Timer class to measure time intervals in milliseconds and seconds.
|
|
3
|
+
* Upon instantiation, it stores the current timestamp as the starting point.
|
|
4
|
+
* The 'ms()' method returns the elapsed time in milliseconds,
|
|
5
|
+
* while the 's()' method returns the elapsed time in seconds.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* const timer = new Timer();
|
|
9
|
+
* setTimeout(() =\> \{
|
|
10
|
+
* console.log(`Elapsed time: ${timer.ms()} ms`);
|
|
11
|
+
* \}, 1000);
|
|
12
|
+
*/
|
|
13
|
+
export declare class Timer {
|
|
14
|
+
private start;
|
|
15
|
+
constructor();
|
|
16
|
+
/**
|
|
17
|
+
* Returns the elapsed time in milliseconds since the Timer instance was created.
|
|
18
|
+
* Provides a simple and convenient way to measure the time duration between two events
|
|
19
|
+
* or monitor performance of specific code sections.
|
|
20
|
+
*
|
|
21
|
+
* @returns The elapsed time in milliseconds.
|
|
22
|
+
*/
|
|
23
|
+
ms(): number;
|
|
24
|
+
/**
|
|
25
|
+
* Returns the time elapsed since the Timer instance was created, in seconds.
|
|
26
|
+
* The value is calculated by subtracting the initial start time from the current time
|
|
27
|
+
* and dividing the result by 1000 to convert milliseconds to seconds.
|
|
28
|
+
*
|
|
29
|
+
* @returns The elapsed time in seconds.
|
|
30
|
+
*/
|
|
31
|
+
s(): number;
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=timer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"timer.d.ts","sourceRoot":"","sources":["../../src/timer/timer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,qBAAa,KAAK;IAChB,OAAO,CAAC,KAAK,CAAS;;IAMtB;;;;;;OAMG;IACI,EAAE;IAIT;;;;;;OAMG;IACI,CAAC;CAGT"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Timer class to measure time intervals in milliseconds and seconds.
|
|
3
|
+
* Upon instantiation, it stores the current timestamp as the starting point.
|
|
4
|
+
* The 'ms()' method returns the elapsed time in milliseconds,
|
|
5
|
+
* while the 's()' method returns the elapsed time in seconds.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* const timer = new Timer();
|
|
9
|
+
* setTimeout(() =\> \{
|
|
10
|
+
* console.log(`Elapsed time: ${timer.ms()} ms`);
|
|
11
|
+
* \}, 1000);
|
|
12
|
+
*/
|
|
13
|
+
export class Timer {
|
|
14
|
+
constructor() {
|
|
15
|
+
this.start = new Date().getTime();
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Returns the elapsed time in milliseconds since the Timer instance was created.
|
|
19
|
+
* Provides a simple and convenient way to measure the time duration between two events
|
|
20
|
+
* or monitor performance of specific code sections.
|
|
21
|
+
*
|
|
22
|
+
* @returns The elapsed time in milliseconds.
|
|
23
|
+
*/
|
|
24
|
+
ms() {
|
|
25
|
+
return new Date().getTime() - this.start;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Returns the time elapsed since the Timer instance was created, in seconds.
|
|
29
|
+
* The value is calculated by subtracting the initial start time from the current time
|
|
30
|
+
* and dividing the result by 1000 to convert milliseconds to seconds.
|
|
31
|
+
*
|
|
32
|
+
* @returns The elapsed time in seconds.
|
|
33
|
+
*/
|
|
34
|
+
s() {
|
|
35
|
+
return (new Date().getTime() - this.start) / 1000;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGltZXIuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvdGltZXIvdGltZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUE7Ozs7Ozs7Ozs7O0dBV0c7QUFDSCxNQUFNLE9BQU8sS0FBSztJQUdoQjtRQUNFLElBQUksQ0FBQyxLQUFLLEdBQUcsSUFBSSxJQUFJLEVBQUUsQ0FBQyxPQUFPLEVBQUUsQ0FBQztJQUNwQyxDQUFDO0lBRUQ7Ozs7OztPQU1HO0lBQ0ksRUFBRTtRQUNQLE9BQU8sSUFBSSxJQUFJLEVBQUUsQ0FBQyxPQUFPLEVBQUUsR0FBRyxJQUFJLENBQUMsS0FBSyxDQUFDO0lBQzNDLENBQUM7SUFFRDs7Ozs7O09BTUc7SUFDSSxDQUFDO1FBQ04sT0FBTyxDQUFDLElBQUksSUFBSSxFQUFFLENBQUMsT0FBTyxFQUFFLEdBQUcsSUFBSSxDQUFDLEtBQUssQ0FBQyxHQUFHLElBQUksQ0FBQztJQUNwRCxDQUFDO0NBQ0YifQ==
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/transport/browser/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAC;AACtC,cAAc,sBAAsB,CAAC;AACrC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,6BAA6B,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export * from './worker_connector.js';
|
|
2
|
+
export * from './worker_listener.js';
|
|
3
|
+
export * from './shared_worker_connector.js';
|
|
4
|
+
export * from './shared_worker_listener.js';
|
|
5
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvdHJhbnNwb3J0L2Jyb3dzZXIvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsY0FBYyx1QkFBdUIsQ0FBQztBQUN0QyxjQUFjLHNCQUFzQixDQUFDO0FBQ3JDLGNBQWMsOEJBQThCLENBQUM7QUFDN0MsY0FBYyw2QkFBNkIsQ0FBQyJ9
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Socket } from '../interface/socket.js';
|
|
2
|
+
/**
|
|
3
|
+
* An implementation of a TransportSocket using MessagePorts.
|
|
4
|
+
*/
|
|
5
|
+
export declare class MessagePortSocket implements Socket {
|
|
6
|
+
private port;
|
|
7
|
+
constructor(port: MessagePort);
|
|
8
|
+
/**
|
|
9
|
+
* Send a message to the connected MessagePort, optionally transferring ownership of certain objects.
|
|
10
|
+
* The 'msg' parameter can be any structured data type and will be sent to the other end of the MessagePort.
|
|
11
|
+
* The optional 'transfer' parameter is an array of Transferable objects whose ownership will be transferred,
|
|
12
|
+
* making them inaccessible on the sending side. This can improve performance for large data transfers.
|
|
13
|
+
*
|
|
14
|
+
* @param msg - The message to be sent through the MessagePort.
|
|
15
|
+
* @param transfer - An optional array of Transferable objects to transfer ownership.
|
|
16
|
+
* @returns A Promise that resolves when the message has been sent.
|
|
17
|
+
*/
|
|
18
|
+
send(msg: any, transfer?: Transferable[]): Promise<void>;
|
|
19
|
+
/**
|
|
20
|
+
* Register a callback function to handle incoming messages from the MessagePort.
|
|
21
|
+
* The provided callback will be invoked with the message data whenever a new message arrives.
|
|
22
|
+
* Note that only one callback can be registered at a time. Subsequent calls to this method
|
|
23
|
+
* will overwrite the previously registered callback.
|
|
24
|
+
*
|
|
25
|
+
* @param cb - The callback function to handle incoming messages.
|
|
26
|
+
*/
|
|
27
|
+
registerHandler(cb: (msg: any) => any): void;
|
|
28
|
+
/**
|
|
29
|
+
* Close the MessagePort, unregister the message handler, and send an undefined message.
|
|
30
|
+
* The 'close' function is useful for gracefully shutting down a connection between two
|
|
31
|
+
* endpoints by sending an undefined message as an indication of disconnection before
|
|
32
|
+
* closing the port. After calling this method, the MessagePortSocket instance should not
|
|
33
|
+
* be used again.
|
|
34
|
+
*/
|
|
35
|
+
close(): void;
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=message_port_socket.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"message_port_socket.d.ts","sourceRoot":"","sources":["../../../src/transport/browser/message_port_socket.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAC;AAEhD;;GAEG;AACH,qBAAa,iBAAkB,YAAW,MAAM;IAClC,OAAO,CAAC,IAAI;gBAAJ,IAAI,EAAE,WAAW;IAErC;;;;;;;;;OASG;IACH,IAAI,CAAC,GAAG,EAAE,GAAG,EAAE,QAAQ,GAAE,YAAY,EAAO,GAAG,OAAO,CAAC,IAAI,CAAC;IAK5D;;;;;;;OAOG;IACH,eAAe,CAAC,EAAE,EAAE,CAAC,GAAG,EAAE,GAAG,KAAK,GAAG,GAAG,IAAI;IAI5C;;;;;;OAMG;IACH,KAAK;CAKN"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An implementation of a TransportSocket using MessagePorts.
|
|
3
|
+
*/
|
|
4
|
+
export class MessagePortSocket {
|
|
5
|
+
constructor(port) {
|
|
6
|
+
this.port = port;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Send a message to the connected MessagePort, optionally transferring ownership of certain objects.
|
|
10
|
+
* The 'msg' parameter can be any structured data type and will be sent to the other end of the MessagePort.
|
|
11
|
+
* The optional 'transfer' parameter is an array of Transferable objects whose ownership will be transferred,
|
|
12
|
+
* making them inaccessible on the sending side. This can improve performance for large data transfers.
|
|
13
|
+
*
|
|
14
|
+
* @param msg - The message to be sent through the MessagePort.
|
|
15
|
+
* @param transfer - An optional array of Transferable objects to transfer ownership.
|
|
16
|
+
* @returns A Promise that resolves when the message has been sent.
|
|
17
|
+
*/
|
|
18
|
+
send(msg, transfer = []) {
|
|
19
|
+
this.port.postMessage(msg, transfer);
|
|
20
|
+
return Promise.resolve();
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Register a callback function to handle incoming messages from the MessagePort.
|
|
24
|
+
* The provided callback will be invoked with the message data whenever a new message arrives.
|
|
25
|
+
* Note that only one callback can be registered at a time. Subsequent calls to this method
|
|
26
|
+
* will overwrite the previously registered callback.
|
|
27
|
+
*
|
|
28
|
+
* @param cb - The callback function to handle incoming messages.
|
|
29
|
+
*/
|
|
30
|
+
registerHandler(cb) {
|
|
31
|
+
this.port.onmessage = event => cb(event.data);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Close the MessagePort, unregister the message handler, and send an undefined message.
|
|
35
|
+
* The 'close' function is useful for gracefully shutting down a connection between two
|
|
36
|
+
* endpoints by sending an undefined message as an indication of disconnection before
|
|
37
|
+
* closing the port. After calling this method, the MessagePortSocket instance should not
|
|
38
|
+
* be used again.
|
|
39
|
+
*/
|
|
40
|
+
close() {
|
|
41
|
+
void this.send(undefined);
|
|
42
|
+
this.port.onmessage = null;
|
|
43
|
+
this.port.close();
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibWVzc2FnZV9wb3J0X3NvY2tldC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uL3NyYy90cmFuc3BvcnQvYnJvd3Nlci9tZXNzYWdlX3BvcnRfc29ja2V0LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUVBOztHQUVHO0FBQ0gsTUFBTSxPQUFPLGlCQUFpQjtJQUM1QixZQUFvQixJQUFpQjtRQUFqQixTQUFJLEdBQUosSUFBSSxDQUFhO0lBQUcsQ0FBQztJQUV6Qzs7Ozs7Ozs7O09BU0c7SUFDSCxJQUFJLENBQUMsR0FBUSxFQUFFLFdBQTJCLEVBQUU7UUFDMUMsSUFBSSxDQUFDLElBQUksQ0FBQyxXQUFXLENBQUMsR0FBRyxFQUFFLFFBQVEsQ0FBQyxDQUFDO1FBQ3JDLE9BQU8sT0FBTyxDQUFDLE9BQU8sRUFBRSxDQUFDO0lBQzNCLENBQUM7SUFFRDs7Ozs7OztPQU9HO0lBQ0gsZUFBZSxDQUFDLEVBQXFCO1FBQ25DLElBQUksQ0FBQyxJQUFJLENBQUMsU0FBUyxHQUFHLEtBQUssQ0FBQyxFQUFFLENBQUMsRUFBRSxDQUFDLEtBQUssQ0FBQyxJQUFJLENBQUMsQ0FBQztJQUNoRCxDQUFDO0lBRUQ7Ozs7OztPQU1HO0lBQ0gsS0FBSztRQUNILEtBQUssSUFBSSxDQUFDLElBQUksQ0FBQyxTQUFTLENBQUMsQ0FBQztRQUMxQixJQUFJLENBQUMsSUFBSSxDQUFDLFNBQVMsR0FBRyxJQUFJLENBQUM7UUFDM0IsSUFBSSxDQUFDLElBQUksQ0FBQyxLQUFLLEVBQUUsQ0FBQztJQUNwQixDQUFDO0NBQ0YifQ==
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Connector } from '../interface/connector.js';
|
|
2
|
+
import { MessagePortSocket } from './message_port_socket.js';
|
|
3
|
+
/**
|
|
4
|
+
* SharedWorkerConnector is an implementation of the Connector interface, specifically for SharedWorkers.
|
|
5
|
+
* It enables the creation of MessagePortSockets that communicate with a shared worker and allow
|
|
6
|
+
* multiple scripts to communicate with the worker using the same connection.
|
|
7
|
+
*/
|
|
8
|
+
export declare class SharedWorkerConnector implements Connector {
|
|
9
|
+
private worker;
|
|
10
|
+
constructor(worker: SharedWorker);
|
|
11
|
+
/**
|
|
12
|
+
* Creates a new MessagePortSocket instance using the SharedWorker's port.
|
|
13
|
+
* This method allows for easy creation of sockets to communicate with the SharedWorker.
|
|
14
|
+
*
|
|
15
|
+
* @returns A Promise that resolves to a new MessagePortSocket instance.
|
|
16
|
+
*/
|
|
17
|
+
createSocket(): Promise<MessagePortSocket>;
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=shared_worker_connector.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shared_worker_connector.d.ts","sourceRoot":"","sources":["../../../src/transport/browser/shared_worker_connector.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AACtD,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAE7D;;;;GAIG;AACH,qBAAa,qBAAsB,YAAW,SAAS;IACzC,OAAO,CAAC,MAAM;gBAAN,MAAM,EAAE,YAAY;IAExC;;;;;OAKG;IACH,YAAY;CAGb"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { MessagePortSocket } from './message_port_socket.js';
|
|
2
|
+
/**
|
|
3
|
+
* SharedWorkerConnector is an implementation of the Connector interface, specifically for SharedWorkers.
|
|
4
|
+
* It enables the creation of MessagePortSockets that communicate with a shared worker and allow
|
|
5
|
+
* multiple scripts to communicate with the worker using the same connection.
|
|
6
|
+
*/
|
|
7
|
+
export class SharedWorkerConnector {
|
|
8
|
+
constructor(worker) {
|
|
9
|
+
this.worker = worker;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Creates a new MessagePortSocket instance using the SharedWorker's port.
|
|
13
|
+
* This method allows for easy creation of sockets to communicate with the SharedWorker.
|
|
14
|
+
*
|
|
15
|
+
* @returns A Promise that resolves to a new MessagePortSocket instance.
|
|
16
|
+
*/
|
|
17
|
+
createSocket() {
|
|
18
|
+
return Promise.resolve(new MessagePortSocket(this.worker.port));
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic2hhcmVkX3dvcmtlcl9jb25uZWN0b3IuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvdHJhbnNwb3J0L2Jyb3dzZXIvc2hhcmVkX3dvcmtlcl9jb25uZWN0b3IudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQ0EsT0FBTyxFQUFFLGlCQUFpQixFQUFFLE1BQU0sMEJBQTBCLENBQUM7QUFFN0Q7Ozs7R0FJRztBQUNILE1BQU0sT0FBTyxxQkFBcUI7SUFDaEMsWUFBb0IsTUFBb0I7UUFBcEIsV0FBTSxHQUFOLE1BQU0sQ0FBYztJQUFHLENBQUM7SUFFNUM7Ozs7O09BS0c7SUFDSCxZQUFZO1FBQ1YsT0FBTyxPQUFPLENBQUMsT0FBTyxDQUFDLElBQUksaUJBQWlCLENBQUMsSUFBSSxDQUFDLE1BQU0sQ0FBQyxJQUFJLENBQUMsQ0FBQyxDQUFDO0lBQ2xFLENBQUM7Q0FDRiJ9
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
import EventEmitter from 'events';
|
|
3
|
+
import { Listener } from '../interface/listener.js';
|
|
4
|
+
/**
|
|
5
|
+
* Represents the global scope of a Shared Worker.
|
|
6
|
+
* Provides functionality to handle incoming connections and manage communication with other scripts
|
|
7
|
+
* running in a shared context, enabling concurrent access and efficient resource sharing among those scripts.
|
|
8
|
+
*/
|
|
9
|
+
declare interface SharedWorkerGlobalScope {
|
|
10
|
+
/**
|
|
11
|
+
* Event handler for new connections to the Shared Worker.
|
|
12
|
+
*/
|
|
13
|
+
onconnect: (...args: any) => any;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* SharedWorkerListener is an extension of the EventEmitter class that implements the Listener interface.
|
|
17
|
+
* It provides functionality to handle incoming messages from a shared worker and emit events for new sockets
|
|
18
|
+
* created in response to these incoming connections. This class is meant to be used in the context of managing
|
|
19
|
+
* MessagePort connections within the SharedWorkerGlobalScope.
|
|
20
|
+
*/
|
|
21
|
+
export declare class SharedWorkerListener extends EventEmitter implements Listener {
|
|
22
|
+
private worker;
|
|
23
|
+
constructor(worker: SharedWorkerGlobalScope);
|
|
24
|
+
/**
|
|
25
|
+
* Initializes the shared worker listener by assigning the 'handleMessageEvent' method as the event handler
|
|
26
|
+
* for the 'onconnect' event of the SharedWorkerGlobalScope. The 'handleMessageEvent' function will be called
|
|
27
|
+
* whenever a new connection is established with the shared worker.
|
|
28
|
+
*/
|
|
29
|
+
open(): void;
|
|
30
|
+
/**
|
|
31
|
+
* Closes the SharedWorkerListener by detaching the 'onconnect' event handler.
|
|
32
|
+
* This stops the listener from emitting new sockets on incoming connections.
|
|
33
|
+
*/
|
|
34
|
+
close(): void;
|
|
35
|
+
private handleMessageEvent;
|
|
36
|
+
}
|
|
37
|
+
export {};
|
|
38
|
+
//# sourceMappingURL=shared_worker_listener.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shared_worker_listener.d.ts","sourceRoot":"","sources":["../../../src/transport/browser/shared_worker_listener.ts"],"names":[],"mappings":";AAAA,OAAO,YAAY,MAAM,QAAQ,CAAC;AAElC,OAAO,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AAGpD;;;;GAIG;AACH,OAAO,WAAW,uBAAuB;IACvC;;OAEG;IACH,SAAS,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,KAAK,GAAG,CAAC;CAClC;AAED;;;;;GAKG;AACH,qBAAa,oBAAqB,SAAQ,YAAa,YAAW,QAAQ;IAC5D,OAAO,CAAC,MAAM;gBAAN,MAAM,EAAE,uBAAuB;IAInD;;;;OAIG;IACH,IAAI;IAIJ;;;OAGG;IACH,KAAK;IAIL,OAAO,CAAC,kBAAkB,CAMxB;CACH"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import EventEmitter from 'events';
|
|
2
|
+
import { MessagePortSocket } from './message_port_socket.js';
|
|
3
|
+
/**
|
|
4
|
+
* SharedWorkerListener is an extension of the EventEmitter class that implements the Listener interface.
|
|
5
|
+
* It provides functionality to handle incoming messages from a shared worker and emit events for new sockets
|
|
6
|
+
* created in response to these incoming connections. This class is meant to be used in the context of managing
|
|
7
|
+
* MessagePort connections within the SharedWorkerGlobalScope.
|
|
8
|
+
*/
|
|
9
|
+
export class SharedWorkerListener extends EventEmitter {
|
|
10
|
+
constructor(worker) {
|
|
11
|
+
super();
|
|
12
|
+
this.worker = worker;
|
|
13
|
+
this.handleMessageEvent = (event) => {
|
|
14
|
+
const [port] = event.ports;
|
|
15
|
+
if (!port) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
this.emit('new_socket', new MessagePortSocket(port));
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Initializes the shared worker listener by assigning the 'handleMessageEvent' method as the event handler
|
|
23
|
+
* for the 'onconnect' event of the SharedWorkerGlobalScope. The 'handleMessageEvent' function will be called
|
|
24
|
+
* whenever a new connection is established with the shared worker.
|
|
25
|
+
*/
|
|
26
|
+
open() {
|
|
27
|
+
this.worker.onconnect = this.handleMessageEvent;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Closes the SharedWorkerListener by detaching the 'onconnect' event handler.
|
|
31
|
+
* This stops the listener from emitting new sockets on incoming connections.
|
|
32
|
+
*/
|
|
33
|
+
close() {
|
|
34
|
+
this.worker.onconnect = () => { };
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic2hhcmVkX3dvcmtlcl9saXN0ZW5lci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uL3NyYy90cmFuc3BvcnQvYnJvd3Nlci9zaGFyZWRfd29ya2VyX2xpc3RlbmVyLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sWUFBWSxNQUFNLFFBQVEsQ0FBQztBQUdsQyxPQUFPLEVBQUUsaUJBQWlCLEVBQUUsTUFBTSwwQkFBMEIsQ0FBQztBQWM3RDs7Ozs7R0FLRztBQUNILE1BQU0sT0FBTyxvQkFBcUIsU0FBUSxZQUFZO0lBQ3BELFlBQW9CLE1BQStCO1FBQ2pELEtBQUssRUFBRSxDQUFDO1FBRFUsV0FBTSxHQUFOLE1BQU0sQ0FBeUI7UUFxQjNDLHVCQUFrQixHQUFHLENBQUMsS0FBbUIsRUFBRSxFQUFFO1lBQ25ELE1BQU0sQ0FBQyxJQUFJLENBQUMsR0FBRyxLQUFLLENBQUMsS0FBSyxDQUFDO1lBQzNCLElBQUksQ0FBQyxJQUFJLEVBQUU7Z0JBQ1QsT0FBTzthQUNSO1lBQ0QsSUFBSSxDQUFDLElBQUksQ0FBQyxZQUFZLEVBQUUsSUFBSSxpQkFBaUIsQ0FBQyxJQUFJLENBQUMsQ0FBQyxDQUFDO1FBQ3ZELENBQUMsQ0FBQztJQXpCRixDQUFDO0lBRUQ7Ozs7T0FJRztJQUNILElBQUk7UUFDRixJQUFJLENBQUMsTUFBTSxDQUFDLFNBQVMsR0FBRyxJQUFJLENBQUMsa0JBQWtCLENBQUM7SUFDbEQsQ0FBQztJQUVEOzs7T0FHRztJQUNILEtBQUs7UUFDSCxJQUFJLENBQUMsTUFBTSxDQUFDLFNBQVMsR0FBRyxHQUFHLEVBQUUsR0FBRSxDQUFDLENBQUM7SUFDbkMsQ0FBQztDQVNGIn0=
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Connector } from '../interface/connector.js';
|
|
2
|
+
import { MessagePortSocket } from './message_port_socket.js';
|
|
3
|
+
/**
|
|
4
|
+
* WorkerConnector is a class implementing the Connector interface for creating communication sockets
|
|
5
|
+
* with Web Workers. It allows to establish a connection with the worker and create MessagePortSockets
|
|
6
|
+
* using MessageChannels, enabling seamless communication between the main thread and worker threads.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const worker = new Worker('./myWorker.js');
|
|
10
|
+
* const connector = new WorkerConnector(worker);
|
|
11
|
+
* const socket = await connector.createSocket();
|
|
12
|
+
* socket.send('Hello, worker!');
|
|
13
|
+
*/
|
|
14
|
+
export declare class WorkerConnector implements Connector {
|
|
15
|
+
private worker;
|
|
16
|
+
constructor(worker: Worker);
|
|
17
|
+
/**
|
|
18
|
+
* Creates a new MessagePortSocket instance by establishing a connection between the Worker and the main thread.
|
|
19
|
+
* A MessageChannel is created, and one of its ports is sent to the Worker using postMessage.
|
|
20
|
+
* The other port is used to create a new MessagePortSocket which is then returned as a Promise.
|
|
21
|
+
*
|
|
22
|
+
* @returns A Promise that resolves to a new MessagePortSocket instance.
|
|
23
|
+
*/
|
|
24
|
+
createSocket(): Promise<MessagePortSocket>;
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=worker_connector.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"worker_connector.d.ts","sourceRoot":"","sources":["../../../src/transport/browser/worker_connector.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AACtD,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAE7D;;;;;;;;;;GAUG;AACH,qBAAa,eAAgB,YAAW,SAAS;IACnC,OAAO,CAAC,MAAM;gBAAN,MAAM,EAAE,MAAM;IAElC;;;;;;OAMG;IACH,YAAY;CAKb"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { MessagePortSocket } from './message_port_socket.js';
|
|
2
|
+
/**
|
|
3
|
+
* WorkerConnector is a class implementing the Connector interface for creating communication sockets
|
|
4
|
+
* with Web Workers. It allows to establish a connection with the worker and create MessagePortSockets
|
|
5
|
+
* using MessageChannels, enabling seamless communication between the main thread and worker threads.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* const worker = new Worker('./myWorker.js');
|
|
9
|
+
* const connector = new WorkerConnector(worker);
|
|
10
|
+
* const socket = await connector.createSocket();
|
|
11
|
+
* socket.send('Hello, worker!');
|
|
12
|
+
*/
|
|
13
|
+
export class WorkerConnector {
|
|
14
|
+
constructor(worker) {
|
|
15
|
+
this.worker = worker;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Creates a new MessagePortSocket instance by establishing a connection between the Worker and the main thread.
|
|
19
|
+
* A MessageChannel is created, and one of its ports is sent to the Worker using postMessage.
|
|
20
|
+
* The other port is used to create a new MessagePortSocket which is then returned as a Promise.
|
|
21
|
+
*
|
|
22
|
+
* @returns A Promise that resolves to a new MessagePortSocket instance.
|
|
23
|
+
*/
|
|
24
|
+
createSocket() {
|
|
25
|
+
const channel = new MessageChannel();
|
|
26
|
+
this.worker.postMessage('', [channel.port2]);
|
|
27
|
+
return Promise.resolve(new MessagePortSocket(channel.port1));
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoid29ya2VyX2Nvbm5lY3Rvci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uL3NyYy90cmFuc3BvcnQvYnJvd3Nlci93b3JrZXJfY29ubmVjdG9yLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUNBLE9BQU8sRUFBRSxpQkFBaUIsRUFBRSxNQUFNLDBCQUEwQixDQUFDO0FBRTdEOzs7Ozs7Ozs7O0dBVUc7QUFDSCxNQUFNLE9BQU8sZUFBZTtJQUMxQixZQUFvQixNQUFjO1FBQWQsV0FBTSxHQUFOLE1BQU0sQ0FBUTtJQUFHLENBQUM7SUFFdEM7Ozs7OztPQU1HO0lBQ0gsWUFBWTtRQUNWLE1BQU0sT0FBTyxHQUFHLElBQUksY0FBYyxFQUFFLENBQUM7UUFDckMsSUFBSSxDQUFDLE1BQU0sQ0FBQyxXQUFXLENBQUMsRUFBRSxFQUFFLENBQUMsT0FBTyxDQUFDLEtBQUssQ0FBQyxDQUFDLENBQUM7UUFDN0MsT0FBTyxPQUFPLENBQUMsT0FBTyxDQUFDLElBQUksaUJBQWlCLENBQUMsT0FBTyxDQUFDLEtBQUssQ0FBQyxDQUFDLENBQUM7SUFDL0QsQ0FBQztDQUNGIn0=
|