@creact-labs/creact 0.1.8 → 0.2.1

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 (146) hide show
  1. package/README.md +85 -22
  2. package/dist/cli.d.ts +11 -0
  3. package/dist/cli.js +88 -0
  4. package/dist/index.d.ts +19 -44
  5. package/dist/index.js +20 -68
  6. package/dist/jsx/index.d.ts +2 -0
  7. package/dist/jsx/index.js +1 -0
  8. package/dist/jsx/jsx-dev-runtime.d.ts +4 -0
  9. package/dist/jsx/jsx-dev-runtime.js +4 -0
  10. package/dist/jsx/jsx-runtime.d.ts +38 -0
  11. package/dist/jsx/jsx-runtime.js +38 -0
  12. package/dist/jsx/types.d.ts +12 -0
  13. package/dist/jsx/types.js +4 -0
  14. package/dist/primitives/context.d.ts +34 -0
  15. package/dist/primitives/context.js +63 -0
  16. package/dist/primitives/index.d.ts +3 -0
  17. package/dist/primitives/index.js +3 -0
  18. package/dist/primitives/instance.d.ts +72 -0
  19. package/dist/primitives/instance.js +235 -0
  20. package/dist/primitives/store.d.ts +22 -0
  21. package/dist/primitives/store.js +97 -0
  22. package/dist/provider/backend.d.ts +110 -0
  23. package/dist/provider/backend.js +37 -0
  24. package/dist/provider/interface.d.ts +48 -0
  25. package/dist/provider/interface.js +39 -0
  26. package/dist/reactive/effect.d.ts +11 -0
  27. package/dist/reactive/effect.js +42 -0
  28. package/dist/reactive/index.d.ts +3 -0
  29. package/dist/reactive/index.js +3 -0
  30. package/dist/reactive/signal.d.ts +32 -0
  31. package/dist/reactive/signal.js +60 -0
  32. package/dist/reactive/tracking.d.ts +41 -0
  33. package/dist/reactive/tracking.js +161 -0
  34. package/dist/runtime/fiber.d.ts +21 -0
  35. package/dist/runtime/fiber.js +16 -0
  36. package/dist/runtime/index.d.ts +4 -0
  37. package/dist/runtime/index.js +4 -0
  38. package/dist/runtime/reconcile.d.ts +66 -0
  39. package/dist/runtime/reconcile.js +210 -0
  40. package/dist/runtime/render.d.ts +42 -0
  41. package/dist/runtime/render.js +231 -0
  42. package/dist/runtime/run.d.ts +119 -0
  43. package/dist/runtime/run.js +334 -0
  44. package/dist/runtime/state-machine.d.ts +95 -0
  45. package/dist/runtime/state-machine.js +209 -0
  46. package/dist/types.d.ts +13 -0
  47. package/dist/types.js +4 -0
  48. package/package.json +29 -24
  49. package/dist/cli/commands/BuildCommand.d.ts +0 -40
  50. package/dist/cli/commands/BuildCommand.js +0 -151
  51. package/dist/cli/commands/DeployCommand.d.ts +0 -38
  52. package/dist/cli/commands/DeployCommand.js +0 -194
  53. package/dist/cli/commands/DevCommand.d.ts +0 -52
  54. package/dist/cli/commands/DevCommand.js +0 -394
  55. package/dist/cli/commands/PlanCommand.d.ts +0 -39
  56. package/dist/cli/commands/PlanCommand.js +0 -164
  57. package/dist/cli/commands/index.d.ts +0 -36
  58. package/dist/cli/commands/index.js +0 -43
  59. package/dist/cli/core/ArgumentParser.d.ts +0 -46
  60. package/dist/cli/core/ArgumentParser.js +0 -127
  61. package/dist/cli/core/BaseCommand.d.ts +0 -75
  62. package/dist/cli/core/BaseCommand.js +0 -95
  63. package/dist/cli/core/CLIContext.d.ts +0 -68
  64. package/dist/cli/core/CLIContext.js +0 -183
  65. package/dist/cli/core/CommandRegistry.d.ts +0 -64
  66. package/dist/cli/core/CommandRegistry.js +0 -89
  67. package/dist/cli/core/index.d.ts +0 -36
  68. package/dist/cli/core/index.js +0 -43
  69. package/dist/cli/index.d.ts +0 -35
  70. package/dist/cli/index.js +0 -100
  71. package/dist/cli/output.d.ts +0 -204
  72. package/dist/cli/output.js +0 -437
  73. package/dist/cli/utils.d.ts +0 -59
  74. package/dist/cli/utils.js +0 -76
  75. package/dist/context/createContext.d.ts +0 -90
  76. package/dist/context/createContext.js +0 -113
  77. package/dist/context/index.d.ts +0 -30
  78. package/dist/context/index.js +0 -35
  79. package/dist/core/CReact.d.ts +0 -409
  80. package/dist/core/CReact.js +0 -1151
  81. package/dist/core/CloudDOMBuilder.d.ts +0 -447
  82. package/dist/core/CloudDOMBuilder.js +0 -1234
  83. package/dist/core/ContextDependencyTracker.d.ts +0 -165
  84. package/dist/core/ContextDependencyTracker.js +0 -448
  85. package/dist/core/ErrorRecoveryManager.d.ts +0 -145
  86. package/dist/core/ErrorRecoveryManager.js +0 -443
  87. package/dist/core/EventBus.d.ts +0 -91
  88. package/dist/core/EventBus.js +0 -185
  89. package/dist/core/ProviderOutputTracker.d.ts +0 -211
  90. package/dist/core/ProviderOutputTracker.js +0 -476
  91. package/dist/core/ReactiveUpdateQueue.d.ts +0 -76
  92. package/dist/core/ReactiveUpdateQueue.js +0 -121
  93. package/dist/core/Reconciler.d.ts +0 -415
  94. package/dist/core/Reconciler.js +0 -1044
  95. package/dist/core/RenderScheduler.d.ts +0 -153
  96. package/dist/core/RenderScheduler.js +0 -519
  97. package/dist/core/Renderer.d.ts +0 -336
  98. package/dist/core/Renderer.js +0 -944
  99. package/dist/core/Runtime.d.ts +0 -246
  100. package/dist/core/Runtime.js +0 -640
  101. package/dist/core/StateBindingManager.d.ts +0 -121
  102. package/dist/core/StateBindingManager.js +0 -309
  103. package/dist/core/StateMachine.d.ts +0 -441
  104. package/dist/core/StateMachine.js +0 -883
  105. package/dist/core/StructuralChangeDetector.d.ts +0 -140
  106. package/dist/core/StructuralChangeDetector.js +0 -363
  107. package/dist/core/Validator.d.ts +0 -127
  108. package/dist/core/Validator.js +0 -279
  109. package/dist/core/errors.d.ts +0 -153
  110. package/dist/core/errors.js +0 -202
  111. package/dist/core/index.d.ts +0 -38
  112. package/dist/core/index.js +0 -64
  113. package/dist/core/types.d.ts +0 -265
  114. package/dist/core/types.js +0 -48
  115. package/dist/hooks/context.d.ts +0 -147
  116. package/dist/hooks/context.js +0 -334
  117. package/dist/hooks/useContext.d.ts +0 -113
  118. package/dist/hooks/useContext.js +0 -169
  119. package/dist/hooks/useEffect.d.ts +0 -105
  120. package/dist/hooks/useEffect.js +0 -540
  121. package/dist/hooks/useInstance.d.ts +0 -139
  122. package/dist/hooks/useInstance.js +0 -455
  123. package/dist/hooks/useState.d.ts +0 -120
  124. package/dist/hooks/useState.js +0 -298
  125. package/dist/jsx.d.ts +0 -143
  126. package/dist/jsx.js +0 -76
  127. package/dist/providers/DummyBackendProvider.d.ts +0 -193
  128. package/dist/providers/DummyBackendProvider.js +0 -189
  129. package/dist/providers/DummyCloudProvider.d.ts +0 -128
  130. package/dist/providers/DummyCloudProvider.js +0 -157
  131. package/dist/providers/IBackendProvider.d.ts +0 -177
  132. package/dist/providers/IBackendProvider.js +0 -31
  133. package/dist/providers/ICloudProvider.d.ts +0 -230
  134. package/dist/providers/ICloudProvider.js +0 -31
  135. package/dist/providers/index.d.ts +0 -31
  136. package/dist/providers/index.js +0 -31
  137. package/dist/test-event-callbacks.d.ts +0 -0
  138. package/dist/test-event-callbacks.js +0 -1
  139. package/dist/utils/Logger.d.ts +0 -144
  140. package/dist/utils/Logger.js +0 -220
  141. package/dist/utils/Output.d.ts +0 -161
  142. package/dist/utils/Output.js +0 -401
  143. package/dist/utils/deepEqual.d.ts +0 -71
  144. package/dist/utils/deepEqual.js +0 -276
  145. package/dist/utils/naming.d.ts +0 -241
  146. package/dist/utils/naming.js +0 -376
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Render - transform JSX to Fiber tree
3
+ */
4
+ import type { Fiber } from './fiber';
5
+ /**
6
+ * Get current fiber (for hooks)
7
+ */
8
+ export declare function getCurrentFiber(): Fiber | null;
9
+ /**
10
+ * Get current path (for hooks)
11
+ */
12
+ export declare function getCurrentPath(): string[];
13
+ /**
14
+ * Get current resource path (for useInstance)
15
+ * Only components with useInstance contribute to this path
16
+ */
17
+ export declare function getCurrentResourcePath(): string[];
18
+ /**
19
+ * Push a segment to resource path (called by useInstance)
20
+ */
21
+ export declare function pushResourcePath(segment: string): void;
22
+ /**
23
+ * Pop a segment from resource path (called after component with instance renders)
24
+ */
25
+ export declare function popResourcePath(): void;
26
+ /**
27
+ * Reset resource path (called at start of render)
28
+ */
29
+ export declare function resetResourcePath(): void;
30
+ /**
31
+ * Render a JSX element to a Fiber
32
+ */
33
+ export declare function renderFiber(element: any, path: string[]): Fiber;
34
+ /**
35
+ * Collect all instance nodes from fiber tree
36
+ * Returns cloned nodes to ensure returned arrays are independent snapshots
37
+ */
38
+ export declare function collectInstanceNodes(fiber: Fiber): any[];
39
+ /**
40
+ * Clean up a fiber tree
41
+ */
42
+ export declare function cleanupFiber(fiber: Fiber): void;
@@ -0,0 +1,231 @@
1
+ /**
2
+ * Render - transform JSX to Fiber tree
3
+ */
4
+ import { popContext, pushContext } from '../primitives/context';
5
+ import { cleanComputation, runComputation } from '../reactive/tracking';
6
+ import { createFiber } from './fiber';
7
+ // Current render context
8
+ let currentFiber = null;
9
+ let currentPath = [];
10
+ // Resource path - only components with useInstance contribute to this
11
+ // This makes wrapper components (without useInstance) transparent
12
+ let resourcePath = [];
13
+ /**
14
+ * Get current fiber (for hooks)
15
+ */
16
+ export function getCurrentFiber() {
17
+ return currentFiber;
18
+ }
19
+ /**
20
+ * Get current path (for hooks)
21
+ */
22
+ export function getCurrentPath() {
23
+ return [...currentPath];
24
+ }
25
+ /**
26
+ * Get current resource path (for useInstance)
27
+ * Only components with useInstance contribute to this path
28
+ */
29
+ export function getCurrentResourcePath() {
30
+ return [...resourcePath];
31
+ }
32
+ /**
33
+ * Push a segment to resource path (called by useInstance)
34
+ */
35
+ export function pushResourcePath(segment) {
36
+ resourcePath.push(segment);
37
+ }
38
+ /**
39
+ * Pop a segment from resource path (called after component with instance renders)
40
+ */
41
+ export function popResourcePath() {
42
+ resourcePath.pop();
43
+ }
44
+ /**
45
+ * Reset resource path (called at start of render)
46
+ */
47
+ export function resetResourcePath() {
48
+ resourcePath = [];
49
+ }
50
+ /**
51
+ * Render a JSX element to a Fiber
52
+ */
53
+ export function renderFiber(element, path) {
54
+ // Handle null/undefined/boolean
55
+ if (element == null || typeof element === 'boolean') {
56
+ return createFiber(null, {}, path);
57
+ }
58
+ // Handle primitives (text)
59
+ if (typeof element === 'string' || typeof element === 'number') {
60
+ return createFiber('text', { value: element }, path);
61
+ }
62
+ // Handle arrays
63
+ if (Array.isArray(element)) {
64
+ const fiber = createFiber('fragment', {}, path);
65
+ fiber.children = element.map((child, i) => {
66
+ const childKey = child?.key ?? i;
67
+ return renderFiber(child, [...path, String(childKey)]);
68
+ });
69
+ return fiber;
70
+ }
71
+ // Handle JSX element
72
+ const { type, props = {}, key } = element;
73
+ const { children, ...restProps } = props;
74
+ // Generate path segment
75
+ const name = getNodeName(type, props, key);
76
+ const fiberPath = [...path, name];
77
+ const fiber = createFiber(type, restProps, fiberPath, key);
78
+ // Check if this is a context provider
79
+ if (element.__isProvider && element.__context) {
80
+ pushContext(element.__context, props.value);
81
+ try {
82
+ fiber.children = renderChildren(children, fiberPath);
83
+ }
84
+ finally {
85
+ popContext(element.__context);
86
+ }
87
+ return fiber;
88
+ }
89
+ // Handle function components
90
+ if (typeof type === 'function') {
91
+ // Capture incoming resource path for reactive re-renders
92
+ fiber.incomingResourcePath = [...resourcePath];
93
+ // Create computation for this component
94
+ const computation = {
95
+ fn: () => executeComponent(fiber, type, { ...restProps, children }),
96
+ sources: null,
97
+ sourceSlots: null,
98
+ state: 1, // STALE
99
+ cleanups: null,
100
+ };
101
+ fiber.computation = computation;
102
+ // Initial render
103
+ runComputation(computation);
104
+ }
105
+ else {
106
+ // Intrinsic element - just render children
107
+ fiber.children = renderChildren(children, fiberPath);
108
+ }
109
+ return fiber;
110
+ }
111
+ /**
112
+ * Recursively clean up computations from a fiber tree
113
+ * This prevents stale computations from observing signals after re-render
114
+ * and ensures queued computations won't run (by marking them CLEAN)
115
+ */
116
+ function cleanupFiberTree(fibers) {
117
+ for (const fiber of fibers) {
118
+ if (fiber.computation) {
119
+ cleanComputation(fiber.computation);
120
+ // Mark as CLEAN (0) so it won't run if already queued in batch
121
+ fiber.computation.state = 0;
122
+ }
123
+ if (fiber.children.length > 0) {
124
+ cleanupFiberTree(fiber.children);
125
+ }
126
+ }
127
+ }
128
+ /**
129
+ * Execute a component function
130
+ */
131
+ // biome-ignore lint/complexity/noBannedTypes: JSX component types are dynamically resolved at runtime
132
+ function executeComponent(fiber, type, props) {
133
+ const prevFiber = currentFiber;
134
+ const prevPath = currentPath;
135
+ currentFiber = fiber;
136
+ currentPath = fiber.path;
137
+ // Restore resource path for reactive re-renders
138
+ // This ensures components see the correct resource path even when
139
+ // re-executing independently (not from root)
140
+ resourcePath = [...(fiber.incomingResourcePath ?? [])];
141
+ // Clean up old children's computations before re-rendering
142
+ // This prevents stale computations from observing signals
143
+ if (fiber.children.length > 0) {
144
+ cleanupFiberTree(fiber.children);
145
+ }
146
+ // Clear instance nodes and placeholder flag before re-executing component
147
+ fiber.instanceNodes = [];
148
+ fiber.hasPlaceholderInstance = false;
149
+ try {
150
+ // Execute component
151
+ const result = type(props);
152
+ // Render children from result
153
+ fiber.children = renderChildren(result, fiber.path);
154
+ }
155
+ finally {
156
+ // If this component had useInstance (real or placeholder), pop its resource path segment
157
+ // (useInstance pushes a segment so children see it in their resource path)
158
+ if (fiber.instanceNodes.length > 0 || fiber.hasPlaceholderInstance) {
159
+ popResourcePath();
160
+ }
161
+ currentFiber = prevFiber;
162
+ currentPath = prevPath;
163
+ }
164
+ }
165
+ /**
166
+ * Render children (handles various child types)
167
+ */
168
+ function renderChildren(children, parentPath) {
169
+ if (children == null || typeof children === 'boolean') {
170
+ return [];
171
+ }
172
+ if (Array.isArray(children)) {
173
+ return children.flatMap((child, i) => {
174
+ const childKey = child?.key ?? i;
175
+ const fiber = renderFiber(child, [...parentPath, String(childKey)]);
176
+ return fiber.type === null ? [] : [fiber];
177
+ });
178
+ }
179
+ const fiber = renderFiber(children, parentPath);
180
+ return fiber.type === null ? [] : [fiber];
181
+ }
182
+ /**
183
+ * Get node name from type and props
184
+ */
185
+ function getNodeName(type, _props, key) {
186
+ if (key !== undefined) {
187
+ return String(key);
188
+ }
189
+ if (typeof type === 'string') {
190
+ return type;
191
+ }
192
+ if (typeof type === 'function') {
193
+ return type.name || 'Component';
194
+ }
195
+ return 'unknown';
196
+ }
197
+ /**
198
+ * Collect all instance nodes from fiber tree
199
+ * Returns cloned nodes to ensure returned arrays are independent snapshots
200
+ */
201
+ export function collectInstanceNodes(fiber) {
202
+ const nodes = [];
203
+ function walk(f) {
204
+ for (const instanceNode of f.instanceNodes) {
205
+ // Clone the node to create an independent snapshot
206
+ // (outputSignals are not cloned - they're internal reactive state)
207
+ const snapshot = {
208
+ ...instanceNode,
209
+ // Deep clone the store so it's independent between runs
210
+ store: f.store ? JSON.parse(JSON.stringify(f.store)) : undefined,
211
+ };
212
+ nodes.push(snapshot);
213
+ }
214
+ for (const child of f.children) {
215
+ walk(child);
216
+ }
217
+ }
218
+ walk(fiber);
219
+ return nodes;
220
+ }
221
+ /**
222
+ * Clean up a fiber tree
223
+ */
224
+ export function cleanupFiber(fiber) {
225
+ if (fiber.computation) {
226
+ cleanComputation(fiber.computation);
227
+ }
228
+ for (const child of fiber.children) {
229
+ cleanupFiber(child);
230
+ }
231
+ }
@@ -0,0 +1,119 @@
1
+ /**
2
+ * CReact - Continuous Reactive Runtime with State Machine
3
+ *
4
+ * Event-driven runtime that:
5
+ * 1. Renders JSX -> Fiber tree
6
+ * 2. Reconciles and applies changes with dependency ordering
7
+ * 3. Persists state to backend for crash recovery
8
+ * 4. Subscribes to provider events
9
+ * 5. When events arrive -> updates affected fibers -> re-renders -> applies new changes
10
+ */
11
+ import type { InstanceNode } from '../primitives/instance';
12
+ import type { Backend } from '../provider/backend';
13
+ import type { OutputChangeEvent, Provider } from '../provider/interface';
14
+ import type { Fiber } from './fiber';
15
+ import { StateMachine } from './state-machine';
16
+ export interface CReactOptions {
17
+ maxIterations?: number;
18
+ /** User identifier for audit logs */
19
+ user?: string;
20
+ /** Enable audit logging (requires backend support) */
21
+ enableAuditLog?: boolean;
22
+ }
23
+ /**
24
+ * CReact - Continuous reactive runtime with state machine
25
+ *
26
+ * Subscribes to provider events and automatically re-renders
27
+ * components when outputs change. This is event-driven, not poll-driven.
28
+ *
29
+ * Requires both:
30
+ * - CReact.provider: Materializes resources (AWS, Terraform, etc.)
31
+ * - CReact.backend: Persists deployment state (file, DynamoDB, etc.)
32
+ */
33
+ export declare class CReact {
34
+ static provider: Provider;
35
+ static backend: Backend;
36
+ private static _lastRuntime;
37
+ private static _lastStackName;
38
+ protected instanceProvider: Provider;
39
+ protected instanceBackend: Backend;
40
+ protected stateMachine: StateMachine;
41
+ protected rootFiber: Fiber | null;
42
+ protected currentNodes: InstanceNode[];
43
+ protected options: CReactOptions;
44
+ protected outputChangeHandler: ((change: OutputChangeEvent) => void) | null;
45
+ protected stackName: string | null;
46
+ constructor(provider: Provider, backend: Backend, options?: CReactOptions);
47
+ /**
48
+ * Start the runtime - render, apply, and begin listening for events
49
+ *
50
+ * @param element - JSX element to render
51
+ * @param stackNameOrPrevious - Stack name (string) or previous nodes (array) for backward compat
52
+ */
53
+ run(element: any, stackNameOrPrevious?: string | InstanceNode[]): Promise<void>;
54
+ /**
55
+ * Apply changes between previous and current nodes
56
+ *
57
+ * Uses dependency ordering from reconciler:
58
+ * 1. Apply deletes (reverse dependency order)
59
+ * 2. Apply creates/updates in deployment order
60
+ * 3. Recursively apply any new nodes that appeared
61
+ *
62
+ * With synchronous batching, fillInstanceOutputs triggers dependent
63
+ * re-renders immediately. New nodes appear in registry right away.
64
+ */
65
+ protected applyChanges(previousNodes: InstanceNode[]): Promise<void>;
66
+ /**
67
+ * Handle provider output change events
68
+ *
69
+ * This is the event-driven part - when provider emits changes,
70
+ * we update signals (triggering synchronous re-renders via batch),
71
+ * and apply any new nodes.
72
+ */
73
+ protected handleOutputChange(change: OutputChangeEvent): Promise<void>;
74
+ /**
75
+ * Get current instance nodes (for debugging/testing)
76
+ */
77
+ getNodes(): InstanceNode[];
78
+ /**
79
+ * Get the state machine (for advanced usage)
80
+ */
81
+ getStateMachine(): StateMachine | null;
82
+ /**
83
+ * Stop the runtime
84
+ */
85
+ stop(): void;
86
+ /**
87
+ * Render CloudDOM - main entry point
88
+ *
89
+ * Usage:
90
+ * CReact.provider = myProvider;
91
+ * CReact.backend = myBackend;
92
+ * export default () => renderCloudDOM(<App />, 'my-stack');
93
+ */
94
+ static renderCloudDOM(element: any, stackName: string): Promise<InstanceNode[]>;
95
+ /**
96
+ * Get last runtime instance (for hot reload / CLI tooling)
97
+ * Hot reload uses this to re-render with updated code without full restart
98
+ */
99
+ static getLastRuntime(): {
100
+ runtime: CReact;
101
+ stackName: string;
102
+ } | null;
103
+ }
104
+ /**
105
+ * Reset all runtime state (for testing)
106
+ */
107
+ export declare function resetRuntime(): void;
108
+ /**
109
+ * Convenience function to run once and return nodes (for testing)
110
+ *
111
+ * Creates a runtime with a simple in-memory backend and runs it.
112
+ * Use this for tests where you don't need persistent state.
113
+ */
114
+ export declare function run(rootElement: any, provider: Provider, previousNodes?: InstanceNode[], options?: CReactOptions): Promise<InstanceNode[]>;
115
+ /**
116
+ * Run with explicit backend (for production use)
117
+ */
118
+ export declare function runWithBackend(rootElement: any, provider: Provider, backend: Backend, stackName: string, options?: CReactOptions): Promise<InstanceNode[]>;
119
+ export declare const renderCloudDOM: typeof CReact.renderCloudDOM;