From 4b3fe54456a97b4710ee83aef061dc399278ae01 Mon Sep 17 00:00:00 2001 From: flakey5 <73616808+flakey5@users.noreply.github.com> Date: Fri, 29 Jul 2022 11:31:24 -0700 Subject: [PATCH] lib: refactor transferable AbortSignal Co-authored-by: James M Snell --- doc/api/util.md | 31 +++++++++++++ lib/internal/abort_controller.js | 50 +++++++++++++++++++-- lib/internal/worker/js_transferable.js | 2 + lib/util.js | 13 ++++++ test/parallel/test-abortsignal-cloneable.js | 11 +++-- 5 files changed, 100 insertions(+), 7 deletions(-) diff --git a/doc/api/util.md b/doc/api/util.md index 9b9c01a4cc2906..fecd6d341f3ce6 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -1593,6 +1593,37 @@ Returns the `string` after replacing any surrogate code points (or equivalently, any unpaired surrogate code units) with the Unicode "replacement character" U+FFFD. +## `util.transferableAbortController()` + + + +> Stability: 1 - Experimental + +Creates and returns an {AbortController} instance whose {AbortSignal} is marked +as transferable and can be used with `structuredClone()` or `postMessage()`. + +## `util.transferableAbortSignal(signal)` + + + +> Stability: 1 - Experimental + +* `signal` {AbortSignal} +* Returns: {AbortSignal} + +Marks the given {AbortSignal} as transferable so that it can be used with +`structuredClone()` and `postMessage()`. + +```js +const signal = transferableAbortSignal(AbortSignal.timeout(100)); +const channel = new MessageChannel(); +channel.port2.postMessage(signal, [signal]); +``` + ## `util.types`