@govela/runtime 0.4.8 → 0.5.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/README.md CHANGED
@@ -13,6 +13,10 @@ Development checks are removed when the bundler replaces
13
13
  `process.env.NODE_ENV` with `"production"` (esbuild and Vite do this for
14
14
  production builds; Rollup needs `@rollup/plugin-replace`).
15
15
 
16
+ TypeScript: the package is typed. Component scripts get the runes and the
17
+ `vela` and `vela/store` modules from `@govela/runtime/ambient`; list it in
18
+ `compilerOptions.types` of tsconfig.json.
19
+
16
20
  Generated from `runtime/` of the compiler repository; do not edit.
17
21
 
18
22
  License: MIT.
package/ambient.d.ts ADDED
@@ -0,0 +1,222 @@
1
+ // Generated from runtime/types/ of the Vela compiler repository; do not edit.
2
+ // The ambient types of Vela component scripts: list "@govela/runtime/ambient"
3
+ // in the `types` of tsconfig.json.
4
+
5
+ /** Reactive state: assignments update the template. */
6
+ declare function $state<T>(initial: T): T;
7
+ /** Reactive state that is undefined until assigned. */
8
+ declare function $state<T>(): T | undefined;
9
+ declare namespace $state {
10
+ /** Reactive state whose nested objects and arrays are observed too. */
11
+ function deep<T>(initial: T): T;
12
+ function deep<T>(): T | undefined;
13
+ }
14
+
15
+ /** A value computed from reactive state; the expression must be pure. */
16
+ declare function $derived<T>(expression: T): T;
17
+
18
+ /**
19
+ * Runs fn after mount and again when the state it read changes. A function
20
+ * it returns runs before the next run and on destroy.
21
+ */
22
+ declare function $effect(fn: () => void | (() => void)): void;
23
+
24
+ /** A prop the parent may bind with `bind:`; fallback is its default. */
25
+ declare function $bindable<T>(fallback: T): T;
26
+ declare function $bindable<T>(): T | undefined;
27
+
28
+ /**
29
+ * Dispatches the component event name to the parent's `@on:name` handler,
30
+ * as a CustomEvent with detail.
31
+ */
32
+ declare function $$dispatch(name: string, detail?: any): void;
33
+
34
+ declare module 'vela/store' {
35
+ /** Removes a subscription. Idempotent. */
36
+ export type Unsubscriber = () => void;
37
+
38
+ /** A store that can be read and subscribed to. */
39
+ export interface Readable<T> {
40
+ /** Calls run with the current value now and with every later value. */
41
+ subscribe(run: (value: T) => void): Unsubscriber;
42
+ /** The current value. */
43
+ get(): T;
44
+ }
45
+
46
+ /** A store whose value can be set from outside. */
47
+ export interface Writable<T> extends Readable<T> {
48
+ set(value: T): void;
49
+ update(fn: (value: T) => T): void;
50
+ }
51
+
52
+ /** A store computed from other stores. */
53
+ export interface Derived<T> extends Readable<T> {
54
+ /** Unsubscribes from the sources for good. Idempotent. */
55
+ destroy(): void;
56
+ }
57
+
58
+ /** The values of the stores of `derived()`: one value, or a tuple of them. */
59
+ export type StoresValues<S> = S extends Readable<infer T>
60
+ ? T
61
+ : { [K in keyof S]: S[K] extends Readable<infer T> ? T : never };
62
+
63
+ export function writable<T>(value: T): Writable<T>;
64
+ export function writable<T = undefined>(): Writable<T | undefined>;
65
+
66
+ /**
67
+ * A read-only store. start runs for the first subscriber and may return a
68
+ * cleanup, which runs after the last unsubscribes.
69
+ */
70
+ export function readable<T>(value: T, start?: (set: (value: T) => void) => void | (() => void)): Readable<T>;
71
+
72
+ /**
73
+ * A store computed by fn from one store or a list of stores. Before its
74
+ * first subscriber, get() returns initial.
75
+ */
76
+ export function derived<S extends Readable<any> | readonly Readable<any>[], T>(
77
+ stores: S,
78
+ fn: (values: StoresValues<S>) => T,
79
+ initial?: T,
80
+ ): Derived<T>;
81
+ }
82
+
83
+ declare module 'vela' {
84
+ /** Props of a component: an object of named values. */
85
+ export type Props = Record<string, any>;
86
+
87
+ /**
88
+ * The slots a parent passes to a component. Their values are produced by
89
+ * the compiler from the parent's template.
90
+ */
91
+ export type Slots = Record<string, unknown>;
92
+
93
+ /** A mounted component. */
94
+ export interface ComponentInstance<P extends Props = Props> {
95
+ /** The root element; null when the template's root is not one element. */
96
+ readonly el: Element | null;
97
+ /** Removes the component's DOM and stops its effects. Idempotent. */
98
+ destroy(): void;
99
+ /** Sets props; a component without props has no set(). */
100
+ set?(props: Partial<P>): void;
101
+ }
102
+
103
+ /**
104
+ * A component: the export of a compiled `.vela` module, or a value made by
105
+ * `serverComponent()` or `staticHTML()`. P are its props, E maps the names
106
+ * of the events it dispatches to their `detail`, S names its slots, B are
107
+ * the names of its `$bindable` props.
108
+ */
109
+ export interface Component<P extends Props = Props, E extends Record<string, any> = Record<string, any>, S extends Record<string, any> = Record<string, any>, B extends string = string> {
110
+ (target: Element | DocumentFragment | Comment, props?: P, slots?: Slots): ComponentInstance<P>;
111
+ /** Type-level only: the events of the component; absent at run time. */
112
+ readonly $$events_def?: E;
113
+ /** Type-level only: the slots of the component; absent at run time. */
114
+ readonly $$slot_def?: S;
115
+ /** Type-level only: the props a parent may bind; absent at run time. */
116
+ readonly $$bindings_def?: B;
117
+ }
118
+
119
+ /** The props of a component type. */
120
+ export type ComponentProps<C> = C extends Component<infer P, any, any> ? P : never;
121
+
122
+ /** The events of a component type: event names to their `detail`. */
123
+ export type ComponentEvents<C> = C extends Component<any, infer E, any> ? E : never;
124
+
125
+ /** The slots of a component type. */
126
+ export type ComponentSlots<C> = C extends Component<any, any, infer S> ? S : never;
127
+
128
+ /** The names of the props of a component type that a parent may bind. */
129
+ export type ComponentBindings<C> = C extends Component<any, any, any, infer B> ? B : never;
130
+
131
+ /**
132
+ * A component that loads on demand: `lazy(() => import('./Panel.vela'))`.
133
+ * `<vela:component :this>` accepts it; `mount()` and `hydrate()` need the
134
+ * loaded component (`await panel.load()`).
135
+ */
136
+ export interface Lazy<C extends Component<any, any, any> = Component> {
137
+ readonly id: string;
138
+ readonly loaded: boolean;
139
+ /** The component once loaded, else null. */
140
+ readonly component: C | null;
141
+ /** Loads the module; a failed load can be tried again. */
142
+ load(): Promise<C>;
143
+ }
144
+
145
+ /** The options of `mount()`: props are required when the component has required props. */
146
+ export type MountOptions<P extends Props = Props> = {
147
+ target: Element | DocumentFragment;
148
+ slots?: Slots;
149
+ } & ({} extends P ? { props?: P } : { props: P });
150
+
151
+ /** Mounts a component into target. The props are checked against the component's. */
152
+ export function mount<P extends Props>(component: Component<P, any, any>, options: MountOptions<NoInfer<P>>): ComponentInstance<P>;
153
+
154
+ /**
155
+ * A component whose module loads when it is first shown, for
156
+ * `<vela:component :this>`. The loader must be `() => import('<literal .vela path>')`.
157
+ * id is the component's `$$id`, which the compiler and `vela-app` pass.
158
+ */
159
+ export function lazy<C extends Component<any, any, any>>(loader: () => Promise<C | { default: C }>, id?: string): Lazy<C>;
160
+
161
+ /** Resolves after pending updates were applied to the DOM. */
162
+ export function tick(): Promise<void>;
163
+
164
+ /** Logs a warning in development builds; nothing in production. */
165
+ export function warn(message: string): void;
166
+
167
+ /** Removes a subscription. Idempotent. */
168
+ export type Unsubscriber = () => void;
169
+
170
+ /** A store that can be read and subscribed to. */
171
+ export interface Readable<T> {
172
+ /** Calls run with the current value now and with every later value. */
173
+ subscribe(run: (value: T) => void): Unsubscriber;
174
+ /** The current value. */
175
+ get(): T;
176
+ }
177
+
178
+ /** A store whose value can be set from outside. */
179
+ export interface Writable<T> extends Readable<T> {
180
+ set(value: T): void;
181
+ update(fn: (value: T) => T): void;
182
+ }
183
+
184
+ /** A store computed from other stores. */
185
+ export interface Derived<T> extends Readable<T> {
186
+ /** Unsubscribes from the sources for good. Idempotent. */
187
+ destroy(): void;
188
+ }
189
+
190
+ /** The values of the stores of `derived()`: one value, or a tuple of them. */
191
+ export type StoresValues<S> = S extends Readable<infer T>
192
+ ? T
193
+ : { [K in keyof S]: S[K] extends Readable<infer T> ? T : never };
194
+
195
+ export function writable<T>(value: T): Writable<T>;
196
+ export function writable<T = undefined>(): Writable<T | undefined>;
197
+
198
+ /**
199
+ * A read-only store. start runs for the first subscriber and may return a
200
+ * cleanup, which runs after the last unsubscribes.
201
+ */
202
+ export function readable<T>(value: T, start?: (set: (value: T) => void) => void | (() => void)): Readable<T>;
203
+
204
+ /**
205
+ * A store computed by fn from one store or a list of stores. Before its
206
+ * first subscriber, get() returns initial.
207
+ */
208
+ export function derived<S extends Readable<any> | readonly Readable<any>[], T>(
209
+ stores: S,
210
+ fn: (values: StoresValues<S>) => T,
211
+ initial?: T,
212
+ ): Derived<T>;
213
+
214
+ /**
215
+ * Runs fn after the component is mounted. A function it returns runs when
216
+ * the component is destroyed.
217
+ */
218
+ export function onMount(fn: () => void | (() => void) | Promise<void>): void;
219
+
220
+ /** Runs fn when the component is destroyed. */
221
+ export function onDestroy(fn: () => void): void;
222
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@govela/runtime",
3
- "version": "0.4.8",
3
+ "version": "0.5.0",
4
4
  "description": "Runtime of components compiled by the Vela compiler. Generated from runtime/; do not edit.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -13,14 +13,27 @@
13
13
  "./runtime.js"
14
14
  ],
