[]previous and next page

runWithOwner

Runs fn with owner as the current owner, untracked.

Runs fn with owner as the current owner, untracked. Use it to restore ownership after an await or inside callbacks that outlive the synchronous run, so that effects, cleanups and context reads inside fn attach to that owner. A disposed owner is ignored: fn then runs without an owner, and development builds warn RUN_WITH_DISPOSED_OWNER.

Import#

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

Signature#

ts
function runWithOwner<T>(owner: Owner | null, fn: () => T): T

Parameters#

Name Description
owner The owner to make current, usually captured with getOwner. null runs fn without an owner.
fn The function to run.

Returns#

Whatever fn returns. An error thrown by fn propagates unchanged.

Examples#

ts
import { createContext, getOwner, runWithOwner, useContext } from "@shadow-js/core";

const Locale = createContext("en");

function Clock() {
  const owner = getOwner();
  queueMicrotask(() => {
    const locale = runWithOwner(owner, () => useContext(Locale));
    console.log(new Date().toLocaleTimeString(locale));
  });
  return null;
}