From f362f233787f8763a6b8dffc2dddedfd674f6652 Mon Sep 17 00:00:00 2001 From: Tim Date: Fri, 13 Nov 2026 12:45:51 +1300 Subject: [PATCH] effect/ui: write public types out instead of aliasing them (#10590) --- packages/effect/src/ui/Command.ts | 49 ++---- packages/effect/src/ui/Component.ts | 156 +++++++----------- packages/effect/src/ui/Renderer.ts | 36 +--- packages/effect/src/ui/Route.ts | 41 ++--- packages/effect/src/ui/Router.ts | 14 +- packages/effect/src/ui/View.ts | 33 +--- packages/effect/src/ui/html/BrowserHost.ts | 4 +- packages/effect/src/ui/html/Html.ts | 9 +- packages/effect/src/ui/internal/runtime.ts | 4 +- packages/effect/src/ui/testing/Story.ts | 70 ++++---- packages/effect/src/ui/testing/TestHost.ts | 4 +- packages/effect/test/ui/Command.test.ts | 2 +- .../effect/test/ui/html/BrowserHost.test.ts | 2 +- packages/effect/typetest/ui/Command.tst.ts | 50 +++--- packages/effect/typetest/ui/Component.tst.ts | 4 +- packages/effect/typetest/ui/Loaders.tst.ts | 48 +++--- packages/effect/typetest/ui/Router.tst.ts | 2 +- packages/effect/typetest/ui/Story.tst.ts | 2 +- 18 files changed, 222 insertions(+), 308 deletions(-) diff --git a/packages/effect/src/ui/Command.ts b/packages/effect/src/ui/Command.ts index baa12d4652..789bb36a86 100644 --- a/packages/effect/src/ui/Command.ts +++ b/packages/effect/src/ui/Command.ts @@ -185,24 +185,15 @@ export type GroupField = & string /** - * The `group` fields of a Command's args: what `Command.interrupt` and - * `h.isRunning` take to address one group. + * The optional group argument of `Command.interrupt` and `h.isRunning`: the + * `group` fields of the Command's args, or none for a Command without a + * `group`. * * @stability experimental * @category utility types * @since 4.1.5 */ -export type GroupPick = Pick - -/** - * The optional group argument of `Command.interrupt` and `h.isRunning`: none - * for a Command without a `group`. - * - * @stability experimental - * @category utility types - * @since 4.1.5 - */ -export type GroupArg = [G] extends [never] ? [] : [group?: GroupPick] +export type GroupArg = [G] extends [never] ? [] : [group?: Pick] /** * The arguments of `make`: optional when every field of the args is. @@ -417,9 +408,9 @@ export type Kind = "Effect" | "Stream" * the success of the Command's definition before any `map`, and `SourceE` * its typed failure before `match`; they're what a story's `resolve` and * `fail` steps take. `K` is the kind, which decides whether `effect` or - * `stream` holds the work; the other field is `undefined`. Use the - * `Effectful` and `Streaming` aliases rather than naming `K` yourself, and - * narrow a `Command` on `_tag` before reading the work. + * `stream` holds the work; the other field is `undefined`: `"Effect"` for a + * Command whose result is one message, `"Stream"` for one declared with + * `stream: true`. Narrow a `Command` on `_tag` before reading the work. * * One interface for both kinds, rather than two, is what lets * `command.pipe(Command.map((result) => ...))` infer `result`. @@ -472,24 +463,6 @@ export interface Unmatched extends Pipe readonly stream: K extends "Stream" ? Stream.Stream : undefined } -/** - * A Command whose result is one message. - * - * @stability experimental - * @category models - * @since 4.1.5 - */ -export type Effectful = Issued - -/** - * A Command declared with `stream: true`, whose result is a Stream of messages. - * - * @stability experimental - * @category models - * @since 4.1.5 - */ -export type Streaming = Issued - /** * A Command that interrupts the running and queued runs of another Command. * @@ -526,7 +499,7 @@ export interface Interrupt extends Pipeable.Pipeable { * @category models * @since 4.1.5 */ -export type Command = Effectful | Streaming | Interrupt +export type Command = Issued | Issued | Interrupt /** * The requirements of a Command, or of a union of them. @@ -674,7 +647,9 @@ export declare namespace Definition { export interface Factory extends Definition { - (...args: MakeArgs): IsStream extends true ? Streaming : Effectful + ( + ...args: MakeArgs + ): IsStream extends true ? Issued : Issued readonly name: string /** The args Schema. */ readonly args: Schema.Top @@ -719,7 +694,7 @@ export interface Service< */ make( ...args: MakeArgs - ): [Err] extends [never] ? (IsStream extends true ? Streaming : Effectful) + ): [Err] extends [never] ? (IsStream extends true ? Issued : Issued) : Unmatched } diff --git a/packages/effect/src/ui/Component.ts b/packages/effect/src/ui/Component.ts index 95e5f7004a..11b371de07 100644 --- a/packages/effect/src/ui/Component.ts +++ b/packages/effect/src/ui/Component.ts @@ -218,7 +218,7 @@ export interface Update + readonly loaded: { readonly [K in keyof L]: LoadedOf } } /** @@ -420,18 +420,36 @@ export interface Component< } /** - * What `h` gains in `view` for a component with loaders: `loaded`, - * `previous` and `patched`. `previous` and `patched` have no `loadMany` - * loader. + * What `h` gains in `view` for a component with loaders. + * + * **Details** + * + * `loaded` holds each loader's result, as `$.loaded` does in `update`. + * + * `previous` holds, per single or list loader, the Success value its slot + * holds for args other than the loader's current ones. It's `Some` only while + * the loader is enabled, so it is `None` whenever `h.loaded` has the stored + * result. + * + * `patched` tells, per single or list loader, whether the value includes an + * optimistic patch the server hasn't confirmed. It is per entry, not per + * instance. + * + * `previous` and `patched` have no `loadMany` loader. * * @stability experimental * @category utility types * @since 4.1.5 */ export type LoaderHelpers = [keyof L] extends [never] ? {} : { - readonly loaded: Loaded - readonly previous: Previous - readonly patched: Patched + readonly loaded: { readonly [K in keyof L]: LoadedOf } + readonly previous: { + readonly [K in SingleNames]: L[K] extends Load ? + R extends Loader.ListRead.Any ? Option.Option, "more">> + : Option.Option> + : never + } + readonly patched: { readonly [K in SingleNames]: boolean } } /** @@ -527,20 +545,6 @@ export interface LoadMany { */ export type LoaderSpec = { readonly [name: string]: Load | LoadMany } -/** - * The `LoaderSpec` `make` infers from `loaders`: `S` holds each entry's - * `load` and `M` each entry's `loadMany`. - * - * @stability experimental - * @category utility types - * @since 4.1.5 - */ -export type SpecOf = { - readonly [K in keyof S]: M[K & keyof M] extends Loader.Read.Any ? LoadMany - : S[K] extends Loader.Read.Any ? Load - : never -} - // A `load` beside a `loadMany` in one entry, or a list read under `loadMany`, is rejected. type OneRead = [M] extends [Loader.Read.Any] ? { readonly "~error": "an entry takes one of load and loadMany" } : unknown @@ -586,24 +590,6 @@ export type ListNames = */ export type KeyedNames = { readonly [K in keyof L]: L[K] extends LoadMany ? K : never }[keyof L] & string -/** - * The read of a `Load` or a `LoadMany`. - * - * @stability experimental - * @category utility types - * @since 4.1.5 - */ -export type ReadOf = D extends Load ? R : D extends LoadMany ? R : never - -/** - * The result a read opens to: an `AsyncResult` of its success and error. - * - * @stability experimental - * @category utility types - * @since 4.1.5 - */ -export type ResultOf = AsyncResult.AsyncResult, Loader.Read.Error> - /** * The services a component's loaders add to its requirements: the reads * themselves, never what their fetchers use. @@ -612,13 +598,21 @@ export type ResultOf = AsyncResult.AsyncResult, Loader * @category utility types * @since 4.1.5 */ -export type LoaderServices = ServiceOf> +export type LoaderServices = ServiceOf -type ServiceOf = R extends { readonly Identifier: infer Self } ? Self : never +type ServiceOf = D extends { readonly read: { readonly Identifier: infer Self } } ? Self : never /** - * One loader opened: a single read's result, a list's result opened to - * `LoadedList`, or a keyed read's entries. + * One loader as `h.loaded`, `$.loaded`, and the `loaded` argument of `head`, + * `subscriptions` and a loader's `args` open it. + * + * **Details** + * + * A single loader opens to an `AsyncResult` of its read's success and error, + * a list loader to an `AsyncResult` of its `LoadedList`, and a `loadMany` + * loader to its entries in the order of its args. A single or list loader + * holds the result its slot took at the loader's current args, and otherwise + * `Initial`, waiting while the loader is enabled. * * @stability experimental * @category utility types @@ -627,26 +621,9 @@ type ServiceOf = R extends { readonly Identifier: infer Self } ? Self : never export type LoadedOf = D extends LoadMany ? KeyedLoaded : D extends Load ? R extends Loader.ListRead.Any ? AsyncResult.AsyncResult, Loader.Read.Error> - : ResultOf + : AsyncResult.AsyncResult, Loader.Read.Error> : never -/** - * `h.loaded` in `view`, `$.loaded` in `update`, and `loaded` in `head`, - * `subscriptions` and a loader's `args`. - * - * **Details** - * - * Per single or list loader, the result its slot holds when that was taken - * at the loader's current args, and otherwise `Initial`, waiting while the - * loader is enabled. A `loadMany` loader opens to its entries in the order - * of its args. - * - * @stability experimental - * @category utility types - * @since 4.1.5 - */ -export type Loaded = { readonly [K in keyof L]: LoadedOf } - /** * A list loader as `h.loaded` opens it, inside its `AsyncResult`. * @@ -712,33 +689,6 @@ export interface KeyedChange { readonly result: AsyncResult.AsyncResult, Loader.Read.Error> } -/** - * `h.previous` in `view`: per single or list loader, the Success value its - * slot holds for args other than the loader's current ones. `Some` only while - * the loader is enabled, so it is `None` whenever `h.loaded` has the stored - * result. - * - * @stability experimental - * @category utility types - * @since 4.1.5 - */ -export type Previous = { - readonly [K in SingleNames]: ReadOf extends infer R extends Loader.ListRead.Any - ? Option.Option, "more">> - : Option.Option>> -} - -/** - * `h.patched` in `view`: per single or list loader, whether the value - * includes an optimistic patch the server hasn't confirmed. It is per entry, - * not per instance. - * - * @stability experimental - * @category utility types - * @since 4.1.5 - */ -export type Patched = { readonly [K in SingleNames]: boolean } - /** * What a loader's `args` returns: the args of a `load`'s read, or * `undefined` to disable it, and an array of args for a `loadMany`, empty @@ -915,6 +865,13 @@ export const make = < Events = never, S = {}, M = {}, + // The loaders as `Load` and `LoadMany` entries, by name. Nothing infers it, so it's always this default: + // the mapped types over it use `NoInfer`, or an annotated `loaded` argument would. + L = { + readonly [K in keyof S]: M[K & keyof M] extends Loader.Read.Any ? LoadMany + : S[K] extends Loader.Read.Any ? Load + : never + }, UI extends Model | InitStep = Model, U extends Model | Step = Model, UV extends View.Content = View.Node, @@ -936,7 +893,7 @@ export const make = < readonly args: ( model: Model, props: Props, - loaded: Omit>, K> + loaded: { readonly [P in Exclude, K>]: LoadedOf[P]> } ) => ArgsFor readonly debounce?: Duration.Input | undefined readonly onChange?: OnChange> | undefined @@ -946,15 +903,24 @@ export const make = < ) | undefined readonly init: ($: Init) => UI - readonly update: (model: Model, msg: Msg, $: Update>) => U + readonly update: (model: Model, msg: Msg, $: Update) => U readonly view: ( model: Model, - h: View.Helper & View.Tags & LoaderHelpers> + h: View.Helper & View.Tags & LoaderHelpers ) => UV readonly subscriptions?: - | ((model: Model, props: Props, loaded: Loaded>) => ReadonlyArray) + | (( + model: Model, + props: Props, + loaded: { readonly [K in keyof NoInfer]: LoadedOf[K]> } + ) => ReadonlyArray) + | undefined + readonly head?: + | (( + model: Model, + loaded: { readonly [K in keyof NoInfer]: LoadedOf[K]> } + ) => ReadonlyArray) | undefined - readonly head?: ((model: Model, loaded: Loaded>) => ReadonlyArray) | undefined readonly elements?: View.Vocabulary | undefined } ): Component< @@ -968,11 +934,11 @@ export const make = < | StepRequirements | View.Requirements | Subscription.Requirements - | LoaderServices>, + | LoaderServices, Command.FrameworkService >, V, - SpecOf + L > => { const bound = loaders.bind(options.name, options.loaders) const open = (model: unknown, props: unknown, slots: Slots | undefined): any => diff --git a/packages/effect/src/ui/Renderer.ts b/packages/effect/src/ui/Renderer.ts index d8c399cebd..25f750333b 100644 --- a/packages/effect/src/ui/Renderer.ts +++ b/packages/effect/src/ui/Renderer.ts @@ -16,7 +16,6 @@ * @stability experimental * @since 4.1.5 */ -import type * as Cause from "../Cause.ts" import type * as Context from "../Context.ts" import type * as Duration from "../Duration.ts" import type * as Effect from "../Effect.ts" @@ -33,25 +32,6 @@ import * as runtime from "./internal/runtime.ts" import * as services from "./internal/services.ts" import type * as View from "./View.ts" -/** - * An element's handlers, by event name, as the view declared them. - * - * @stability experimental - * @category models - * @since 4.1.5 - */ -export type Handlers = Readonly>> - -/** - * An element's event options, by event name, as the view declared them with - * its handlers. - * - * @stability experimental - * @category models - * @since 4.1.5 - */ -export type EventOptions = Readonly> - /** * How a host reports an event: the event's name, the handler it found on the * innermost element with one, and the payload it built. @@ -119,7 +99,12 @@ export interface Host { remove(node: Node): void parentOf(node: Node): Node | undefined nextOf(node: Node): Node | undefined - setEvents(node: Node, handlers: Handlers, dispatch: Dispatch, options: EventOptions): void + setEvents( + node: Node, + handlers: Readonly>>, + dispatch: Dispatch, + options: Readonly> + ): void readonly elements: ReadonlyArray | "any" readonly controlled: ReadonlySet readonly runsMounts: boolean @@ -547,12 +532,3 @@ export const prerender: & { readonly cause: Cause.Cause } diff --git a/packages/effect/src/ui/Route.ts b/packages/effect/src/ui/Route.ts index a3a807ec9a..ddbbda91f3 100644 --- a/packages/effect/src/ui/Route.ts +++ b/packages/effect/src/ui/Route.ts @@ -157,37 +157,17 @@ export type MakeField = S extends { : S /** - * The fields of a params or query Struct, each as `MakeField` gives it. - * - * @stability experimental - * @category utility types - * @since 4.1.5 - */ -export type MakeFields = { readonly [K in keyof Fields]: MakeField } - -/** - * A params or query Struct, which `make` fills in, defaults included, when - * no field needs a value. + * A params or query Struct, each field as `MakeField` gives it, which `make` + * fills in, defaults included, when no field needs a value. * * @stability experimental * @category utility types * @since 4.1.5 */ export type FieldsSchema = {} extends - Schema.Struct>["~type.make.in"] ? Schema.withConstructorDefault>> - : Schema.Struct> - -/** - * The Schema of one route's values in a union. - * - * @stability experimental - * @category models - * @since 4.1.5 - */ -export type Case = Schema.TaggedStruct> - readonly query: FieldsSchema> -}> + Schema.Struct<{ readonly [K in keyof Fields]: MakeField }>["~type.make.in"] + ? Schema.withConstructorDefault }>> + : Schema.Struct<{ readonly [K in keyof Fields]: MakeField }> /** * A redirect: a template whose raw params and query map to a route value. @@ -240,8 +220,15 @@ export interface Resolved { * @category models * @since 4.1.5 */ -export interface Union> - extends Schema.TaggedUnion<{ readonly [K in keyof Routes & string]: Case }> +export interface Union> extends + Schema.TaggedUnion< + { + readonly [K in keyof Routes & string]: Schema.TaggedStruct> + readonly query: FieldsSchema> + }> + } + > { /** The route definitions, by tag. */ readonly routes: Routes diff --git a/packages/effect/src/ui/Router.ts b/packages/effect/src/ui/Router.ts index 9c63a05826..994f10bc33 100644 --- a/packages/effect/src/ui/Router.ts +++ b/packages/effect/src/ui/Router.ts @@ -295,8 +295,10 @@ export class Replace extends Command.Service()( * @category commands * @since 4.1.5 */ -export const push = (_routes: U, route: U["Type"]): Command.Effectful => - Push.make({ route }) +export const push = ( + _routes: U, + route: U["Type"] +): Command.Issued => Push.make({ route }) /** * Replaces the current history entry with a route value of the given union. @@ -312,8 +314,10 @@ export const push = (_routes: U, route: U["Type"]): Command.Ef * @category commands * @since 4.1.5 */ -export const replace = (_routes: U, route: U["Type"]): Command.Effectful => - Replace.make({ route }) +export const replace = ( + _routes: U, + route: U["Type"] +): Command.Issued => Replace.make({ route }) /** A query patch: the args Schema of `PatchQuery`. The log encodes it structurally. */ const QueryPatchValue = Schema.declare( @@ -411,7 +415,7 @@ export const patchQuery = ( readonly history?: "push" | "replace" | undefined readonly scroll?: boolean | undefined } | undefined -): Command.Effectful => +): Command.Issued => PatchQuery.make({ route, patch: patch as Readonly>, diff --git a/packages/effect/src/ui/View.ts b/packages/effect/src/ui/View.ts index 3b36f73b2b..48e2c4e908 100644 --- a/packages/effect/src/ui/View.ts +++ b/packages/effect/src/ui/View.ts @@ -175,7 +175,9 @@ export interface Child { readonly props: unknown readonly onEmit: (events: any) => Msg | undefined /** What the slot shows while its child can't be shown. Required for a feature child. */ - readonly fallback: ((result: Child.Fallback) => Content) | undefined + readonly fallback: + | ((result: AsyncResult.Initial | AsyncResult.Failure) => Content) + | undefined /** Also tells the parent that the child failed. */ readonly onFailure: ((failure: AsyncResult.Failure) => Msg | undefined) | undefined /** Phantom: the child's requirements surface in its parent's. */ @@ -189,15 +191,6 @@ export interface Child { * @since 4.1.5 */ export declare namespace Child { - /** - * What a slot's `fallback` receives: the child's service is still building, - * or the child failed. - * - * @category models - * @since 4.1.5 - */ - export type Fallback = AsyncResult.Initial | AsyncResult.Failure - /** * The options of `h.child`. `props` may be left out when the component takes * none. @@ -352,18 +345,6 @@ export const build = >>( children: inputs.length === 0 ? [] : children(inputs) }) -/** - * The fields an element builder holds, which `build` copies onto the element. - * - * @stability experimental - * @category models - * @since 4.1.5 - */ -export type BuilderFields = Pick< - Element, - "elementKey" | "attrs" | "props" | "handlers" | "eventOptions" | "refKey" | "mount" -> - /** * Copies a builder with some of its fields replaced, keeping its class. Every * chain method is written with it, so a builder stays a value. @@ -374,7 +355,9 @@ export type BuilderFields = Pick< */ export const withFields = >( builder: B, - fields: Partial + fields: Partial< + Pick, "elementKey" | "attrs" | "props" | "handlers" | "eventOptions" | "refKey" | "mount"> + > ): B => Object.assign(Object.create(Object.getPrototypeOf(builder)), builder, fields) /** @@ -794,7 +777,9 @@ export interface Helper { service: Feature.Service, options: Child.Options & { /** Required: the service may be building (`Initial`) or failed (`Failure`). */ - readonly fallback: (result: Child.Fallback) => Content + readonly fallback: ( + result: AsyncResult.Initial | AsyncResult.Failure + ) => Content } ): Child> /** A component by value. Its requirements surface in the parent's. */ diff --git a/packages/effect/src/ui/html/BrowserHost.ts b/packages/effect/src/ui/html/BrowserHost.ts index 82f52f2240..931854fbde 100644 --- a/packages/effect/src/ui/html/BrowserHost.ts +++ b/packages/effect/src/ui/html/BrowserHost.ts @@ -168,9 +168,9 @@ const controlled: ReadonlySet = new Set(["value", "checked", "selected", const emptyValue = (name: string): unknown => name === "value" ? "" : false interface Bound { - readonly handlers: Renderer.Handlers + readonly handlers: Readonly>> readonly dispatch: Renderer.Dispatch - readonly options: Renderer.EventOptions + readonly options: Readonly> } const modifiers = (event: Event) => { diff --git a/packages/effect/src/ui/html/Html.ts b/packages/effect/src/ui/html/Html.ts index c17b36a05a..ca73002666 100644 --- a/packages/effect/src/ui/html/Html.ts +++ b/packages/effect/src/ui/html/Html.ts @@ -1324,7 +1324,7 @@ export const focus = ( ref: Renderer.Ref, f: (result: Found) => Msg, options?: { readonly preventScroll?: boolean | undefined } -): Command.Mapped, Msg> => +): Command.Mapped, Msg> => Command.map(Focus(options === undefined ? { ref } : { ref, options }), f) /** @@ -1337,7 +1337,7 @@ export const focus = ( export const blur = ( ref: Renderer.Ref, f: (result: Found) => Msg -): Command.Mapped, Msg> => Command.map(Blur({ ref }), f) +): Command.Mapped, Msg> => Command.map(Blur({ ref }), f) /** * Scrolls the element `ref` names into view, and sends `f`'s message. @@ -1350,7 +1350,7 @@ export const scrollIntoView = ( ref: Renderer.Ref, f: (result: Found) => Msg, options?: ScrollOptions -): Command.Mapped, Msg> => +): Command.Mapped, Msg> => Command.map(ScrollIntoView(options === undefined ? { ref } : { ref, options }), f) /** @@ -1364,4 +1364,5 @@ export const scrollIntoView = ( export const measure = ( ref: Renderer.Ref, f: (rect: Option.Option) => Msg -): Command.Mapped>, Msg> => Command.map(Measure({ ref }), f) +): Command.Mapped, never, Option.Option, "Effect">, Msg> => + Command.map(Measure({ ref }), f) diff --git a/packages/effect/src/ui/internal/runtime.ts b/packages/effect/src/ui/internal/runtime.ts index 86209a816e..ef01e4dd2a 100644 --- a/packages/effect/src/ui/internal/runtime.ts +++ b/packages/effect/src/ui/internal/runtime.ts @@ -99,7 +99,7 @@ const failEnv = (env: Env, cause: Cause.Cause) => { for (const waiter of env.waiters.splice(0)) waiter.failed?.(cause) } const memoMapOf = (env: Env): Layer.MemoMap => env.memoMap ??= Layer.CurrentMemoMap.forkOrCreate(env.ctx!) -const unavailable = (env: Env): View.Child.Fallback => +const unavailable = (env: Env): AsyncResult.Initial | AsyncResult.Failure => env.failure !== undefined ? AsyncResult.failure(env.failure) : AsyncResult.initial(true) // --------------------------------------------------------------------------- @@ -141,7 +141,7 @@ interface RSlot { // --------------------------------------------------------------------------- interface Run { - readonly command: Command.Effectful | Command.Streaming + readonly command: Command.Issued | Command.Issued readonly issued: MessageLog.Issued /** The run at the cache, begun at issue. `undefined` for a `Command.make` Command. */ readonly handle: Loader.RunHandle | undefined diff --git a/packages/effect/src/ui/testing/Story.ts b/packages/effect/src/ui/testing/Story.ts index d951601145..39c45c3c06 100644 --- a/packages/effect/src/ui/testing/Story.ts +++ b/packages/effect/src/ui/testing/Story.ts @@ -90,7 +90,7 @@ import * as Query from "./Query.ts" * @since 4.1.5 */ export interface Pending { - readonly command: Command.Effectful | Command.Streaming + readonly command: Command.Issued | Command.Issued readonly status: "running" | "queued" | "awaiting" /** On an awaiting run: the message it delivers when its wait ends. */ readonly message?: Msg | undefined @@ -144,9 +144,11 @@ export interface Story | Command.Streaming> + readonly interrupted: ReadonlyArray< + Command.Issued | Command.Issued + > /** Commands ignored by `mode: "drop"`. */ - readonly dropped: ReadonlyArray | Command.Streaming> + readonly dropped: ReadonlyArray | Command.Issued> /** The list loaders with a load more `update` issued and no step has settled yet. */ readonly loadingMore: ReadonlyArray /** @@ -199,6 +201,9 @@ export interface LoaderInfo { readonly args: unknown } +// The read of a loader entry, for the steps that name a loader. +type ReadOf = D extends Component.Load ? R : D extends Component.LoadMany ? R : never + /** * One key of a `loadMany` loader for `loadedMany`: its args and a Success * value or a typed failure. @@ -220,19 +225,6 @@ export type KeyedLoad = readonly waiting?: boolean | undefined } -/** - * Loaded results for `given`: per loader, the Success value its slot - * starts with, or the entries of a `loadMany` loader. - * - * @stability experimental - * @category models - * @since 4.1.5 - */ -export type GivenLoaded = { - readonly [K in keyof L]?: L[K] extends Component.LoadMany ? ReadonlyArray> - : Loader.Read.Success> -} - /** * A pending Command's definition, or an issued Command to also match the * arguments. @@ -241,7 +233,10 @@ export type GivenLoaded = { * @category models * @since 4.1.5 */ -export type Target = Command.Definition.Any | Command.Effectful | Command.Streaming +export type Target = + | Command.Definition.Any + | Command.Issued + | Command.Issued /** * A stream Command's definition, or an issued stream Command: what `next` and @@ -251,7 +246,7 @@ export type Target = Command.Definition.Any | Command.Effectful * @category models * @since 4.1.5 */ -export type StreamTarget = Command.Definition | Command.Streaming +export type StreamTarget = Command.Definition | Command.Issued // A component with no messages has `Msg = never`, and its `update` can't take `any`, so it needs its own member. type AnyComponent = @@ -302,7 +297,7 @@ export interface Steps extends Pipeable { Component.Model, Component.Message, Component.Events, - Component.Loaded> + { readonly [K in keyof Component.Loaders]: Component.LoadedOf[K]> } > /** @@ -327,7 +322,11 @@ export interface Steps extends Pipeable { */ given( model: Component.Model | ((model: Component.Model) => Component.Model), - loaded?: GivenLoaded> + loaded?: { + readonly [K in keyof Component.Loaders]?: Component.Loaders[K] extends Component.LoadMany + ? ReadonlyArray> + : Loader.Read.Success[K]>> + } ): this /** * Changes the props. A props change never reaches `update`: it re-renders the @@ -413,7 +412,7 @@ export interface Steps extends Pipeable { */ loaded>>( name: N, - value: Loader.Read.Success[N]>>, + value: Loader.Read.Success[N]>>, options?: { readonly waiting?: boolean | undefined; readonly patched?: boolean | undefined } ): this /** @@ -422,8 +421,8 @@ export interface Steps extends Pipeable { */ loaded>>( name: N, - args: Loader.Read.Args[N]>>, - value: Loader.Read.Success[N]>>, + args: Loader.Read.Args[N]>>, + value: Loader.Read.Success[N]>>, options?: { readonly waiting?: boolean | undefined; readonly patched?: boolean | undefined } ): this /** @@ -434,14 +433,14 @@ export interface Steps extends Pipeable { */ loadFailed>>( name: N, - error: Loader.Read.Error[N]>>, + error: Loader.Read.Error[N]>>, options?: { readonly waiting?: boolean | undefined } ): this /** Loads one key of a `loadMany` loader with a typed failure, as `loadedMany` does with one entry. */ loadFailed>>( name: N, - args: Loader.Read.Args[N]>>, - error: Loader.Read.Error[N]>>, + args: Loader.Read.Args[N]>>, + error: Loader.Read.Error[N]>>, options?: { readonly waiting?: boolean | undefined } ): this /** @@ -451,7 +450,7 @@ export interface Steps extends Pipeable { */ loadedMore>>( name: N, - page: Loader.ListRead.Page[N]>> + page: Loader.ListRead.Page[N]>> ): this /** * Settles a list loader's pending load more with a failure: the pages stay, @@ -460,7 +459,7 @@ export interface Steps extends Pipeable { */ loadMoreFailed>>( name: N, - error: Loader.Read.Error[N]>> + error: Loader.Read.Error[N]>> ): this /** * Loads several keys of a `loadMany` loader in one write, as the runtime @@ -470,7 +469,7 @@ export interface Steps extends Pipeable { */ loadedMany>>( name: N, - entries: ReadonlyArray[N]>>> + entries: ReadonlyArray[N]>>> ): this /** Checks the Model. `f` throws to fail the test. */ @@ -1132,7 +1131,7 @@ function* currentEntries( */ const awaitedLoaders = ( story: Story, - command: Command.Effectful | Command.Streaming, + command: Command.Issued | Command.Issued, result: unknown ): ReadonlyArray => { const budget = command.afterRefetch @@ -1163,7 +1162,7 @@ const livePatches = new WeakMap, ReadonlyArray<{ readonly name: str /** The component's own loaders, at their current args, that `command`'s patches register on. */ const registeredPatches = ( story: Story, - command: Command.Effectful | Command.Streaming + command: Command.Issued | Command.Issued ): ReadonlyArray<{ readonly name: string; readonly argsKey: string }> => { if (command.optimistic.length === 0) return [] const out: Array<{ readonly name: string; readonly argsKey: string }> = [] @@ -1198,8 +1197,9 @@ const checkUnpatched = (story: Story, step: string, name: st } } -const describe = (command: Command.Effectful | Command.Streaming): string => - `${command.name}(${JSON.stringify(MessageLog.encodeArgs(command.argsSchema, command.args))})` +const describe = ( + command: Command.Issued | Command.Issued +): string => `${command.name}(${JSON.stringify(MessageLog.encodeArgs(command.argsSchema, command.args))})` const describePending = (pending: ReadonlyArray>): string => pending.length === 0 ? @@ -1584,7 +1584,7 @@ const findChild = (view: View.Node, key: string, step: string): V export const fallback = ( view: View.Node, key: string, - result: View.Child.Fallback + result: AsyncResult.Initial | AsyncResult.Failure ): View.Node | undefined => { const fallback = findChild(view, key, "fallback").fallback return fallback === undefined ? undefined : View.toNode(fallback(result)) @@ -1926,6 +1926,6 @@ export const fromLog: ( Component.Model, Component.Message, Component.Events, - Component.Loaded>, + { readonly [K in keyof Component.Loaders]: Component.LoadedOf[K]> }, Component.Props > = replayLog as any diff --git a/packages/effect/src/ui/testing/TestHost.ts b/packages/effect/src/ui/testing/TestHost.ts index 86ac7f560b..6b707f322c 100644 --- a/packages/effect/src/ui/testing/TestHost.ts +++ b/packages/effect/src/ui/testing/TestHost.ts @@ -90,8 +90,8 @@ export interface TestElement extends Query.Firable { readonly props: Record readonly children: Array parent: TestElement | undefined - handlers: Renderer.Handlers - eventOptions: Renderer.EventOptions + handlers: Readonly>> + eventOptions: Readonly> dispatch: Renderer.Dispatch | undefined } diff --git a/packages/effect/test/ui/Command.test.ts b/packages/effect/test/ui/Command.test.ts index 199c65ab28..01740521ed 100644 --- a/packages/effect/test/ui/Command.test.ts +++ b/packages/effect/test/ui/Command.test.ts @@ -256,7 +256,7 @@ describe("Command", () => { const queueing = Command.make("Q", { args: { key: Schema.String }, mode: "queue", group: ["key"] }, done) const q = (key: string) => queueing({ key }) const entry = ( - command: Command.Effectful, + command: Command.Issued, status: "running" | "queued" = "running" ): schedule.Entry => ({ name: command.name, diff --git a/packages/effect/test/ui/html/BrowserHost.test.ts b/packages/effect/test/ui/html/BrowserHost.test.ts index 22144c4a84..ed60cb1c21 100644 --- a/packages/effect/test/ui/html/BrowserHost.test.ts +++ b/packages/effect/test/ui/html/BrowserHost.test.ts @@ -771,7 +771,7 @@ describe("BrowserHost", { concurrent: false }, () => { return Effect.acquireRelease(Effect.succeed({ host, dispatch }), () => Effect.sync(() => host.dispose())) } /** The options `onDrop` stores, for a target bound by hand. */ - const accepting = (...accepts: ReadonlyArray): Renderer.EventOptions => { + const accepting = (...accepts: ReadonlyArray): Readonly> => { const drop: Html.DropTargetOptions = { accepts, preventDefault: "always" } return { drop } } diff --git a/packages/effect/typetest/ui/Command.tst.ts b/packages/effect/typetest/ui/Command.tst.ts index 60c2401e82..9ac4fb8fd9 100644 --- a/packages/effect/typetest/ui/Command.tst.ts +++ b/packages/effect/typetest/ui/Command.tst.ts @@ -44,8 +44,12 @@ describe("Command", () => { describe("map", () => { it("keeps the kind and the source result", () => { const toMessage = (text: string) => Message.make({ _tag: "Got", n: text.length }) - expect(Fx().pipe(Command.map(toMessage))).type.toBe>() - expect(St().pipe(Command.map(toMessage))).type.toBe>() + expect(Fx().pipe(Command.map(toMessage))).type.toBe< + Command.Issued + >() + expect(St().pipe(Command.map(toMessage))).type.toBe< + Command.Issued + >() expect(Command.map(Command.interrupt(Fx), toMessage)).type.toBe() }) @@ -53,8 +57,8 @@ describe("Command", () => { const input = condition ? Fx() : St() const mapped = input.pipe(Command.map((text) => Message.make({ _tag: "Got", n: text.length }))) expect(mapped).type.toBe< - | Command.Effectful - | Command.Streaming + | Command.Issued + | Command.Issued >() expect($.run).type.toBeCallableWith(mapped) expect($.run).type.toBeCallableWith(Command.map(input, (text) => Message.make({ _tag: "Got", n: text.length }))) @@ -66,22 +70,27 @@ describe("Command", () => { Command.map((a) => a.length), Command.map((n) => Message.make({ _tag: "Got", n })) ) - expect(chained).type.toBe>() + expect(chained).type.toBe>() const stream = SB.make().pipe( Command.map((n) => n + 1), Command.map((n) => Message.make({ _tag: "Got", n })) ) - expect(stream).type.toBe>() + expect(stream).type.toBe>() }) it("data-first maps keep each branch's source and requirements across a mixed union", () => { const input = condition ? Command.map(SA.make(), (a) => a.length) : SB.make() - expect(input).type.toBe | Command.Streaming>() + expect(input).type.toBe< + Command.Issued | Command.Issued + >() const once = Command.map(input, (n) => n + 1) - expect(once).type.toBe | Command.Streaming>() + expect(once).type.toBe< + Command.Issued | Command.Issued + >() const twice = Command.map(once, (n) => Message.make({ _tag: "Got", n })) expect(twice).type.toBe< - Command.Effectful | Command.Streaming + | Command.Issued + | Command.Issued >() expect($.run).type.toBeCallableWith(twice) // The pipeable form infers one source for its input, so a mixed-source union @@ -93,18 +102,19 @@ describe("Command", () => { describe("as", () => { it("preserves kind, source and requirements in both forms", () => { const message = Message.make({ _tag: "Got", n: 1 }) - expect(SA.make().pipe(Command.as(message))).type.toBe>() - expect(Command.as(SB.make(), message)).type.toBe>() + expect(SA.make().pipe(Command.as(message))).type.toBe>() + expect(Command.as(SB.make(), message)).type.toBe>() expect(Command.as(Command.interrupt(Fx), message)).type.toBe() const input = condition ? SA.make() : SB.make() expect(Command.as(input, message)).type.toBe< - Command.Effectful | Command.Streaming + | Command.Issued + | Command.Issued >() const matched = Save.make({ id: "t1", title: "" }).pipe( Command.match({ onSuccess: () => 1, onFailure: () => 0 }), Command.as(message) ) - expect(matched).type.toBe>() + expect(matched).type.toBe>() expect(Command.as).type.not.toBeCallableWith(Save.make({ id: "t1", title: "" }), message) expect(Command.interrupt(Fx).pipe).type.not.toBeCallableWith(Command.as(message)) }) @@ -210,7 +220,7 @@ describe("Command", () => { }) it("maps both channels, keeping the source result and failure", () => { - expect(matched).type.toBe>() + expect(matched).type.toBe>() expect($.run).type.toBeCallableWith(matched) expect>().type.toBe() }) @@ -233,12 +243,14 @@ describe("Command", () => { Command.afterRefetch("Infinity"), Command.match({ onSuccess: (title) => title.length, onFailure: (error) => error.reason.length }) ) - expect(matched).type.toBe>() - expect(matched.pipe(Command.afterRefetch(5))).type.toBe>() - expect(SA.make().pipe(Command.map((a) => a.length), Command.afterRefetch("1 second"))).type.toBe< - Command.Effectful + expect(matched).type.toBe>() + expect(matched.pipe(Command.afterRefetch(5))).type.toBe< + Command.Issued >() - expect(Command.afterRefetch(SA.make(), "1 second")).type.toBe>() + expect(SA.make().pipe(Command.map((a) => a.length), Command.afterRefetch("1 second"))).type.toBe< + Command.Issued + >() + expect(Command.afterRefetch(SA.make(), "1 second")).type.toBe>() }) it("rejects a Stream Command and a Command.make Command", () => { diff --git a/packages/effect/typetest/ui/Component.tst.ts b/packages/effect/typetest/ui/Component.tst.ts index cdcdb42e78..f1b13eb396 100644 --- a/packages/effect/typetest/ui/Component.tst.ts +++ b/packages/effect/typetest/ui/Component.tst.ts @@ -110,7 +110,7 @@ describe("Component", () => { { args: {} }, () => Effect.map(Scope.Scope, () => Message.make({ _tag: "Done" })) ) - expect(UsesScope()).type.toBe>() + expect(UsesScope()).type.toBe>() const C = Component.make({ name: "C", model: Schema.Struct({}), @@ -141,7 +141,7 @@ describe("Component", () => { describe("Command.Service", () => { it("types the implementation by the message it returns", () => { expect(SaveTodo.make({ id: "t1", title: "milk", done: false })).type.toBe< - Command.Effectful + Command.Issued >() expect(Layer.succeed(SaveTodo)).type.toBeCallableWith(() => Effect.succeed(TodoItemMessage.make({ _tag: "Saved" })) diff --git a/packages/effect/typetest/ui/Loaders.tst.ts b/packages/effect/typetest/ui/Loaders.tst.ts index 72ec729ccd..42200e7d5b 100644 --- a/packages/effect/typetest/ui/Loaders.tst.ts +++ b/packages/effect/typetest/ui/Loaders.tst.ts @@ -103,16 +103,21 @@ describe("Component loaders", () => { expect>().type.toBe<"user" | "team">() expect>().type.toBe() expect>().type.toBe() - expect>().type.toBe() - expect>().type.toBe<{ - readonly user: AsyncResult.AsyncResult - readonly team: AsyncResult.AsyncResult + expect().type.toBe< + { readonly user: Component.Load; readonly team: Component.Load } + >() + expect>().type.toBe>() + expect>().type.toBe<{ + readonly loaded: { + readonly user: AsyncResult.AsyncResult + readonly team: AsyncResult.AsyncResult + } + readonly previous: { + readonly user: Option.Option + readonly team: Option.Option + } + readonly patched: { readonly user: boolean; readonly team: boolean } }>() - expect>().type.toBe<{ - readonly user: Option.Option - readonly team: Option.Option - }>() - expect>().type.toBe<{ readonly user: boolean; readonly team: boolean }>() expect>().type.toBe() expect>().type.toBe() }) @@ -254,11 +259,14 @@ describe("Story loader steps", () => { Component.Model, Component.Message, never, - Component.Loaded> + { + readonly user: AsyncResult.AsyncResult + readonly team: AsyncResult.AsyncResult + } > >() // `loaded()` is typed as `h.loaded` is. - expect(story.loaded().user).type.toBe>() + expect(story.loaded().user).type.toBe>() expect(story.loaded()).type.not.toHaveProperty("n") }) @@ -276,7 +284,7 @@ describe("Story loader steps", () => { it("fromLog types the replay's props and loaded results", () => { const replay = Story.fromLog(Card, []) expect(replay.props).type.toBe<{ readonly id: string }>() - expect(replay.loaded().user).type.toBe>() + expect(replay.loaded().user).type.toBe>() expect(replay.loaded()).type.not.toHaveProperty("n") }) }) @@ -332,21 +340,21 @@ describe("Keyed loaders", () => { expect>().type.toBe() expect>().type.toBe() expect>().type.toBe() - expect["users"]>().type.toBe>() - expect["users"][number]["args"]>().type.toBe<{ readonly id: string }>() - expect["users"][number]["result"]>().type.toBe< + expect>().type.toBe>() + expect[number]["args"]>().type.toBe<{ readonly id: string }>() + expect[number]["result"]>().type.toBe< AsyncResult.AsyncResult >() - expect["users"]["get"]>>().type.toBe< + expect["get"]>>().type.toBe< Option.Option> >() - expect["users"]["get"]>[0]>().type.toBe<{ readonly id: string }>() + expect["get"]>[0]>().type.toBe<{ readonly id: string }>() }) it("have no previous or patched view", () => { type L = Component.Loaders - expect>().type.toBe<{}>() - expect>().type.toBe<{}>() + expect["previous"]>().type.toBe<{}>() + expect["patched"]>().type.toBe<{}>() }) it("a keyed loader's loaded doesn't have its own name", () => { @@ -424,7 +432,7 @@ describe("Keyed loaders", () => { update: (model) => model, view: () => null as never }) - expect>["counts"]>().type.toBe< + expect["counts"]>>().type.toBe< Component.KeyedLoaded >() }) diff --git a/packages/effect/typetest/ui/Router.tst.ts b/packages/effect/typetest/ui/Router.tst.ts index 4e5b3b224f..1046241d60 100644 --- a/packages/effect/typetest/ui/Router.tst.ts +++ b/packages/effect/typetest/ui/Router.tst.ts @@ -21,7 +21,7 @@ describe("Router.patchQuery", () => { { readonly q?: string | undefined; readonly page?: number | undefined } >() expect(Router.patchQuery(AppRoute, search, { q: "a" })).type.toBe< - Command.Effectful + Command.Issued >() expect(Router.patchQuery).type.toBeCallableWith(AppRoute, search, {}) // undefined removes a param, or resets a defaulted one. diff --git a/packages/effect/typetest/ui/Story.tst.ts b/packages/effect/typetest/ui/Story.tst.ts index 6c89b7e687..9f1bc601ae 100644 --- a/packages/effect/typetest/ui/Story.tst.ts +++ b/packages/effect/typetest/ui/Story.tst.ts @@ -105,7 +105,7 @@ describe("Story", () => { ({ name }) => Effect.succeed(`https://cdn/${name}`) ) const mapped = UploadFile({ name: "me.png" }).pipe(Command.map((url) => Uploaded.make({ _tag: "Uploaded", url }))) - expect(mapped).type.toBe>() + expect(mapped).type.toBe>() expect(counter.resolve).type.toBeCallableWith(mapped, "https://cdn/me.png") expect(counter.resolve).type.not.toBeCallableWith(mapped, Uploaded.make({ _tag: "Uploaded", url: "" })) const Progress = Command.make(