Playwright.AbortSignal (Playwright v1.65.0-rc.3)

Copy Markdown View Source

The DOM AbortSignal of the signal: options, from a Playwright.AbortController:

controller = Playwright.AbortController.new()
task = Task.async(fn -> Playwright.Locator.click(button, signal: Playwright.AbortController.signal(controller)) end)
Playwright.AbortController.abort(controller, "user cancelled")
{:error, %Playwright.AbortError{cause: "user cancelled"}} = Task.await(task)

Runtime support (not ported from upstream): the part of the DOM class that Playwright uses (aborted, reason, throwIfAborted, abort listeners). A signal is global, as in JS: it belongs to no driver and works with calls on any. Its state lives in a public ETS table of the application, so reads are lock-free from any process, and any process aborts.

Listeners run synchronously in the aborting process (as dispatchEvent runs them), so they must not block: the client's listeners only send messages. A listener added after the abort never runs (as in the DOM).

Deviation: a signal's rows (its reason, its listeners) are freed when the process that created it exits; JS frees them with the signal.

Summary

Functions

signal.aborted: whether the controller aborted.

signal.addEventListener('abort', listener, options): listener (a 0-arity function) runs in the aborting process when the signal aborts (see the moduledoc). A listener is added once (equal functions are the same listener); once: true is implied, a signal aborts once.

signal.reason: the reason given to Playwright.AbortController.abort/2, nil while not aborted.

signal.removeEventListener('abort', listener).

signal.throwIfAborted(): raises the reason when aborted, if it is an exception, else an AbortError with the reason as its cause (JS throws any value).

Types

t()

@type t() :: %Playwright.AbortSignal{id: term()}

An abort signal (Playwright.AbortController.signal/1).

Functions

aborted(abort_signal)

@spec aborted(t()) :: boolean()

signal.aborted: whether the controller aborted.

add_event_listener(this, type, listener, options \\ %{})

@spec add_event_listener(t(), :abort | String.t(), (-> term()), map() | keyword()) ::
  :ok

signal.addEventListener('abort', listener, options): listener (a 0-arity function) runs in the aborting process when the signal aborts (see the moduledoc). A listener is added once (equal functions are the same listener); once: true is implied, a signal aborts once.

reason(abort_signal)

@spec reason(t()) :: term()

signal.reason: the reason given to Playwright.AbortController.abort/2, nil while not aborted.

remove_event_listener(abort_signal, type, listener)

@spec remove_event_listener(t(), :abort | String.t(), (-> term())) :: :ok

signal.removeEventListener('abort', listener).

throw_if_aborted(this)

@spec throw_if_aborted(t()) :: :ok

signal.throwIfAborted(): raises the reason when aborted, if it is an exception, else an AbortError with the reason as its cause (JS throws any value).