import type * as Cause from "effect/Cause" import * as Effect from "effect/Effect" import type { Scope } from "effect/Scope" import type { ElementSource } from "../../ElementSource.js" import type { EventHandler } from "../../EventHandler.js" import type * as Part from "../../Part.js" import { DEFAULT_PRIORITY } from "../../RenderQueue.js" import type * as SyncPart from "./SyncPart.js" const base = (tag: T) => (class Base { readonly _tag: T = tag constructor( readonly sync: Extract, readonly commit: ( params: { previous: Extract["value"] value: Extract["value"] part: Extract }, priority: number ) => Effect.Effect ) { this.update = this.update.bind(this) } update(input: any, priority: number = DEFAULT_PRIORITY) { const current = this.sync.value if (this.sync.update(input)) { return this.commit( { previous: current, value: input, part: this as any } as any, priority ) } return Effect.void } get value() { return this.sync.value as Extract["value"] } }) export class AttributePartImpl extends base("attribute") implements Part.AttributePart { readonly name: string readonly index: number constructor( readonly sync: SyncPart.AttributeSyncPart, commit: ( params: { previous: SyncPart.AttributeSyncPart["value"] value: SyncPart.AttributeSyncPart["value"] part: Part.AttributePart }, priority: number ) => Effect.Effect ) { super(sync, commit) this.name = sync.name this.index = sync.index } } export class BooleanPartImpl extends base("boolean") implements Part.BooleanPart { readonly name: string readonly index: number constructor( readonly sync: SyncPart.BooleanSyncPart, commit: ( params: { previous: SyncPart.BooleanSyncPart["value"] value: SyncPart.BooleanSyncPart["value"] part: Part.BooleanPart }, priority: number ) => Effect.Effect ) { super(sync, commit) this.name = sync.name this.index = sync.index } } export class ClassNamePartImpl extends base("className") implements Part.ClassNamePart { readonly index: number constructor( readonly sync: SyncPart.ClassNameSyncPart, commit: ( params: { previous: SyncPart.ClassNameSyncPart["value"] value: SyncPart.ClassNameSyncPart["value"] part: Part.ClassNamePart }, priority: number ) => Effect.Effect ) { super(sync, commit) this.index = sync.index } } export class CommentPartImpl extends base("comment") implements Part.CommentPart { readonly index: number constructor( readonly sync: SyncPart.CommentSyncPart, commit: ( params: { previous: SyncPart.CommentSyncPart["value"] value: SyncPart.CommentSyncPart["value"] part: Part.CommentPart }, priority: number ) => Effect.Effect ) { super(sync, commit) this.index = sync.index } } export class DataPartImpl extends base("data") implements Part.DataPart { readonly index: number constructor( readonly sync: SyncPart.DataSyncPart, commit: ( params: { previous: SyncPart.DataSyncPart["value"] value: SyncPart.DataSyncPart["value"] part: Part.DataPart }, priority: number ) => Effect.Effect ) { super(sync, commit) this.index = sync.index } } export class EventPartImpl implements Part.EventPart { readonly _tag = "event" readonly value: Part.EventPart["value"] = null constructor( readonly name: string, readonly index: number, readonly source: ElementSource, readonly onCause: (cause: Cause.Cause) => Effect.Effect, readonly addEventListener: (handler: EventHandler) => void ) { } } export class NodePartImpl extends base("node") implements Part.NodePart { readonly index: number constructor( readonly sync: SyncPart.NodeSyncPart, commit: ( params: { previous: SyncPart.NodeSyncPart["value"] value: SyncPart.NodeSyncPart["value"] part: Part.NodePart }, priority: number ) => Effect.Effect ) { super(sync, commit) this.index = sync.index } } export class PropertyPartImpl extends base("property") implements Part.PropertyPart { readonly name: string readonly index: number constructor( readonly sync: SyncPart.PropertySyncPart, commit: ( params: { previous: SyncPart.PropertySyncPart["value"] value: SyncPart.PropertySyncPart["value"] part: Part.PropertyPart }, priority: number ) => Effect.Effect ) { super(sync, commit) this.name = sync.name this.index = sync.index } } export class RefPartImpl implements Part.RefPart { readonly _tag = "ref" constructor(readonly value: ElementSource, readonly index: number) {} } export class TextPartImpl extends base("text") implements Part.TextPart { readonly index: number constructor( readonly sync: SyncPart.TextSyncPart, commit: ( params: { previous: SyncPart.TextSyncPart["value"] value: SyncPart.TextSyncPart["value"] part: Part.TextPart }, priority: number ) => Effect.Effect ) { super(sync, commit) this.index = sync.index } } export function syncPartToPart( part: SyncPart.SyncPart, commit: ( params: { previous: SyncPart.TextSyncPart["value"] value: SyncPart.TextSyncPart["value"] part: Extract }, priority: number ) => Effect.Effect ): Extract { switch (part._tag) { case "attribute": return new AttributePartImpl(part, commit as any) as any case "boolean": return new BooleanPartImpl(part, commit as any) as any case "className": return new ClassNamePartImpl(part, commit as any) as any case "comment": return new CommentPartImpl(part, commit as any) as any case "data": return new DataPartImpl(part, commit as any) as any case "node": return new NodePartImpl(part, commit as any) as any case "property": return new PropertyPartImpl(part, commit as any) as any case "text": return new TextPartImpl(part, commit as any) as any } }