[]previous and next page

runInScope

Runs fn with the given owner and listener in effect, and restores both afterward, even when fn throws.

Runs fn with the given owner and listener in effect, and restores both afterward, even when fn throws. The low-level form of runWithOwner and untrack; unlike runWithOwner it does not check whether the owner is disposed.

Import#

ts
import { runInScope } from "@shadow-js/core/extend";

Signature#

ts
function runInScope<T>(
  scope: { owner?: Owner | null; listener?: Computation | null },
  fn: () => T,
): T

Parameters#

Name Description
scope owner and listener to install. For each, undefined (or absent) keeps the current one and null clears it. Getters are evaluated once; a failed getter restores the original scope.
fn The function to run.

Returns#

Whatever fn returns. An error thrown by fn propagates after the registers are restored.

Examples#

ts
import { onCleanup } from "@shadow-js/core";
import { createOwner, disposeOwner, runInScope } from "@shadow-js/core/extend";

const owner = createOwner(null);
runInScope({ owner, listener: null }, () => {
  onCleanup(() => console.log("released"));
});
disposeOwner(owner); // logs "released"