Realms
Example
A realm holds the observables it makes, with their updates and
watchers. The chat makes one, app, in module code.
import { Realm } from "@frondruntime/sorus";
// The chat's realm, made once in module code.
const app = Realm.make({
name: "app",
// An error with no caller, such as a throw in a watch's effect, lands here.
onError: (error) => console.error("chat:", error),
});A screen disposes its realm when it closes.
import { Box, Realm, Source } from "@frondruntime/sorus";
// A screen's realm, disposed when the screen closes.
const screen = Realm.make({ name: "screen" });
const $unread = Box.make(screen, 3, { name: "unread" });
const closeScreen = () => {
Realm.status(screen); // "open"
Realm.dispose(screen);
Realm.status(screen); // "disposed"
Source.peek($unread); // 3: reads return the last values
// Realm.update(screen, ...) now throws sorus/realm-disposed.
Realm.dispose(screen); // a second call does nothing
};Rules
- There is no default realm: every call that makes an observable or opens an update takes the realm first, so a request or a test with its own realm shares no update, watcher, or error.
- A realm’s
readandwritetake only what that realm made: another realm’s observable throwssorus/foreign-realm. onErrorreceives the errors no caller can catch, from a watch’s effect, an autorun, or a hook, because the write that caused them has landed. It defaults to the host’sreportError.- Dispose a realm when its screen, request, or program ends, from an event
handler or a teardown: inside an update or a watcher,
Realm.disposethrowssorus/dispose-not-at-rest.
Answers
| Call | Answers |
|---|---|
Realm.make({ onError?, name? }) |
A realm. name labels it in Source.inspect and in errors, and is never compared. |
Realm.status(realm) |
"open", "disposing" while Realm.dispose runs, or "disposed". |
Realm.dispose(realm) |
void; also realm[Symbol.dispose](), and a second call does nothing. Ends every autorun, row listener, open stage, and hook; after it, reads give the last values and an update or a new observer throws sorus/realm-disposed. |
With Effect
Realm.scoped(options?) makes a realm and disposes it when its scope closes:
see Programs.
See also
- Start: the app’s realm, boxes, and a computed value.
- Reacting: where reads are tracked, and
Realm.untracked. - Lifecycles: what disposal ends, and a realm in React.
- Testing:
testRealm, one realm per test. - Error messages: every error named here.