@krate/runtime 0.5.0-alpha.1 → 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 (87) 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 +8 -0
  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/router-lite.d.ts +8 -0
  66. package/dist/router-lite.js +11 -0
  67. package/dist/server-renderer.js +39 -1
  68. package/dist/server.d.ts +4 -1
  69. package/dist/server.js +30 -1
  70. package/dist/server.test.js +20 -0
  71. package/dist/signal-lanes.test.d.ts +1 -0
  72. package/dist/signal-lanes.test.js +32 -0
  73. package/dist/signal.d.ts +150 -1
  74. package/dist/signal.js +750 -65
  75. package/dist/slots.d.ts +18 -0
  76. package/dist/slots.js +108 -0
  77. package/dist/slots.test.d.ts +1 -0
  78. package/dist/slots.test.js +169 -0
  79. package/dist/snapshots.test.d.ts +1 -0
  80. package/dist/snapshots.test.js +208 -0
  81. package/dist/suspense-actions.test.d.ts +1 -0
  82. package/dist/suspense-actions.test.js +72 -0
  83. package/dist/suspense.d.ts +220 -0
  84. package/dist/suspense.js +901 -0
  85. package/dist/suspense.test.d.ts +1 -0
  86. package/dist/suspense.test.js +518 -0
  87. package/package.json +1 -1
package/dist/signal.d.ts CHANGED
@@ -6,7 +6,108 @@ export interface SignalOptions<T> {
6
6
  * the key, store (local/session/memory), serialization, and cross-tab sync.
7
7
  */
8
8
  persist?: PersistSpec<T>;
9
+ /**
10
+ * React state semantics (set by the compiler for useState/useReducer).
11
+ * Outside Krate's render phase (event handlers, effect bodies, cleanups)
12
+ * the getter returns the value of the last commit, so a handler sees its
13
+ * render's snapshot and a cleanup sees the render its effect ran for.
14
+ * Writes still apply immediately to functional updaters, and an update made
15
+ * outside a discrete event commits in a later task, as React schedules it.
16
+ */
17
+ react?: boolean;
18
+ }
19
+ /** Signal options that give a signal React state semantics. */
20
+ export declare const krateHookState: {
21
+ readonly react: true;
22
+ };
23
+ /**
24
+ * Owner integration (installed by boundary.ts). An owner is the component
25
+ * instance that is current while code runs; effects, cleanups and mount
26
+ * callbacks created under it are tied to it, and errors they throw are routed
27
+ * to it (error boundaries, Suspense). With no hooks installed every code path
28
+ * below behaves exactly as before.
29
+ */
30
+ export interface OwnerHooks {
31
+ /** The owner active right now, or null. */
32
+ current(): unknown;
33
+ /** Runs fn with owner current, routing anything it throws. */
34
+ run(owner: unknown, fn: () => void): void;
35
+ /** Registers fn to run when owner is unmounted. */
36
+ cleanup(owner: unknown, fn: () => void): void;
37
+ /** Routes an error thrown by owner's code; true when handled. */
38
+ error(owner: unknown, err: unknown): boolean;
9
39
  }
40
+ /** Installs (or with null removes) the owner integration hooks. */
41
+ export declare function setOwnerHooks(hooks: OwnerHooks | null): void;
42
+ /**
43
+ * Disposes every effect created under `owner` and runs its top-level
44
+ * onCleanup callbacks (internal, used when a component unmounts).
45
+ */
46
+ export declare function krateDisposeOwner(owner: unknown): void;
47
+ /**
48
+ * Runs fn as render work: React state reads see the latest values rather than
49
+ * the last commit's (internal; the class component render phase).
50
+ */
51
+ export declare function krateRenderPhase<T>(fn: () => T): T;
52
+ /** Records the current React state values as commit `epoch` (internal). */
53
+ export declare function krateCommitSnapshot(): number;
54
+ /** The epoch of the last commit snapshot (internal). */
55
+ export declare function krateEpoch(): number;
56
+ /** Runs fn reading React state as of commit `at` (internal). */
57
+ export declare function krateReadAt<T>(at: number | null, fn: () => T): T;
58
+ /** Drops snapshot history older than commit `min` (internal). */
59
+ export declare function kratePruneSnapshots(min: number): void;
60
+ /**
61
+ * Runs fn with its signal writes in the transition lane: they commit in a
62
+ * later task, like React's transition and default updates, even for plain
63
+ * signals. Used for the updates an async action makes when it settles
64
+ * (pending flags, results), so the pending render stays on screen for at
65
+ * least one task, as it does in React.
66
+ */
67
+ export declare function krateTransitionWrites<T>(fn: () => T): T;
68
+ /** Reports whether a native event type is discrete (React's SyncLane). */
69
+ export declare function krateIsDiscrete(type: string): boolean;
70
+ /**
71
+ * Runs the handlers of one native event dispatch. For a discrete event
72
+ * (click, input, keydown, submit, change, focus, ...) the updates they made
73
+ * are flushed synchronously when `flush` is set, as React does at the end of
74
+ * a discrete event: render, commit, layout and passive effects all run before
75
+ * the browser event returns. Continuous events keep the normal batching.
76
+ */
77
+ export declare function krateRunEvent(type: string, flush: boolean, fn: () => void): void;
78
+ /** Marks a commit in progress (internal): writes there flush in a microtask. */
79
+ export declare function krateCommitting(on: boolean): void;
80
+ /** Reports and clears whether the pending commit came from a discrete event. */
81
+ export declare function krateTakeSyncLane(): boolean;
82
+ /** Returns the current component owner (internal, used by effects.ts). */
83
+ export declare function krateGetOwner(): unknown;
84
+ /** Sets the current component owner and returns the previous one (internal). */
85
+ export declare function krateSetOwner(owner: unknown): unknown;
86
+ /** Registers fn to run before every effect flush starts (internal). */
87
+ export declare function krateOnBeforeFlush(fn: () => void): void;
88
+ /** Registers fn to run after every settled effect flush (internal). */
89
+ export declare function krateOnAfterFlush(fn: () => void): void;
90
+ /** Registers fn to run at the start of disposeAll (internal). */
91
+ export declare function krateOnDispose(fn: () => void): void;
92
+ /** Reports whether signal writes are waiting for the next flush. */
93
+ export declare function kratePendingFlush(): boolean;
94
+ /**
95
+ * Runs fn without subscribing the surrounding effect to the signals it reads.
96
+ */
97
+ export declare function untrack<T>(fn: () => T): T;
98
+ /**
99
+ * Runs fn once after the pending effect queue has fully drained (including
100
+ * effects scheduled while it drained) and the after-flush hooks ran, so the
101
+ * DOM reflects every write made so far. Schedules a flush when none is
102
+ * pending. Used by the class component commit phase.
103
+ */
104
+ export declare function afterFlush(fn: () => void): void;
105
+ /**
106
+ * Same-value equality used for every React-facing comparison (state
107
+ * bail-out, dependency arrays): Object.is, so NaN equals NaN and +0 differs
108
+ * from -0.
109
+ */
110
+ export declare function sameValue(a: unknown, b: unknown): boolean;
10
111
  export declare function disposeAll(): void;
