@hypen-space/core 0.3.12 → 0.4.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/app.js +7 -3
- package/dist/app.js.map +3 -3
- package/dist/components/builtin.js +7 -3
- package/dist/components/builtin.js.map +3 -3
- package/dist/discovery.js +8 -7
- package/dist/discovery.js.map +4 -4
- package/dist/engine.browser.js.map +2 -2
- package/dist/engine.js +3 -7
- package/dist/engine.js.map +3 -3
- package/dist/index.browser.js +242 -371
- package/dist/index.browser.js.map +8 -9
- package/dist/index.js +546 -656
- package/dist/index.js.map +12 -13
- package/dist/remote/client.js.map +1 -1
- package/dist/remote/index.js +336 -336
- package/dist/remote/index.js.map +8 -8
- package/dist/remote/server.js +219 -219
- package/dist/remote/server.js.map +7 -7
- package/dist/renderer.js.map +1 -1
- package/dist/resolver.js +31 -4
- package/dist/resolver.js.map +3 -3
- package/package.json +7 -1
- package/src/app.ts +14 -2
- package/src/discovery.ts +3 -1
- package/src/engine.browser.ts +4 -32
- package/src/engine.ts +8 -44
- package/src/index.browser.ts +6 -4
- package/src/index.ts +6 -6
- package/src/managed-router.ts +195 -0
- package/src/module-registry.ts +76 -0
- package/src/remote/client.ts +1 -1
- package/src/remote/index.ts +2 -2
- package/src/remote/server.ts +1 -1
- package/src/remote/types.ts +1 -1
- package/src/renderer.ts +1 -1
- package/src/resolver.ts +56 -9
- package/src/types.ts +38 -0
- package/wasm-browser/README.md +7 -1
- package/wasm-browser/hypen_engine.d.ts +1 -0
- package/wasm-browser/hypen_engine.js +1 -0
- package/wasm-browser/hypen_engine_bg.wasm +0 -0
- package/wasm-browser/package.json +1 -1
- package/wasm-node/README.md +7 -1
- package/wasm-node/hypen_engine.d.ts +1 -0
- package/wasm-node/hypen_engine.js +1 -0
- package/wasm-node/hypen_engine_bg.wasm +0 -0
- package/wasm-node/package.json +1 -1
package/dist/resolver.js
CHANGED
|
@@ -31,14 +31,32 @@ var __require = /* @__PURE__ */ createRequire(import.meta.url);
|
|
|
31
31
|
class ComponentResolver {
|
|
32
32
|
cache = new Map;
|
|
33
33
|
options;
|
|
34
|
+
moduleRegistry;
|
|
34
35
|
constructor(options = {}) {
|
|
35
36
|
this.options = {
|
|
36
37
|
baseDir: options.baseDir || process.cwd(),
|
|
37
38
|
cache: options.cache ?? true,
|
|
38
|
-
customFetch: options.customFetch || this.defaultFetch.bind(this)
|
|
39
|
+
customFetch: options.customFetch || this.defaultFetch.bind(this),
|
|
40
|
+
moduleRegistry: options.moduleRegistry
|
|
39
41
|
};
|
|
42
|
+
this.moduleRegistry = options.moduleRegistry;
|
|
40
43
|
}
|
|
41
44
|
async resolve(importStmt) {
|
|
45
|
+
if (this.moduleRegistry) {
|
|
46
|
+
const names = importStmt.clause.type === "named" ? importStmt.clause.names : [importStmt.clause.name];
|
|
47
|
+
const allFound = names.every((name) => this.moduleRegistry.has(name));
|
|
48
|
+
if (allFound) {
|
|
49
|
+
const result = {};
|
|
50
|
+
for (const name of names) {
|
|
51
|
+
const def = this.moduleRegistry.get(name);
|
|
52
|
+
result[name] = {
|
|
53
|
+
module: def,
|
|
54
|
+
template: def?.template || ""
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
return result;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
42
60
|
const sourcePath = this.getSourcePath(importStmt.source);
|
|
43
61
|
if (this.options.cache && this.cache.has(sourcePath)) {
|
|
44
62
|
const cached = this.cache.get(sourcePath);
|
|
@@ -56,8 +74,17 @@ class ComponentResolver {
|
|
|
56
74
|
return this.extractComponents(importStmt.clause, component);
|
|
57
75
|
}
|
|
58
76
|
async resolveLocal(path) {
|
|
59
|
-
|
|
60
|
-
|
|
77
|
+
const { resolve, join } = await import("path");
|
|
78
|
+
const { readFile } = await import("fs/promises");
|
|
79
|
+
const basePath = resolve(this.options.baseDir, path);
|
|
80
|
+
const hypenPath = basePath.endsWith(".hypen") ? basePath : `${basePath}.hypen`;
|
|
81
|
+
const template = await readFile(hypenPath, "utf-8");
|
|
82
|
+
const modulePath = hypenPath.replace(/\.hypen$/, ".ts");
|
|
83
|
+
let module = {};
|
|
84
|
+
try {
|
|
85
|
+
module = await import(modulePath);
|
|
86
|
+
} catch {}
|
|
87
|
+
return { module, template };
|
|
61
88
|
}
|
|
62
89
|
async resolveUrl(url) {
|
|
63
90
|
try {
|
|
@@ -124,4 +151,4 @@ export {
|
|
|
124
151
|
ComponentResolver
|
|
125
152
|
};
|
|
126
153
|
|
|
127
|
-
//# debugId=
|
|
154
|
+
//# debugId=20EC11627252563C64756E2164756E21
|
package/dist/resolver.js.map
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/resolver.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
|
-
"/**\n * Component Resolver\n * Resolves component imports from local paths or web URLs\n */\n\nexport interface ImportStatement {\n clause: ImportClause;\n source: ImportSource;\n}\n\nexport type ImportClause =\n | { type: \"named\"; names: string[] }\n | { type: \"default\"; name: string };\n\nexport type ImportSource =\n | { type: \"local\"; path: string }\n | { type: \"url\"; url: string };\n\nexport interface ComponentDefinition {\n module: any;\n template: string;\n}\n\nexport interface ResolverOptions {\n /**\n * Base directory for resolving relative local paths\n * Defaults to current working directory\n */\n baseDir?: string;\n\n /**\n * Cache resolved components to avoid re-fetching\n * Defaults to true\n */\n cache?: boolean;\n\n /**\n * Custom fetch function for URL imports\n * Useful for adding authentication, custom headers, etc.\n */\n customFetch?: (url: string) => Promise<string>;\n}\n\n/**\n * Component Resolver\n * Resolves and loads components from local files or remote URLs\n */\nexport class ComponentResolver {\n private cache = new Map<string, ComponentDefinition>();\n private options: Required<ResolverOptions>;\n\n constructor(options: ResolverOptions = {}) {\n this.options = {\n baseDir: options.baseDir || process.cwd(),\n cache: options.cache ?? true,\n customFetch: options.customFetch || this.defaultFetch.bind(this),\n };\n }\n\n /**\n * Resolve a component from an import statement
|
|
5
|
+
"/**\n * Component Resolver\n * Resolves component imports from local paths or web URLs\n */\n\nexport interface ImportStatement {\n clause: ImportClause;\n source: ImportSource;\n}\n\nexport type ImportClause =\n | { type: \"named\"; names: string[] }\n | { type: \"default\"; name: string };\n\nexport type ImportSource =\n | { type: \"local\"; path: string }\n | { type: \"url\"; url: string };\n\nexport interface ComponentDefinition {\n module: any;\n template: string;\n}\n\nexport interface ResolverOptions {\n /**\n * Base directory for resolving relative local paths\n * Defaults to current working directory\n */\n baseDir?: string;\n\n /**\n * Cache resolved components to avoid re-fetching\n * Defaults to true\n */\n cache?: boolean;\n\n /**\n * Custom fetch function for URL imports\n * Useful for adding authentication, custom headers, etc.\n */\n customFetch?: (url: string) => Promise<string>;\n\n /**\n * Module registry for looking up pre-registered module definitions.\n * When set, the resolver will check the registry before doing file I/O.\n */\n moduleRegistry?: { has: (name: string) => boolean; get: (name: string) => any };\n}\n\n/**\n * Component Resolver\n * Resolves and loads components from local files or remote URLs\n */\nexport class ComponentResolver {\n private cache = new Map<string, ComponentDefinition>();\n private options: Required<Pick<ResolverOptions, 'baseDir' | 'cache' | 'customFetch'>> & Pick<ResolverOptions, 'moduleRegistry'>;\n\n private moduleRegistry?: { has: (name: string) => boolean; get: (name: string) => any };\n\n constructor(options: ResolverOptions = {}) {\n this.options = {\n baseDir: options.baseDir || process.cwd(),\n cache: options.cache ?? true,\n customFetch: options.customFetch || this.defaultFetch.bind(this),\n moduleRegistry: options.moduleRegistry,\n };\n this.moduleRegistry = options.moduleRegistry;\n }\n\n /**\n * Resolve a component from an import statement.\n * Checks the module registry first (if available), then falls back to file I/O.\n */\n async resolve(\n importStmt: ImportStatement\n ): Promise<Record<string, ComponentDefinition>> {\n // Check module registry first — if a component is pre-registered,\n // use its template directly (it will be mounted as a full module)\n if (this.moduleRegistry) {\n const names = importStmt.clause.type === \"named\"\n ? importStmt.clause.names\n : [importStmt.clause.name];\n\n const allFound = names.every(name => this.moduleRegistry!.has(name));\n if (allFound) {\n const result: Record<string, ComponentDefinition> = {};\n for (const name of names) {\n const def = this.moduleRegistry!.get(name);\n result[name] = {\n module: def,\n template: def?.template || \"\",\n };\n }\n return result;\n }\n }\n\n const sourcePath = this.getSourcePath(importStmt.source);\n\n // Check cache first\n if (this.options.cache && this.cache.has(sourcePath)) {\n const cached = this.cache.get(sourcePath)!;\n return this.extractComponents(importStmt.clause, cached);\n }\n\n // Load the component\n let component: ComponentDefinition;\n if (importStmt.source.type === \"local\") {\n component = await this.resolveLocal(importStmt.source.path);\n } else {\n component = await this.resolveUrl(importStmt.source.url);\n }\n\n // Cache it\n if (this.options.cache) {\n this.cache.set(sourcePath, component);\n }\n\n return this.extractComponents(importStmt.clause, component);\n }\n\n /**\n * Resolve a component from a local file path\n */\n private async resolveLocal(path: string): Promise<ComponentDefinition> {\n const { resolve, join } = await import(\"path\");\n const { readFile } = await import(\"fs/promises\");\n\n const basePath = resolve(this.options.baseDir, path);\n\n // Try .hypen extension\n const hypenPath = basePath.endsWith(\".hypen\")\n ? basePath\n : `${basePath}.hypen`;\n\n const template = await readFile(hypenPath, \"utf-8\");\n\n // Try loading sibling .ts module (stateless components have no module)\n const modulePath = hypenPath.replace(/\\.hypen$/, \".ts\");\n let module = {};\n try {\n module = await import(modulePath);\n } catch {\n // Stateless component — no module file\n }\n\n return { module, template };\n }\n\n /**\n * Resolve a component from a URL\n */\n private async resolveUrl(url: string): Promise<ComponentDefinition> {\n try {\n const response = await this.options.customFetch(url);\n const data = JSON.parse(response);\n\n // Expected format: { module: {...}, template: \"...\" }\n if (!data.module || !data.template) {\n throw new Error(\n `Invalid component format from ${url}. Expected { module, template }`\n );\n }\n\n return data;\n } catch (error) {\n throw new Error(\n `Failed to resolve component from ${url}: ${error instanceof Error ? error.message : String(error)}`\n );\n }\n }\n\n /**\n * Default fetch implementation\n */\n private async defaultFetch(url: string): Promise<string> {\n const response = await fetch(url);\n if (!response.ok) {\n throw new Error(`HTTP ${response.status}: ${response.statusText}`);\n }\n return response.text();\n }\n\n /**\n * Extract the requested components based on the import clause\n */\n private extractComponents(\n clause: ImportClause,\n component: ComponentDefinition\n ): Record<string, ComponentDefinition> {\n if (clause.type === \"default\") {\n return {\n [clause.name]: component,\n };\n } else {\n // Named imports - for now, we only support single exports\n // In the future, we could support exporting multiple components\n // from a single file\n const result: Record<string, ComponentDefinition> = {};\n for (const name of clause.names) {\n result[name] = component;\n }\n return result;\n }\n }\n\n /**\n * Get the source path as a string (for caching)\n */\n private getSourcePath(source: ImportSource): string {\n return source.type === \"local\" ? source.path : source.url;\n }\n\n /**\n * Clear the component cache\n */\n clearCache(): void {\n this.cache.clear();\n }\n\n /**\n * Parse import statements from Hypen DSL text\n * This is a simple parser that extracts import statements\n */\n static parseImports(text: string): ImportStatement[] {\n const imports: ImportStatement[] = [];\n const importRegex =\n /import\\s+(?:(\\{[^}]*\\})|(\\w+))\\s+from\\s+[\"']([^\"']+)[\"']/g;\n\n let match;\n while ((match = importRegex.exec(text)) !== null) {\n const [, namedImports, defaultImport, source] = match;\n\n if (!source) continue;\n\n let clause: ImportClause;\n if (namedImports) {\n // Named imports: { Button, Card }\n const names = namedImports\n .slice(1, -1) // Remove { and }\n .split(\",\")\n .map((n) => n.trim())\n .filter((n) => n.length > 0);\n clause = { type: \"named\", names };\n } else if (defaultImport) {\n // Default import: HomePage\n clause = { type: \"default\", name: defaultImport };\n } else {\n continue;\n }\n\n // Determine if source is URL or local path\n const sourceObj: ImportSource = source.startsWith(\"http://\") ||\n source.startsWith(\"https://\")\n ? { type: \"url\", url: source }\n : { type: \"local\", path: source };\n\n imports.push({ clause, source: sourceObj });\n }\n\n return imports;\n }\n}\n"
|
|
6
6
|
],
|
|
7
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
8
|
-
"debugId": "
|
|
7
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqDO,MAAM,kBAAkB;AAAA,EACrB,QAAQ,IAAI;AAAA,EACZ;AAAA,EAEA;AAAA,EAER,WAAW,CAAC,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21",
|
|
9
9
|
"names": []
|
|
10
10
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hypen-space/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Hypen core engine - Platform-agnostic reactive UI runtime",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -129,6 +129,12 @@
|
|
|
129
129
|
"bun": "./src/disposable.ts",
|
|
130
130
|
"import": "./dist/disposable.js",
|
|
131
131
|
"default": "./dist/disposable.js"
|
|
132
|
+
},
|
|
133
|
+
"./types": {
|
|
134
|
+
"types": "./dist/types.d.ts",
|
|
135
|
+
"bun": "./src/types.ts",
|
|
136
|
+
"import": "./dist/types.js",
|
|
137
|
+
"default": "./dist/types.js"
|
|
132
138
|
}
|
|
133
139
|
},
|
|
134
140
|
"files": [
|
package/src/app.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* Implements the stateful module system from RFC-0001
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import type { Action } from "./
|
|
6
|
+
import type { Action } from "./types.js";
|
|
7
7
|
import type { Session } from "./remote/types.js";
|
|
8
8
|
import { type Result, Ok, Err, fromPromise, ActionError, HypenError } from "./result.js";
|
|
9
9
|
|
|
@@ -383,7 +383,11 @@ export class HypenModuleInstance<T extends object = any> {
|
|
|
383
383
|
this.routerContext = routerContext;
|
|
384
384
|
this.globalContext = globalContext;
|
|
385
385
|
|
|
386
|
+
// Named state prefix — all lowercase per convention
|
|
387
|
+
const statePrefix = (definition.name || "").toLowerCase();
|
|
388
|
+
|
|
386
389
|
// Create observable state that sends only changed paths and values to engine
|
|
390
|
+
// When a prefix is set, all paths are namespaced (e.g., "homepage.count")
|
|
387
391
|
this.state = createObservableState<T>(definition.initialState as T & object, {
|
|
388
392
|
onChange: (change: StateChange) => {
|
|
389
393
|
// Send only the changed paths and their new values (not the whole state)
|
|
@@ -391,14 +395,22 @@ export class HypenModuleInstance<T extends object = any> {
|
|
|
391
395
|
// Notify all registered callbacks
|
|
392
396
|
this.stateChangeCallbacks.forEach(cb => cb());
|
|
393
397
|
},
|
|
398
|
+
pathPrefix: statePrefix || undefined,
|
|
394
399
|
});
|
|
395
400
|
|
|
396
401
|
// Register with engine (initial state registration)
|
|
402
|
+
// When prefixed, nest the state under the module name so the engine sees
|
|
403
|
+
// e.g., { homepage: { count: 0 } } instead of { count: 0 }
|
|
404
|
+
const snapshot = getStateSnapshot(this.state);
|
|
405
|
+
const prefixedState = statePrefix
|
|
406
|
+
? { [statePrefix]: snapshot }
|
|
407
|
+
: snapshot;
|
|
408
|
+
|
|
397
409
|
this.engine.setModule(
|
|
398
410
|
definition.name || "AnonymousModule",
|
|
399
411
|
definition.actions,
|
|
400
412
|
definition.stateKeys,
|
|
401
|
-
|
|
413
|
+
prefixedState
|
|
402
414
|
);
|
|
403
415
|
|
|
404
416
|
// Register action handlers with flexible parameter support
|
package/src/discovery.ts
CHANGED
|
@@ -123,7 +123,9 @@ export async function discoverComponents(
|
|
|
123
123
|
|
|
124
124
|
seen.add(name);
|
|
125
125
|
const templateRaw = readFileSync(hypenPath, "utf-8");
|
|
126
|
-
|
|
126
|
+
// Preserve import statements in templates — the engine now processes them
|
|
127
|
+
// via parse_document() and resolves imports through the component resolver
|
|
128
|
+
const template = templateRaw.trim();
|
|
127
129
|
|
|
128
130
|
components.push({
|
|
129
131
|
name,
|
package/src/engine.browser.ts
CHANGED
|
@@ -4,6 +4,10 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import { frameworkLoggers } from "./logger.js";
|
|
7
|
+
import type { Patch, Action, RenderCallback, ActionHandler, ResolvedComponent, ComponentResolver } from "./types.js";
|
|
8
|
+
|
|
9
|
+
// Re-export types so existing consumers of "./engine.browser.js" still work
|
|
10
|
+
export type { Patch, Action, RenderCallback, ActionHandler, ResolvedComponent, ComponentResolver } from "./types.js";
|
|
7
11
|
|
|
8
12
|
const log = frameworkLoggers.engine;
|
|
9
13
|
|
|
@@ -12,38 +16,6 @@ const log = frameworkLoggers.engine;
|
|
|
12
16
|
let wasmInit: ((path?: string) => Promise<void>) | null = null;
|
|
13
17
|
let WasmEngineClass: any = null;
|
|
14
18
|
|
|
15
|
-
export type Patch = {
|
|
16
|
-
type: "create" | "setProp" | "setText" | "insert" | "move" | "remove" | "attachEvent" | "detachEvent";
|
|
17
|
-
id?: string;
|
|
18
|
-
elementType?: string;
|
|
19
|
-
props?: Record<string, any>;
|
|
20
|
-
name?: string;
|
|
21
|
-
value?: any;
|
|
22
|
-
text?: string;
|
|
23
|
-
parentId?: string;
|
|
24
|
-
beforeId?: string;
|
|
25
|
-
eventName?: string;
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
export type Action = {
|
|
29
|
-
name: string;
|
|
30
|
-
payload?: any;
|
|
31
|
-
sender?: string;
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
export type RenderCallback = (patches: Patch[]) => void;
|
|
35
|
-
export type ActionHandler = (action: Action) => void | Promise<void>;
|
|
36
|
-
|
|
37
|
-
export type ResolvedComponent = {
|
|
38
|
-
source: string;
|
|
39
|
-
path: string;
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
export type ComponentResolver = (
|
|
43
|
-
componentName: string,
|
|
44
|
-
contextPath: string | null
|
|
45
|
-
) => ResolvedComponent | null;
|
|
46
|
-
|
|
47
19
|
export interface EngineInitOptions {
|
|
48
20
|
/**
|
|
49
21
|
* URL to the WASM binary file.
|
package/src/engine.ts
CHANGED
|
@@ -3,12 +3,13 @@
|
|
|
3
3
|
* Node.js / Bundler target
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
// WASM module
|
|
7
|
-
import
|
|
8
|
-
type WasmEngine = WasmEngineType;
|
|
9
|
-
let WasmEngineClass: (new () => WasmEngine) | null = null;
|
|
10
|
-
|
|
6
|
+
// WASM module types
|
|
7
|
+
import { WasmEngine } from "../wasm-node/hypen_engine.js";
|
|
11
8
|
import { frameworkLoggers } from "./logger.js";
|
|
9
|
+
import type { Patch, Action, RenderCallback, ActionHandler, ResolvedComponent, ComponentResolver } from "./types.js";
|
|
10
|
+
|
|
11
|
+
// Re-export types so existing consumers of "./engine.js" still work
|
|
12
|
+
export type { Patch, Action, RenderCallback, ActionHandler, ResolvedComponent, ComponentResolver } from "./types.js";
|
|
12
13
|
|
|
13
14
|
const log = frameworkLoggers.engine;
|
|
14
15
|
|
|
@@ -37,38 +38,6 @@ function unwrapForWasm<T>(value: T): T {
|
|
|
37
38
|
}
|
|
38
39
|
}
|
|
39
40
|
|
|
40
|
-
export type Patch = {
|
|
41
|
-
type: "create" | "setProp" | "setText" | "insert" | "move" | "remove" | "attachEvent" | "detachEvent";
|
|
42
|
-
id?: string;
|
|
43
|
-
elementType?: string;
|
|
44
|
-
props?: Record<string, any>;
|
|
45
|
-
name?: string;
|
|
46
|
-
value?: any;
|
|
47
|
-
text?: string;
|
|
48
|
-
parentId?: string;
|
|
49
|
-
beforeId?: string;
|
|
50
|
-
eventName?: string;
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
export type Action = {
|
|
54
|
-
name: string;
|
|
55
|
-
payload?: any;
|
|
56
|
-
sender?: string;
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
export type RenderCallback = (patches: Patch[]) => void;
|
|
60
|
-
export type ActionHandler = (action: Action) => void | Promise<void>;
|
|
61
|
-
|
|
62
|
-
export type ResolvedComponent = {
|
|
63
|
-
source: string;
|
|
64
|
-
path: string;
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
export type ComponentResolver = (
|
|
68
|
-
componentName: string,
|
|
69
|
-
contextPath: string | null
|
|
70
|
-
) => ResolvedComponent | null;
|
|
71
|
-
|
|
72
41
|
/**
|
|
73
42
|
* Engine wraps the WASM engine and provides a TypeScript-friendly API
|
|
74
43
|
*/
|
|
@@ -82,13 +51,8 @@ export class Engine {
|
|
|
82
51
|
async init(): Promise<void> {
|
|
83
52
|
if (this.initialized) return;
|
|
84
53
|
|
|
85
|
-
//
|
|
86
|
-
|
|
87
|
-
const wasm = await import("../wasm-node/hypen_engine.js");
|
|
88
|
-
WasmEngineClass = wasm.WasmEngine;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
this.wasmEngine = new WasmEngineClass();
|
|
54
|
+
// For bundler target, WASM is auto-initialized
|
|
55
|
+
this.wasmEngine = new WasmEngine();
|
|
92
56
|
this.initialized = true;
|
|
93
57
|
}
|
|
94
58
|
|
package/src/index.browser.ts
CHANGED
|
@@ -5,10 +5,9 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
// ============================================================================
|
|
8
|
-
//
|
|
8
|
+
// CORE TYPES (WASM-free)
|
|
9
9
|
// ============================================================================
|
|
10
10
|
|
|
11
|
-
export { Engine } from "./engine.browser.js";
|
|
12
11
|
export type {
|
|
13
12
|
Patch,
|
|
14
13
|
Action,
|
|
@@ -16,8 +15,11 @@ export type {
|
|
|
16
15
|
ActionHandler as EngineActionHandler,
|
|
17
16
|
ResolvedComponent,
|
|
18
17
|
ComponentResolver,
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
} from "./types.js";
|
|
19
|
+
|
|
20
|
+
// NOTE: Engine (BrowserEngine) is NOT exported from the barrel.
|
|
21
|
+
// Import it explicitly from its subpath to avoid pulling in WASM:
|
|
22
|
+
// import { Engine } from "@hypen-space/core/engine/browser"
|
|
21
23
|
|
|
22
24
|
// ============================================================================
|
|
23
25
|
// APP / MODULE SYSTEM
|
package/src/index.ts
CHANGED
|
@@ -42,10 +42,9 @@
|
|
|
42
42
|
*/
|
|
43
43
|
|
|
44
44
|
// ============================================================================
|
|
45
|
-
//
|
|
45
|
+
// CORE TYPES (WASM-free)
|
|
46
46
|
// ============================================================================
|
|
47
47
|
|
|
48
|
-
export { Engine } from "./engine.js";
|
|
49
48
|
export type {
|
|
50
49
|
Patch,
|
|
51
50
|
Action,
|
|
@@ -53,11 +52,12 @@ export type {
|
|
|
53
52
|
ActionHandler as EngineActionHandler,
|
|
54
53
|
ResolvedComponent,
|
|
55
54
|
ComponentResolver as EngineComponentResolver,
|
|
56
|
-
} from "./
|
|
55
|
+
} from "./types.js";
|
|
57
56
|
|
|
58
|
-
//
|
|
59
|
-
|
|
60
|
-
|
|
57
|
+
// NOTE: Engine and BrowserEngine are NOT exported from the barrel.
|
|
58
|
+
// Import them explicitly from their subpaths to avoid pulling in WASM:
|
|
59
|
+
// import { Engine } from "@hypen-space/core/engine"
|
|
60
|
+
// import { Engine } from "@hypen-space/core/engine/browser"
|
|
61
61
|
|
|
62
62
|
// ============================================================================
|
|
63
63
|
// APP / MODULE SYSTEM
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Managed Router — orchestrates module mount/unmount on route changes.
|
|
3
|
+
*
|
|
4
|
+
* When the router navigates to a route:
|
|
5
|
+
* 1. Unmounts the previous module (destroy instance, unregister from GlobalContext)
|
|
6
|
+
* 2. Mounts the new module (create instance with namespaced state, register)
|
|
7
|
+
*
|
|
8
|
+
* Module names are used as state prefixes (lowercased) for isolation.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type { IEngine, HypenModuleDefinition } from "./app.js";
|
|
12
|
+
import { HypenModuleInstance } from "./app.js";
|
|
13
|
+
import type { HypenRouter, RouteState } from "./router.js";
|
|
14
|
+
import type { HypenGlobalContext } from "./context.js";
|
|
15
|
+
import { ModuleRegistry } from "./module-registry.js";
|
|
16
|
+
import { createLogger } from "./logger.js";
|
|
17
|
+
|
|
18
|
+
const log = createLogger("ManagedRouter");
|
|
19
|
+
|
|
20
|
+
export interface RouteDefinition {
|
|
21
|
+
/** Route path pattern (e.g., "/", "/profile/:id") */
|
|
22
|
+
path: string;
|
|
23
|
+
/** Component name — used to look up in ModuleRegistry */
|
|
24
|
+
component: string;
|
|
25
|
+
/** Inline module definition (alternative to registry lookup) */
|
|
26
|
+
module?: HypenModuleDefinition;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export class ManagedRouter {
|
|
30
|
+
private router: HypenRouter;
|
|
31
|
+
private engine: IEngine;
|
|
32
|
+
private registry: ModuleRegistry;
|
|
33
|
+
private globalContext: HypenGlobalContext;
|
|
34
|
+
private routes: RouteDefinition[] = [];
|
|
35
|
+
private activeModule: HypenModuleInstance | null = null;
|
|
36
|
+
private activeRoute: RouteDefinition | null = null;
|
|
37
|
+
private unsubscribe: (() => void) | null = null;
|
|
38
|
+
/** Cached instances for modules with persist=true */
|
|
39
|
+
private persistedModules = new Map<string, HypenModuleInstance>();
|
|
40
|
+
|
|
41
|
+
constructor(
|
|
42
|
+
router: HypenRouter,
|
|
43
|
+
engine: IEngine,
|
|
44
|
+
registry: ModuleRegistry,
|
|
45
|
+
globalContext: HypenGlobalContext
|
|
46
|
+
) {
|
|
47
|
+
this.router = router;
|
|
48
|
+
this.engine = engine;
|
|
49
|
+
this.registry = registry;
|
|
50
|
+
this.globalContext = globalContext;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Add a route definition.
|
|
55
|
+
*/
|
|
56
|
+
addRoute(route: RouteDefinition): this {
|
|
57
|
+
this.routes.push(route);
|
|
58
|
+
return this;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Start listening for route changes and mount the initial route.
|
|
63
|
+
*/
|
|
64
|
+
start(): void {
|
|
65
|
+
this.unsubscribe = this.router.onNavigate((routeState: RouteState) => {
|
|
66
|
+
this.handleRouteChange(routeState);
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Stop listening and unmount the active module.
|
|
72
|
+
* Also destroys all persisted modules.
|
|
73
|
+
*/
|
|
74
|
+
stop(): void {
|
|
75
|
+
if (this.unsubscribe) {
|
|
76
|
+
this.unsubscribe();
|
|
77
|
+
this.unsubscribe = null;
|
|
78
|
+
}
|
|
79
|
+
this.unmountActive();
|
|
80
|
+
|
|
81
|
+
// Destroy all persisted modules on full stop
|
|
82
|
+
for (const [moduleId, instance] of this.persistedModules) {
|
|
83
|
+
log.debug(`Destroying persisted module on stop: ${moduleId}`);
|
|
84
|
+
instance.destroy();
|
|
85
|
+
this.globalContext.unregisterModule(moduleId);
|
|
86
|
+
}
|
|
87
|
+
this.persistedModules.clear();
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Get the currently active module instance.
|
|
92
|
+
*/
|
|
93
|
+
getActiveModule(): HypenModuleInstance | null {
|
|
94
|
+
return this.activeModule;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Get the currently active route.
|
|
99
|
+
*/
|
|
100
|
+
getActiveRoute(): RouteDefinition | null {
|
|
101
|
+
return this.activeRoute;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
private handleRouteChange(routeState: RouteState): void {
|
|
105
|
+
const path = routeState.currentPath;
|
|
106
|
+
const matched = this.matchRoute(path);
|
|
107
|
+
|
|
108
|
+
if (!matched) {
|
|
109
|
+
log.debug(`No route matched for path: ${path}`);
|
|
110
|
+
this.unmountActive();
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// If same route, no need to remount
|
|
115
|
+
if (this.activeRoute && this.activeRoute.path === matched.path) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Unmount old, mount new
|
|
120
|
+
this.unmountActive();
|
|
121
|
+
this.mount(matched);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
private matchRoute(path: string): RouteDefinition | null {
|
|
125
|
+
for (const route of this.routes) {
|
|
126
|
+
if (this.router.matchPath(route.path, path) !== null) {
|
|
127
|
+
return route;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
private mount(route: RouteDefinition): void {
|
|
134
|
+
// Look up module definition: inline first, then registry
|
|
135
|
+
const definition = route.module || this.registry.get(route.component);
|
|
136
|
+
if (!definition) {
|
|
137
|
+
log.debug(`No module definition found for component: ${route.component}`);
|
|
138
|
+
this.activeRoute = route;
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// Ensure the definition has a name for state namespacing
|
|
143
|
+
const namedDef = {
|
|
144
|
+
...definition,
|
|
145
|
+
name: definition.name || route.component.toLowerCase(),
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
const moduleId = namedDef.name!.toLowerCase();
|
|
149
|
+
|
|
150
|
+
// Check for a persisted instance first
|
|
151
|
+
const persisted = this.persistedModules.get(moduleId);
|
|
152
|
+
if (persisted) {
|
|
153
|
+
log.debug(`Restoring persisted module: ${moduleId} for route: ${route.path}`);
|
|
154
|
+
this.activeModule = persisted;
|
|
155
|
+
this.activeRoute = route;
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
log.debug(`Mounting module: ${namedDef.name} for route: ${route.path}`);
|
|
160
|
+
|
|
161
|
+
const instance = new HypenModuleInstance(
|
|
162
|
+
this.engine,
|
|
163
|
+
namedDef as HypenModuleDefinition<object>,
|
|
164
|
+
{ root: this.router },
|
|
165
|
+
this.globalContext
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
// Register in global context under the module name (lowercase)
|
|
169
|
+
this.globalContext.registerModule(moduleId, instance);
|
|
170
|
+
|
|
171
|
+
this.activeModule = instance;
|
|
172
|
+
this.activeRoute = route;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
private unmountActive(): void {
|
|
176
|
+
if (this.activeModule && this.activeRoute) {
|
|
177
|
+
const definition = this.activeRoute.module || this.registry.get(this.activeRoute.component);
|
|
178
|
+
const moduleId = (definition?.name || this.activeRoute.component).toLowerCase();
|
|
179
|
+
const persist = definition?.persist === true;
|
|
180
|
+
|
|
181
|
+
if (persist) {
|
|
182
|
+
log.debug(`Persisting module: ${moduleId} (persist=true)`);
|
|
183
|
+
this.persistedModules.set(moduleId, this.activeModule);
|
|
184
|
+
// Keep registered in GlobalContext so other modules can still access its state
|
|
185
|
+
} else {
|
|
186
|
+
log.debug(`Unmounting module: ${moduleId}`);
|
|
187
|
+
this.activeModule.destroy();
|
|
188
|
+
this.globalContext.unregisterModule(moduleId);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
this.activeModule = null;
|
|
193
|
+
this.activeRoute = null;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Module Registry — maps component names to module definitions.
|
|
3
|
+
*
|
|
4
|
+
* Used by the import resolution pipeline and ManagedRouter to look up
|
|
5
|
+
* module definitions by their component name (e.g., "HomePage", "ProfilePage").
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { HypenModuleDefinition } from "./app.js";
|
|
9
|
+
import { createLogger } from "./logger.js";
|
|
10
|
+
|
|
11
|
+
const log = createLogger("ModuleRegistry");
|
|
12
|
+
|
|
13
|
+
export class ModuleRegistry {
|
|
14
|
+
private definitions = new Map<string, HypenModuleDefinition>();
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Register a module definition under a component name.
|
|
18
|
+
*/
|
|
19
|
+
register(name: string, definition: HypenModuleDefinition): void {
|
|
20
|
+
if (this.definitions.has(name)) {
|
|
21
|
+
log.warn(`Module "${name}" is already registered. Overwriting.`);
|
|
22
|
+
}
|
|
23
|
+
this.definitions.set(name, definition);
|
|
24
|
+
log.debug(`Registered module definition: ${name}`);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Get a module definition by component name.
|
|
29
|
+
*/
|
|
30
|
+
get(name: string): HypenModuleDefinition | undefined {
|
|
31
|
+
return this.definitions.get(name);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Check if a module definition exists.
|
|
36
|
+
*/
|
|
37
|
+
has(name: string): boolean {
|
|
38
|
+
return this.definitions.has(name);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Get all registered definitions.
|
|
43
|
+
*/
|
|
44
|
+
getAll(): Map<string, HypenModuleDefinition> {
|
|
45
|
+
return new Map(this.definitions);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Get all registered component names.
|
|
50
|
+
*/
|
|
51
|
+
getNames(): string[] {
|
|
52
|
+
return Array.from(this.definitions.keys());
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Unregister a module definition.
|
|
57
|
+
*/
|
|
58
|
+
unregister(name: string): void {
|
|
59
|
+
this.definitions.delete(name);
|
|
60
|
+
log.debug(`Unregistered module definition: ${name}`);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Number of registered definitions.
|
|
65
|
+
*/
|
|
66
|
+
get size(): number {
|
|
67
|
+
return this.definitions.size;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Clear all registered definitions.
|
|
72
|
+
*/
|
|
73
|
+
clear(): void {
|
|
74
|
+
this.definitions.clear();
|
|
75
|
+
}
|
|
76
|
+
}
|
package/src/remote/client.ts
CHANGED
package/src/remote/index.ts
CHANGED