@hypen-space/core 0.4.2 → 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.
Files changed (66) hide show
  1. package/README.md +1 -1
  2. package/dist/app.d.ts +379 -0
  3. package/dist/app.js +111 -18
  4. package/dist/app.js.map +4 -4
  5. package/dist/components/builtin.d.ts +50 -0
  6. package/dist/components/builtin.js +114 -21
  7. package/dist/components/builtin.js.map +5 -5
  8. package/dist/context.d.ts +90 -0
  9. package/dist/discovery.d.ts +90 -0
  10. package/dist/discovery.js +121 -23
  11. package/dist/discovery.js.map +5 -5
  12. package/dist/disposable.d.ts +122 -0
  13. package/dist/engine.browser.d.ts +101 -0
  14. package/dist/engine.browser.js +192 -5
  15. package/dist/engine.browser.js.map +5 -4
  16. package/dist/engine.d.ts +85 -0
  17. package/dist/engine.js +192 -5
  18. package/dist/engine.js.map +5 -4
  19. package/dist/events.d.ts +78 -0
  20. package/dist/hypen.d.ts +127 -0
  21. package/dist/index.browser.d.ts +25 -0
  22. package/dist/index.browser.js +116 -19
  23. package/dist/index.browser.js.map +6 -6
  24. package/dist/index.d.ts +76 -0
  25. package/dist/index.js +373 -1235
  26. package/dist/index.js.map +10 -15
  27. package/dist/loader.d.ts +51 -0
  28. package/dist/logger.d.ts +141 -0
  29. package/dist/managed-router.d.ts +59 -0
  30. package/dist/plugin.d.ts +39 -0
  31. package/dist/remote/client.d.ts +154 -0
  32. package/dist/remote/client.js +34 -2
  33. package/dist/remote/client.js.map +3 -3
  34. package/dist/remote/index.d.ts +13 -0
  35. package/dist/remote/index.js +472 -26
  36. package/dist/remote/index.js.map +8 -7
  37. package/dist/remote/server.d.ts +173 -0
  38. package/dist/remote/server.js +472 -26
  39. package/dist/remote/server.js.map +8 -7
  40. package/dist/remote/session.d.ts +115 -0
  41. package/dist/remote/types.d.ts +98 -0
  42. package/dist/renderer.d.ts +54 -0
  43. package/dist/renderer.js +6 -2
  44. package/dist/renderer.js.map +3 -3
  45. package/dist/resolver.d.ts +95 -0
  46. package/dist/result.d.ts +134 -0
  47. package/dist/retry.d.ts +150 -0
  48. package/dist/router.d.ts +94 -0
  49. package/dist/state.d.ts +42 -0
  50. package/dist/types.d.ts +30 -0
  51. package/package.json +2 -1
  52. package/src/app.ts +162 -41
  53. package/src/components/builtin.ts +3 -4
  54. package/src/discovery.ts +2 -2
  55. package/src/engine.browser.ts +27 -4
  56. package/src/engine.ts +27 -4
  57. package/src/index.browser.ts +0 -2
  58. package/src/index.ts +3 -2
  59. package/src/managed-router.ts +5 -6
  60. package/src/remote/server.ts +116 -21
  61. package/src/result.ts +48 -0
  62. package/wasm-browser/hypen_engine_bg.wasm +0 -0
  63. package/wasm-browser/package.json +1 -1
  64. package/wasm-node/hypen_engine_bg.wasm +0 -0
  65. package/wasm-node/package.json +5 -3
  66. package/src/module-registry.ts +0 -76
package/dist/discovery.js CHANGED
@@ -361,7 +361,20 @@ function all(results) {
361
361
  }
362
362
  return Ok(values);
363
363
  }
364
- var HypenError, ActionError, ConnectionError, StateError;
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
- constructor(initialState, options) {
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
- return {
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
- routerContext;
773
+ router;
706
774
  globalContext;
707
775
  stateChangeCallbacks = [];
708
- constructor(engine, definition, routerContext, globalContext) {
776
+ constructor(engine, definition, router, globalContext) {
709
777
  this.engine = engine;
710
778
  this.definition = definition;
711
- this.routerContext = routerContext;
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
- await this.definition.handlers.onCreated(this.state, context);
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
- await this.definition.handlers.onDestroyed(this.state);
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
  }
@@ -854,11 +947,11 @@ var init_app = __esm(() => {
854
947
  // src/discovery.ts
855
948
  init_logger();
856
949
  import { existsSync, readdirSync, readFileSync, watch } from "fs";
857
- import { join, basename, resolve } from "path";
950
+ import { join, basename, resolve, relative } from "path";
858
951
  async function discoverComponents(baseDir, options = {}) {
859
952
  const {
860
953
  patterns = ["single-file", "folder", "sibling", "index"],
861
- recursive = false,
954
+ recursive = true,
862
955
  debug = false
863
956
  } = options;
864
957
  const log3 = debug ? (...args) => frameworkLoggers.discovery.debug(...args) : () => {};
@@ -1090,6 +1183,7 @@ function watchComponents(baseDir, options = {}) {
1090
1183
  async function generateComponentsCode(baseDir, options = {}) {
1091
1184
  const components = await discoverComponents(baseDir, options);
1092
1185
  const resolvedDir = resolve(baseDir);
1186
+ const outputBase = options.outputDir ? resolve(options.outputDir) : resolvedDir;
1093
1187
  let code = `/**
1094
1188
  * Auto-generated component imports
1095
1189
  * Generated by @hypen-space/core discovery
@@ -1097,9 +1191,13 @@ async function generateComponentsCode(baseDir, options = {}) {
1097
1191
 
1098
1192
  `;
1099
1193
  for (const component of components) {
1100
- const relativePath = component.modulePath ? "./" + component.modulePath.replace(resolvedDir + "/", "").replace(/\.ts$/, ".js") : null;
1101
- if (relativePath) {
1102
- code += `import ${component.name}Module from "${relativePath}";
1194
+ let importPath = null;
1195
+ if (component.modulePath) {
1196
+ const rel = relative(outputBase, component.modulePath).replace(/\.ts$/, ".js");
1197
+ importPath = rel.startsWith(".") ? rel : "./" + rel;
1198
+ }
1199
+ if (importPath) {
1200
+ code += `import ${component.name}Module from "${importPath}";
1103
1201
  `;
1104
1202
  }
1105
1203
  }
@@ -1143,4 +1241,4 @@ export {
1143
1241
  discoverComponents
1144
1242
  };
1145
1243
 
1146
- //# debugId=DEF834620FC1CE5864756E2164756E21
1244
+ //# debugId=DA797A6F874A544F64756E2164756E21