11
112
  /** ARIA role presets and raw overrides accepted by every CSS primitive. */
12
113
  export interface CSSARIAOptions {
@@ -141,8 +242,56 @@ export declare function createCSSStack<K extends string>(_nodes: readonly K[], _
141
242
  peek: () => K;
142
243
  }, CSSStackActions<K>];
143
244
  export declare function createSignal<T>(initial: T, options?: SignalOptions<T>): [() => T, (next: T | ((prev: T) => T)) => void];
245
+ /**
246
+ * A lazily evaluated derived value: the compile target of a React render-body
247
+ * value (`const total = rows.reduce(...)`). The getter recomputes on read when
248
+ * a signal (or another derived value) it read last time has changed since, so
249
+ * every reader in a flush sees the same current value. Nothing is computed
250
+ * until the first read, and a write only marks it stale. triggers, when
251
+ * given, is read for its subscriptions only: an object or array literal built
252
+ * in render gets a new identity whenever the component would re-render.
253
+ */
254
+ export declare function krateDerived<T>(fn: () => T, triggers?: () => unknown): () => T;
255
+ /** Reports whether fn is a getter returned by krateDerived. */
256
+ export declare function isDerivedGetter(fn: unknown): boolean;
257
+ /**
258
+ * Normalizes a custom hook's return value to a getter. A hook compiled from
259
+ * React source returns a derived getter for a render-dependent result; any
260
+ * other value (a hook from a plain module, a constant) is wrapped so the call
261
+ * site can read every hook result the same way.
262
+ */
263
+ export declare function krateHook<T>(value: T | (() => T)): () => T;
264
+ /** Reports whether fn is a getter returned by createSignal. */
265
+ export declare function isSignalGetter(fn: unknown): boolean;
266
+ /**
267
+ * Registers fn to run when a flush has no pending effects, before the
268
+ * after-flush hooks. Signal writes it makes are flushed first (internal).
269
+ */
270
+ export declare function krateOnSettle(fn: () => void): void;
271
+ /** Ensures a flush runs soon even when no signal was written (internal). */
272
+ export declare function krateRequestFlush(): void;
273
+ /**
274
+ * Runs the after-flush hooks now. Used when no signal flush is pending (for
275
+ * example, right after hydration registered effects).
276
+ */
277
+ export declare function runAfterFlush(): void;
278
+ /**
279
+ * Synchronously flushes pending signal effects and the commit that follows
280
+ * them (the building block for flushSync and tests).
281
+ */
282
+ export declare function krateFlushSync(): void;
144
283
  export declare function createEffect(fn: () => void): () => void;
145
284
  export declare function onCleanup(fn: () => void): void;
285
+ /**
286
+ * Run `fn` in a new ownership root. Effects, memos, onCleanup callbacks and
287
+ * onMount callbacks registered while it runs (and the cleanups those mount
288
+ * callbacks register) belong to the root and are torn down, in registration
289
+ * order, when the returned `dispose` is called. Signal reads inside `fn` are
290
+ * not tracked by any enclosing effect. Keyed list rows and keyed remounts use
291
+ * one root per row so a removed row runs its cleanups without touching its
292
+ * siblings.
293
+ */
294
+ export declare function createRoot<T>(fn: (dispose: () => void) => T): T;
146
295
  export declare function onMount(fn: () => void): void;
147
296
  export declare function createMemo<T>(fn: () => T): () => T;
148
297
  /**
@@ -152,7 +301,7 @@ export declare function createMemo<T>(fn: () => T): () => T;
152
301
  * other signal. Used both directly and as the lowering target for React's
153
302
  * `useReducer`.
154
303
  */
155
- export declare function createReducer<S, A>(reducer: (state: S, action: A) => S, initial: S): [() => S, (action: A) => void];
304
+ export declare function createReducer<S, A, I = S>(reducer: (state: S, action: A) => S, initial: I, init?: ((arg: I) => S) | SignalOptions<S> | null, options?: SignalOptions<S>): [() => S, (action: A) => void];
156
305
  /** A mutable ref object. `current` starts as `initial` and is assigned the
157
306
  * DOM element when passed as `ref={refObj}` on an element. */
158
307
  export interface RefObject<T> {