15
15
  "exports": {
16
- ".": "./runtime.js",
16
+ ".": {
17
+ "types": "./runtime.d.ts",
18
+ "default": "./runtime.js"
19
+ },
20
+ "./ambient": {
21
+ "types": "./ambient.d.ts"
22
+ },
17
23
  "./blocks": "./runtime-blocks.js",
18
- "./hydrate": "./runtime-hydrate.js"
24
+ "./hydrate": {
25
+ "types": "./runtime-hydrate.d.ts",
26
+ "default": "./runtime-hydrate.js"
27
+ }
19
28
  },
29
+ "types": "runtime.d.ts",
20
30
  "files": [
21
31
  "runtime.js",
32
+ "runtime.d.ts",
22
33
  "runtime-blocks.js",
23
34
  "runtime-hydrate.js",
35
+ "runtime-hydrate.d.ts",
36
+ "ambient.d.ts",
24
37
  "LICENSE"
25
38
  ]
26
39
  }
@@ -0,0 +1,54 @@
1
+ // Generated from runtime/types/ of the Vela compiler repository; do not edit.
2
+ // Types of @govela/runtime/hydrate (runtime-hydrate.js): hydration of
3
+ // server-rendered HTML and server fragments (docs/SSR.md).
4
+
5
+ import type { Component, ComponentInstance, Props, Slots } from './runtime.js';
6
+
7
+ /** The code of every hydration mismatch. */
8
+ export const MISMATCH: 'VELA-HYDRATION-MISMATCH';
9
+
10
+ /** A difference between the server HTML and the client render (X-7.5). */
11
+ export interface MismatchReport {
12
+ code: typeof MISMATCH;
13
+ kind: string;
14
+ message: string;
15
+ policy: 'repair' | 'fail' | 'production';
16
+ /** The site of the mismatch, in development builds. */
17
+ site?: number;
18
+ /** `File.vela:line:column` of the site. */
19
+ source?: string;
20
+ /** The Go host symbol of the site. */
21
+ host?: string;
22
+ server?: unknown;
23
+ client?: unknown;
24
+ }
25
+
26
+ export interface HydrateOptions {
27
+ /** What a mismatch does in development builds; production builds always repair silently. */
28
+ policy?: 'repair' | 'fail' | 'off';
29
+ /** Receives every mismatch. */
30
+ onMismatch?: (report: MismatchReport) => void;
31
+ slots?: Slots;
32
+ }
33
+
34
+ /**
35
+ * Adopts the HTML and payload a Go renderer wrote into container and
36
+ * returns the component instance. Props come from the payload.
37
+ */
38
+ export function hydrate<P extends Props>(component: Component<P, any, any>, container: Element, options?: HydrateOptions): ComponentInstance<P>;
39
+
40
+ /** Loads the lazy components the server rendered in container, before hydrate(). */
41
+ export function preloadHydration(container: Element): Promise<void>;
42
+
43
+ /**
44
+ * A component value whose first mount adopts html, the output of the Go
45
+ * renderer of component, for `<vela:component :this>`. Only trusted HTML.
46
+ */
47
+ export function serverComponent<P extends Props, E extends Record<string, any>, S extends Record<string, any>>(
48
+ component: Component<P, E, S>,
49
+ html: string,
50
+ options?: HydrateOptions,
51
+ ): Component<P, E, S>;
52
+
53
+ /** A component value that inserts html and has no state. Only trusted HTML. */
54
+ export function staticHTML(html: string): Component<Props, {}, {}>;
package/runtime.d.ts ADDED
@@ -0,0 +1,132 @@
1
+ // Generated from runtime/types/ of the Vela compiler repository; do not edit.
2
+ // Types of @govela/runtime: the component API and the stores.
3
+
4
+ /** Props of a component: an object of named values. */
5
+ export type Props = Record<string, any>;
6
+
7
+ /**
8
+ * The slots a parent passes to a component. Their values are produced by
9
+ * the compiler from the parent's template.
10
+ */
11
+ export type Slots = Record<string, unknown>;
12
+
13
+ /** A mounted component. */
14
+ export interface ComponentInstance<P extends Props = Props> {
15
+ /** The root element; null when the template's root is not one element. */
16
+ readonly el: Element | null;
17
+ /** Removes the component's DOM and stops its effects. Idempotent. */
18
+ destroy(): void;
19
+ /** Sets props; a component without props has no set(). */
20
+ set?(props: Partial<P>): void;
21
+ }
22
+
23
+ /**
24
+ * A component: the export of a compiled `.vela` module, or a value made by
25
+ * `serverComponent()` or `staticHTML()`. P are its props, E maps the names
26
+ * of the events it dispatches to their `detail`, S names its slots, B are
27
+ * the names of its `$bindable` props.
28
+ */
29
+ export interface Component<P extends Props = Props, E extends Record<string, any> = Record<string, any>, S extends Record<string, any> = Record<string, any>, B extends string = string> {
30
+ (target: Element | DocumentFragment | Comment, props?: P, slots?: Slots): ComponentInstance<P>;
31
+ /** Type-level only: the events of the component; absent at run time. */
32
+ readonly $$events_def?: E;
33
+ /** Type-level only: the slots of the component; absent at run time. */
34
+ readonly $$slot_def?: S;
35
+ /** Type-level only: the props a parent may bind; absent at run time. */
36
+ readonly $$bindings_def?: B;
37
+ }
38
+
39
+ /** The props of a component type. */
40
+ export type ComponentProps<C> = C extends Component<infer P, any, any> ? P : never;
41
+
42
+ /** The events of a component type: event names to their `detail`. */
43
+ export type ComponentEvents<C> = C extends Component<any, infer E, any> ? E : never;
44
+
45
+ /** The slots of a component type. */
46
+ export type ComponentSlots<C> = C extends Component<any, any, infer S> ? S : never;
47
+
48
+ /** The names of the props of a component type that a parent may bind. */
49
+ export type ComponentBindings<C> = C extends Component<any, any, any, infer B> ? B : never;
50
+
51
+ /**
52
+ * A component that loads on demand: `lazy(() => import('./Panel.vela'))`.
53
+ * `<vela:component :this>` accepts it; `mount()` and `hydrate()` need the
54
+ * loaded component (`await panel.load()`).
55
+ */
56
+ export interface Lazy<C extends Component<any, any, any> = Component> {
57
+ readonly id: string;
58
+ readonly loaded: boolean;
59
+ /** The component once loaded, else null. */
60
+ readonly component: C | null;
61
+ /** Loads the module; a failed load can be tried again. */
62
+ load(): Promise<C>;
63
+ }
64
+
65
+ /** The options of `mount()`: props are required when the component has required props. */
66
+ export type MountOptions<P extends Props = Props> = {
67
+ target: Element | DocumentFragment;
68
+ slots?: Slots;
69
+ } & ({} extends P ? { props?: P } : { props: P });
70
+
71
+ /** Mounts a component into target. The props are checked against the component's. */
72
+ export function mount<P extends Props>(component: Component<P, any, any>, options: MountOptions<NoInfer<P>>): ComponentInstance<P>;
73
+
74
+ /**
75
+ * A component whose module loads when it is first shown, for
76
+ * `<vela:component :this>`. The loader must be `() => import('<literal .vela path>')`.
77
+ * id is the component's `$$id`, which the compiler and `vela-app` pass.
78
+ */
79
+ export function lazy<C extends Component<any, any, any>>(loader: () => Promise<C | { default: C }>, id?: string): Lazy<C>;
80
+
81
+ /** Resolves after pending updates were applied to the DOM. */
82
+ export function tick(): Promise<void>;
83
+
84
+ /** Logs a warning in development builds; nothing in production. */
85
+ export function warn(message: string): void;
86
+
87
+ /** Removes a subscription. Idempotent. */
88
+ export type Unsubscriber = () => void;
89
+
90
+ /** A store that can be read and subscribed to. */
91
+ export interface Readable<T> {
92
+ /** Calls run with the current value now and with every later value. */
93
+ subscribe(run: (value: T) => void): Unsubscriber;
94
+ /** The current value. */
95
+ get(): T;
96
+ }
97
+
98
+ /** A store whose value can be set from outside. */
99
+ export interface Writable<T> extends Readable<T> {
100
+ set(value: T): void;
101
+ update(fn: (value: T) => T): void;
102
+ }
103
+
104
+ /** A store computed from other stores. */
105
+ export interface Derived<T> extends Readable<T> {
106
+ /** Unsubscribes from the sources for good. Idempotent. */
107
+ destroy(): void;
108
+ }
109
+
110
+ /** The values of the stores of `derived()`: one value, or a tuple of them. */
111
+ export type StoresValues<S> = S extends Readable<infer T>
112
+ ? T
113
+ : { [K in keyof S]: S[K] extends Readable<infer T> ? T : never };
114
+
115
+ export function writable<T>(value: T): Writable<T>;
116
+ export function writable<T = undefined>(): Writable<T | undefined>;
117
+
118
+ /**
119
+ * A read-only store. start runs for the first subscriber and may return a
120
+ * cleanup, which runs after the last unsubscribes.
121
+ */
122
+ export function readable<T>(value: T, start?: (set: (value: T) => void) => void | (() => void)): Readable<T>;
123
+
124
+ /**
125
+ * A store computed by fn from one store or a list of stores. Before its
126
+ * first subscriber, get() returns initial.
127
+ */
128
+ export function derived<S extends Readable<any> | readonly Readable<any>[], T>(
129
+ stores: S,
130
+ fn: (values: StoresValues<S>) => T,
131
+ initial?: T,
132
+ ): Derived<T>;