Mount unstable
Mounts are the work an element starts with its host node, such as drawing on a canvas or measuring a box.
A mount is a definition: Mount.make(name, { args }, run) takes a name, an
args Schema and a body, and returns a factory. A view calls the factory
with the args and gives the result to an element's onMount. The runtime
runs the body once the element is committed, and a test names the mount by
its definition without running anything.
The body gets the host node, the args and a Stream of the args, and ends
with its own result. The view maps that result to a message with
Mount.map. When there is nothing to report, return Component.noop.
It sends no message, but the log still records when the body starts and
ends. Use Mount.makeStream for a mount that reports as it goes, such as
a widget that sends events. Its body is a Stream of results.
Example A mount that follows its args
import { Component, Mount } from "effect/ui"import { Effect, Schema, Stream } from "effect"
declare const draw: (canvas: HTMLCanvasElement, points: ReadonlyArray<number>) => void
const DrawChart = Mount.make( "DrawChart", { args: { points: Schema.Array(Schema.Number) } }, (canvas: HTMLCanvasElement, _args, changes) => Stream.runForEach(changes, ({ points }) => Effect.sync(() => draw(canvas, points))).pipe( Effect.as(Component.noop) ))
// In a view: `h.canvas.onMount(DrawChart({ points: model.points }))`Constructors
Defines a mount.
When to use
Use when work needs an element's host node, such as focusing or measuring it, or handing it to an imperative library such as a chart or a map.
Details
Takes an optional args Schema and returns a factory that takes the args
struct and issues the mount: h.canvas.onMount(DrawChart({ points })). The body
runs once per element, after it's committed (or just before, with
beforeCommit), in a Scope that closes when the element goes. It gets:
node, the host node. Annotate it with the node the body needs, andonMountthen rejects an element that renders to another kind of node. Left unannotated it'sunknown, and the mount goes on any element.args, the args the element had when the mount started.changes, a Stream that starts each reader with the latest published args. The runtime publishes updates that differ by the Schema's equivalence after the host commits, or after rendering but before the host commits withbeforeCommit: true. A reader that falls behind gets only the latest.
Mount startup, publishing new args to changes and Scope closure run
synchronously with their triggering commit until the work waits or reaches
Scheduler.MaxOpsBeforeYield steps. If a required Layer is still being
built, the body starts once that Layer is ready.
The body ends with its own result, which a view maps to a message with
map. When there is nothing to report, return Component.noop: the log
records MountEnded with silent: true instead of an Update. The body's
requirements join those of the component that renders it.
Gotchas
Args with no real Schema, such as a callback or a class instance, need
Schema.declare or Schema.Any. They then compare by identity and show
in the log as a placeholder.
Signature
declare function make<Name extends string, In extends ArgsInput = {}, A = never, Node = unknown, R = never>(name: Name, options: Options<In, Node, R> & NoInfer<Given<"args", In, {}>>, run: (node: Node, args: View<ArgsFields<In>>, changes: Stream<View<ArgsFields<In>, "Type", TypeOptionalKeys<ArgsFields<In>>, TypeMutableKeys<ArgsFields<In>>>>) => Effect<A, never, R>): Factory<View<ArgsFields<In>, "Type", TypeOptionalKeys<ArgsFields<In>>, TypeMutableKeys<ArgsFields<In>>>, A, Node, R>Example A mount with a result
import { Mount, UiSchema } from "effect/ui"import { Effect, Schema } from "effect"
const Message = UiSchema.TaggedEnum({ CompletedMeasure: { width: Schema.Number } })
const Measure = Mount.make("Measure", {}, (node: HTMLElement) => Effect.sync(() => node.getBoundingClientRect().width))
// In a viewconst measure = Measure().pipe(Mount.map((width) => Message.CompletedMeasure({ width })))makeStream
unstableDefines a mount that sends a result for each value of a Stream, for as long as its element lives.
When to use
Use when the work behind a host node reports more than once, such as a chart's clicks, a map's moves or an editor's changes.
Details
As make, but the body returns a Stream. Each value goes through the
map of the latest render and is sent to the component, and a value
mapped to Component.noop sends nothing. The Stream runs in the mount's
Scope, so a listener it adds with Stream.callback is removed when the
element goes. When the Stream ends, the mount sends nothing more.
In a story, each mounted step sends one value.
Signature
declare function makeStream<Name extends string, In extends ArgsInput = {}, A = never, Node = unknown, R = never>(name: Name, options: Options<In, Node, R> & NoInfer<Given<"args", In, {}>>, run: (node: Node, args: View<ArgsFields<In>>, changes: Stream<View<ArgsFields<In>, "Type", TypeOptionalKeys<ArgsFields<In>>, TypeMutableKeys<ArgsFields<In>>>>) => Stream<A, never, R>): Factory<View<ArgsFields<In>, "Type", TypeOptionalKeys<ArgsFields<In>>, TypeMutableKeys<ArgsFields<In>>>, A, Node, R>Example A widget's events
import { Mount, UiSchema } from "effect/ui"import { Effect, Queue, Schema, Stream } from "effect"
interface Editor { readonly onChange: (f: (text: string) => void) => () => void}declare const createEditor: (node: HTMLElement, text: string) => Editor
const Message = UiSchema.TaggedEnum({ ChangedText: { text: Schema.String } })
const AttachEditor = Mount.makeStream( "AttachEditor", { args: { text: Schema.String } }, (node: HTMLElement, { text }) => Stream.callback<string>((queue) => Effect.acquireRelease( Effect.sync(() => createEditor(node, text).onChange((text) => Queue.offerUnsafe(queue, text))), (unsubscribe) => Effect.sync(unsubscribe) ) ))
// In a viewconst editor = AttachEditor({ text: "" }).pipe(Mount.map((text) => Message.ChangedText({ text })))Guards
isDefinition
unstableReturns true if u is a mount definition.
Signature
declare function isDefinition(u: unknown): u is AnyReturns true if u is an issued mount.
Signature
declare function isMount(u: unknown): u is AnyMapping
Maps what a mount ends with.
When to use
Use to turn a mount's own result into the component's message, in a view.
Details
The name and args are kept, so the log and tests still see the original
mount, and a story's mounted step still takes the definition's own
result. The runtime applies the map of the latest render when the mount
ends, so a value the map closes over is current.
Signature
declare const map: { <A, B>(f: (a: A) => B): <R, Node, Source>(self: Issued<A, R, Node, Source>) => Issued<B, R, Node, Source>; <A, R, Node, Source, B>(self: Issued<A, R, Node, Source>, f: (a: A) => B): Issued<B, R, Node, Source>;}Models
Any issued mount.
Signature
type Any = Issued<any, any, never, never>A mount definition: what a test names a mount by.
Details
Args is the args struct, A the body's result, Node the host node the
body needs and R what it requires. Story steps such as mounted take a
definition and are typed by it.
Signature
interface Definition<in Args, out A, in Node = unknown, out R = never> { readonly "~effect/ui/Mount/Definition": Variance<Args, A, Node, R>; readonly args: Top; readonly name: string;}What make returns: call it with the mount's args struct to issue it.
Signature
interface Factory<in out Args, in out A, in Node = unknown, out R = never> extends Definition<Args, A, Node, R> { (...args: MakeArgs<Args>): Issued<A, R, Node, A>;}An issued mount: what a definition's factory returns, and what onMount
takes.
Details
Msg is what it ends with, after every map. R is what its body
requires and Node the host node the body needs: onMount accepts a
mount whose Node the element's node is assignable to. Source is the
definition's own result, before any map, which a story's mounted step
takes.
Signature
interface Issued<out Msg, out R = never, in Node = unknown, in Source = Msg> extends Pipeable { readonly "~effect/ui/Mount": Variance<Msg, R, Node, Source>; readonly args: unknown; readonly name: string;}The options of make.
Details
args: a fields record or a Schema.Struct, none when omitted. The log
encodes a mount's args through it, redacting Schema.Redacted fields, and
the runtime uses its equivalence to tell whether a render changed them.
The optional paint callback draws the node synchronously from its args
on a copy of the page that runs no mounts, such as DevTools time travel.
The runtime calls it once after the node is attached, without send or
a Scope. Only draw: do not attach listeners or acquire resources.
If paint is absent or throws, the copy reports the instance as skipped;
without paint, the node stays as the view rendered it.
With beforeCommit: true, the body starts and receives updated args after
rendering, before the host commit shows popovers and modal dialogs. Its node
is already in the document. Use it to capture focus before a dialog moves
focus inside. The body must require no services, so it can start without
waiting for an ancestor Layer.
Signature
interface Options<In extends Command.ArgsInput, Node = unknown, R = never> { readonly args?: In; readonly beforeCommit?: [R] extends [never] ? boolean | undefined : false | undefined; readonly paint?: (node: Node, args: View<ArgsFields<In>>) => void;}Phantom variance for an issued mount.
Signature
interface Variance<out Msg, out R, in Node, in Source> { readonly _Msg: Covariant<Msg>; readonly _Node: Contravariant<Node>; readonly _R: Covariant<R>; readonly _Source: Contravariant<Source>;}Other
Definition
unstableHelper types for Definition.
Type Ids
DefinitionTypeId
unstableIdentifies a mount definition.
Signature
declare const DefinitionTypeId: "~effect/ui/Mount/Definition"The type of DefinitionTypeId.
Signature
type DefinitionTypeId = typeof DefinitionTypeIdIdentifies an issued mount.
Signature
declare const TypeId: "~effect/ui/Mount"The type of TypeId.
Signature
type TypeId = typeof TypeIdUtility Types
The args struct a definition is issued with.
Signature
type Args<D> = D extends Definition<infer Args, infer _A, infer _Node, infer _R> ? Args : neverThe result of a mount's definition, before any map: what a story's
mounted step takes. Accepts a definition or an issued mount.
Signature
type Result<M> = M extends Definition<infer _Args, infer A, infer _Node, infer _R> ? A : M extends Issued<infer _Msg, infer _R, infer _Node, infer Source> ? Source : never