@constela/runtime 0.1.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/LICENSE +21 -0
- package/dist/index.d.ts +101 -0
- package/dist/index.js +583 -0
- package/package.json +40 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Constela Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
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 };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,583 @@
|
|
|
1
|
+
// src/reactive/signal.ts
|
|
2
|
+
var currentEffect = null;
|
|
3
|
+
var trackingEnabled = true;
|
|
4
|
+
var effectDependencies = /* @__PURE__ */ new Map();
|
|
5
|
+
function setCurrentEffect(effect) {
|
|
6
|
+
currentEffect = effect;
|
|
7
|
+
}
|
|
8
|
+
function registerEffectCleanup(effect) {
|
|
9
|
+
if (!effectDependencies.has(effect)) {
|
|
10
|
+
effectDependencies.set(effect, /* @__PURE__ */ new Set());
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
function cleanupEffect(effect) {
|
|
14
|
+
const deps = effectDependencies.get(effect);
|
|
15
|
+
if (deps) {
|
|
16
|
+
for (const signalSubscribers of deps) {
|
|
17
|
+
signalSubscribers.delete(effect);
|
|
18
|
+
}
|
|
19
|
+
deps.clear();
|
|
20
|
+
effectDependencies.delete(effect);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
function createSignal(initial) {
|
|
24
|
+
let value = initial;
|
|
25
|
+
const subscribers = /* @__PURE__ */ new Set();
|
|
26
|
+
const effectSubscribers = /* @__PURE__ */ new Set();
|
|
27
|
+
const signal = {
|
|
28
|
+
get() {
|
|
29
|
+
if (trackingEnabled && currentEffect) {
|
|
30
|
+
effectSubscribers.add(currentEffect);
|
|
31
|
+
const deps = effectDependencies.get(currentEffect);
|
|
32
|
+
if (deps) {
|
|
33
|
+
deps.add(effectSubscribers);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (Array.isArray(value)) {
|
|
37
|
+
return [...value];
|
|
38
|
+
}
|
|
39
|
+
return value;
|
|
40
|
+
},
|
|
41
|
+
set(newValue) {
|
|
42
|
+
value = newValue;
|
|
43
|
+
subscribers.forEach((fn) => fn(newValue));
|
|
44
|
+
const effects = [...effectSubscribers];
|
|
45
|
+
effects.forEach((effect) => effect());
|
|
46
|
+
},
|
|
47
|
+
subscribe(fn) {
|
|
48
|
+
subscribers.add(fn);
|
|
49
|
+
return () => {
|
|
50
|
+
subscribers.delete(fn);
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
return signal;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// src/reactive/effect.ts
|
|
58
|
+
var effectStack = [];
|
|
59
|
+
function createEffect(fn) {
|
|
60
|
+
let cleanup;
|
|
61
|
+
let isDisposed = false;
|
|
62
|
+
const execute = () => {
|
|
63
|
+
if (isDisposed) return;
|
|
64
|
+
if (typeof cleanup === "function") {
|
|
65
|
+
cleanup();
|
|
66
|
+
cleanup = void 0;
|
|
67
|
+
}
|
|
68
|
+
cleanupEffect(execute);
|
|
69
|
+
effectStack.push(execute);
|
|
70
|
+
setCurrentEffect(execute);
|
|
71
|
+
registerEffectCleanup(execute);
|
|
72
|
+
try {
|
|
73
|
+
const result = fn();
|
|
74
|
+
if (typeof result === "function") {
|
|
75
|
+
cleanup = result;
|
|
76
|
+
}
|
|
77
|
+
} finally {
|
|
78
|
+
effectStack.pop();
|
|
79
|
+
const parentEffect = effectStack[effectStack.length - 1];
|
|
80
|
+
setCurrentEffect(parentEffect ?? null);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
execute();
|
|
84
|
+
return () => {
|
|
85
|
+
isDisposed = true;
|
|
86
|
+
cleanupEffect(execute);
|
|
87
|
+
if (typeof cleanup === "function") {
|
|
88
|
+
cleanup();
|
|
89
|
+
cleanup = void 0;
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// src/state/store.ts
|
|
95
|
+
function createStateStore(definitions) {
|
|
96
|
+
const signals = /* @__PURE__ */ new Map();
|
|
97
|
+
for (const [name, def] of Object.entries(definitions)) {
|
|
98
|
+
signals.set(name, createSignal(def.initial));
|
|
99
|
+
}
|
|
100
|
+
return {
|
|
101
|
+
get(name) {
|
|
102
|
+
const signal = signals.get(name);
|
|
103
|
+
if (!signal) {
|
|
104
|
+
throw new Error(`State field "${name}" does not exist`);
|
|
105
|
+
}
|
|
106
|
+
return signal.get();
|
|
107
|
+
},
|
|
108
|
+
set(name, value) {
|
|
109
|
+
const signal = signals.get(name);
|
|
110
|
+
if (!signal) {
|
|
111
|
+
throw new Error(`State field "${name}" does not exist`);
|
|
112
|
+
}
|
|
113
|
+
signal.set(value);
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// src/expression/evaluator.ts
|
|
119
|
+
function evaluate(expr, ctx) {
|
|
120
|
+
switch (expr.expr) {
|
|
121
|
+
case "lit":
|
|
122
|
+
return expr.value;
|
|
123
|
+
case "state":
|
|
124
|
+
return ctx.state.get(expr.name);
|
|
125
|
+
case "var": {
|
|
126
|
+
let varName = expr.name;
|
|
127
|
+
let pathParts = [];
|
|
128
|
+
if (varName.includes(".")) {
|
|
129
|
+
const parts = varName.split(".");
|
|
130
|
+
varName = parts[0];
|
|
131
|
+
pathParts = parts.slice(1);
|
|
132
|
+
}
|
|
133
|
+
if (expr.path) {
|
|
134
|
+
pathParts = pathParts.concat(expr.path.split("."));
|
|
135
|
+
}
|
|
136
|
+
const forbiddenKeys = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
|
|
137
|
+
for (const part of pathParts) {
|
|
138
|
+
if (forbiddenKeys.has(part)) {
|
|
139
|
+
return void 0;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
let value = ctx.locals[varName];
|
|
143
|
+
for (const part of pathParts) {
|
|
144
|
+
if (value == null) break;
|
|
145
|
+
value = value[part];
|
|
146
|
+
}
|
|
147
|
+
return value;
|
|
148
|
+
}
|
|
149
|
+
case "bin":
|
|
150
|
+
return evaluateBinary(expr.op, expr.left, expr.right, ctx);
|
|
151
|
+
case "not":
|
|
152
|
+
return !evaluate(expr.operand, ctx);
|
|
153
|
+
default:
|
|
154
|
+
throw new Error("Unknown expression type");
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
function evaluateBinary(op, left, right, ctx) {
|
|
158
|
+
if (op === "&&") {
|
|
159
|
+
const leftVal2 = evaluate(left, ctx);
|
|
160
|
+
if (!leftVal2) return leftVal2;
|
|
161
|
+
return evaluate(right, ctx);
|
|
162
|
+
}
|
|
163
|
+
if (op === "||") {
|
|
164
|
+
const leftVal2 = evaluate(left, ctx);
|
|
165
|
+
if (leftVal2) return leftVal2;
|
|
166
|
+
return evaluate(right, ctx);
|
|
167
|
+
}
|
|
168
|
+
const leftVal = evaluate(left, ctx);
|
|
169
|
+
const rightVal = evaluate(right, ctx);
|
|
170
|
+
switch (op) {
|
|
171
|
+
// Arithmetic
|
|
172
|
+
case "+":
|
|
173
|
+
if (typeof leftVal === "number" && typeof rightVal === "number") {
|
|
174
|
+
return leftVal + rightVal;
|
|
175
|
+
}
|
|
176
|
+
return String(leftVal) + String(rightVal);
|
|
177
|
+
case "-":
|
|
178
|
+
return (typeof leftVal === "number" ? leftVal : 0) - (typeof rightVal === "number" ? rightVal : 0);
|
|
179
|
+
case "*":
|
|
180
|
+
return (typeof leftVal === "number" ? leftVal : 0) * (typeof rightVal === "number" ? rightVal : 0);
|
|
181
|
+
case "/": {
|
|
182
|
+
const dividend = typeof leftVal === "number" ? leftVal : 0;
|
|
183
|
+
const divisor = typeof rightVal === "number" ? rightVal : 0;
|
|
184
|
+
if (divisor === 0) {
|
|
185
|
+
return dividend === 0 ? NaN : dividend > 0 ? Infinity : -Infinity;
|
|
186
|
+
}
|
|
187
|
+
return dividend / divisor;
|
|
188
|
+
}
|
|
189
|
+
// Comparison (using strict equality)
|
|
190
|
+
case "==":
|
|
191
|
+
return leftVal === rightVal;
|
|
192
|
+
case "!=":
|
|
193
|
+
return leftVal !== rightVal;
|
|
194
|
+
case "<":
|
|
195
|
+
if (typeof leftVal === "number" && typeof rightVal === "number") {
|
|
196
|
+
return leftVal < rightVal;
|
|
197
|
+
}
|
|
198
|
+
return String(leftVal) < String(rightVal);
|
|
199
|
+
case "<=":
|
|
200
|
+
if (typeof leftVal === "number" && typeof rightVal === "number") {
|
|
201
|
+
return leftVal <= rightVal;
|
|
202
|
+
}
|
|
203
|
+
return String(leftVal) <= String(rightVal);
|
|
204
|
+
case ">":
|
|
205
|
+
if (typeof leftVal === "number" && typeof rightVal === "number") {
|
|
206
|
+
return leftVal > rightVal;
|
|
207
|
+
}
|
|
208
|
+
return String(leftVal) > String(rightVal);
|
|
209
|
+
case ">=":
|
|
210
|
+
if (typeof leftVal === "number" && typeof rightVal === "number") {
|
|
211
|
+
return leftVal >= rightVal;
|
|
212
|
+
}
|
|
213
|
+
return String(leftVal) >= String(rightVal);
|
|
214
|
+
default:
|
|
215
|
+
throw new Error("Unknown binary operator: " + op);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// src/action/executor.ts
|
|
220
|
+
async function executeAction(action, ctx) {
|
|
221
|
+
for (const step of action.steps) {
|
|
222
|
+
await executeStep(step, ctx);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
async function executeStep(step, ctx) {
|
|
226
|
+
switch (step.do) {
|
|
227
|
+
case "set":
|
|
228
|
+
await executeSetStep(step.target, step.value, ctx);
|
|
229
|
+
break;
|
|
230
|
+
case "update":
|
|
231
|
+
await executeUpdateStep(step.target, step.operation, step.value, ctx);
|
|
232
|
+
break;
|
|
233
|
+
case "fetch":
|
|
234
|
+
await executeFetchStep(step, ctx);
|
|
235
|
+
break;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
async function executeSetStep(target, value, ctx) {
|
|
239
|
+
const evalCtx = { state: ctx.state, locals: ctx.locals };
|
|
240
|
+
const newValue = evaluate(value, evalCtx);
|
|
241
|
+
ctx.state.set(target, newValue);
|
|
242
|
+
}
|
|
243
|
+
async function executeUpdateStep(target, operation, value, ctx) {
|
|
244
|
+
const evalCtx = { state: ctx.state, locals: ctx.locals };
|
|
245
|
+
const currentValue = ctx.state.get(target);
|
|
246
|
+
switch (operation) {
|
|
247
|
+
case "increment": {
|
|
248
|
+
const evalResult = value ? evaluate(value, evalCtx) : 1;
|
|
249
|
+
const amount = typeof evalResult === "number" ? evalResult : 1;
|
|
250
|
+
const current = typeof currentValue === "number" ? currentValue : 0;
|
|
251
|
+
ctx.state.set(target, current + amount);
|
|
252
|
+
break;
|
|
253
|
+
}
|
|
254
|
+
case "decrement": {
|
|
255
|
+
const evalResult = value ? evaluate(value, evalCtx) : 1;
|
|
256
|
+
const amount = typeof evalResult === "number" ? evalResult : 1;
|
|
257
|
+
const current = typeof currentValue === "number" ? currentValue : 0;
|
|
258
|
+
ctx.state.set(target, current - amount);
|
|
259
|
+
break;
|
|
260
|
+
}
|
|
261
|
+
case "push": {
|
|
262
|
+
const item = value ? evaluate(value, evalCtx) : void 0;
|
|
263
|
+
const arr = Array.isArray(currentValue) ? currentValue : [];
|
|
264
|
+
ctx.state.set(target, [...arr, item]);
|
|
265
|
+
break;
|
|
266
|
+
}
|
|
267
|
+
case "pop": {
|
|
268
|
+
const arr = Array.isArray(currentValue) ? currentValue : [];
|
|
269
|
+
ctx.state.set(target, arr.slice(0, -1));
|
|
270
|
+
break;
|
|
271
|
+
}
|
|
272
|
+
case "remove": {
|
|
273
|
+
const removeValue = value ? evaluate(value, evalCtx) : void 0;
|
|
274
|
+
const arr = Array.isArray(currentValue) ? currentValue : [];
|
|
275
|
+
if (typeof removeValue === "number") {
|
|
276
|
+
ctx.state.set(target, arr.filter((_, i) => i !== removeValue));
|
|
277
|
+
} else {
|
|
278
|
+
ctx.state.set(target, arr.filter((x) => x !== removeValue));
|
|
279
|
+
}
|
|
280
|
+
break;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
async function executeFetchStep(step, ctx) {
|
|
285
|
+
const evalCtx = { state: ctx.state, locals: ctx.locals };
|
|
286
|
+
const url = evaluate(step.url, evalCtx);
|
|
287
|
+
const method = step.method ?? "GET";
|
|
288
|
+
const fetchOptions = {
|
|
289
|
+
method
|
|
290
|
+
};
|
|
291
|
+
if (step.body) {
|
|
292
|
+
fetchOptions.body = evaluate(step.body, evalCtx);
|
|
293
|
+
}
|
|
294
|
+
try {
|
|
295
|
+
const response = await fetch(url, fetchOptions);
|
|
296
|
+
if (response.ok) {
|
|
297
|
+
const data = await response.json();
|
|
298
|
+
if (step.result) {
|
|
299
|
+
ctx.locals[step.result] = data;
|
|
300
|
+
}
|
|
301
|
+
if (step.onSuccess) {
|
|
302
|
+
for (const successStep of step.onSuccess) {
|
|
303
|
+
await executeStep(successStep, ctx);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
} else {
|
|
307
|
+
if (step.onError) {
|
|
308
|
+
for (const errorStep of step.onError) {
|
|
309
|
+
await executeStep(errorStep, ctx);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
} catch (_error) {
|
|
314
|
+
if (step.onError) {
|
|
315
|
+
for (const errorStep of step.onError) {
|
|
316
|
+
await executeStep(errorStep, ctx);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
// src/renderer/index.ts
|
|
323
|
+
function isEventHandler(value) {
|
|
324
|
+
return typeof value === "object" && value !== null && "event" in value && "action" in value;
|
|
325
|
+
}
|
|
326
|
+
function render(node, ctx) {
|
|
327
|
+
switch (node.kind) {
|
|
328
|
+
case "element":
|
|
329
|
+
return renderElement(node, ctx);
|
|
330
|
+
case "text":
|
|
331
|
+
return renderText(node, ctx);
|
|
332
|
+
case "if":
|
|
333
|
+
return renderIf(node, ctx);
|
|
334
|
+
case "each":
|
|
335
|
+
return renderEach(node, ctx);
|
|
336
|
+
default:
|
|
337
|
+
throw new Error("Unknown node kind");
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
function renderElement(node, ctx) {
|
|
341
|
+
const el = document.createElement(node.tag);
|
|
342
|
+
if (node.props) {
|
|
343
|
+
for (const [propName, propValue] of Object.entries(node.props)) {
|
|
344
|
+
if (isEventHandler(propValue)) {
|
|
345
|
+
const handler = propValue;
|
|
346
|
+
const eventName = handler.event;
|
|
347
|
+
el.addEventListener(eventName, async (event) => {
|
|
348
|
+
const action = ctx.actions[handler.action];
|
|
349
|
+
if (action) {
|
|
350
|
+
let payload = void 0;
|
|
351
|
+
if (handler.payload) {
|
|
352
|
+
payload = evaluate(handler.payload, { state: ctx.state, locals: ctx.locals });
|
|
353
|
+
}
|
|
354
|
+
const actionCtx = {
|
|
355
|
+
state: ctx.state,
|
|
356
|
+
actions: ctx.actions,
|
|
357
|
+
locals: { ...ctx.locals, payload },
|
|
358
|
+
eventPayload: payload
|
|
359
|
+
};
|
|
360
|
+
await executeAction(action, actionCtx);
|
|
361
|
+
}
|
|
362
|
+
});
|
|
363
|
+
} else {
|
|
364
|
+
const cleanup = createEffect(() => {
|
|
365
|
+
const value = evaluate(propValue, { state: ctx.state, locals: ctx.locals });
|
|
366
|
+
applyProp(el, propName, value);
|
|
367
|
+
});
|
|
368
|
+
ctx.cleanups?.push(cleanup);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
if (node.children) {
|
|
373
|
+
for (const child of node.children) {
|
|
374
|
+
const childNode = render(child, ctx);
|
|
375
|
+
el.appendChild(childNode);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
return el;
|
|
379
|
+
}
|
|
380
|
+
function applyProp(el, propName, value) {
|
|
381
|
+
if (propName === "className") {
|
|
382
|
+
el.className = String(value ?? "");
|
|
383
|
+
} else if (propName === "style" && typeof value === "string") {
|
|
384
|
+
el.setAttribute("style", value);
|
|
385
|
+
} else if (propName === "disabled") {
|
|
386
|
+
if (value) {
|
|
387
|
+
el.setAttribute("disabled", "disabled");
|
|
388
|
+
el.disabled = true;
|
|
389
|
+
} else {
|
|
390
|
+
el.removeAttribute("disabled");
|
|
391
|
+
el.disabled = false;
|
|
392
|
+
}
|
|
393
|
+
} else if (propName === "value" && el instanceof HTMLInputElement) {
|
|
394
|
+
el.value = String(value ?? "");
|
|
395
|
+
} else if (propName.startsWith("data-")) {
|
|
396
|
+
el.setAttribute(propName, String(value ?? ""));
|
|
397
|
+
} else {
|
|
398
|
+
if (value === true) {
|
|
399
|
+
el.setAttribute(propName, "");
|
|
400
|
+
} else if (value === false || value === null || value === void 0) {
|
|
401
|
+
el.removeAttribute(propName);
|
|
402
|
+
} else {
|
|
403
|
+
el.setAttribute(propName, String(value));
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
function renderText(node, ctx) {
|
|
408
|
+
const textNode = document.createTextNode("");
|
|
409
|
+
const cleanup = createEffect(() => {
|
|
410
|
+
const value = evaluate(node.value, { state: ctx.state, locals: ctx.locals });
|
|
411
|
+
textNode.textContent = formatValue(value);
|
|
412
|
+
});
|
|
413
|
+
ctx.cleanups?.push(cleanup);
|
|
414
|
+
return textNode;
|
|
415
|
+
}
|
|
416
|
+
function formatValue(value) {
|
|
417
|
+
if (value === null || value === void 0) {
|
|
418
|
+
return "";
|
|
419
|
+
}
|
|
420
|
+
if (typeof value === "object") {
|
|
421
|
+
return JSON.stringify(value);
|
|
422
|
+
}
|
|
423
|
+
return String(value);
|
|
424
|
+
}
|
|
425
|
+
function renderIf(node, ctx) {
|
|
426
|
+
const anchor = document.createComment("if");
|
|
427
|
+
let currentNode = null;
|
|
428
|
+
let currentBranch = "none";
|
|
429
|
+
let branchCleanups = [];
|
|
430
|
+
const effectCleanup = createEffect(() => {
|
|
431
|
+
const condition = evaluate(node.condition, { state: ctx.state, locals: ctx.locals });
|
|
432
|
+
const shouldShowThen = Boolean(condition);
|
|
433
|
+
const newBranch = shouldShowThen ? "then" : node.else ? "else" : "none";
|
|
434
|
+
if (newBranch !== currentBranch) {
|
|
435
|
+
for (const cleanup of branchCleanups) {
|
|
436
|
+
cleanup();
|
|
437
|
+
}
|
|
438
|
+
branchCleanups = [];
|
|
439
|
+
if (currentNode && currentNode.parentNode) {
|
|
440
|
+
currentNode.parentNode.removeChild(currentNode);
|
|
441
|
+
}
|
|
442
|
+
const localCleanups = [];
|
|
443
|
+
const branchCtx = { ...ctx, cleanups: localCleanups };
|
|
444
|
+
if (newBranch === "then") {
|
|
445
|
+
currentNode = render(node.then, branchCtx);
|
|
446
|
+
branchCleanups = localCleanups;
|
|
447
|
+
} else if (newBranch === "else" && node.else) {
|
|
448
|
+
currentNode = render(node.else, branchCtx);
|
|
449
|
+
branchCleanups = localCleanups;
|
|
450
|
+
} else {
|
|
451
|
+
currentNode = null;
|
|
452
|
+
}
|
|
453
|
+
if (currentNode && anchor.parentNode) {
|
|
454
|
+
anchor.parentNode.insertBefore(currentNode, anchor.nextSibling);
|
|
455
|
+
}
|
|
456
|
+
currentBranch = newBranch;
|
|
457
|
+
}
|
|
458
|
+
});
|
|
459
|
+
ctx.cleanups?.push(effectCleanup);
|
|
460
|
+
ctx.cleanups?.push(() => {
|
|
461
|
+
for (const cleanup of branchCleanups) {
|
|
462
|
+
cleanup();
|
|
463
|
+
}
|
|
464
|
+
});
|
|
465
|
+
const fragment = document.createDocumentFragment();
|
|
466
|
+
fragment.appendChild(anchor);
|
|
467
|
+
if (currentNode) {
|
|
468
|
+
fragment.appendChild(currentNode);
|
|
469
|
+
}
|
|
470
|
+
return fragment;
|
|
471
|
+
}
|
|
472
|
+
function renderEach(node, ctx) {
|
|
473
|
+
const anchor = document.createComment("each");
|
|
474
|
+
let currentNodes = [];
|
|
475
|
+
let itemCleanups = [];
|
|
476
|
+
const effectCleanup = createEffect(() => {
|
|
477
|
+
const items = evaluate(node.items, { state: ctx.state, locals: ctx.locals });
|
|
478
|
+
for (const cleanup of itemCleanups) {
|
|
479
|
+
cleanup();
|
|
480
|
+
}
|
|
481
|
+
itemCleanups = [];
|
|
482
|
+
for (const oldNode of currentNodes) {
|
|
483
|
+
if (oldNode.parentNode) {
|
|
484
|
+
oldNode.parentNode.removeChild(oldNode);
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
currentNodes = [];
|
|
488
|
+
if (Array.isArray(items)) {
|
|
489
|
+
items.forEach((item, index) => {
|
|
490
|
+
const itemLocals = {
|
|
491
|
+
...ctx.locals,
|
|
492
|
+
[node.as]: item
|
|
493
|
+
};
|
|
494
|
+
if (node.index) {
|
|
495
|
+
itemLocals[node.index] = index;
|
|
496
|
+
}
|
|
497
|
+
const localCleanups = [];
|
|
498
|
+
const itemCtx = {
|
|
499
|
+
...ctx,
|
|
500
|
+
locals: itemLocals,
|
|
501
|
+
cleanups: localCleanups
|
|
502
|
+
};
|
|
503
|
+
const itemNode = render(node.body, itemCtx);
|
|
504
|
+
currentNodes.push(itemNode);
|
|
505
|
+
itemCleanups.push(...localCleanups);
|
|
506
|
+
if (anchor.parentNode) {
|
|
507
|
+
let refNode = anchor.nextSibling;
|
|
508
|
+
if (currentNodes.length > 1) {
|
|
509
|
+
const lastExisting = currentNodes[currentNodes.length - 2];
|
|
510
|
+
if (lastExisting) {
|
|
511
|
+
refNode = lastExisting.nextSibling;
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
anchor.parentNode.insertBefore(itemNode, refNode);
|
|
515
|
+
}
|
|
516
|
+
});
|
|
517
|
+
}
|
|
518
|
+
});
|
|
519
|
+
ctx.cleanups?.push(effectCleanup);
|
|
520
|
+
ctx.cleanups?.push(() => {
|
|
521
|
+
for (const cleanup of itemCleanups) {
|
|
522
|
+
cleanup();
|
|
523
|
+
}
|
|
524
|
+
});
|
|
525
|
+
const fragment = document.createDocumentFragment();
|
|
526
|
+
fragment.appendChild(anchor);
|
|
527
|
+
for (const n of currentNodes) {
|
|
528
|
+
fragment.appendChild(n);
|
|
529
|
+
}
|
|
530
|
+
return fragment;
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
// src/app.ts
|
|
534
|
+
function createApp(program, mount) {
|
|
535
|
+
const state = createStateStore(program.state);
|
|
536
|
+
let actions;
|
|
537
|
+
if (program.actions instanceof Map) {
|
|
538
|
+
actions = {};
|
|
539
|
+
program.actions.forEach((action, name) => {
|
|
540
|
+
actions[name] = action;
|
|
541
|
+
});
|
|
542
|
+
} else {
|
|
543
|
+
actions = program.actions;
|
|
544
|
+
}
|
|
545
|
+
const cleanups = [];
|
|
546
|
+
const ctx = {
|
|
547
|
+
state,
|
|
548
|
+
actions,
|
|
549
|
+
locals: {},
|
|
550
|
+
cleanups
|
|
551
|
+
};
|
|
552
|
+
const rootNode = render(program.view, ctx);
|
|
553
|
+
mount.appendChild(rootNode);
|
|
554
|
+
let destroyed = false;
|
|
555
|
+
return {
|
|
556
|
+
destroy() {
|
|
557
|
+
if (destroyed) return;
|
|
558
|
+
destroyed = true;
|
|
559
|
+
for (const cleanup of cleanups) {
|
|
560
|
+
cleanup();
|
|
561
|
+
}
|
|
562
|
+
while (mount.firstChild) {
|
|
563
|
+
mount.removeChild(mount.firstChild);
|
|
564
|
+
}
|
|
565
|
+
},
|
|
566
|
+
setState(name, value) {
|
|
567
|
+
if (destroyed) return;
|
|
568
|
+
state.set(name, value);
|
|
569
|
+
},
|
|
570
|
+
getState(name) {
|
|
571
|
+
return state.get(name);
|
|
572
|
+
}
|
|
573
|
+
};
|
|
574
|
+
}
|
|
575
|
+
export {
|
|
576
|
+
createApp,
|
|
577
|
+
createEffect,
|
|
578
|
+
createSignal,
|
|
579
|
+
createStateStore,
|
|
580
|
+
evaluate,
|
|
581
|
+
executeAction,
|
|
582
|
+
render
|
|
583
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@constela/runtime",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Runtime DOM renderer for Constela UI framework",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@constela/core": "0.1.0",
|
|
19
|
+
"@constela/compiler": "0.1.0"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@types/node": "^20.10.0",
|
|
23
|
+
"jsdom": "^24.0.0",
|
|
24
|
+
"@types/jsdom": "^21.1.0",
|
|
25
|
+
"tsup": "^8.0.0",
|
|
26
|
+
"typescript": "^5.3.0",
|
|
27
|
+
"vitest": "^2.0.0"
|
|
28
|
+
},
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": ">=20.0.0"
|
|
31
|
+
},
|
|
32
|
+
"license": "MIT",
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "tsup src/index.ts --format esm --dts --clean",
|
|
35
|
+
"type-check": "tsc --noEmit",
|
|
36
|
+
"test": "vitest run",
|
|
37
|
+
"test:watch": "vitest",
|
|
38
|
+
"clean": "rm -rf dist"
|
|
39
|
+
}
|
|
40
|
+
}
|