@krate/runtime 0.5.0-alpha.2 → 0.5.0-alpha.3

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.
Files changed (85) hide show
  1. package/dist/action-context.d.ts +49 -0
  2. package/dist/action-context.js +37 -0
  3. package/dist/action-context.test.d.ts +1 -0
  4. package/dist/action-context.test.js +47 -0
  5. package/dist/actions.d.ts +120 -0
  6. package/dist/actions.js +533 -0
  7. package/dist/actions.test.d.ts +1 -0
  8. package/dist/actions.test.js +391 -0
  9. package/dist/boundary.d.ts +121 -0
  10. package/dist/boundary.js +429 -0
  11. package/dist/class-component.d.ts +66 -0
  12. package/dist/class-component.js +633 -0
  13. package/dist/class-component.test.d.ts +1 -0
  14. package/dist/class-component.test.js +411 -0
  15. package/dist/context.d.ts +13 -0
  16. package/dist/context.js +58 -32
  17. package/dist/context.test.d.ts +1 -0
  18. package/dist/context.test.js +57 -0
  19. package/dist/derived.test.d.ts +1 -0
  20. package/dist/derived.test.js +140 -0
  21. package/dist/discrete.test.d.ts +1 -0
  22. package/dist/discrete.test.js +100 -0
  23. package/dist/dom.d.ts +55 -0
  24. package/dist/dom.js +414 -63
  25. package/dist/effects.d.ts +88 -0
  26. package/dist/effects.js +719 -0
  27. package/dist/effects.test.d.ts +1 -0
  28. package/dist/effects.test.js +485 -0
  29. package/dist/events.d.ts +58 -0
  30. package/dist/events.js +503 -0
  31. package/dist/events.test.d.ts +1 -0
  32. package/dist/events.test.js +447 -0
  33. package/dist/index.d.ts +12 -2
  34. package/dist/index.js +8 -2
  35. package/dist/jsx-types.d.ts +2 -0
  36. package/dist/krate-hydrate-lite.js +7 -1
  37. package/dist/krate-hydrate.js +7 -1
  38. package/dist/krate-runtime.js +7 -1
  39. package/dist/krate-suspense.js +2 -0
  40. package/dist/lanes.test.d.ts +1 -0
  41. package/dist/lanes.test.js +59 -0
  42. package/dist/list.d.ts +20 -0
  43. package/dist/list.js +466 -0
  44. package/dist/list.test.d.ts +1 -0
  45. package/dist/list.test.js +344 -0
  46. package/dist/optimistic-lanes.test.d.ts +1 -0
  47. package/dist/optimistic-lanes.test.js +45 -0
  48. package/dist/portal.d.ts +43 -0
  49. package/dist/portal.js +178 -0
  50. package/dist/portal.test.d.ts +1 -0
  51. package/dist/portal.test.js +99 -0
  52. package/dist/react-effects.test.d.ts +1 -0
  53. package/dist/react-effects.test.js +96 -0
  54. package/dist/react-suspense.test.d.ts +1 -0
  55. package/dist/react-suspense.test.js +194 -0
  56. package/dist/react.d.ts +132 -32
  57. package/dist/react.js +335 -81
  58. package/dist/react.test.js +12 -2
  59. package/dist/reconcile.js +13 -0
  60. package/dist/refs.d.ts +33 -0
  61. package/dist/refs.js +142 -0
  62. package/dist/refs.test.d.ts +1 -0
  63. package/dist/refs.test.js +257 -0
  64. package/dist/resource.js +20 -1
  65. package/dist/server-renderer.js +39 -1
  66. package/dist/server.d.ts +4 -1
  67. package/dist/server.js +30 -1
  68. package/dist/server.test.js +20 -0
  69. package/dist/signal-lanes.test.d.ts +1 -0
  70. package/dist/signal-lanes.test.js +32 -0
  71. package/dist/signal.d.ts +150 -1
  72. package/dist/signal.js +750 -65
  73. package/dist/slots.d.ts +18 -0
  74. package/dist/slots.js +108 -0
  75. package/dist/slots.test.d.ts +1 -0
  76. package/dist/slots.test.js +169 -0
  77. package/dist/snapshots.test.d.ts +1 -0
  78. package/dist/snapshots.test.js +208 -0
  79. package/dist/suspense-actions.test.d.ts +1 -0
  80. package/dist/suspense-actions.test.js +72 -0
  81. package/dist/suspense.d.ts +220 -0
  82. package/dist/suspense.js +901 -0
  83. package/dist/suspense.test.d.ts +1 -0
  84. package/dist/suspense.test.js +518 -0
  85. package/package.json +1 -1
