@constela/runtime 0.1.0 → 0.3.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/dist/index.js CHANGED
@@ -150,8 +150,25 @@ function evaluate(expr, ctx) {
150
150
  return evaluateBinary(expr.op, expr.left, expr.right, ctx);
151
151
  case "not":
152
152
  return !evaluate(expr.operand, ctx);
153
- default:
154
- throw new Error("Unknown expression type");
153
+ case "cond":
154
+ return evaluate(expr.if, ctx) ? evaluate(expr.then, ctx) : evaluate(expr.else, ctx);
155
+ case "get": {
156
+ const baseValue = evaluate(expr.base, ctx);
157
+ if (baseValue == null) return void 0;
158
+ const pathParts = expr.path.split(".");
159
+ const forbiddenKeys = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
160
+ let value = baseValue;
161
+ for (const part of pathParts) {
162
+ if (forbiddenKeys.has(part)) return void 0;
163
+ if (value == null) return void 0;
164
+ value = value[part];
165
+ }
166
+ return value;
167
+ }
168
+ default: {
169
+ const _exhaustiveCheck = expr;
170
+ throw new Error(`Unknown expression type: ${JSON.stringify(_exhaustiveCheck)}`);
171
+ }
155
172
  }
156
173
  }
157
174
  function evaluateBinary(op, left, right, ctx) {
@@ -228,7 +245,7 @@ async function executeStep(step, ctx) {
228
245
  await executeSetStep(step.target, step.value, ctx);
229
246
  break;
230
247
  case "update":
231
- await executeUpdateStep(step.target, step.operation, step.value, ctx);
248
+ await executeUpdateStep(step, ctx);
232
249
  break;
233
250
  case "fetch":
234
251
  await executeFetchStep(step, ctx);
@@ -240,7 +257,8 @@ async function executeSetStep(target, value, ctx) {
240
257
  const newValue = evaluate(value, evalCtx);
241
258
  ctx.state.set(target, newValue);
242
259
  }
243
- async function executeUpdateStep(target, operation, value, ctx) {
260
+ async function executeUpdateStep(step, ctx) {
261
+ const { target, operation, value } = step;
244
262
  const evalCtx = { state: ctx.state, locals: ctx.locals };
245
263
  const currentValue = ctx.state.get(target);
246
264
  switch (operation) {
@@ -279,6 +297,50 @@ async function executeUpdateStep(target, operation, value, ctx) {
279
297
  }
280
298
  break;
281
299
  }
300
+ case "toggle": {
301
+ const current = typeof currentValue === "boolean" ? currentValue : false;
302
+ ctx.state.set(target, !current);
303
+ break;
304
+ }
305
+ case "merge": {
306
+ const evalResult = value ? evaluate(value, evalCtx) : {};
307
+ const mergeValue = typeof evalResult === "object" && evalResult !== null ? evalResult : {};
308
+ const current = typeof currentValue === "object" && currentValue !== null ? currentValue : {};
309
+ ctx.state.set(target, { ...current, ...mergeValue });
310
+ break;
311
+ }
312
+ case "replaceAt": {
313
+ const idx = step.index ? evaluate(step.index, evalCtx) : 0;
314
+ const newValue = value ? evaluate(value, evalCtx) : void 0;
315
+ const arr = Array.isArray(currentValue) ? [...currentValue] : [];
316
+ if (typeof idx === "number" && idx >= 0 && idx < arr.length) {
317
+ arr[idx] = newValue;
318
+ }
319
+ ctx.state.set(target, arr);
320
+ break;
321
+ }
322
+ case "insertAt": {
323
+ const idx = step.index ? evaluate(step.index, evalCtx) : 0;
324
+ const newValue = value ? evaluate(value, evalCtx) : void 0;
325
+ const arr = Array.isArray(currentValue) ? [...currentValue] : [];
326
+ if (typeof idx === "number" && idx >= 0) {
327
+ arr.splice(idx, 0, newValue);
328
+ }
329
+ ctx.state.set(target, arr);
330
+ break;
331
+ }
332
+ case "splice": {
333
+ const idx = step.index ? evaluate(step.index, evalCtx) : 0;
334
+ const delCount = step.deleteCount ? evaluate(step.deleteCount, evalCtx) : 0;
335
+ const items = value ? evaluate(value, evalCtx) : [];
336
+ const arr = Array.isArray(currentValue) ? [...currentValue] : [];
337
+ if (typeof idx === "number" && typeof delCount === "number") {
338
+ const insertItems = Array.isArray(items) ? items : [];
339
+ arr.splice(idx, delCount, ...insertItems);
340
+ }
341
+ ctx.state.set(target, arr);
342
+ break;
343
+ }
282
344
  }
283
345
  }
284
346
  async function executeFetchStep(step, ctx) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@constela/runtime",
3
- "version": "0.1.0",
3
+ "version": "0.3.0",
4
4
  "description": "Runtime DOM renderer for Constela UI framework",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -15,8 +15,8 @@
15
15
  "dist"
16
16
  ],
17
17
  "dependencies": {
18
- "@constela/core": "0.1.0",
19
- "@constela/compiler": "0.1.0"
18
+ "@constela/core": "0.3.0",
19
+ "@constela/compiler": "0.3.0"
20
20
  },
21
21
  "devDependencies": {
22
22
  "@types/node": "^20.10.0",
package/dist/index.d.ts DELETED
@@ -1,101 +0,0 @@
1
- import { CompiledExpression, CompiledAction, CompiledNode, CompiledProgram } from '@constela/compiler';
2
-
3
- interface Signal<T> {
4
- get(): T;
5
- set(value: T): void;
6
- subscribe?(fn: (value: T) => void): () => void;
7
- }
8
- declare function createSignal<T>(initial: T): Signal<T>;
9
-
10
- /**
11
- * Effect - Reactive side effect
12
- *
13
- * An effect runs a function and automatically tracks signal dependencies.
14
- * When any tracked signal changes, the effect re-runs.
15
- */
16
- type CleanupFn = () => void;
17
- type EffectFn = () => void | CleanupFn;
18
- declare function createEffect(fn: EffectFn): () => void;
19
-
20
- /**
21
- * StateStore - Centralized state management
22
- *
23
- * Creates signals for each state field defined in the program.
24
- * Provides get/set methods for accessing state.
25
- */
26
- interface StateStore {
27
- get(name: string): unknown;
28
- set(name: string, value: unknown): void;
29
- }
30
- interface StateDefinition {
31
- type: string;
32
- initial: unknown;
33
- }
34
- declare function createStateStore(definitions: Record<string, StateDefinition>): StateStore;
35
-
36
- /**
37
- * Expression Evaluator - Evaluates compiled expressions
38
- *
39
- * Supports:
40
- * - Literal values (lit)
41
- * - State reads (state)
42
- * - Variable reads (var)
43
- * - Binary operations (bin)
44
- * - Not operation (not)
45
- */
46
-
47
- interface EvaluationContext {
48
- state: StateStore;
49
- locals: Record<string, unknown>;
50
- }
51
- declare function evaluate(expr: CompiledExpression, ctx: EvaluationContext): unknown;
52
-
53
- /**
54
- * Action Executor - Executes compiled action steps
55
- *
56
- * Supports:
57
- * - set: Update state with value
58
- * - update: Increment/decrement numbers, push/pop/remove for arrays
59
- * - fetch: Make HTTP requests with onSuccess/onError handlers
60
- */
61
-
62
- interface ActionContext {
63
- state: StateStore;
64
- actions: Record<string, CompiledAction>;
65
- locals: Record<string, unknown>;
66
- eventPayload?: unknown;
67
- }
68
- declare function executeAction(action: CompiledAction, ctx: ActionContext): Promise<void>;
69
-
70
- /**
71
- * Renderer - DOM rendering for compiled view nodes
72
- *
73
- * Renders:
74
- * - element: Creates DOM elements with props and event handlers
75
- * - text: Creates text nodes
76
- * - if: Conditional rendering with reactive updates
77
- * - each: List rendering with reactive updates
78
- */
79
-
80
- interface RenderContext {
81
- state: StateStore;
82
- actions: Record<string, CompiledAction>;
83
- locals: Record<string, unknown>;
84
- cleanups?: (() => void)[];
85
- }
86
- declare function render(node: CompiledNode, ctx: RenderContext): Node;
87
-
88
- /**
89
- * App - Main entry point for creating Constela applications
90
- *
91
- * Creates a reactive application from a CompiledProgram and mounts it to the DOM.
92
- */
93
-
94
- interface AppInstance {
95
- destroy(): void;
96
- setState(name: string, value: unknown): void;
97
- getState(name: string): unknown;
98
- }
99
- declare function createApp(program: CompiledProgram, mount: HTMLElement): AppInstance;
100
-
101
- export { type ActionContext, type AppInstance, type EvaluationContext, type RenderContext, type Signal, type StateStore, createApp, createEffect, createSignal, createStateStore, evaluate, executeAction, render };