/** * @since 1.0.0 */ import type { Fx } from "@typed/fx/Fx" import { compact, FxEffectBase } from "@typed/fx/Fx" import * as RefSubject from "@typed/fx/RefSubject" import type { Versioned } from "@typed/fx/Versioned" import { type Rendered } from "@typed/wire" import type { NoSuchElementException } from "effect/Cause" import * as Effect from "effect/Effect" import { dual } from "effect/Function" import * as Option from "effect/Option" import { hasProperty } from "effect/Predicate" import type * as Scope from "effect/Scope" import type { DefaultEventMap } from "./ElementSource.js" import { ElementSource, getElements } from "./ElementSource.js" import { adjustTime } from "./internal/utils.js" export const ElementRefTypeId = Symbol.for("@typed/template/ElementRef") export type ElementRefTypeId = typeof ElementRefTypeId /** * A reference to a rendered element. * @since 1.0.0 */ export interface ElementRef extends Versioned< never, never, T, never, Scope.Scope, T, NoSuchElementException, never > { readonly [ElementRefTypeId]: RefSubject.RefSubject> readonly query: ElementSource["query"] readonly events: ElementSource["events"] readonly elements: ElementSource["elements"] readonly dispatchEvent: ElementSource["dispatchEvent"] } const strictEqual = Option.getEquivalence((a, b) => a === b) /** * @since 1.0.0 */ export function make(): Effect.Effect< ElementRef, never, Scope.Scope > { return Effect.map( RefSubject.of(Option.none(), { eq: strictEqual }), (ref) => new ElementRefImpl(ref) as any as ElementRef ) } /** * @since 1.0.0 */ export function of( rendered: T ): Effect.Effect, never, Scope.Scope> { return Effect.map( RefSubject.of(Option.some(rendered), { eq: strictEqual }), (ref) => new ElementRefImpl(ref) as any as ElementRef ) } // @ts-expect-error Missing PlaceholderTypeId class ElementRefImpl extends FxEffectBase implements ElementRef { readonly [ElementRefTypeId]: RefSubject.RefSubject> private source: ElementSource> readonly query: ElementRef["query"] readonly events: ElementRef["events"] readonly elements: ElementRef["elements"] readonly version: ElementRef["version"] readonly dispatchEvent: ElementRef["dispatchEvent"] constructor(readonly ref: RefSubject.RefSubject>) { super() this[ElementRefTypeId] = ref this.source = ElementSource(RefSubject.compact(ref)) this.query = this.source.query this.events = this.source.events this.elements = this.source.elements this.dispatchEvent = this.source.dispatchEvent this.version = ref.version } toFx(): Fx { return compact(this.ref) } toEffect(): Effect.Effect { return Effect.flatten(this.ref) } } /** * @since 1.0.0 */ export const set: { ( value: A ): (elementRef: ElementRef) => Effect.Effect (elementRef: ElementRef, value: A): Effect.Effect } = dual(2, function set< A extends Rendered >(elementRef: ElementRef, value: A) { return Effect.as( RefSubject.set(elementRef[ElementRefTypeId], Option.some(value)), value ) }) /** * @since 1.0.0 */ export function dispatchEvent( ref: ElementRef, event: Event ) { return ref.pipe( Effect.flatMap((rendered) => { const elements = getElements(rendered) if (elements.length === 0) return Option.none() // TODO: How should we manage multiple elements? return Effect.sync(() => elements[0].dispatchEvent(event)) }), // Allow changes to take place Effect.zipRight(adjustTime(1)), // Allow additional fibers to start Effect.zipRight(adjustTime(1)) ) } export function isElementRef(value: unknown): value is ElementRef { return hasProperty(value, ElementRefTypeId) }