effect-ui-tsx 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +295 -2
- package/dist/component/component.d.ts +56 -0
- package/dist/component/component.js +86 -0
- package/dist/component/component.js.map +1 -0
- package/dist/controller/controller.d.ts +157 -0
- package/dist/controller/controller.js +55 -0
- package/dist/controller/controller.js.map +1 -0
- package/dist/controller/operation.d.ts +61 -0
- package/dist/controller/operation.js +47 -0
- package/dist/controller/operation.js.map +1 -0
- package/dist/controller/queries.d.ts +48 -0
- package/dist/controller/queries.js +2 -0
- package/dist/controller/queries.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/model/model.d.ts +22 -0
- package/dist/model/model.js +21 -0
- package/dist/model/model.js.map +1 -0
- package/dist/runtime/jsx-dev-runtime.d.ts +1 -0
- package/dist/runtime/jsx-dev-runtime.js +2 -0
- package/dist/runtime/jsx-dev-runtime.js.map +1 -0
- package/dist/runtime/jsx-runtime.d.ts +25 -0
- package/dist/runtime/jsx-runtime.js +178 -0
- package/dist/runtime/jsx-runtime.js.map +1 -0
- package/dist/runtime/jsx.d.ts +148 -0
- package/dist/runtime/jsx.js +2 -0
- package/dist/runtime/jsx.js.map +1 -0
- package/package.json +42 -3
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { helpers } from "./operation.js";
|
|
2
|
+
/**
|
|
3
|
+
* Starts a feature's controller from its model. Declare `.props()`, `.events()`, `.slots()`,
|
|
4
|
+
* `.queries()`, `.head()` and `.init()` as needed, then finish with `.actions()`.
|
|
5
|
+
* `.subscriptions()` may follow it.
|
|
6
|
+
*/
|
|
7
|
+
export function controller(model) {
|
|
8
|
+
// Each step returns a new builder, so the type parameters it widens are stated by the interface.
|
|
9
|
+
const builder = (declared) => ({
|
|
10
|
+
props: (props) => builder({ ...declared, props }),
|
|
11
|
+
events: (events) => builder({ ...declared, events }),
|
|
12
|
+
slots: (...slots) => builder({ ...declared, slots }),
|
|
13
|
+
components: (components) => builder({ ...declared, components }),
|
|
14
|
+
reads: (reads) => builder({ ...declared, reads }),
|
|
15
|
+
queries: (queries) => builder({ ...declared, queries: { ...declared.queries, ...queries } }),
|
|
16
|
+
// Effect UI tells the two kinds apart by the name of the entry's query field.
|
|
17
|
+
keyedQueries: (queries) => builder({
|
|
18
|
+
...declared,
|
|
19
|
+
queries: {
|
|
20
|
+
...declared.queries,
|
|
21
|
+
...Object.fromEntries(Object.entries(queries).map(([name, { query, args }]) => [name, { queryMany: query, args }])),
|
|
22
|
+
},
|
|
23
|
+
}),
|
|
24
|
+
head: (head) => builder({ ...declared, head }),
|
|
25
|
+
init: (init) => builder({ ...declared, init }),
|
|
26
|
+
restore: (restore) => builder({ ...declared, restore }),
|
|
27
|
+
actions: (configure) => {
|
|
28
|
+
// Both steps after .actions() return the controller with one more thing declared.
|
|
29
|
+
const finish = (finished) => ({
|
|
30
|
+
...finished,
|
|
31
|
+
queryChanges: (changes) => finish({ ...finished, changes }),
|
|
32
|
+
subscriptions: (subscribed) => ({ ...finished, subscribed }),
|
|
33
|
+
});
|
|
34
|
+
return finish({
|
|
35
|
+
...declared,
|
|
36
|
+
model,
|
|
37
|
+
operations: configure(helpers(model, declared.init)),
|
|
38
|
+
changes: undefined,
|
|
39
|
+
subscribed: undefined,
|
|
40
|
+
});
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
return builder({
|
|
44
|
+
props: {},
|
|
45
|
+
events: {},
|
|
46
|
+
slots: [],
|
|
47
|
+
components: {},
|
|
48
|
+
reads: {},
|
|
49
|
+
queries: {},
|
|
50
|
+
head: undefined,
|
|
51
|
+
init: undefined,
|
|
52
|
+
restore: undefined,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
//# sourceMappingURL=controller.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"controller.js","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AAS;KACnB,CAAiC,CAAA;AACpC,CAAC","sourcesContent":["import type { Command, Component, Subscription, UiSchema, View } from \"@effect/ui\"\nimport type { Html } from \"@effect/ui/html\"\nimport { helpers, type AnyOperation, type Cases, type Helpers } from \"src/controller/operation\"\nimport type {\n Keyed,\n KeyedDeclarations,\n Queries,\n QueryDeclarations,\n QueryResults,\n Single,\n} from \"src/controller/queries\"\nimport type { Fields, Model, State } from \"src/model/model\"\n\n/** The tagged union of a controller's messages. */\nexport type Message<O> = {\n [K in keyof O & string]: O[K] extends { payload: infer P extends Fields }\n ? { readonly _tag: K } & State<P>\n : never\n}[keyof O & string]\n/** The union of the commands a controller's operations run. */\nexport type CommandOf<O> = {\n [K in keyof O]: O[K] extends { commands: ReadonlyArray<(...args: never[]) => infer C> }\n ? C\n : never\n}[keyof O]\n\n/** The message each action builds, by action name. Subscriptions send these. */\nexport type Messages<O> = {\n [K in keyof O & string]: O[K] extends { args: (...args: infer Args) => unknown }\n ? (...args: Args) => Extract<Message<O>, { readonly _tag: K }>\n : never\n}\n/** The components a feature embeds without providing their Layers itself, by name. */\nexport type Components = Record<string, Component.Any>\n// The same component with nothing left to provide, which is what makes it usable as a tag. Its\n// requirements have become the embedding feature's own.\ntype Embedded<C> =\n C extends Component.Component<\n infer Name,\n infer Props,\n infer Model,\n infer Message,\n infer Events,\n infer _Requirements,\n infer Elements,\n infer QueryList,\n infer Slot\n >\n ? Component.Component<Name, Props, Model, Message, Events, never, Elements, QueryList, Slot>\n : never\nexport type EmbeddedAll<Parts> = { readonly [Name in keyof Parts]: Embedded<Parts[Name]> }\ntype RequiredBy<Parts> = { [Name in keyof Parts]: Component.Requirements<Parts[Name]> }[keyof Parts]\n\n/** The owners of shared state a feature reads, by the name it reads each under. */\nexport type Reads = Readonly<Record<string, Component.Shared.Owner>>\n\n// What a feature's head and subscriptions are declared from.\ntype Shown<F extends Fields, PR extends Fields, Q> = {\n state: State<F>\n props: State<PR>\n queries: QueryResults<Q>\n}\ntype Current<F extends Fields, PR extends Fields, O, Q> = Shown<F, PR, Q> & { action: Messages<O> }\ntype Declared<Sub> = ReadonlyArray<Sub | false | null | undefined>\n// A list with nothing but switched-off entries gives TypeScript no subscription to read from.\ntype Known<R> = unknown extends R ? never : R\ntype HeadEntries = ReadonlyArray<View.HeadEntry<Html.Elements>>\n\n// Reports a misnamed or misbuilt operation at its own property.\ntype Checked<O> = {\n [K in keyof O]: K extends UiSchema.TaggedEnum.ReservedTag\n ? \"This action name is reserved by Effect UI\"\n : O[K] extends AnyOperation\n ? [CommandOf<Pick<O, K>>] extends [Command.Command<Message<O> | Component.Noop, unknown>]\n ? O[K]\n : \"A command must resolve to one of this controller's messages\"\n : \"Build each operation with on, set or reset\"\n}\n// A parent passes props to a child tag next to its key and its on… event handlers.\ntype PropNames<P> = {\n [K in keyof P]: K extends \"key\" | \"children\" | \"fallback\" | \"slots\" | \"view\" | \"keepAlive\"\n ? \"This prop name is taken by JSX\"\n : K extends `on${infer Rest}`\n ? Rest extends \"\" | Uncapitalize<Rest>\n ? P[K]\n : Rest extends Capitalize<Rest>\n ? \"A prop cannot be named like an event handler\"\n : P[K]\n : P[K]\n}\ntype Unreserved<E> = {\n [K in keyof E]: K extends \"failure\" | \"Failure\"\n ? \"A tag already uses onFailure for a failed child\"\n : K extends UiSchema.TaggedEnum.ReservedTag\n ? \"This event name is reserved by Effect UI\"\n : E[K]\n}\n\n/** A model together with the messages that change it, and what the feature takes and reports. */\nexport interface Controller<\n F extends Fields,\n PR extends Fields,\n E extends Cases,\n O,\n Slot extends string = never,\n SR = never,\n Q = {},\n Parts = {},\n Rd = {},\n> {\n model: Model<F>\n props: PR\n events: E\n init: ((props: State<PR>) => State<F>) | undefined\n restore: false | ((state: State<F>, props: State<PR>) => State<F>) | undefined\n slots: ReadonlyArray<Slot>\n components: Parts\n reads: Rd\n queries: Readonly<Record<string, object>>\n head: ((shown: Shown<F, PR, Q>) => HeadEntries) | undefined\n operations: O\n /** The action to send when a query's result changes, by query name, if any is declared. */\n changes:\n | ((current: { action: Messages<O> }) => Readonly<Record<string, (result: never) => unknown>>)\n | undefined\n /** The subscriptions the feature wants for its current state and props, if it declares any. */\n subscribed:\n | ((current: Current<F, PR, O, Q>) => Declared<Subscription.Subscription<any, SR>>)\n | undefined\n}\n\n/** A controller whose actions are named. It may still declare subscriptions that send them. */\nexport interface ActionsController<\n F extends Fields,\n PR extends Fields,\n E extends Cases,\n O,\n Slot extends string,\n Q,\n Parts,\n Rd,\n> extends Controller<\n F,\n PR,\n E,\n O,\n Slot,\n RequiredBy<Parts> | Component.SharedRequirements<Rd>,\n Q,\n Parts,\n Rd\n> {\n /**\n * Declares what the feature listens to while it is mounted, from its state and props: a stream\n * made with `Subscription.make`, or a document or window event. An entry that is `false` is off.\n * Each subscription sends the feature's own actions.\n */\n /**\n * Sends an action when a query's result changes, for a transition that has to react to it, such\n * as moving focus once a load fails. Showing a result needs none of this: the view reads it.\n */\n queryChanges(\n react: (current: { action: Messages<O> }) => {\n readonly [Name in keyof Q]?: (result: QueryResults<Q>[Name]) => Message<O>\n },\n ): ActionsController<F, PR, E, O, Slot, Q, Parts, Rd>\n subscriptions<Sub extends Subscription.Subscription<Message<O>, any> = never>(\n declare: (current: Current<F, PR, O, Q>) => Declared<Sub>,\n ): Controller<\n F,\n PR,\n E,\n O,\n Slot,\n RequiredBy<Parts> | Component.SharedRequirements<Rd> | Known<Subscription.Requirements<Sub>>,\n Q,\n Parts,\n Rd\n >\n}\n\nexport interface ControllerBuilder<\n F extends Fields,\n PR extends Fields,\n E extends Cases,\n Slot extends string = never,\n Q = {},\n Parts = {},\n Rd = {},\n> {\n /** Declares what the parent passes in: an Effect schema for each prop. */\n props<const P extends Fields>(\n props: P & PropNames<P>,\n ): ControllerBuilder<F, P, E, Slot, Q, Parts, Rd>\n /** Declares what the feature reports to its parent: a payload for each event. */\n events<const Ev extends Cases>(\n events: Ev & Unreserved<Ev>,\n ): ControllerBuilder<F, PR, Ev, Slot, Q, Parts, Rd>\n /** Computes the initial state from the props the feature mounts with. */\n init(init: (props: State<PR>) => State<F>): ControllerBuilder<F, PR, E, Slot, Q, Parts, Rd>\n /**\n * Says what the router saves of this feature's state when the user leaves a page, to bring back\n * on Back and Forward. By default it saves all of it. Return the state without what must not\n * come back, such as a password or a flag for a request that is no longer running, or pass\n * `false` to start over each time.\n */\n restore(\n save: false | ((state: State<F>, props: State<PR>) => State<F>),\n ): ControllerBuilder<F, PR, E, Slot, Q, Parts, Rd>\n /**\n * Names the slots a parent may fill with its own markup. The view places each one where it\n * belongs. A slot named `children` is filled by the children of the feature's tag.\n */\n slots<const Name extends string>(\n ...names: ReadonlyArray<Name>\n ): ControllerBuilder<F, PR, E, Name, Q, Parts, Rd>\n /**\n * Declares the data the feature shows, by name. Effect UI fetches each query with the args its\n * entry returns, keeps the latest result beside the model, and renders again when it changes.\n * The view reads the results as `queries`.\n */\n queries<Next extends Queries>(\n queries: QueryDeclarations<F, PR, Next, Q>,\n ): ControllerBuilder<F, PR, E, Slot, Q & Single<Next>, Parts, Rd>\n /**\n * Declares queries read once for each of several args, such as one user per id in a list. The\n * view reads each as a list of entries, one per args, each with its own result.\n */\n keyedQueries<Next extends Queries>(\n queries: KeyedDeclarations<F, PR, Next, Q>,\n ): ControllerBuilder<F, PR, E, Slot, Q & Keyed<Next>, Parts, Rd>\n /**\n * Declares components the feature embeds while leaving their Layers to whoever mounts it. What\n * they require becomes a requirement of this feature, and the view receives them as\n * `components`, ready to use as tags. Use it to provide a Layer once, where the feature is\n * mounted, instead of on each child.\n */\n components<const Next extends Components>(\n components: Next,\n ): ControllerBuilder<F, PR, E, Slot, Q, Next, Rd>\n /**\n * Declares the shared state the feature reads, by name. An owner is a component that shares its\n * state with everything inside it, or an atom shared with `UiAtom.share`. The view reads each\n * value as `reads.name`, and renders again when it changes.\n */\n reads<const Next extends Reads>(reads: Next): ControllerBuilder<F, PR, E, Slot, Q, Parts, Next>\n /**\n * Declares what the feature puts in the document head, such as the title, from its state, props\n * and query results.\n */\n head(\n head: (shown: Shown<F, PR, Q>) => HeadEntries,\n ): ControllerBuilder<F, PR, E, Slot, Q, Parts, Rd>\n /**\n * Names each message and says how it changes the model, which finishes the controller. Every key\n * of the returned object becomes a message tag and an action of the same name in the view.\n */\n // O is unconstrained so that run() infers its command from its argument alone. Checked<O> verifies it.\n actions<O>(\n configure: (helpers: Helpers<F, PR, E, Q>) => O & Checked<NoInfer<O>>,\n ): ActionsController<F, PR, E, O, Slot, Q, Parts, Rd>\n}\n\n// What the steps before .actions() have declared so far.\ninterface Declarations {\n props: Fields\n events: Cases\n slots: ReadonlyArray<string>\n components: Components\n reads: Reads\n queries: Record<string, object>\n head: ((shown: any) => HeadEntries) | undefined\n init: ((props: any) => unknown) | undefined\n restore: false | ((state: any, props: any) => unknown) | undefined\n}\n\n/**\n * Starts a feature's controller from its model. Declare `.props()`, `.events()`, `.slots()`,\n * `.queries()`, `.head()` and `.init()` as needed, then finish with `.actions()`.\n * `.subscriptions()` may follow it.\n */\nexport function controller<F extends Fields>(model: Model<F>): ControllerBuilder<F, {}, {}> {\n // Each step returns a new builder, so the type parameters it widens are stated by the interface.\n const builder = (declared: Declarations): ControllerBuilder<F, any, any, any, any, any, any> => ({\n props: (props) => builder({ ...declared, props }),\n events: (events) => builder({ ...declared, events }),\n slots: (...slots) => builder({ ...declared, slots }),\n components: (components) => builder({ ...declared, components }),\n reads: (reads) => builder({ ...declared, reads }),\n queries: (queries) => builder({ ...declared, queries: { ...declared.queries, ...queries } }),\n // Effect UI tells the two kinds apart by the name of the entry's query field.\n keyedQueries: (queries) =>\n builder({\n ...declared,\n queries: {\n ...declared.queries,\n ...Object.fromEntries(\n Object.entries<{ query: unknown; args: unknown }>(queries).map(\n ([name, { query, args }]) => [name, { queryMany: query, args }],\n ),\n ),\n },\n }),\n head: (head) => builder({ ...declared, head }),\n init: (init) => builder({ ...declared, init }),\n restore: (restore) => builder({ ...declared, restore }),\n actions: (configure) => {\n // Both steps after .actions() return the controller with one more thing declared.\n const finish = (finished: object): any => ({\n ...finished,\n queryChanges: (changes: unknown) => finish({ ...finished, changes }),\n subscriptions: (subscribed: unknown) => ({ ...finished, subscribed }),\n })\n return finish({\n ...declared,\n model,\n operations: configure(helpers(model, declared.init as never)),\n changes: undefined,\n subscribed: undefined,\n })\n },\n })\n return builder({\n props: {},\n events: {},\n slots: [],\n components: {},\n reads: {},\n queries: {},\n head: undefined,\n init: undefined,\n restore: undefined,\n }) as ControllerBuilder<F, {}, {}>\n}\n"]}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { Component } from "@effect/ui";
|
|
2
|
+
import type { QueryResults } from "./queries.js";
|
|
3
|
+
import type { Fields, Model, State } from "../model/model.js";
|
|
4
|
+
/** A table of tagged payloads. A controller declares its events with one, like its messages. */
|
|
5
|
+
export type Cases = Record<string, Fields>;
|
|
6
|
+
/** The tagged union an event table describes. */
|
|
7
|
+
export type EventOf<E extends Cases> = {
|
|
8
|
+
[K in keyof E & string]: {
|
|
9
|
+
readonly _tag: K;
|
|
10
|
+
} & State<E[K]>;
|
|
11
|
+
}[keyof E & string];
|
|
12
|
+
type Context<PR, QR> = keyof PR extends never ? [props?: PR, queries?: QR] : [props: PR, queries?: QR];
|
|
13
|
+
/** One message of a feature: its payload schema, its action's arguments and what follows it. */
|
|
14
|
+
export interface Operation<S, PR, E extends Cases, P extends Fields, Args extends ReadonlyArray<unknown>, C = never, Q = {}> {
|
|
15
|
+
payload: P;
|
|
16
|
+
args: (...args: Args) => State<P>;
|
|
17
|
+
update: (state: S, message: State<P>, ...context: Context<PR, QueryResults<Q>>) => S;
|
|
18
|
+
/** The commands run after the transition, in the order they were added. */
|
|
19
|
+
commands: ReadonlyArray<(next: S, message: State<P>, props: PR, queries: QueryResults<Q>) => C>;
|
|
20
|
+
/** The events reported after the transition. A payload of undefined reports nothing. */
|
|
21
|
+
emits: ReadonlyArray<{
|
|
22
|
+
tag: keyof E & string;
|
|
23
|
+
payload: (next: S, message: State<P>, props: PR, queries: QueryResults<Q>) => unknown;
|
|
24
|
+
}>;
|
|
25
|
+
/** What the transition asks of the feature's infinite queries. */
|
|
26
|
+
lists: ReadonlyArray<{
|
|
27
|
+
readonly issue: "loadMore" | "reset";
|
|
28
|
+
readonly query: string;
|
|
29
|
+
}>;
|
|
30
|
+
/** Whether commands and events are skipped when the transition returned the model unchanged. */
|
|
31
|
+
whenChanged: boolean;
|
|
32
|
+
/** Runs a command after the transition. It must resolve to one of the controller's messages. */
|
|
33
|
+
run<C2>(command: (next: S, message: State<P>, props: PR, queries: QueryResults<Q>) => C2): Operation<S, PR, E, P, Args, C | C2, Q>;
|
|
34
|
+
/** Reports an event to the parent after the transition. Return undefined to stay silent. */
|
|
35
|
+
emit<K extends keyof E & string>(tag: K, payload: (next: S, message: State<P>, props: PR, queries: QueryResults<Q>) => State<E[K]> | undefined): Operation<S, PR, E, P, Args, C, Q>;
|
|
36
|
+
/** Loads the next page of one of the feature's infinite queries. */
|
|
37
|
+
loadMore(query: Component.InfiniteNames<Q>): Operation<S, PR, E, P, Args, C, Q>;
|
|
38
|
+
/** Drops the pages of one of the feature's infinite queries and loads the first again. */
|
|
39
|
+
reload(query: Component.InfiniteNames<Q>): Operation<S, PR, E, P, Args, C, Q>;
|
|
40
|
+
}
|
|
41
|
+
export type AnyOperation = Operation<any, any, any, any, any, unknown, any>;
|
|
42
|
+
type Field<F extends Fields> = Extract<keyof State<F> & keyof F, string>;
|
|
43
|
+
type Payload<P extends Fields> = keyof P extends never ? [] : [payload: State<P>];
|
|
44
|
+
export interface Helpers<F extends Fields, PR extends Fields, E extends Cases, Q = {}> {
|
|
45
|
+
/**
|
|
46
|
+
* A message with its own payload and a pure transition. The transition also receives the props
|
|
47
|
+
* and the latest query results.
|
|
48
|
+
*/
|
|
49
|
+
on<const P extends Fields>(payload: P, update: (state: State<F>, message: State<P>, props: State<PR>, queries: QueryResults<Q>) => State<F>): Operation<State<F>, State<PR>, E, P, Payload<P>, never, Q>;
|
|
50
|
+
/** Replaces one field with the action's argument. Does nothing more if the value is the same. */
|
|
51
|
+
set<K extends Field<F>>(field: K): Operation<State<F>, State<PR>, E, {
|
|
52
|
+
value: F[K];
|
|
53
|
+
}, [value: State<F>[K]], never, Q>;
|
|
54
|
+
/**
|
|
55
|
+
* Restores one field to its initial value, which `.init()` may compute from the props. Does
|
|
56
|
+
* nothing more if it already has it.
|
|
57
|
+
*/
|
|
58
|
+
reset(field: Field<F>): Operation<State<F>, State<PR>, E, {}, [], never, Q>;
|
|
59
|
+
}
|
|
60
|
+
export declare function helpers<F extends Fields, PR extends Fields, E extends Cases, Q>(model: Model<F>, init: ((props: State<PR>) => State<F>) | undefined): Helpers<F, PR, E, Q>;
|
|
61
|
+
export {};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// An operation is immutable: run and emit return a new one. Its precise type is stated by Helpers,
|
|
2
|
+
// which TypeScript cannot follow through the generic payload and field schemas.
|
|
3
|
+
function operation({ commands = [], emits = [], lists = [], ...parts }) {
|
|
4
|
+
const next = (changed) => operation({ ...parts, commands, emits, lists, ...changed });
|
|
5
|
+
return {
|
|
6
|
+
...parts,
|
|
7
|
+
commands,
|
|
8
|
+
emits,
|
|
9
|
+
lists,
|
|
10
|
+
run: (command) => next({ commands: [...commands, command] }),
|
|
11
|
+
emit: (tag, payload) => next({ emits: [...emits, { tag, payload }] }),
|
|
12
|
+
loadMore: (query) => next({ lists: [...lists, { issue: "loadMore", query }] }),
|
|
13
|
+
reload: (query) => next({ lists: [...lists, { issue: "reset", query }] }),
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
function replaceField(state, key, value) {
|
|
17
|
+
if (Object.is(state[key], value)) {
|
|
18
|
+
return state;
|
|
19
|
+
}
|
|
20
|
+
return { ...state, [key]: value };
|
|
21
|
+
}
|
|
22
|
+
export function helpers(model, init) {
|
|
23
|
+
// What a field starts as: the model's own value, or the one computed from the props.
|
|
24
|
+
const initial = (props) => (init ? init(props) : model.initial);
|
|
25
|
+
return {
|
|
26
|
+
on: (payload, update) => operation({
|
|
27
|
+
payload,
|
|
28
|
+
update,
|
|
29
|
+
whenChanged: false,
|
|
30
|
+
// An action without a payload ignores its arguments, so it can serve directly as an event handler.
|
|
31
|
+
args: (...[message]) => (Object.keys(payload).length ? message : {}),
|
|
32
|
+
}),
|
|
33
|
+
set: (field) => operation({
|
|
34
|
+
payload: { value: model.fields[field] },
|
|
35
|
+
whenChanged: true,
|
|
36
|
+
args: (value) => ({ value }),
|
|
37
|
+
update: (state, message) => replaceField(state, field, message.value),
|
|
38
|
+
}),
|
|
39
|
+
reset: (field) => operation({
|
|
40
|
+
payload: {},
|
|
41
|
+
whenChanged: true,
|
|
42
|
+
args: () => ({}),
|
|
43
|
+
update: (state, _message, props) => replaceField(state, field, initial(props)[field]),
|
|
44
|
+
}),
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=operation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"operation.js","sourceRoot":"","sources":["../../src/controller/operation.ts"],"names":[],"mappings":"AAoGA,mGAAmG;AACnG,gFAAgF;AAChF,SAAS,SAAS,CAAC,EAAE,QAAQ,GAAG,EAAE,EAAE,KAAK,GAAG,EAAE,EAAE,KAAK,GAAG,EAAE,EAAE,GAAG,KAAK,EAAS;IAC3E,MAAM,IAAI,GAAG,CAAC,OAAuB,EAAE,EAAE,CACvC,SAAS,CAAC,EAAE,GAAG,KAAK,EAAE,QAAQ,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,OAAO,EAAE,CAAC,CAAA;IAC7D,OAAO;QACL,GAAG,KAAK;QACR,QAAQ;QACR,KAAK;QACL,KAAK;QACL,GAAG,EAAE,CAAC,OAAyC,EAAE,EAAE,CAAC,IAAI,CAAC,EAAE,QAAQ,EAAE,CAAC,GAAG,QAAQ,EAAE,OAAO,CAAC,EAAE,CAAC;QAC9F,IAAI,EAAE,CAAC,GAAW,EAAE,OAAiD,EAAE,EAAE,CACvE,I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,CAAC,CAAC,EAAE,CAAC;SACrE,CAAC;QACJ,GAAG,EAAE,CAAC,KAAK,EAAE,EAAE,CACb,SAAS,CAAC;YACR,OAAO,EAAE,EAAE,KAAK,EAAE,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE;YACvC,WAAW,EAAE,IAAI;YACjB,IAAI,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC;YAC5B,MAAM,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE,CAAC,YAAY,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,CAAC;SACtE,CAAC;QACJ,KAAK,EAAE,CAAC,KAAK,EAAE,EAAE,CACf,SAAS,CAAC;YACR,OAAO,EAAE,EAAE;YACX,WAAW,EAAE,IAAI;YACjB,IAAI,EAAE,GAAG,EAAE,CAAC,CAAC,EAAE,CAAC;YAChB,MAAM,EAAE,CAAC,KAAK,EAAE,QAAQ,EAAE,KAAK,EAAE,EAAE,CAAC,YAAY,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC;SACtF,CAAC;KACL,CAAA;AACH,CAAC","sourcesContent":["import type { Component } from \"@effect/ui\"\nimport type { QueryResults } from \"src/controller/queries\"\nimport type { Fields, Model, State } from \"src/model/model\"\n\n/** A table of tagged payloads. A controller declares its events with one, like its messages. */\nexport type Cases = Record<string, Fields>\n/** The tagged union an event table describes. */\nexport type EventOf<E extends Cases> = {\n [K in keyof E & string]: { readonly _tag: K } & State<E[K]>\n}[keyof E & string]\n\n// What a transition reads next to the state and the message. A feature without props takes none,\n// so its transitions also run as update(state, message).\ntype Context<PR, QR> = keyof PR extends never\n ? [props?: PR, queries?: QR]\n : [props: PR, queries?: QR]\n\n/** One message of a feature: its payload schema, its action's arguments and what follows it. */\nexport interface Operation<\n S,\n PR,\n E extends Cases,\n P extends Fields,\n Args extends ReadonlyArray<unknown>,\n C = never,\n Q = {},\n> {\n payload: P\n args: (...args: Args) => State<P>\n update: (state: S, message: State<P>, ...context: Context<PR, QueryResults<Q>>) => S\n /** The commands run after the transition, in the order they were added. */\n commands: ReadonlyArray<(next: S, message: State<P>, props: PR, queries: QueryResults<Q>) => C>\n /** The events reported after the transition. A payload of undefined reports nothing. */\n emits: ReadonlyArray<{\n tag: keyof E & string\n payload: (next: S, message: State<P>, props: PR, queries: QueryResults<Q>) => unknown\n }>\n /** What the transition asks of the feature's infinite queries. */\n lists: ReadonlyArray<{ readonly issue: \"loadMore\" | \"reset\"; readonly query: string }>\n /** Whether commands and events are skipped when the transition returned the model unchanged. */\n whenChanged: boolean\n /** Runs a command after the transition. It must resolve to one of the controller's messages. */\n run<C2>(\n command: (next: S, message: State<P>, props: PR, queries: QueryResults<Q>) => C2,\n ): Operation<S, PR, E, P, Args, C | C2, Q>\n /** Reports an event to the parent after the transition. Return undefined to stay silent. */\n emit<K extends keyof E & string>(\n tag: K,\n payload: (\n next: S,\n message: State<P>,\n props: PR,\n queries: QueryResults<Q>,\n ) => State<E[K]> | undefined,\n ): Operation<S, PR, E, P, Args, C, Q>\n /** Loads the next page of one of the feature's infinite queries. */\n loadMore(query: Component.InfiniteNames<Q>): Operation<S, PR, E, P, Args, C, Q>\n /** Drops the pages of one of the feature's infinite queries and loads the first again. */\n reload(query: Component.InfiniteNames<Q>): Operation<S, PR, E, P, Args, C, Q>\n}\nexport type AnyOperation = Operation<any, any, any, any, any, unknown, any>\n\ntype Field<F extends Fields> = Extract<keyof State<F> & keyof F, string>\ntype Payload<P extends Fields> = keyof P extends never ? [] : [payload: State<P>]\n\nexport interface Helpers<F extends Fields, PR extends Fields, E extends Cases, Q = {}> {\n /**\n * A message with its own payload and a pure transition. The transition also receives the props\n * and the latest query results.\n */\n on<const P extends Fields>(\n payload: P,\n update: (\n state: State<F>,\n message: State<P>,\n props: State<PR>,\n queries: QueryResults<Q>,\n ) => State<F>,\n ): Operation<State<F>, State<PR>, E, P, Payload<P>, never, Q>\n /** Replaces one field with the action's argument. Does nothing more if the value is the same. */\n set<K extends Field<F>>(\n field: K,\n ): Operation<State<F>, State<PR>, E, { value: F[K] }, [value: State<F>[K]], never, Q>\n /**\n * Restores one field to its initial value, which `.init()` may compute from the props. Does\n * nothing more if it already has it.\n */\n reset(field: Field<F>): Operation<State<F>, State<PR>, E, {}, [], never, Q>\n}\n\ninterface Parts {\n payload: Fields\n args: (...args: any[]) => unknown\n update: (state: any, message: any, props: any, queries: any) => unknown\n whenChanged: boolean\n commands?: AnyOperation[\"commands\"]\n emits?: AnyOperation[\"emits\"]\n lists?: AnyOperation[\"lists\"]\n}\n\n// An operation is immutable: run and emit return a new one. Its precise type is stated by Helpers,\n// which TypeScript cannot follow through the generic payload and field schemas.\nfunction operation({ commands = [], emits = [], lists = [], ...parts }: Parts): any {\n const next = (changed: Partial<Parts>) =>\n operation({ ...parts, commands, emits, lists, ...changed })\n return {\n ...parts,\n commands,\n emits,\n lists,\n run: (command: AnyOperation[\"commands\"][number]) => next({ commands: [...commands, command] }),\n emit: (tag: string, payload: AnyOperation[\"emits\"][number][\"payload\"]) =>\n next({ emits: [...emits, { tag, payload }] }),\n loadMore: (query: string) => next({ lists: [...lists, { issue: \"loadMore\", query }] }),\n reload: (query: string) => next({ lists: [...lists, { issue: \"reset\", query }] }),\n }\n}\n\nfunction replaceField<S, K extends keyof S>(state: S, key: K, value: S[K]): S {\n if (Object.is(state[key], value)) {\n return state\n }\n return { ...state, [key]: value }\n}\n\nexport function helpers<F extends Fields, PR extends Fields, E extends Cases, Q>(\n model: Model<F>,\n init: ((props: State<PR>) => State<F>) | undefined,\n): Helpers<F, PR, E, Q> {\n // What a field starts as: the model's own value, or the one computed from the props.\n const initial = (props: State<PR>) => (init ? init(props) : model.initial)\n return {\n on: (payload, update) =>\n operation({\n payload,\n update,\n whenChanged: false,\n // An action without a payload ignores its arguments, so it can serve directly as an event handler.\n args: (...[message]) => (Object.keys(payload).length ? message : {}),\n }),\n set: (field) =>\n operation({\n payload: { value: model.fields[field] },\n whenChanged: true,\n args: (value) => ({ value }),\n update: (state, message) => replaceField(state, field, message.value),\n }),\n reset: (field) =>\n operation({\n payload: {},\n whenChanged: true,\n args: () => ({}),\n update: (state, _message, props) => replaceField(state, field, initial(props)[field]),\n }),\n }\n}\n"]}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { Component, Query } from "@effect/ui";
|
|
2
|
+
import type { Duration } from "effect";
|
|
3
|
+
import type { Fields, State } from "../model/model.js";
|
|
4
|
+
type Reads<F extends Fields, PR extends Fields, Before> = {
|
|
5
|
+
state: State<F>;
|
|
6
|
+
props: State<PR>;
|
|
7
|
+
queries: QueryResults<Before>;
|
|
8
|
+
};
|
|
9
|
+
/** Query services by the name a feature gives each one. */
|
|
10
|
+
export type Queries = Record<string, Query.Query.Any>;
|
|
11
|
+
/** How a feature declares queries it reads once each. */
|
|
12
|
+
export type QueryDeclarations<F extends Fields, PR extends Fields, Q, Before = {}> = {
|
|
13
|
+
readonly [Name in keyof Q]: {
|
|
14
|
+
/** A query service made with `Query.Service`, or a list made with `Query.InfiniteService`. */
|
|
15
|
+
readonly query: Q[Name];
|
|
16
|
+
/** The args to read the query with. Return undefined to leave the query off. */
|
|
17
|
+
readonly args: (current: Reads<F, PR, Before>) => Query.Query.Args<Q[Name]> | undefined;
|
|
18
|
+
/** Waits for the args to stay the same this long before fetching. */
|
|
19
|
+
readonly debounce?: Duration.Input;
|
|
20
|
+
/** Shows no result while new args load, instead of the previous one. */
|
|
21
|
+
readonly dropPrevious?: true;
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
/** How a feature declares queries it reads once for each of several args. */
|
|
25
|
+
export type KeyedDeclarations<F extends Fields, PR extends Fields, Q, Before = {}> = {
|
|
26
|
+
readonly [Name in keyof Q]: {
|
|
27
|
+
readonly query: Q[Name];
|
|
28
|
+
/** The args to read the query with, one entry of the result for each. */
|
|
29
|
+
readonly args: (current: Reads<F, PR, Before>) => ReadonlyArray<Query.Query.Args<Q[Name]>>;
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
/** Queries read once each, as Effect UI lists them on a component. */
|
|
33
|
+
export type Single<Q> = {
|
|
34
|
+
readonly [Name in keyof Q]: Q[Name] extends Query.Query.Any ? Component.QueryOne<Q[Name]> : never;
|
|
35
|
+
};
|
|
36
|
+
/** Queries read once per args, as Effect UI lists them on a component. */
|
|
37
|
+
export type Keyed<Q> = {
|
|
38
|
+
readonly [Name in keyof Q]: Q[Name] extends Query.Query.Any ? Component.QueryMany<Q[Name]> : never;
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* The latest result of each query. A query is an `AsyncResult`: initial, a success or a failure.
|
|
42
|
+
* A keyed query is a list of entries, each with its args and its own result. An infinite query
|
|
43
|
+
* succeeds with its `items`, its `pages` and whether there are `more`.
|
|
44
|
+
*/
|
|
45
|
+
export type QueryResults<Q> = {
|
|
46
|
+
readonly [Name in keyof Q]: Component.QueryResultOf<Q[Name]>;
|
|
47
|
+
};
|
|
48
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"queries.js","sourceRoot":"","sources":["../../src/controller/queries.ts"],"names":[],"mappings":"","sourcesContent":["import type { Component, Query } from \"@effect/ui\"\nimport type { Duration } from \"effect\"\nimport type { Fields, State } from \"src/model/model\"\n\n// What a query's args are read from: the state, the props, and the results of the queries\n// declared in earlier steps.\ntype Reads<F extends Fields, PR extends Fields, Before> = {\n state: State<F>\n props: State<PR>\n queries: QueryResults<Before>\n}\n\n/** Query services by the name a feature gives each one. */\nexport type Queries = Record<string, Query.Query.Any>\n\n/** How a feature declares queries it reads once each. */\nexport type QueryDeclarations<F extends Fields, PR extends Fields, Q, Before = {}> = {\n readonly [Name in keyof Q]: {\n /** A query service made with `Query.Service`, or a list made with `Query.InfiniteService`. */\n readonly query: Q[Name]\n /** The args to read the query with. Return undefined to leave the query off. */\n readonly args: (current: Reads<F, PR, Before>) => Query.Query.Args<Q[Name]> | undefined\n /** Waits for the args to stay the same this long before fetching. */\n readonly debounce?: Duration.Input\n /** Shows no result while new args load, instead of the previous one. */\n readonly dropPrevious?: true\n }\n}\n\n/** How a feature declares queries it reads once for each of several args. */\nexport type KeyedDeclarations<F extends Fields, PR extends Fields, Q, Before = {}> = {\n readonly [Name in keyof Q]: {\n readonly query: Q[Name]\n /** The args to read the query with, one entry of the result for each. */\n readonly args: (current: Reads<F, PR, Before>) => ReadonlyArray<Query.Query.Args<Q[Name]>>\n }\n}\n\n/** Queries read once each, as Effect UI lists them on a component. */\nexport type Single<Q> = {\n readonly [Name in keyof Q]: Q[Name] extends Query.Query.Any ? Component.QueryOne<Q[Name]> : never\n}\n/** Queries read once per args, as Effect UI lists them on a component. */\nexport type Keyed<Q> = {\n readonly [Name in keyof Q]: Q[Name] extends Query.Query.Any ? Component.QueryMany<Q[Name]> : never\n}\n\n/**\n * The latest result of each query. A query is an `AsyncResult`: initial, a success or a failure.\n * A keyed query is a list of entries, each with its args and its own result. An infinite query\n * succeeds with its `items`, its `pages` and whether there are `more`.\n */\nexport type QueryResults<Q> = { readonly [Name in keyof Q]: Component.QueryResultOf<Q[Name]> }\n"]}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { component, module, type Mountable, type View, type ViewProps, } from "./component/component.js";
|
|
2
|
+
export { controller, type ActionsController, type Controller, type ControllerBuilder, } from "./controller/controller.js";
|
|
3
|
+
export type { Helpers, Operation } from "./controller/operation.js";
|
|
4
|
+
export { model, type Model } from "./model/model.js";
|
|
5
|
+
export { createElement } from "./runtime/jsx-runtime.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,EACT,MAAM,GAIP,MAAM,yBAAyB,CAAA;AAChC,OAAO,EACL,UAAU,GAIX,MAAM,2BAA2B,CAAA;AAElC,OAAO,EAAE,KAAK,EAAc,MAAM,iBAAiB,CAAA;AACnD,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAA","sourcesContent":["export {\n component,\n module,\n type Mountable,\n type View,\n type ViewProps,\n} from \"src/component/component\"\nexport {\n controller,\n type ActionsController,\n type Controller,\n type ControllerBuilder,\n} from \"src/controller/controller\"\nexport type { Helpers, Operation } from \"src/controller/operation\"\nexport { model, type Model } from \"src/model/model\"\nexport { createElement } from \"src/runtime/jsx-runtime\"\n"]}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
type Scalar = string | number | boolean;
|
|
3
|
+
type ScalarSchema<T> = T extends string ? Schema.String : T extends number ? Schema.Number : Schema.Boolean;
|
|
4
|
+
type SingleKind<T> = [T] extends [string] ? T : [T] extends [number] ? T : [T] extends [boolean] ? T : never;
|
|
5
|
+
export type Fields = Schema.Struct.Fields;
|
|
6
|
+
export type State<F extends Fields> = Schema.Struct<F>["Type"];
|
|
7
|
+
/** A feature's state: a schema for each field and the value the feature starts with. */
|
|
8
|
+
export interface Model<F extends Fields> {
|
|
9
|
+
fields: F;
|
|
10
|
+
initial: State<F>;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Declares a feature's state from its initial value. Strings, numbers and booleans infer their
|
|
14
|
+
* schemas. Pass an Effect schema for each field as the second argument for anything richer.
|
|
15
|
+
*/
|
|
16
|
+
export declare function model<S extends Record<string, Scalar>>(initial: S & {
|
|
17
|
+
[K in keyof S & string]: SingleKind<NoInfer<S[K]>>;
|
|
18
|
+
}): Model<{
|
|
19
|
+
[K in keyof S]: ScalarSchema<S[K]>;
|
|
20
|
+
}>;
|
|
21
|
+
export declare function model<const F extends Fields>(initial: NoInfer<State<F>>, fields: F): Model<F>;
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
function inferField(name, value) {
|
|
3
|
+
if (typeof value === "string") {
|
|
4
|
+
return [name, Schema.String];
|
|
5
|
+
}
|
|
6
|
+
if (typeof value === "number") {
|
|
7
|
+
return [name, Schema.Number];
|
|
8
|
+
}
|
|
9
|
+
if (typeof value === "boolean") {
|
|
10
|
+
return [name, Schema.Boolean];
|
|
11
|
+
}
|
|
12
|
+
throw new TypeError(`Provide an Effect schema for field "${name}"`);
|
|
13
|
+
}
|
|
14
|
+
export function model(initial, fields) {
|
|
15
|
+
return {
|
|
16
|
+
initial,
|
|
17
|
+
fields: fields ??
|
|
18
|
+
Object.fromEntries(Object.entries(initial).map(([name, value]) => inferField(name, value))),
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=model.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"model.js","sourceRoot":"","sources":["../../src/model/model.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAyB/B,SAAS,UAAU,CAAC,IAAY,EAAE,KAAc;IAC9C,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,CAAC,CAAA;IAC9B,CAAC;IAED,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,CAAC,CAAA;IAC9B,CAAC;IAED,IAAI,OAAO,KAAK,KAAK,SAAS,EAAE,CAAC;QAC/B,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,OAAO,CAAC,CAAA;IAC/B,CAAC;IAED,MAAM,IAAI,SAAS,CAAC,uCAAuC,IAAI,GAAG,CAAC,CAAA;AACrE,CAAC;AAUD,MAAM,UAAU,KAAK,CAAC,OAAe,EAAE,MAAe;IACpD,OAAO;QACL,OAAO;QACP,MAAM,EACJ,MAAM;YACN,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,UAAU,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC;KAC9F,CAAA;AACH,CAAC","sourcesContent":["import { Schema } from \"effect\"\n\ntype Scalar = string | number | boolean\ntype ScalarSchema<T> = T extends string\n ? Schema.String\n : T extends number\n ? Schema.Number\n : Schema.Boolean\ntype SingleKind<T> = [T] extends [string]\n ? T\n : [T] extends [number]\n ? T\n : [T] extends [boolean]\n ? T\n : never\n\nexport type Fields = Schema.Struct.Fields\nexport type State<F extends Fields> = Schema.Struct<F>[\"Type\"]\n\n/** A feature's state: a schema for each field and the value the feature starts with. */\nexport interface Model<F extends Fields> {\n fields: F\n initial: State<F>\n}\n\nfunction inferField(name: string, value: unknown) {\n if (typeof value === \"string\") {\n return [name, Schema.String]\n }\n\n if (typeof value === \"number\") {\n return [name, Schema.Number]\n }\n\n if (typeof value === \"boolean\") {\n return [name, Schema.Boolean]\n }\n\n throw new TypeError(`Provide an Effect schema for field \"${name}\"`)\n}\n\n/**\n * Declares a feature's state from its initial value. Strings, numbers and booleans infer their\n * schemas. Pass an Effect schema for each field as the second argument for anything richer.\n */\nexport function model<S extends Record<string, Scalar>>(\n initial: S & { [K in keyof S & string]: SingleKind<NoInfer<S[K]>> },\n): Model<{ [K in keyof S]: ScalarSchema<S[K]> }>\nexport function model<const F extends Fields>(initial: NoInfer<State<F>>, fields: F): Model<F>\nexport function model(initial: object, fields?: Fields) {\n return {\n initial,\n fields:\n fields ??\n Object.fromEntries(Object.entries(initial).map(([name, value]) => inferField(name, value))),\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Fragment, jsx as jsxDEV, type JSX } from "./jsx-runtime.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jsx-dev-runtime.js","sourceRoot":"","sources":["../../src/runtime/jsx-dev-runtime.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,GAAG,IAAI,MAAM,EAAY,MAAM,yBAAyB,CAAA","sourcesContent":["export { Fragment, jsx as jsxDEV, type JSX } from \"src/runtime/jsx-runtime\"\n"]}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Component, type View } from "@effect/ui";
|
|
2
|
+
import type { Html } from "@effect/ui/html";
|
|
3
|
+
import type { JSX, Tag } from "./jsx.js";
|
|
4
|
+
export type { ActionMessage, JSX } from "./jsx.js";
|
|
5
|
+
export declare const Fragment: unique symbol;
|
|
6
|
+
type Props = Record<string, unknown> & {
|
|
7
|
+
children?: JSX.Child;
|
|
8
|
+
};
|
|
9
|
+
type AnyComponent = Component.Any | Component.Headless.Any | Component.Lazy.Any;
|
|
10
|
+
type AnyTag = Tag | typeof Fragment | ((props: Props) => JSX.Child) | AnyComponent;
|
|
11
|
+
export declare function jsx<P>(tag: (props: P) => JSX.Child, props: P, key?: string | number): JSX.Element;
|
|
12
|
+
export declare function jsx(tag: typeof Fragment, props: {
|
|
13
|
+
children?: JSX.Child;
|
|
14
|
+
}, key?: string | number): JSX.Element;
|
|
15
|
+
export declare function jsx<K extends Tag>(tag: K, props: JSX.Elements[K], key?: string | number): JSX.Element;
|
|
16
|
+
/**
|
|
17
|
+
* What compilers emit for `<li {...props} key={id} />`, where a key follows a spread. Write the key
|
|
18
|
+
* first to get the faster `jsx` call.
|
|
19
|
+
*/
|
|
20
|
+
export declare function createElement(tag: AnyTag, { key, ...props }: Props & {
|
|
21
|
+
key?: string | number;
|
|
22
|
+
}, ...children: JSX.Child[]): JSX.Element;
|
|
23
|
+
export { jsx as jsxs };
|
|
24
|
+
/** Runs a view against an Effect UI host and returns its single root node. */
|
|
25
|
+
export declare function render<M>(element: JSX.Element, host: Html.H<M>): View.Content<M>;
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { Component } from "@effect/ui";
|
|
2
|
+
export const Fragment = Symbol("effect-ui-tsx.fragment");
|
|
3
|
+
// Props that call the builder method of the same name. An array supplies several arguments.
|
|
4
|
+
const methods = new Set([
|
|
5
|
+
"id",
|
|
6
|
+
"ref",
|
|
7
|
+
"onMount",
|
|
8
|
+
"draggable",
|
|
9
|
+
"dropTarget",
|
|
10
|
+
"class",
|
|
11
|
+
"style",
|
|
12
|
+
"disabled",
|
|
13
|
+
"checked",
|
|
14
|
+
"selected",
|
|
15
|
+
"open",
|
|
16
|
+
"modal",
|
|
17
|
+
"popover",
|
|
18
|
+
"attrs",
|
|
19
|
+
]);
|
|
20
|
+
const valueTags = new Set(["input", "textarea", "select"]);
|
|
21
|
+
// Props that set where a field starts, by the attribute that holds it.
|
|
22
|
+
const defaults = new Map([
|
|
23
|
+
["defaultValue", "value"],
|
|
24
|
+
["defaultChecked", "checked"],
|
|
25
|
+
]);
|
|
26
|
+
// A boolean attribute is present or absent. ARIA and data attributes spell their booleans out.
|
|
27
|
+
function attribute(name, value) {
|
|
28
|
+
if (typeof value !== "boolean" || /^(aria|data)-/.test(name)) {
|
|
29
|
+
return String(value);
|
|
30
|
+
}
|
|
31
|
+
return value ? "" : undefined;
|
|
32
|
+
}
|
|
33
|
+
function renderChild(child, host) {
|
|
34
|
+
if (Array.isArray(child)) {
|
|
35
|
+
return child.flatMap((entry) => renderChild(entry, host));
|
|
36
|
+
}
|
|
37
|
+
// A function child receives the host. What it returns may be a node or more JSX.
|
|
38
|
+
if (typeof child === "function") {
|
|
39
|
+
return renderChild(child(host), host);
|
|
40
|
+
}
|
|
41
|
+
if (typeof child === "number") {
|
|
42
|
+
return String(child);
|
|
43
|
+
}
|
|
44
|
+
if (child === true) {
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
return child;
|
|
48
|
+
}
|
|
49
|
+
function intrinsic(tag, props, key) {
|
|
50
|
+
return (host, inherited) => {
|
|
51
|
+
// A name with a hyphen is a custom element, which the host builds by name.
|
|
52
|
+
let element = (tag.includes("-") ? host.el(tag) : host[tag]);
|
|
53
|
+
// The key a list gives a function view identifies the row, so it wins over the view's own.
|
|
54
|
+
const ownKey = inherited ?? key;
|
|
55
|
+
if (ownKey !== undefined) {
|
|
56
|
+
element = element.key(String(ownKey));
|
|
57
|
+
}
|
|
58
|
+
for (const [name, value] of Object.entries(props)) {
|
|
59
|
+
if (name === "children" || value === undefined || value === null) {
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
if (name === "apply") {
|
|
63
|
+
for (const part of Array.isArray(value) ? value : [value]) {
|
|
64
|
+
element = part(element);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
else if (name === "properties") {
|
|
68
|
+
for (const [property, set] of Object.entries(value)) {
|
|
69
|
+
element = element.prop(property, set);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
else if (methods.has(name) &&
|
|
73
|
+
typeof element[name] === "function" &&
|
|
74
|
+
!(name === "style" && typeof value === "string")) {
|
|
75
|
+
element = element[name](...(Array.isArray(value) ? value : [value]));
|
|
76
|
+
}
|
|
77
|
+
else if (name.length > 2 && name.startsWith("on") && typeof value !== "string") {
|
|
78
|
+
const [handler, options] = Array.isArray(value) ? value : [value];
|
|
79
|
+
element = element.on(name.slice(2).toLowerCase(), handler, options);
|
|
80
|
+
}
|
|
81
|
+
else if (name === "value" && valueTags.has(tag)) {
|
|
82
|
+
element = element.value(value);
|
|
83
|
+
}
|
|
84
|
+
else if (defaults.has(name)) {
|
|
85
|
+
// A starting value is the HTML attribute, which the browser reads once.
|
|
86
|
+
const initial = defaults.get(name);
|
|
87
|
+
element = element.attr(initial, attribute(initial, value));
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
element = element.attr(name, attribute(name, value));
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (props.children === undefined) {
|
|
94
|
+
// A shorthand that no prop has called a method on is not a view yet. Called with no
|
|
95
|
+
// children, it is an empty element.
|
|
96
|
+
return typeof element === "function" ? element() : element;
|
|
97
|
+
}
|
|
98
|
+
return element.children(renderChild(props.children, host));
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
// What a tag passes to Effect UI as an option of the child instead of as one of its props.
|
|
102
|
+
const childOptions = new Set(["children", "fallback", "onFailure", "slots", "view", "keepAlive"]);
|
|
103
|
+
const handlerName = (event) => "on" + event[0].toUpperCase() + event.slice(1);
|
|
104
|
+
// An Effect UI component as a tag: on… props are its events and the rest are its props.
|
|
105
|
+
function child(component, props, key) {
|
|
106
|
+
const { children, fallback, onFailure, view, keepAlive } = props;
|
|
107
|
+
// The tag's own children fill the slot named children.
|
|
108
|
+
const named = props.slots;
|
|
109
|
+
const slots = children === undefined ? named : { ...named, children };
|
|
110
|
+
const passed = Object.fromEntries(Object.entries(props).filter(([name]) => !childOptions.has(name) && !/^on[A-Z]/.test(name)));
|
|
111
|
+
// Every event the child reports comes here. One the tag has no handler for is ignored.
|
|
112
|
+
const onEmit = (event) => props[handlerName(event._tag)]?.(event);
|
|
113
|
+
const embed = component;
|
|
114
|
+
return (host, inherited) => {
|
|
115
|
+
const ownKey = inherited ?? key;
|
|
116
|
+
// The fallback, the slots and a headless view are this view's own markup, so they render
|
|
117
|
+
// against its host.
|
|
118
|
+
const options = {
|
|
119
|
+
props: passed,
|
|
120
|
+
onEmit,
|
|
121
|
+
onFailure,
|
|
122
|
+
fallback: fallback && ((failure) => renderChild(fallback(failure), host)),
|
|
123
|
+
slots: slots &&
|
|
124
|
+
Object.fromEntries(Object.entries(slots).map(([name, content]) => [name, renderChild(content, host)])),
|
|
125
|
+
...(view && { view: (parts) => renderChild(view(parts), host) }),
|
|
126
|
+
...(ownKey !== undefined && { key: String(ownKey) }),
|
|
127
|
+
};
|
|
128
|
+
// A kept child is written through the helper Effect UI passes to keepAlive.
|
|
129
|
+
return keepAlive
|
|
130
|
+
? host.keepAlive((kept) => embed(kept, options))
|
|
131
|
+
: embed(host, options);
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
function element(tag, props, key) {
|
|
135
|
+
if (tag === Fragment) {
|
|
136
|
+
return (host) => renderChild(props.children, host);
|
|
137
|
+
}
|
|
138
|
+
if (Component.isComponent(tag) || Component.isHeadless(tag) || Component.isLazy(tag)) {
|
|
139
|
+
return child(tag, props, key);
|
|
140
|
+
}
|
|
141
|
+
if (typeof tag !== "function") {
|
|
142
|
+
return intrinsic(tag, props, key);
|
|
143
|
+
}
|
|
144
|
+
return (host, inherited) => {
|
|
145
|
+
const result = tag(props);
|
|
146
|
+
// A keyed function view hands its key to the element it returns. It may also return text or nothing.
|
|
147
|
+
return typeof result === "function"
|
|
148
|
+
? result(host, key === undefined ? inherited : String(key))
|
|
149
|
+
: renderChild(result, host);
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
export function jsx(tag, props, key) {
|
|
153
|
+
return element(tag, props, key);
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* What compilers emit for `<li {...props} key={id} />`, where a key follows a spread. Write the key
|
|
157
|
+
* first to get the faster `jsx` call.
|
|
158
|
+
*/
|
|
159
|
+
export function createElement(tag, { key, ...props }, ...children) {
|
|
160
|
+
// Children written between the tags replace any that came in the spread. Without them the
|
|
161
|
+
// spread's own children stay.
|
|
162
|
+
const written = children.length > 1 ? children : children[0];
|
|
163
|
+
return element(tag, written === undefined ? props : { ...props, children: written }, key);
|
|
164
|
+
}
|
|
165
|
+
export { jsx as jsxs };
|
|
166
|
+
/** Runs a view against an Effect UI host and returns its single root node. */
|
|
167
|
+
export function render(element, host) {
|
|
168
|
+
// JSX has one element type, so the feature's message type is restored here.
|
|
169
|
+
const root = element(host);
|
|
170
|
+
if (typeof root === "string") {
|
|
171
|
+
return host.text(root);
|
|
172
|
+
}
|
|
173
|
+
if (!root || Array.isArray(root)) {
|
|
174
|
+
throw new TypeError("An Effect UI view requires one root node");
|
|
175
|
+
}
|
|
176
|
+
return root;
|
|
177
|
+
}
|
|
178
|
+
//# sourceMappingURL=jsx-runtime.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+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esContent":["import { Component, type View } from \"@effect/ui\"\nimport type { Html } from \"@effect/ui/html\"\nimport type { Host, JSX, Tag } from \"src/runtime/jsx\"\n\nexport type { ActionMessage, JSX } from \"src/runtime/jsx\"\n\nexport const Fragment = Symbol(\"effect-ui-tsx.fragment\")\n\n// Each tag has its own builder type. Props are checked against JSX.IntrinsicElements instead.\ninterface Builder {\n [method: string]: (...args: unknown[]) => Builder\n}\n// A container tag starts as a shorthand: its builder, callable to add children.\ntype Shorthand = Builder & ((...children: unknown[]) => Builder)\ntype Props = Record<string, unknown> & { children?: JSX.Child }\n\n// Props that call the builder method of the same name. An array supplies several arguments.\nconst methods = new Set([\n \"id\",\n \"ref\",\n \"onMount\",\n \"draggable\",\n \"dropTarget\",\n \"class\",\n \"style\",\n \"disabled\",\n \"checked\",\n \"selected\",\n \"open\",\n \"modal\",\n \"popover\",\n \"attrs\",\n])\nconst valueTags = new Set([\"input\", \"textarea\", \"select\"])\n// Props that set where a field starts, by the attribute that holds it.\nconst defaults = new Map([\n [\"defaultValue\", \"value\"],\n [\"defaultChecked\", \"checked\"],\n])\n\n// A boolean attribute is present or absent. ARIA and data attributes spell their booleans out.\nfunction attribute(name: string, value: unknown) {\n if (typeof value !== \"boolean\" || /^(aria|data)-/.test(name)) {\n return String(value)\n }\n return value ? \"\" : undefined\n}\n\nfunction renderChild(child: JSX.Child, host: Host): unknown {\n if (Array.isArray(child)) {\n return child.flatMap((entry: JSX.Child) => renderChild(entry, host))\n }\n // A function child receives the host. What it returns may be a node or more JSX.\n if (typeof child === \"function\") {\n return renderChild(child(host) as JSX.Child, host)\n }\n if (typeof child === \"number\") {\n return String(child)\n }\n if (child === true) {\n return undefined\n }\n return child\n}\n\nfunction intrinsic(tag: Tag, props: Props, key: string | number | undefined): JSX.Element {\n return (host, inherited) => {\n // A name with a hyphen is a custom element, which the host builds by name.\n let element = (tag.includes(\"-\") ? host.el(tag) : host[tag]) as unknown as Builder | Shorthand\n // The key a list gives a function view identifies the row, so it wins over the view's own.\n const ownKey = inherited ?? key\n if (ownKey !== undefined) {\n element = element.key(String(ownKey))\n }\n for (const [name, value] of Object.entries(props)) {\n if (name === \"children\" || value === undefined || value === null) {\n continue\n }\n if (name === \"apply\") {\n for (const part of Array.isArray(value) ? value : [value]) {\n element = part(element)\n }\n } else if (name === \"properties\") {\n for (const [property, set] of Object.entries(value)) {\n element = element.prop(property, set)\n }\n } else if (\n methods.has(name) &&\n typeof element[name] === \"function\" &&\n !(name === \"style\" && typeof value === \"string\")\n ) {\n element = element[name](...(Array.isArray(value) ? value : [value]))\n } else if (name.length > 2 && name.startsWith(\"on\") && typeof value !== \"string\") {\n const [handler, options] = Array.isArray(value) ? value : [value]\n element = element.on(name.slice(2).toLowerCase(), handler, options)\n } else if (name === \"value\" && valueTags.has(tag)) {\n element = element.value(value)\n } else if (defaults.has(name)) {\n // A starting value is the HTML attribute, which the browser reads once.\n const initial = defaults.get(name)!\n element = element.attr(initial, attribute(initial, value))\n } else {\n element = element.attr(name, attribute(name, value))\n }\n }\n if (props.children === undefined) {\n // A shorthand that no prop has called a method on is not a view yet. Called with no\n // children, it is an empty element.\n return typeof element === \"function\" ? element() : element\n }\n return element.children(renderChild(props.children, host))\n }\n}\n\ntype AnyComponent = Component.Any | Component.Headless.Any | Component.Lazy.Any\ntype AnyTag = Tag | typeof Fragment | ((props: Props) => JSX.Child) | AnyComponent\n\n// What a tag passes to Effect UI as an option of the child instead of as one of its props.\nconst childOptions = new Set([\"children\", \"fallback\", \"onFailure\", \"slots\", \"view\", \"keepAlive\"])\n\nconst handlerName = (event: string) => \"on\" + event[0]!.toUpperCase() + event.slice(1)\n\n// An Effect UI component as a tag: on… props are its events and the rest are its props.\nfunction child(component: AnyComponent, props: Props, key: string | number | undefined) {\n const { children, fallback, onFailure, view, keepAlive } = props as Props & {\n fallback?: (failure: unknown) => JSX.Child\n view?: (parts: unknown) => JSX.Child\n }\n // The tag's own children fill the slot named children.\n const named = props.slots as Record<string, JSX.Child> | undefined\n const slots = children === undefined ? named : { ...named, children }\n const passed = Object.fromEntries(\n Object.entries(props).filter(([name]) => !childOptions.has(name) && !/^on[A-Z]/.test(name)),\n )\n // Every event the child reports comes here. One the tag has no handler for is ignored.\n const onEmit = (event: { _tag: string }) =>\n (props[handlerName(event._tag)] as ((event: unknown) => unknown) | undefined)?.(event)\n const embed = component as unknown as (host: object, options: object) => unknown\n return (host: Host, inherited?: string) => {\n const ownKey = inherited ?? key\n // The fallback, the slots and a headless view are this view's own markup, so they render\n // against its host.\n const options = {\n props: passed,\n onEmit,\n onFailure,\n fallback: fallback && ((failure: unknown) => renderChild(fallback(failure), host)),\n slots:\n slots &&\n Object.fromEntries(\n Object.entries(slots).map(([name, content]) => [name, renderChild(content, host)]),\n ),\n ...(view && { view: (parts: unknown) => renderChild(view(parts), host) }),\n ...(ownKey !== undefined && { key: String(ownKey) }),\n }\n // A kept child is written through the helper Effect UI passes to keepAlive.\n return keepAlive\n ? (host as unknown as { keepAlive: (write: (kept: object) => unknown) => unknown }).keepAlive(\n (kept) => embed(kept, options),\n )\n : embed(host, options)\n }\n}\n\nfunction element(tag: AnyTag, props: Props, key: string | number | undefined): JSX.Element {\n if (tag === Fragment) {\n return (host) => renderChild(props.children, host)\n }\n if (Component.isComponent(tag) || Component.isHeadless(tag) || Component.isLazy(tag)) {\n return child(tag, props, key)\n }\n if (typeof tag !== \"function\") {\n return intrinsic(tag, props, key)\n }\n return (host, inherited) => {\n const result = tag(props)\n // A keyed function view hands its key to the element it returns. It may also return text or nothing.\n return typeof result === \"function\"\n ? result(host, key === undefined ? inherited : String(key))\n : renderChild(result, host)\n }\n}\n\nexport function jsx<P>(tag: (props: P) => JSX.Child, props: P, key?: string | number): JSX.Element\nexport function jsx(\n tag: typeof Fragment,\n props: { children?: JSX.Child },\n key?: string | number,\n): JSX.Element\nexport function jsx<K extends Tag>(\n tag: K,\n props: JSX.Elements[K],\n key?: string | number,\n): JSX.Element\nexport function jsx(tag: AnyTag, props: Props, key?: string | number): JSX.Element {\n return element(tag, props, key)\n}\n\n/**\n * What compilers emit for `<li {...props} key={id} />`, where a key follows a spread. Write the key\n * first to get the faster `jsx` call.\n */\nexport function createElement(\n tag: AnyTag,\n { key, ...props }: Props & { key?: string | number },\n ...children: JSX.Child[]\n): JSX.Element {\n // Children written between the tags replace any that came in the spread. Without them the\n // spread's own children stay.\n const written = children.length > 1 ? children : children[0]\n return element(tag, written === undefined ? props : { ...props, children: written }, key)\n}\n\nexport { jsx as jsxs }\n\n/** Runs a view against an Effect UI host and returns its single root node. */\nexport function render<M>(element: JSX.Element, host: Html.H<M>): View.Content<M> {\n // JSX has one element type, so the feature's message type is restored here.\n const root = element(host as unknown as Host)\n if (typeof root === \"string\") {\n return host.text(root) as View.Content<M>\n }\n if (!root || Array.isArray(root)) {\n throw new TypeError(\"An Effect UI view requires one root node\")\n }\n return root as View.Content<M>\n}\n"]}
|