@hypen-space/core 0.4.3 → 0.4.5
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/README.md +1 -1
- package/dist/app.d.ts +79 -22
- package/dist/app.js +111 -18
- package/dist/app.js.map +4 -4
- package/dist/components/builtin.js +114 -21
- package/dist/components/builtin.js.map +5 -5
- package/dist/discovery.d.ts +1 -1
- package/dist/discovery.js +112 -19
- package/dist/discovery.js.map +5 -5
- package/dist/engine.browser.d.ts +6 -0
- package/dist/engine.browser.js +192 -5
- package/dist/engine.browser.js.map +5 -4
- package/dist/engine.d.ts +6 -0
- package/dist/engine.js +192 -5
- package/dist/engine.js.map +5 -4
- package/dist/index.browser.d.ts +1 -1
- package/dist/index.browser.js +111 -18
- package/dist/index.browser.js.map +5 -5
- package/dist/index.d.ts +2 -2
- package/dist/index.js +117 -21
- package/dist/index.js.map +6 -6
- package/dist/managed-router.d.ts +3 -4
- package/dist/remote/client.js +34 -2
- package/dist/remote/client.js.map +3 -3
- package/dist/remote/index.js +472 -26
- package/dist/remote/index.js.map +8 -7
- package/dist/remote/server.d.ts +46 -15
- package/dist/remote/server.js +472 -26
- package/dist/remote/server.js.map +8 -7
- package/dist/result.d.ts +18 -0
- package/package.json +1 -1
- package/src/app.ts +162 -41
- package/src/components/builtin.ts +3 -4
- package/src/discovery.ts +2 -2
- package/src/engine.browser.ts +27 -4
- package/src/engine.ts +27 -4
- package/src/index.browser.ts +0 -2
- package/src/index.ts +3 -2
- package/src/managed-router.ts +5 -6
- package/src/remote/server.ts +116 -21
- package/src/result.ts +48 -0
- package/wasm-browser/hypen_engine_bg.wasm +0 -0
- package/wasm-browser/package.json +1 -1
- package/wasm-node/hypen_engine_bg.wasm +0 -0
- package/wasm-node/package.json +1 -1
- package/dist/module-registry.d.ts +0 -42
- package/src/module-registry.ts +0 -76
package/src/app.ts
CHANGED
|
@@ -21,22 +21,12 @@ import { createLogger } from "./logger.js";
|
|
|
21
21
|
|
|
22
22
|
const log = createLogger("ModuleInstance");
|
|
23
23
|
|
|
24
|
-
export type
|
|
25
|
-
root: HypenRouter;
|
|
26
|
-
current?: HypenRouter;
|
|
27
|
-
parent?: HypenRouter;
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
export type ActionContext = {
|
|
24
|
+
export type ActionContext<P = unknown> = {
|
|
31
25
|
name: string;
|
|
32
|
-
payload?:
|
|
26
|
+
payload?: P;
|
|
33
27
|
sender?: string;
|
|
34
28
|
};
|
|
35
29
|
|
|
36
|
-
export type ActionNext = {
|
|
37
|
-
router: HypenRouter; // Direct access to router instance (from nearest parent)
|
|
38
|
-
};
|
|
39
|
-
|
|
40
30
|
export type GlobalContext = {
|
|
41
31
|
getModule: <T = unknown>(id: string) => ModuleReference<T>;
|
|
42
32
|
hasModule: (id: string) => boolean;
|
|
@@ -44,7 +34,7 @@ export type GlobalContext = {
|
|
|
44
34
|
getGlobalState: () => Record<string, unknown>;
|
|
45
35
|
emit: (event: string, payload?: unknown) => void;
|
|
46
36
|
on: (event: string, handler: (payload?: unknown) => void) => () => void;
|
|
47
|
-
|
|
37
|
+
router: HypenRouter | null;
|
|
48
38
|
};
|
|
49
39
|
|
|
50
40
|
export type LifecycleHandler<T> = (
|
|
@@ -55,17 +45,16 @@ export type LifecycleHandler<T> = (
|
|
|
55
45
|
/**
|
|
56
46
|
* Action handler context - all parameters available explicitly
|
|
57
47
|
*/
|
|
58
|
-
export interface ActionHandlerContext<T> {
|
|
59
|
-
action: ActionContext
|
|
48
|
+
export interface ActionHandlerContext<T, P = unknown> {
|
|
49
|
+
action: ActionContext<P>;
|
|
60
50
|
state: T;
|
|
61
|
-
next: ActionNext;
|
|
62
51
|
context: GlobalContext;
|
|
63
52
|
}
|
|
64
53
|
|
|
65
54
|
/**
|
|
66
55
|
* Action handler - receives all context in a single object
|
|
67
56
|
*/
|
|
68
|
-
export type ActionHandler<T> = (ctx: ActionHandlerContext<T>) => void | Promise<void>;
|
|
57
|
+
export type ActionHandler<T, P = unknown> = (ctx: ActionHandlerContext<T, P>) => void | Promise<void>;
|
|
69
58
|
|
|
70
59
|
/**
|
|
71
60
|
* Context passed to onDisconnect handler
|
|
@@ -152,7 +141,7 @@ export interface HypenModuleDefinition<T = unknown> {
|
|
|
152
141
|
template?: string;
|
|
153
142
|
handlers: {
|
|
154
143
|
onCreated?: LifecycleHandler<T>;
|
|
155
|
-
onAction: Map<string, ActionHandler<T>>;
|
|
144
|
+
onAction: Map<string, ActionHandler<T, any>>;
|
|
156
145
|
onDestroyed?: LifecycleHandler<T>;
|
|
157
146
|
/** Called when client disconnects (session persists for TTL) */
|
|
158
147
|
onDisconnect?: DisconnectHandler<T>;
|
|
@@ -177,20 +166,23 @@ export class HypenAppBuilder<T> {
|
|
|
177
166
|
private initialState: T;
|
|
178
167
|
private options: { persist?: boolean; version?: number; name?: string };
|
|
179
168
|
private createdHandler?: LifecycleHandler<T>;
|
|
180
|
-
private actionHandlers: Map<string, ActionHandler<T>> = new Map();
|
|
169
|
+
private actionHandlers: Map<string, ActionHandler<T, any>> = new Map();
|
|
181
170
|
private destroyedHandler?: LifecycleHandler<T>;
|
|
182
171
|
private disconnectHandler?: DisconnectHandler<T>;
|
|
183
172
|
private reconnectHandler?: ReconnectHandler<T>;
|
|
184
173
|
private expireHandler?: ExpireHandler;
|
|
185
174
|
private errorHandler?: ErrorHandler<T>;
|
|
186
175
|
private template?: string;
|
|
176
|
+
private _registry?: Map<string, HypenModuleDefinition>;
|
|
187
177
|
|
|
188
178
|
constructor(
|
|
189
179
|
initialState: T,
|
|
190
|
-
options?: { persist?: boolean; version?: number; name?: string }
|
|
180
|
+
options?: { persist?: boolean; version?: number; name?: string },
|
|
181
|
+
registry?: Map<string, HypenModuleDefinition>
|
|
191
182
|
) {
|
|
192
183
|
this.initialState = initialState;
|
|
193
184
|
this.options = options || {};
|
|
185
|
+
this._registry = registry;
|
|
194
186
|
}
|
|
195
187
|
|
|
196
188
|
/**
|
|
@@ -202,10 +194,24 @@ export class HypenAppBuilder<T> {
|
|
|
202
194
|
}
|
|
203
195
|
|
|
204
196
|
/**
|
|
205
|
-
* Register a handler for a specific action
|
|
197
|
+
* Register a handler for a specific action.
|
|
198
|
+
* Optionally provide a payload type parameter for typed action payloads.
|
|
199
|
+
*
|
|
200
|
+
* @example
|
|
201
|
+
* ```typescript
|
|
202
|
+
* // Typed payload:
|
|
203
|
+
* .onAction<{ amount: number }>("add", ({ action, state }) => {
|
|
204
|
+
* action.payload.amount; // fully typed
|
|
205
|
+
* })
|
|
206
|
+
*
|
|
207
|
+
* // Untyped (payload is unknown):
|
|
208
|
+
* .onAction("add", ({ action, state }) => {
|
|
209
|
+
* action.payload; // unknown
|
|
210
|
+
* })
|
|
211
|
+
* ```
|
|
206
212
|
*/
|
|
207
|
-
onAction(name: string, fn: ActionHandler<T>): this {
|
|
208
|
-
this.actionHandlers.set(name, fn);
|
|
213
|
+
onAction<P = unknown>(name: string, fn: ActionHandler<T, P>): this {
|
|
214
|
+
this.actionHandlers.set(name, fn as ActionHandler<T, any>);
|
|
209
215
|
return this;
|
|
210
216
|
}
|
|
211
217
|
|
|
@@ -319,7 +325,7 @@ export class HypenAppBuilder<T> {
|
|
|
319
325
|
? Object.keys(this.initialState)
|
|
320
326
|
: [];
|
|
321
327
|
|
|
322
|
-
|
|
328
|
+
const definition: HypenModuleDefinition<T> = {
|
|
323
329
|
name: this.options.name,
|
|
324
330
|
actions: Array.from(this.actionHandlers.keys()),
|
|
325
331
|
stateKeys,
|
|
@@ -337,13 +343,27 @@ export class HypenAppBuilder<T> {
|
|
|
337
343
|
onError: this.errorHandler,
|
|
338
344
|
},
|
|
339
345
|
};
|
|
346
|
+
|
|
347
|
+
// Auto-register in the app registry when the module has a name
|
|
348
|
+
if (this.options.name && this._registry) {
|
|
349
|
+
this._registry.set(this.options.name, definition as HypenModuleDefinition);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
return definition;
|
|
340
353
|
}
|
|
341
354
|
}
|
|
342
355
|
|
|
343
356
|
/**
|
|
344
|
-
* Hypen App API
|
|
357
|
+
* Hypen App API — singleton factory and component registry.
|
|
358
|
+
*
|
|
359
|
+
* Modules built with a `name` are automatically registered here.
|
|
360
|
+
* Consumers (ManagedRouter, RemoteServer, ComponentResolver) read from this
|
|
361
|
+
* registry instead of requiring a separate ModuleRegistry instance.
|
|
345
362
|
*/
|
|
346
363
|
export class HypenApp {
|
|
364
|
+
/** @internal */
|
|
365
|
+
readonly _registry = new Map<string, HypenModuleDefinition>();
|
|
366
|
+
|
|
347
367
|
/**
|
|
348
368
|
* Define the initial state for a module
|
|
349
369
|
*/
|
|
@@ -351,7 +371,83 @@ export class HypenApp {
|
|
|
351
371
|
initial: T,
|
|
352
372
|
options?: { persist?: boolean; version?: number; name?: string }
|
|
353
373
|
): HypenAppBuilder<T> {
|
|
354
|
-
return new HypenAppBuilder(initial, options);
|
|
374
|
+
return new HypenAppBuilder(initial, options, this._registry);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* Convenience: start a module builder with a name pre-set.
|
|
379
|
+
*
|
|
380
|
+
* @example
|
|
381
|
+
* ```typescript
|
|
382
|
+
* export default app
|
|
383
|
+
* .module("Settings")
|
|
384
|
+
* .defineState({ theme: "dark" })
|
|
385
|
+
* .ui(hypen`...`);
|
|
386
|
+
* ```
|
|
387
|
+
*/
|
|
388
|
+
module(name: string) {
|
|
389
|
+
const registry = this._registry;
|
|
390
|
+
return {
|
|
391
|
+
defineState: <T>(
|
|
392
|
+
initial: T,
|
|
393
|
+
options?: { persist?: boolean; version?: number }
|
|
394
|
+
): HypenAppBuilder<T> => {
|
|
395
|
+
return new HypenAppBuilder(initial, { ...options, name }, registry);
|
|
396
|
+
},
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// ---------------------------------------------------------------------------
|
|
401
|
+
// Registry API
|
|
402
|
+
// ---------------------------------------------------------------------------
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* Get a registered module definition by component name.
|
|
406
|
+
*/
|
|
407
|
+
get(name: string): HypenModuleDefinition | undefined {
|
|
408
|
+
return this._registry.get(name);
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* Check if a module definition is registered.
|
|
413
|
+
*/
|
|
414
|
+
has(name: string): boolean {
|
|
415
|
+
return this._registry.has(name);
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* Read-only view of all registered component definitions.
|
|
420
|
+
*/
|
|
421
|
+
get components(): ReadonlyMap<string, HypenModuleDefinition> {
|
|
422
|
+
return this._registry;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/**
|
|
426
|
+
* Get all registered component names.
|
|
427
|
+
*/
|
|
428
|
+
getNames(): string[] {
|
|
429
|
+
return Array.from(this._registry.keys());
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/**
|
|
433
|
+
* Number of registered definitions.
|
|
434
|
+
*/
|
|
435
|
+
get size(): number {
|
|
436
|
+
return this._registry.size;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* Unregister a module definition.
|
|
441
|
+
*/
|
|
442
|
+
unregister(name: string): void {
|
|
443
|
+
this._registry.delete(name);
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
/**
|
|
447
|
+
* Clear all registered definitions.
|
|
448
|
+
*/
|
|
449
|
+
clear(): void {
|
|
450
|
+
this._registry.clear();
|
|
355
451
|
}
|
|
356
452
|
}
|
|
357
453
|
|
|
@@ -368,19 +464,19 @@ export class HypenModuleInstance<T extends object = any> {
|
|
|
368
464
|
private definition: HypenModuleDefinition<T>;
|
|
369
465
|
private state: T;
|
|
370
466
|
private isDestroyed = false;
|
|
371
|
-
private
|
|
467
|
+
private router: HypenRouter | null;
|
|
372
468
|
private globalContext?: HypenGlobalContext;
|
|
373
469
|
private stateChangeCallbacks: Array<() => void> = [];
|
|
374
470
|
|
|
375
471
|
constructor(
|
|
376
472
|
engine: IEngine,
|
|
377
473
|
definition: HypenModuleDefinition<T>,
|
|
378
|
-
|
|
474
|
+
router?: HypenRouter | null,
|
|
379
475
|
globalContext?: HypenGlobalContext
|
|
380
476
|
) {
|
|
381
477
|
this.engine = engine;
|
|
382
478
|
this.definition = definition;
|
|
383
|
-
this.
|
|
479
|
+
this.router = router ?? null;
|
|
384
480
|
this.globalContext = globalContext;
|
|
385
481
|
|
|
386
482
|
// Named state prefix — all lowercase per convention
|
|
@@ -425,10 +521,6 @@ export class HypenModuleInstance<T extends object = any> {
|
|
|
425
521
|
sender: action.sender,
|
|
426
522
|
};
|
|
427
523
|
|
|
428
|
-
const next: ActionNext = {
|
|
429
|
-
router: this.routerContext?.root || (null as any),
|
|
430
|
-
};
|
|
431
|
-
|
|
432
524
|
const context: GlobalContext | undefined = this.globalContext
|
|
433
525
|
? this.createGlobalContextAPI()
|
|
434
526
|
: undefined;
|
|
@@ -437,7 +529,6 @@ export class HypenModuleInstance<T extends object = any> {
|
|
|
437
529
|
const result = await this.executeAction(actionName, handler, {
|
|
438
530
|
action: actionCtx,
|
|
439
531
|
state: this.state,
|
|
440
|
-
next,
|
|
441
532
|
context: context!,
|
|
442
533
|
});
|
|
443
534
|
|
|
@@ -452,6 +543,22 @@ export class HypenModuleInstance<T extends object = any> {
|
|
|
452
543
|
});
|
|
453
544
|
}
|
|
454
545
|
|
|
546
|
+
// Auto-register __hypen_bind for .bind() two-way binding support
|
|
547
|
+
this.engine.onAction("__hypen_bind", (action: Action) => {
|
|
548
|
+
const payload = action.payload as { path?: string; value?: unknown } | null;
|
|
549
|
+
if (!payload?.path) return;
|
|
550
|
+
|
|
551
|
+
const segments = payload.path.split(".");
|
|
552
|
+
let target: any = this.state;
|
|
553
|
+
for (let i = 0; i < segments.length - 1; i++) {
|
|
554
|
+
const seg = segments[i]!;
|
|
555
|
+
target = target?.[seg];
|
|
556
|
+
if (target == null) return;
|
|
557
|
+
}
|
|
558
|
+
const lastSeg = segments[segments.length - 1]!;
|
|
559
|
+
target[lastSeg] = payload.value;
|
|
560
|
+
});
|
|
561
|
+
|
|
455
562
|
// Call onCreated
|
|
456
563
|
this.callCreatedHandler();
|
|
457
564
|
}
|
|
@@ -473,12 +580,10 @@ export class HypenModuleInstance<T extends object = any> {
|
|
|
473
580
|
emit: (event: string, payload?: any) => ctx.emit(event, payload),
|
|
474
581
|
on: (event: string, handler: (payload?: any) => void) =>
|
|
475
582
|
ctx.on(event, handler),
|
|
583
|
+
router: this.router,
|
|
476
584
|
};
|
|
477
585
|
|
|
478
|
-
// Expose
|
|
479
|
-
if ((ctx as any).__router) {
|
|
480
|
-
api.__router = (ctx as any).__router;
|
|
481
|
-
}
|
|
586
|
+
// Expose hypen engine for built-in components (if available)
|
|
482
587
|
if ((ctx as any).__hypenEngine) {
|
|
483
588
|
(api as any).__hypenEngine = (ctx as any).__hypenEngine;
|
|
484
589
|
}
|
|
@@ -492,7 +597,7 @@ export class HypenModuleInstance<T extends object = any> {
|
|
|
492
597
|
*/
|
|
493
598
|
private async executeAction(
|
|
494
599
|
actionName: string,
|
|
495
|
-
handler: ActionHandler<T>,
|
|
600
|
+
handler: ActionHandler<T, any>,
|
|
496
601
|
ctx: ActionHandlerContext<T>
|
|
497
602
|
): Promise<Result<void, ActionError>> {
|
|
498
603
|
try {
|
|
@@ -575,7 +680,15 @@ export class HypenModuleInstance<T extends object = any> {
|
|
|
575
680
|
private async callCreatedHandler(): Promise<void> {
|
|
576
681
|
if (this.definition.handlers.onCreated) {
|
|
577
682
|
const context = this.globalContext ? this.createGlobalContextAPI() : undefined;
|
|
578
|
-
|
|
683
|
+
try {
|
|
684
|
+
await this.definition.handlers.onCreated(this.state, context);
|
|
685
|
+
} catch (e) {
|
|
686
|
+
const error = e instanceof HypenError ? e : new ActionError("onCreated", e);
|
|
687
|
+
const shouldRethrow = await this.handleError(error, { lifecycle: "created" });
|
|
688
|
+
if (shouldRethrow) {
|
|
689
|
+
throw error;
|
|
690
|
+
}
|
|
691
|
+
}
|
|
579
692
|
}
|
|
580
693
|
}
|
|
581
694
|
|
|
@@ -593,7 +706,15 @@ export class HypenModuleInstance<T extends object = any> {
|
|
|
593
706
|
if (this.isDestroyed) return;
|
|
594
707
|
|
|
595
708
|
if (this.definition.handlers.onDestroyed) {
|
|
596
|
-
|
|
709
|
+
try {
|
|
710
|
+
await this.definition.handlers.onDestroyed(this.state);
|
|
711
|
+
} catch (e) {
|
|
712
|
+
const error = e instanceof HypenError ? e : new ActionError("onDestroyed", e);
|
|
713
|
+
const shouldRethrow = await this.handleError(error, { lifecycle: "destroyed" });
|
|
714
|
+
if (shouldRethrow) {
|
|
715
|
+
throw error;
|
|
716
|
+
}
|
|
717
|
+
}
|
|
597
718
|
}
|
|
598
719
|
|
|
599
720
|
this.isDestroyed = true;
|
|
@@ -4,7 +4,6 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import { app } from "../app.js";
|
|
7
|
-
import type { HypenRouter } from "../router.js";
|
|
8
7
|
import { frameworkLoggers } from "../logger.js";
|
|
9
8
|
|
|
10
9
|
const log = frameworkLoggers.router;
|
|
@@ -36,7 +35,7 @@ export const Router = app
|
|
|
36
35
|
}
|
|
37
36
|
|
|
38
37
|
// Subscribe to router changes
|
|
39
|
-
const router =
|
|
38
|
+
const router = context.router;
|
|
40
39
|
if (!router) {
|
|
41
40
|
log.error("Router not found in context");
|
|
42
41
|
return;
|
|
@@ -149,7 +148,7 @@ export const Link = app
|
|
|
149
148
|
{ name: "__Link" }
|
|
150
149
|
)
|
|
151
150
|
.onAction("navigate", ({ state, context }) => {
|
|
152
|
-
const router =
|
|
151
|
+
const router = context?.router;
|
|
153
152
|
if (!router) {
|
|
154
153
|
log.error("Link requires router context");
|
|
155
154
|
return;
|
|
@@ -162,7 +161,7 @@ export const Link = app
|
|
|
162
161
|
if (!context) return;
|
|
163
162
|
|
|
164
163
|
// Check if current path matches this link's target
|
|
165
|
-
const router =
|
|
164
|
+
const router = context.router;
|
|
166
165
|
if (router) {
|
|
167
166
|
router.onNavigate((routeState) => {
|
|
168
167
|
state.isActive = routeState.currentPath === state.to;
|
package/src/discovery.ts
CHANGED
|
@@ -43,7 +43,7 @@ export interface DiscoveryOptions {
|
|
|
43
43
|
|
|
44
44
|
/**
|
|
45
45
|
* Recursively scan subdirectories
|
|
46
|
-
* Default:
|
|
46
|
+
* Default: true
|
|
47
47
|
*/
|
|
48
48
|
recursive?: boolean;
|
|
49
49
|
|
|
@@ -95,7 +95,7 @@ export async function discoverComponents(
|
|
|
95
95
|
): Promise<DiscoveredComponent[]> {
|
|
96
96
|
const {
|
|
97
97
|
patterns = ["single-file", "folder", "sibling", "index"],
|
|
98
|
-
recursive =
|
|
98
|
+
recursive = true,
|
|
99
99
|
debug = false,
|
|
100
100
|
} = options;
|
|
101
101
|
|
package/src/engine.browser.ts
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import { frameworkLoggers } from "./logger.js";
|
|
7
|
+
import { classifyEngineError } from "./result.js";
|
|
7
8
|
import type { Patch, Action, RenderCallback, ActionHandler, ResolvedComponent, ComponentResolver } from "./types.js";
|
|
8
9
|
|
|
9
10
|
// Re-export types so existing consumers of "./engine.browser.js" still work
|
|
@@ -125,10 +126,16 @@ export class Engine {
|
|
|
125
126
|
|
|
126
127
|
/**
|
|
127
128
|
* Parse and render Hypen DSL source code
|
|
129
|
+
* @throws {ParseError} if the source fails to parse
|
|
130
|
+
* @throws {RenderError} if rendering fails
|
|
128
131
|
*/
|
|
129
132
|
renderSource(source: string): void {
|
|
130
133
|
const engine = this.ensureInitialized();
|
|
131
|
-
|
|
134
|
+
try {
|
|
135
|
+
engine.renderSource(source);
|
|
136
|
+
} catch (err) {
|
|
137
|
+
throw classifyEngineError(err);
|
|
138
|
+
}
|
|
132
139
|
}
|
|
133
140
|
|
|
134
141
|
/**
|
|
@@ -141,15 +148,22 @@ export class Engine {
|
|
|
141
148
|
|
|
142
149
|
/**
|
|
143
150
|
* Render a component into a specific parent node (subtree rendering)
|
|
151
|
+
* @throws {ParseError} if the source fails to parse
|
|
152
|
+
* @throws {RenderError} if the parent node is not found or rendering fails
|
|
144
153
|
*/
|
|
145
154
|
renderInto(source: string, parentNodeId: string, state: Record<string, any>): void {
|
|
146
155
|
const engine = this.ensureInitialized();
|
|
147
156
|
const safeState = JSON.parse(JSON.stringify(state));
|
|
148
|
-
|
|
157
|
+
try {
|
|
158
|
+
engine.renderInto(source, parentNodeId, safeState);
|
|
159
|
+
} catch (err) {
|
|
160
|
+
throw classifyEngineError(err);
|
|
161
|
+
}
|
|
149
162
|
}
|
|
150
163
|
|
|
151
164
|
/**
|
|
152
165
|
* Notify the engine of state changes using sparse updates
|
|
166
|
+
* @throws {StateError} if the state patch is invalid
|
|
153
167
|
*/
|
|
154
168
|
notifyStateChange(paths: string[], values: Record<string, any>): void {
|
|
155
169
|
const engine = this.ensureInitialized();
|
|
@@ -159,7 +173,11 @@ export class Engine {
|
|
|
159
173
|
}
|
|
160
174
|
|
|
161
175
|
const plainValues = JSON.parse(JSON.stringify(values));
|
|
162
|
-
|
|
176
|
+
try {
|
|
177
|
+
engine.updateStateSparse(paths, plainValues);
|
|
178
|
+
} catch (err) {
|
|
179
|
+
throw classifyEngineError(err);
|
|
180
|
+
}
|
|
163
181
|
log.debug("State changed (sparse):", paths);
|
|
164
182
|
}
|
|
165
183
|
|
|
@@ -189,11 +207,16 @@ export class Engine {
|
|
|
189
207
|
|
|
190
208
|
/**
|
|
191
209
|
* Dispatch an action
|
|
210
|
+
* @throws {HypenError} if the action dispatch fails
|
|
192
211
|
*/
|
|
193
212
|
dispatchAction(name: string, payload?: any): void {
|
|
194
213
|
const engine = this.ensureInitialized();
|
|
195
214
|
log.debug(`Action dispatched: ${name}`);
|
|
196
|
-
|
|
215
|
+
try {
|
|
216
|
+
engine.dispatchAction(name, payload ?? null);
|
|
217
|
+
} catch (err) {
|
|
218
|
+
throw classifyEngineError(err);
|
|
219
|
+
}
|
|
197
220
|
}
|
|
198
221
|
|
|
199
222
|
/**
|
package/src/engine.ts
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
// WASM module types
|
|
7
7
|
import { WasmEngine } from "../wasm-node/hypen_engine.js";
|
|
8
8
|
import { frameworkLoggers } from "./logger.js";
|
|
9
|
+
import { classifyEngineError } from "./result.js";
|
|
9
10
|
import type { Patch, Action, RenderCallback, ActionHandler, ResolvedComponent, ComponentResolver } from "./types.js";
|
|
10
11
|
|
|
11
12
|
// Re-export types so existing consumers of "./engine.js" still work
|
|
@@ -89,10 +90,16 @@ export class Engine {
|
|
|
89
90
|
|
|
90
91
|
/**
|
|
91
92
|
* Parse and render Hypen DSL source code
|
|
93
|
+
* @throws {ParseError} if the source fails to parse
|
|
94
|
+
* @throws {RenderError} if rendering fails
|
|
92
95
|
*/
|
|
93
96
|
renderSource(source: string): void {
|
|
94
97
|
const engine = this.ensureInitialized();
|
|
95
|
-
|
|
98
|
+
try {
|
|
99
|
+
engine.renderSource(source);
|
|
100
|
+
} catch (err) {
|
|
101
|
+
throw classifyEngineError(err);
|
|
102
|
+
}
|
|
96
103
|
}
|
|
97
104
|
|
|
98
105
|
/**
|
|
@@ -105,14 +112,21 @@ export class Engine {
|
|
|
105
112
|
|
|
106
113
|
/**
|
|
107
114
|
* Render a component into a specific parent node (subtree rendering)
|
|
115
|
+
* @throws {ParseError} if the source fails to parse
|
|
116
|
+
* @throws {RenderError} if the parent node is not found or rendering fails
|
|
108
117
|
*/
|
|
109
118
|
renderInto(source: string, parentNodeId: string, state: Record<string, any>): void {
|
|
110
119
|
const engine = this.ensureInitialized();
|
|
111
|
-
|
|
120
|
+
try {
|
|
121
|
+
engine.renderInto(source, parentNodeId, unwrapForWasm(state));
|
|
122
|
+
} catch (err) {
|
|
123
|
+
throw classifyEngineError(err);
|
|
124
|
+
}
|
|
112
125
|
}
|
|
113
126
|
|
|
114
127
|
/**
|
|
115
128
|
* Notify the engine of state changes using sparse updates
|
|
129
|
+
* @throws {StateError} if the state patch is invalid
|
|
116
130
|
*/
|
|
117
131
|
notifyStateChange(paths: string[], values: Record<string, any>): void {
|
|
118
132
|
const engine = this.ensureInitialized();
|
|
@@ -121,7 +135,11 @@ export class Engine {
|
|
|
121
135
|
return;
|
|
122
136
|
}
|
|
123
137
|
|
|
124
|
-
|
|
138
|
+
try {
|
|
139
|
+
engine.updateStateSparse(paths, unwrapForWasm(values));
|
|
140
|
+
} catch (err) {
|
|
141
|
+
throw classifyEngineError(err);
|
|
142
|
+
}
|
|
125
143
|
log.debug("State changed (sparse):", paths);
|
|
126
144
|
}
|
|
127
145
|
|
|
@@ -149,10 +167,15 @@ export class Engine {
|
|
|
149
167
|
|
|
150
168
|
/**
|
|
151
169
|
* Dispatch an action
|
|
170
|
+
* @throws {HypenError} if the action dispatch fails
|
|
152
171
|
*/
|
|
153
172
|
dispatchAction(name: string, payload?: any): void {
|
|
154
173
|
const engine = this.ensureInitialized();
|
|
155
|
-
|
|
174
|
+
try {
|
|
175
|
+
engine.dispatchAction(name, payload ?? null);
|
|
176
|
+
} catch (err) {
|
|
177
|
+
throw classifyEngineError(err);
|
|
178
|
+
}
|
|
156
179
|
}
|
|
157
180
|
|
|
158
181
|
/**
|
package/src/index.browser.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -74,9 +74,7 @@ export type { StateProxy, ItemProxy } from "./hypen.js";
|
|
|
74
74
|
|
|
75
75
|
export type {
|
|
76
76
|
IEngine,
|
|
77
|
-
RouterContext,
|
|
78
77
|
ActionContext,
|
|
79
|
-
ActionNext,
|
|
80
78
|
GlobalContext,
|
|
81
79
|
LifecycleHandler,
|
|
82
80
|
ActionHandlerContext,
|
|
@@ -245,6 +243,9 @@ export {
|
|
|
245
243
|
ActionError,
|
|
246
244
|
ConnectionError,
|
|
247
245
|
StateError,
|
|
246
|
+
ParseError,
|
|
247
|
+
RenderError,
|
|
248
|
+
classifyEngineError,
|
|
248
249
|
} from "./result.js";
|
|
249
250
|
export type { Result } from "./result.js";
|
|
250
251
|
|
package/src/managed-router.ts
CHANGED
|
@@ -9,10 +9,9 @@
|
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
import type { IEngine, HypenModuleDefinition } from "./app.js";
|
|
12
|
-
import { HypenModuleInstance } from "./app.js";
|
|
12
|
+
import { HypenModuleInstance, HypenApp } from "./app.js";
|
|
13
13
|
import type { HypenRouter, RouteState } from "./router.js";
|
|
14
14
|
import type { HypenGlobalContext } from "./context.js";
|
|
15
|
-
import { ModuleRegistry } from "./module-registry.js";
|
|
16
15
|
import { createLogger } from "./logger.js";
|
|
17
16
|
|
|
18
17
|
const log = createLogger("ManagedRouter");
|
|
@@ -20,7 +19,7 @@ const log = createLogger("ManagedRouter");
|
|
|
20
19
|
export interface RouteDefinition {
|
|
21
20
|
/** Route path pattern (e.g., "/", "/profile/:id") */
|
|
22
21
|
path: string;
|
|
23
|
-
/** Component name — used to look up in
|
|
22
|
+
/** Component name — used to look up in app registry */
|
|
24
23
|
component: string;
|
|
25
24
|
/** Inline module definition (alternative to registry lookup) */
|
|
26
25
|
module?: HypenModuleDefinition;
|
|
@@ -29,7 +28,7 @@ export interface RouteDefinition {
|
|
|
29
28
|
export class ManagedRouter {
|
|
30
29
|
private router: HypenRouter;
|
|
31
30
|
private engine: IEngine;
|
|
32
|
-
private registry:
|
|
31
|
+
private registry: HypenApp;
|
|
33
32
|
private globalContext: HypenGlobalContext;
|
|
34
33
|
private routes: RouteDefinition[] = [];
|
|
35
34
|
private activeModule: HypenModuleInstance | null = null;
|
|
@@ -41,7 +40,7 @@ export class ManagedRouter {
|
|
|
41
40
|
constructor(
|
|
42
41
|
router: HypenRouter,
|
|
43
42
|
engine: IEngine,
|
|
44
|
-
registry:
|
|
43
|
+
registry: HypenApp,
|
|
45
44
|
globalContext: HypenGlobalContext
|
|
46
45
|
) {
|
|
47
46
|
this.router = router;
|
|
@@ -161,7 +160,7 @@ export class ManagedRouter {
|
|
|
161
160
|
const instance = new HypenModuleInstance(
|
|
162
161
|
this.engine,
|
|
163
162
|
namedDef as HypenModuleDefinition<object>,
|
|
164
|
-
|
|
163
|
+
this.router,
|
|
165
164
|
this.globalContext
|
|
166
165
|
);
|
|
167
166
|
|