@@ -0,0 +1,220 @@
1
+ /** A promise-like value carrying React-compatible status fields. */
2
+ export type Thenable<T> = PromiseLike<T> & {
3
+ status?: 'pending' | 'fulfilled' | 'rejected';
4
+ value?: T;
5
+ reason?: unknown;
6
+ };
7
+ export declare function isThenable(value: unknown): value is Thenable<unknown>;
8
+ /**
9
+ * Starts tracking a thenable's settlement by writing `status`/`value`/`reason`
10
+ * onto it (the same fields React uses, so libraries that pre-resolve promises
11
+ * for React interoperate). The same object is cached by identity: once it is
12
+ * fulfilled, reading it never suspends again.
13
+ */
14
+ export declare function trackThenable<T>(t: Thenable<T>): Thenable<T>;
15
+ /** Thrown (internally) when a pending thenable is read. */
16
+ export declare class SuspenseSignal {
17
+ readonly thenable: Thenable<unknown>;
18
+ constructor(thenable: Thenable<unknown>);
19
+ }
20
+ export declare function isSuspenseSignal(value: unknown): value is SuspenseSignal;
21
+ /**
22
+ * Replay scope: the thenables read in a previous attempt of the same
23
+ * computation, by position. A read at position i reuses the earlier thenable
24
+ * instead of a freshly created one (React's thenable state).
25
+ */
26
+ export interface ReadScope {
27
+ list: Thenable<unknown>[];
28
+ i: number;
29
+ }
30
+ /**
31
+ * Runs one attempt of a computation that may be replayed after suspending:
32
+ * reads restart at position 0 and reuse the thenables of earlier attempts.
33
+ */
34
+ export declare function withReplayScope<T>(scope: ReadScope, fn: () => T): T;
35
+ /**
36
+ * Unwraps a value the way React's `use()` does: a context yields its current
37
+ * value, a fulfilled thenable its value, a rejected thenable throws its reason
38
+ * and a pending one throws a {@link SuspenseSignal}. Other values pass
39
+ * through unchanged.
40
+ */
41
+ export declare function readValue<T>(value: unknown): T;
42
+ /** Receives errors from suspended work (rejected use(), failed lazy chunks). */
43
+ export type SuspenseErrorHandler = (error: unknown, boundary: Boundary | null) => boolean | void;
44
+ /**
45
+ * Installs the handler for errors raised inside Suspense work. An error
46
+ * boundary implementation registers here (or defines a global
47
+ * `__krateReportError(error, anchor)`); returning `true` marks the error as
48
+ * handled. Unhandled errors are logged with `console.error`.
49
+ */
50
+ export declare function setSuspenseErrorHandler(handler: SuspenseErrorHandler | null): void;
51
+ interface OwnerApi {
52
+ getOwner: () => unknown;
53
+ handleError: (error: unknown, owner: unknown) => void;
54
+ }
55
+ /** Connects error boundaries explicitly (tests, the compat runtime). */
56
+ export declare function connectErrorBoundaries(api: OwnerApi | null): void;
57
+ export declare function reportSuspenseError(error: unknown, boundary: Boundary | null, owner?: unknown): void;
58
+ /** One startTransition call: settles when its action and its suspensions do. */
59
+ export interface Transition {
60
+ /** True until the action and every suspension it caused have settled. */
61
+ readonly pending: () => boolean;
62
+ /** Resolves when the transition settles; rejects with the action's error. */
63
+ readonly promise: Promise<void>;
64
+ }
65
+ /** Reports whether the code running now was started by startTransition. */
66
+ export declare function isInTransition(): boolean;
67
+ /**
68
+ * Runs `fn` as a transition and returns its pending state. This is the
69
+ * shared primitive behind `startTransition`, `useTransition` and the React 19
70
+ * action hooks:
71
+ *
72
+ * - updates made synchronously by `fn`, and the effect flush they schedule,
73
+ * are transition updates;
74
+ * - if `fn` returns a promise (an async action) the transition stays pending
75
+ * until it settles, including the awaited work;
76
+ * - a boundary that is already revealed and suspends during the transition
77
+ * keeps its content, and the transition stays pending until it resolves.
78
+ *
79
+ * Updates made after an `await` inside `fn` are ordinary updates, as in
80
+ * React 19; wrap them in another `startTransition` to mark them.
81
+ */
82
+ export declare function runAsyncTransition(fn: () => unknown, options?: {
83
+ report?: boolean;
84
+ }): Transition;
85
+ type BeginAction = () => () => void;
86
+ /**
87
+ * Makes this module the single transition scope for React 19 actions: form
88
+ * actions and useActionState dispatches run through runAsyncTransition, so
89
+ * they share pending state, transition lanes and Suspense behavior with
90
+ * useTransition. Errors are left to the action runtime, which reports them.
91
+ */
92
+ export declare function connectActionTransitions(api?: {
93
+ setActionTransitionRunner?: (runner: (<T>(fn: () => T | Promise<T>) => Promise<T>) | null) => void;
94
+ __krateBeginAction?: BeginAction;
95
+ }): void;
96
+ /** React's top-level `startTransition`. Async functions are supported. */
97
+ export declare function startTransition(fn: () => unknown): void;
98
+ /**
99
+ * Returns a `startTransition` bound to a pending setter: the setter is set to
100
+ * true when a transition starts and back to false once every transition
101
+ * started through it has settled (overlapping transitions keep it pending).
102
+ * Compiled `useTransition()` calls lower to this.
103
+ */
104
+ export declare function __krateTransitionStarter(setPending: (v: boolean) => void): (fn: () => unknown) => void;
105
+ /** `const [isPending, startTransition] = useTransition()`; isPending is a getter. */
106
+ export declare function useTransition(): [() => boolean, (fn: () => unknown) => void];
107
+ /** A Suspense boundary: tracks outstanding work and shows content or fallback. */
108
+ export interface Boundary {
109
+ readonly id: string;
110
+ /** A node inside the boundary (its start marker), for error boundaries. */
111
+ readonly anchor: Node | null;
112
+ /** Number of suspensions currently hiding the content. */
113
+ readonly pendingCount: number;
114
+ /** Whether the content (rather than the fallback) is showing. */
115
+ showingContent(): boolean;
116
+ /** Suspends the boundary on `t` (see {@link suspendOn} for transitions). */
117
+ suspend(t: PromiseLike<unknown>, inTransition?: boolean): void;
118
+ /** Keeps the content hidden until the matching release (a run in progress). */
119
+ hold(): void;
120
+ release(): void;
121
+ }
122
+ /** Records that a fallback became visible (starts the reveal throttle). */
123
+ export declare function noteFallbackShown(): void;
124
+ /** Sets the reveal throttle (React's FALLBACK_THROTTLE_MS); 0 disables it. */
125
+ export declare function setFallbackThrottle(ms: number): void;
126
+ /**
127
+ * Creates a boundary controller over an arbitrary show/hide function. Used by
128
+ * the compiled boundary markers and the compat `<Suspense>` component, and
129
+ * exported for tests and custom integrations.
130
+ */
131
+ export declare function createBoundary(id: string, apply: (showContent: boolean) => void, options?: {
132
+ anchor?: Node | null;
133
+ initialContent?: boolean;
134
+ }): Boundary;
135
+ /** The template/marker id of a compiled boundary's conditional slot. */
136
+ export declare function boundarySlotId(id: string): string;
137
+ /**
138
+ * Returns the boundary for a compiled `<!--suspense:ID-->` marker. The
139
+ * compiler emits the boundary's two states as a conditional slot: the active
140
+ * one between `<!--k:sID-->` markers and the other in a hoisted
141
+ * `<template data-kc="sID" data-kb="c|a">` (c = content, a = fallback).
142
+ */
143
+ export declare function getBoundary(id: string): Boundary;
144
+ /** Registers a boundary under its id so compiled runs can find it. */
145
+ export declare function registerBoundary(boundary: Boundary): void;
146
+ /** Forgets compiled boundaries (SPA navigation re-hydrates a new page). */
147
+ export declare function resetBoundaries(): void;
148
+ /**
149
+ * Suspends `boundary` on `t`. Inside a transition an already revealed
150
+ * boundary keeps its content and the transition waits instead; with no
151
+ * boundary the work only extends the active transition.
152
+ */
153
+ export declare function suspendOn(boundary: Boundary | null, t: PromiseLike<unknown>): void;
154
+ /** The boundary of the component whose hydration is running, if any. */
155
+ export declare function currentBoundary(): Boundary | null;
156
+ /**
157
+ * Runs a component's hydration code inside the boundary `id` (or none). A
158
+ * pending `use()` during the run throws; the run is disposed and repeated
159
+ * when the promise settles, with earlier promises reused by position. Other
160
+ * errors go to the error handler (see {@link setSuspenseErrorHandler}).
161
+ */
162
+ export declare function __krateBoundary(id: string | null, fn: () => void): void;
163
+ /**
164
+ * The compiled form of `const value = use(...)`: `__krateUse(() => expr)`
165
+ * where every `use(x)` inside `expr` became `__krateRead(x)`. Returns a
166
+ * getter.
167
+ *
168
+ * The first evaluation runs synchronously: a pending promise suspends the
169
+ * whole component run (see {@link __krateBoundary}). Later re-evaluations (a
170
+ * signal read by `expr` changed, for example a new promise in state) keep the
171
+ * previous value while the new promise is pending and suspend the boundary:
172
+ * outside a transition it shows the fallback again, inside a transition it
173
+ * keeps the old content.
174
+ */
175
+ export declare function __krateUse<T>(compute: () => T): () => T;
176
+ /** `use(x)` inside a `__krateUse` computation. */
177
+ export declare function __krateRead<T>(value: unknown): T;
178
+ /**
179
+ * Loads the hydration chunk of a `lazy()` component. The component's HTML was
180
+ * rendered at build time, so the content stays visible while its code loads
181
+ * (as with React hydrating server-rendered lazy content); the boundary is
182
+ * held so a boundary that is still showing its fallback reveals together with
183
+ * the lazy content. Loading starts when the page hydrates, the first point the
184
+ * component is rendered on the client. A failed load is reported to the
185
+ * nearest error boundary.
186
+ */
187
+ export declare function __krateLazy(url: string, boundaryId?: string | null): Promise<unknown>;
188
+ /**
189
+ * The module promise of a lazy() whose loader is not a static import. The
190
+ * loader is called once, when the component first hydrates; it must resolve
191
+ * to a module whose default export is a component.
192
+ */
193
+ export declare function __krateLazyPayload(loader: () => PromiseLike<unknown>): Thenable<unknown>;
194
+ /** Overrides how lazy chunks are fetched (tests, custom loaders). */
195
+ export declare function setLazyChunkImporter(fn: (url: string) => Promise<unknown>): void;
196
+ /** Entry point of a lazy hydration chunk: runs the chunk's component code. */
197
+ export declare function __krateLazyRun(fn: () => void): void;
198
+ /**
199
+ * React's `useDeferredValue(value, initialValue?)` as a getter. During an
200
+ * urgent update the getter keeps returning the previous value and catches up
201
+ * in a later task (a `setTimeout(0)` after the update), so urgent DOM work
202
+ * lands first. With `initialValue` the first value is `initialValue` and the
203
+ * real value follows the same way (React 19). Updates made inside a
204
+ * transition are not deferred again.
205
+ */
206
+ export declare function createDeferredValue<T>(get: () => T, ...initial: [] | [T]): () => T;
207
+ /** React's `useDeferredValue` for compat callers: wraps a getter or a value. */
208
+ export declare function useDeferredValue<T>(value: T | (() => T), ...initial: [] | [T]): T | (() => T);
209
+ /**
210
+ * React's `useSyncExternalStore` as a getter. Subscribes once, re-reads the
211
+ * snapshot on every store notification (and once right after subscribing, to
212
+ * catch changes between the first read and the subscription), stores the
213
+ * snapshot only when it changed by `Object.is`, and unsubscribes when the
214
+ * component is torn down. `getServerSnapshot` is what the build renders;
215
+ * the client hydrates from `getSnapshot`.
216
+ */
217
+ export declare function createExternalStore<T>(subscribe: (onChange: () => void) => (() => void) | void, getSnapshot: () => T, _getServerSnapshot?: () => T): () => T;
218
+ /** React's `useSyncExternalStore` (getter result). */
219
+ export declare function useSyncExternalStore<T>(subscribe: (onChange: () => void) => (() => void) | void, getSnapshot: () => T, getServerSnapshot?: () => T): () => T;
220
+ export {};