@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/dist/index.js
CHANGED
|
@@ -361,7 +361,20 @@ function all(results) {
|
|
|
361
361
|
}
|
|
362
362
|
return Ok(values);
|
|
363
363
|
}
|
|
364
|
-
|
|
364
|
+
function classifyEngineError(err) {
|
|
365
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
366
|
+
if (message.startsWith("Parse error:")) {
|
|
367
|
+
return new ParseError(message);
|
|
368
|
+
}
|
|
369
|
+
if (message.startsWith("Invalid state:") || message.startsWith("Invalid state patch:")) {
|
|
370
|
+
return new StateError(message);
|
|
371
|
+
}
|
|
372
|
+
if (message.startsWith("Parent node not found:")) {
|
|
373
|
+
return new RenderError(message);
|
|
374
|
+
}
|
|
375
|
+
return new RenderError(message);
|
|
376
|
+
}
|
|
377
|
+
var HypenError, ActionError, ConnectionError, StateError, ParseError, RenderError;
|
|
365
378
|
var init_result = __esm(() => {
|
|
366
379
|
HypenError = class HypenError extends Error {
|
|
367
380
|
code;
|
|
@@ -411,6 +424,25 @@ var init_result = __esm(() => {
|
|
|
411
424
|
this.path = path;
|
|
412
425
|
}
|
|
413
426
|
};
|
|
427
|
+
ParseError = class ParseError extends HypenError {
|
|
428
|
+
source;
|
|
429
|
+
constructor(message, source, cause) {
|
|
430
|
+
super("PARSE_ERROR", message, {
|
|
431
|
+
context: { source },
|
|
432
|
+
cause: cause instanceof Error ? cause : undefined
|
|
433
|
+
});
|
|
434
|
+
this.name = "ParseError";
|
|
435
|
+
this.source = source;
|
|
436
|
+
}
|
|
437
|
+
};
|
|
438
|
+
RenderError = class RenderError extends HypenError {
|
|
439
|
+
constructor(message, cause) {
|
|
440
|
+
super("RENDER_ERROR", message, {
|
|
441
|
+
cause: cause instanceof Error ? cause : undefined
|
|
442
|
+
});
|
|
443
|
+
this.name = "RenderError";
|
|
444
|
+
}
|
|
445
|
+
};
|
|
414
446
|
});
|
|
415
447
|
|
|
416
448
|
// src/logger.ts
|
|
@@ -632,9 +664,11 @@ class HypenAppBuilder {
|
|
|
632
664
|
expireHandler;
|
|
633
665
|
errorHandler;
|
|
634
666
|
template;
|
|
635
|
-
|
|
667
|
+
_registry;
|
|
668
|
+
constructor(initialState, options, registry) {
|
|
636
669
|
this.initialState = initialState;
|
|
637
670
|
this.options = options || {};
|
|
671
|
+
this._registry = registry;
|
|
638
672
|
}
|
|
639
673
|
onCreated(fn) {
|
|
640
674
|
this.createdHandler = fn;
|
|
@@ -670,7 +704,7 @@ class HypenAppBuilder {
|
|
|
670
704
|
}
|
|
671
705
|
build() {
|
|
672
706
|
const stateKeys = this.initialState !== null && typeof this.initialState === "object" ? Object.keys(this.initialState) : [];
|
|
673
|
-
|
|
707
|
+
const definition = {
|
|
674
708
|
name: this.options.name,
|
|
675
709
|
actions: Array.from(this.actionHandlers.keys()),
|
|
676
710
|
stateKeys,
|
|
@@ -688,12 +722,46 @@ class HypenAppBuilder {
|
|
|
688
722
|
onError: this.errorHandler
|
|
689
723
|
}
|
|
690
724
|
};
|
|
725
|
+
if (this.options.name && this._registry) {
|
|
726
|
+
this._registry.set(this.options.name, definition);
|
|
727
|
+
}
|
|
728
|
+
return definition;
|
|
691
729
|
}
|
|
692
730
|
}
|
|
693
731
|
|
|
694
732
|
class HypenApp {
|
|
733
|
+
_registry = new Map;
|
|
695
734
|
defineState(initial, options) {
|
|
696
|
-
return new HypenAppBuilder(initial, options);
|
|
735
|
+
return new HypenAppBuilder(initial, options, this._registry);
|
|
736
|
+
}
|
|
737
|
+
module(name) {
|
|
738
|
+
const registry = this._registry;
|
|
739
|
+
return {
|
|
740
|
+
defineState: (initial, options) => {
|
|
741
|
+
return new HypenAppBuilder(initial, { ...options, name }, registry);
|
|
742
|
+
}
|
|
743
|
+
};
|
|
744
|
+
}
|
|
745
|
+
get(name) {
|
|
746
|
+
return this._registry.get(name);
|
|
747
|
+
}
|
|
748
|
+
has(name) {
|
|
749
|
+
return this._registry.has(name);
|
|
750
|
+
}
|
|
751
|
+
get components() {
|
|
752
|
+
return this._registry;
|
|
753
|
+
}
|
|
754
|
+
getNames() {
|
|
755
|
+
return Array.from(this._registry.keys());
|
|
756
|
+
}
|
|
757
|
+
get size() {
|
|
758
|
+
return this._registry.size;
|
|
759
|
+
}
|
|
760
|
+
unregister(name) {
|
|
761
|
+
this._registry.delete(name);
|
|
762
|
+
}
|
|
763
|
+
clear() {
|
|
764
|
+
this._registry.clear();
|
|
697
765
|
}
|
|
698
766
|
}
|
|
699
767
|
|
|
@@ -702,13 +770,13 @@ class HypenModuleInstance {
|
|
|
702
770
|
definition;
|
|
703
771
|
state;
|
|
704
772
|
isDestroyed = false;
|
|
705
|
-
|
|
773
|
+
router;
|
|
706
774
|
globalContext;
|
|
707
775
|
stateChangeCallbacks = [];
|
|
708
|
-
constructor(engine, definition,
|
|
776
|
+
constructor(engine, definition, router, globalContext) {
|
|
709
777
|
this.engine = engine;
|
|
710
778
|
this.definition = definition;
|
|
711
|
-
this.
|
|
779
|
+
this.router = router ?? null;
|
|
712
780
|
this.globalContext = globalContext;
|
|
713
781
|
const statePrefix = (definition.name || "").toLowerCase();
|
|
714
782
|
this.state = createObservableState(definition.initialState, {
|
|
@@ -730,14 +798,10 @@ class HypenModuleInstance {
|
|
|
730
798
|
payload: action.payload,
|
|
731
799
|
sender: action.sender
|
|
732
800
|
};
|
|
733
|
-
const next = {
|
|
734
|
-
router: this.routerContext?.root || null
|
|
735
|
-
};
|
|
736
801
|
const context = this.globalContext ? this.createGlobalContextAPI() : undefined;
|
|
737
802
|
const result = await this.executeAction(actionName, handler, {
|
|
738
803
|
action: actionCtx,
|
|
739
804
|
state: this.state,
|
|
740
|
-
next,
|
|
741
805
|
context
|
|
742
806
|
});
|
|
743
807
|
if (!result.ok) {
|
|
@@ -750,6 +814,21 @@ class HypenModuleInstance {
|
|
|
750
814
|
}
|
|
751
815
|
});
|
|
752
816
|
}
|
|
817
|
+
this.engine.onAction("__hypen_bind", (action) => {
|
|
818
|
+
const payload = action.payload;
|
|
819
|
+
if (!payload?.path)
|
|
820
|
+
return;
|
|
821
|
+
const segments = payload.path.split(".");
|
|
822
|
+
let target = this.state;
|
|
823
|
+
for (let i = 0;i < segments.length - 1; i++) {
|
|
824
|
+
const seg = segments[i];
|
|
825
|
+
target = target?.[seg];
|
|
826
|
+
if (target == null)
|
|
827
|
+
return;
|
|
828
|
+
}
|
|
829
|
+
const lastSeg = segments[segments.length - 1];
|
|
830
|
+
target[lastSeg] = payload.value;
|
|
831
|
+
});
|
|
753
832
|
this.callCreatedHandler();
|
|
754
833
|
}
|
|
755
834
|
createGlobalContextAPI() {
|
|
@@ -763,11 +842,9 @@ class HypenModuleInstance {
|
|
|
763
842
|
getModuleIds: () => ctx.getModuleIds(),
|
|
764
843
|
getGlobalState: () => ctx.getGlobalState(),
|
|
765
844
|
emit: (event, payload) => ctx.emit(event, payload),
|
|
766
|
-
on: (event, handler) => ctx.on(event, handler)
|
|
845
|
+
on: (event, handler) => ctx.on(event, handler),
|
|
846
|
+
router: this.router
|
|
767
847
|
};
|
|
768
|
-
if (ctx.__router) {
|
|
769
|
-
api.__router = ctx.__router;
|
|
770
|
-
}
|
|
771
848
|
if (ctx.__hypenEngine) {
|
|
772
849
|
api.__hypenEngine = ctx.__hypenEngine;
|
|
773
850
|
}
|
|
@@ -818,7 +895,15 @@ class HypenModuleInstance {
|
|
|
818
895
|
async callCreatedHandler() {
|
|
819
896
|
if (this.definition.handlers.onCreated) {
|
|
820
897
|
const context = this.globalContext ? this.createGlobalContextAPI() : undefined;
|
|
821
|
-
|
|
898
|
+
try {
|
|
899
|
+
await this.definition.handlers.onCreated(this.state, context);
|
|
900
|
+
} catch (e) {
|
|
901
|
+
const error = e instanceof HypenError ? e : new ActionError("onCreated", e);
|
|
902
|
+
const shouldRethrow = await this.handleError(error, { lifecycle: "created" });
|
|
903
|
+
if (shouldRethrow) {
|
|
904
|
+
throw error;
|
|
905
|
+
}
|
|
906
|
+
}
|
|
822
907
|
}
|
|
823
908
|
}
|
|
824
909
|
onStateChange(callback) {
|
|
@@ -828,7 +913,15 @@ class HypenModuleInstance {
|
|
|
828
913
|
if (this.isDestroyed)
|
|
829
914
|
return;
|
|
830
915
|
if (this.definition.handlers.onDestroyed) {
|
|
831
|
-
|
|
916
|
+
try {
|
|
917
|
+
await this.definition.handlers.onDestroyed(this.state);
|
|
918
|
+
} catch (e) {
|
|
919
|
+
const error = e instanceof HypenError ? e : new ActionError("onDestroyed", e);
|
|
920
|
+
const shouldRethrow = await this.handleError(error, { lifecycle: "destroyed" });
|
|
921
|
+
if (shouldRethrow) {
|
|
922
|
+
throw error;
|
|
923
|
+
}
|
|
924
|
+
}
|
|
832
925
|
}
|
|
833
926
|
this.isDestroyed = true;
|
|
834
927
|
}
|
|
@@ -1934,7 +2027,7 @@ var Router = app.defineState({
|
|
|
1934
2027
|
log9.error("Requires global context");
|
|
1935
2028
|
return;
|
|
1936
2029
|
}
|
|
1937
|
-
const router = context.
|
|
2030
|
+
const router = context.router;
|
|
1938
2031
|
if (!router) {
|
|
1939
2032
|
log9.error("Router not found in context");
|
|
1940
2033
|
return;
|
|
@@ -1981,7 +2074,7 @@ var Link = app.defineState({
|
|
|
1981
2074
|
to: "/",
|
|
1982
2075
|
isActive: false
|
|
1983
2076
|
}, { name: "__Link" }).onAction("navigate", ({ state, context }) => {
|
|
1984
|
-
const router = context?.
|
|
2077
|
+
const router = context?.router;
|
|
1985
2078
|
if (!router) {
|
|
1986
2079
|
log9.error("Link requires router context");
|
|
1987
2080
|
return;
|
|
@@ -1991,7 +2084,7 @@ var Link = app.defineState({
|
|
|
1991
2084
|
}).onCreated((state, context) => {
|
|
1992
2085
|
if (!context)
|
|
1993
2086
|
return;
|
|
1994
|
-
const router = context.
|
|
2087
|
+
const router = context.router;
|
|
1995
2088
|
if (router) {
|
|
1996
2089
|
router.onNavigate((routeState) => {
|
|
1997
2090
|
state.isActive = routeState.currentPath === state.to;
|
|
@@ -2228,6 +2321,7 @@ export {
|
|
|
2228
2321
|
createEventEmitter,
|
|
2229
2322
|
configureLogger,
|
|
2230
2323
|
compositeDisposable,
|
|
2324
|
+
classifyEngineError,
|
|
2231
2325
|
batchStateUpdates,
|
|
2232
2326
|
app,
|
|
2233
2327
|
all,
|
|
@@ -2238,7 +2332,9 @@ export {
|
|
|
2238
2332
|
Route,
|
|
2239
2333
|
RetryPresets,
|
|
2240
2334
|
RetryConditions,
|
|
2335
|
+
RenderError,
|
|
2241
2336
|
RemoteEngine,
|
|
2337
|
+
ParseError,
|
|
2242
2338
|
Ok,
|
|
2243
2339
|
Logger,
|
|
2244
2340
|
Link,
|
|
@@ -2258,4 +2354,4 @@ export {
|
|
|
2258
2354
|
ActionError
|
|
2259
2355
|
};
|
|
2260
2356
|
|
|
2261
|
-
//# debugId=
|
|
2357
|
+
//# debugId=94AD8348A299084864756E2164756E21
|