@vobs/runtime 1.3.1 → 1.3.3

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/hmr.cjs CHANGED
@@ -33,7 +33,7 @@ __export(hmr_exports, {
33
33
  });
34
34
  module.exports = __toCommonJS(hmr_exports);
35
35
  var globalTarget = globalThis;
36
- var hmrGlobal = globalTarget.__VOBS_HMR__ ?? { modules: /* @__PURE__ */ new Map() };
36
+ var hmrGlobal = globalTarget.__VOBS_HMR__ ?? { modules: /* @__PURE__ */ new Map(), states: /* @__PURE__ */ new Map() };
37
37
  globalTarget.__VOBS_HMR__ = hmrGlobal;
38
38
  function resolveComponent(component, moduleId, exportName) {
39
39
  const module2 = getModule(moduleId);
@@ -97,12 +97,11 @@ function createHmrStateStore(moduleId) {
97
97
  };
98
98
  }
99
99
  __name(createHmrStateStore, "createHmrStateStore");
100
- function hmrStateRef(moduleId, key, create) {
101
- const state = getModule(moduleId).state;
102
- const registryKey = `#state:${key}`;
103
- if (state.has(registryKey)) return state.get(registryKey);
100
+ function hmrStateRef(key, create) {
101
+ const states = hmrGlobal.states;
102
+ if (states.has(key)) return states.get(key);
104
103
  const value = create();
105
- state.set(registryKey, value);
104
+ states.set(key, value);
106
105
  return value;
107
106
  }
108
107
  __name(hmrStateRef, "hmrStateRef");
package/dist/hmr.cjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/hmr.ts"],"sourcesContent":["import type { VobsNode } from './fragment'\n\nexport type HmrComponent<Props extends object = Record<string, unknown>> =\n (props: Props) => VobsNode\n\nexport interface HmrStateStore {\n get<T>(key: string, initial: T | (() => T)): T\n set<T>(key: string, value: T): void\n has(key: string): boolean\n delete(key: string): void\n clear(): void\n}\n\nexport interface HmrInstance {\n node: VobsNode\n parent: Node | null\n refresh(): void\n}\n\ninterface HmrModuleState {\n readonly components: Map<string, HmrComponent>\n readonly state: Map<string, unknown>\n readonly instances: Set<HmrInstance>\n}\n\ninterface HmrGlobal {\n modules: Map<string, HmrModuleState>\n}\n\nconst globalTarget = globalThis as typeof globalThis & { __VOBS_HMR__?: HmrGlobal }\nconst hmrGlobal = globalTarget.__VOBS_HMR__ ?? { modules: new Map<string, HmrModuleState>() }\nglobalTarget.__VOBS_HMR__ = hmrGlobal\n\nexport function resolveComponent<Props extends object>(\n component: HmrComponent<Props>,\n moduleId: string,\n exportName: string\n): HmrComponent<Props> {\n const module = getModule(moduleId)\n const existing = module.components.get(exportName)\n if (existing) return existing as HmrComponent<Props>\n\n const proxy = ((props: Props) => {\n const current = (proxy as HmrComponent<Props> & { current: HmrComponent<Props> }).current\n return current(props)\n }) as HmrComponent<Props> & { current: HmrComponent<Props> }\n proxy.current = component\n Object.defineProperties(proxy, {\n displayName: { configurable: true, value: component.name || exportName },\n hmrKey: { configurable: false, value: `${moduleId}:${exportName}` }\n })\n module.components.set(exportName, proxy as HmrComponent)\n return proxy\n}\n\nexport function updateHmrModule(_moduleId: string, nextModule: Record<string, unknown>): void {\n const modules = [...hmrGlobal.modules.values()]\n for (const module of modules) {\n let changed = false\n for (const [name, proxy] of module.components) {\n const next = nextModule[name]\n if (typeof next !== 'function') continue\n const hmrProxy = proxy as HmrComponent & { current: HmrComponent; displayName?: string }\n hmrProxy.current = next as HmrComponent\n Object.defineProperty(hmrProxy, 'displayName', { configurable: true, value: next.name || name })\n changed = true\n }\n if (!changed) continue\n for (const instance of module.instances) {\n try {\n instance.refresh()\n } catch {\n // HMR failures remain application errors on the next normal render.\n }\n }\n }\n}\n\nexport function disposeHmrModule(_moduleId: string): void {\n // State and component proxies intentionally survive module disposal.\n}\n\nexport function createHmrStateStore(moduleId: string): HmrStateStore {\n const state = getModule(moduleId).state\n return {\n get<T>(key: string, initial: T | (() => T)): T {\n if (!state.has(key)) state.set(key, typeof initial === 'function' ? (initial as () => T)() : initial)\n return state.get(key) as T\n },\n set<T>(key: string, value: T): void {\n state.set(key, value)\n },\n has: key => state.has(key),\n delete: key => { state.delete(key) },\n clear: () => { state.clear() }\n }\n}\n\n/**\n * 编译器为模块顶层 state() 声明生成的取值入口(`#state:` 前缀与手动 store 键隔离)。\n * 模块热更新重执行时复用既有信号实例:旧导入方持有的实例与新模块实例共享同一份状态,\n * 消除\"两份模块、两份状态\"导致的编辑不生效/页面半边失灵。\n */\nexport function hmrStateRef<T>(moduleId: string, key: string, create: () => T): T {\n const state = getModule(moduleId).state\n const registryKey = `#state:${key}`\n if (state.has(registryKey)) return state.get(registryKey) as T\n const value = create()\n state.set(registryKey, value)\n return value\n}\n\nexport function registerHmrInstance(moduleId: string, instance: HmrInstance): () => void {\n const instances = getModule(moduleId).instances\n instances.add(instance)\n return () => instances.delete(instance)\n}\n\nexport function markHmrInstanceMounted(node: VobsNode, parent: Node): void {\n const instance = hmrInstances.get(node as object)\n if (instance) instance.parent = parent\n}\n\nfunction getModule(moduleId: string): HmrModuleState {\n let module = hmrGlobal.modules.get(moduleId)\n if (!module) {\n module = { components: new Map(), state: new Map(), instances: new Set() }\n hmrGlobal.modules.set(moduleId, module)\n }\n return module\n}\n\nconst hmrInstances = new WeakMap<object, HmrInstance>()\n\nexport function associateHmrInstance(node: VobsNode, instance: HmrInstance): void {\n hmrInstances.set(node as object, instance)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA6BA,IAAM,eAAe;AACrB,IAAM,YAAY,aAAa,gBAAgB,EAAE,SAAS,oBAAI,IAA4B,EAAE;AAC5F,aAAa,eAAe;AAErB,SAAS,iBACd,WACA,UACA,YACqB;AACrB,QAAMA,UAAS,UAAU,QAAQ;AACjC,QAAM,WAAWA,QAAO,WAAW,IAAI,UAAU;AACjD,MAAI,SAAU,QAAO;AAErB,QAAM,QAAS,yBAAC,UAAiB;AAC/B,UAAM,UAAW,MAAiE;AAClF,WAAO,QAAQ,KAAK;AAAA,EACtB,IAHe;AAIf,QAAM,UAAU;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YAAM,OAAO,GAAG;AAAA,IAAE,GAA3B;AAAA,IACR,OAAO,6BAAM;AAAE,YAAM,MAAM;AAAA,IAAE,GAAtB;AAAA,EACT;AACF;AAdgB;AAqBT,SAAS,YAAe,UAAkB,KAAa,QAAoB;AAChF,QAAM,QAAQ,UAAU,QAAQ,EAAE;AAClC,QAAM,cAAc,UAAU,GAAG;AACjC,MAAI,MAAM,IAAI,WAAW,EAAG,QAAO,MAAM,IAAI,WAAW;AACxD,QAAM,QAAQ,OAAO;AACrB,QAAM,IAAI,aAAa,KAAK;AAC5B,SAAO;AACT;AAPgB;AAST,SAAS,oBAAoB,UAAkB,UAAmC;AACvF,QAAM,YAAY,UAAU,QAAQ,EAAE;AACtC,YAAU,IAAI,QAAQ;AACtB,SAAO,MAAM,UAAU,OAAO,QAAQ;AACxC;AAJgB;AAMT,SAAS,uBAAuB,MAAgB,QAAoB;AACzE,QAAM,WAAW,aAAa,IAAI,IAAc;AAChD,MAAI,SAAU,UAAS,SAAS;AAClC;AAHgB;AAKhB,SAAS,UAAU,UAAkC;AACnD,MAAIA,UAAS,UAAU,QAAQ,IAAI,QAAQ;AAC3C,MAAI,CAACA,SAAQ;AACX,IAAAA,UAAS,EAAE,YAAY,oBAAI,IAAI,GAAG,OAAO,oBAAI,IAAI,GAAG,WAAW,oBAAI,IAAI,EAAE;AACzE,cAAU,QAAQ,IAAI,UAAUA,OAAM;AAAA,EACxC;AACA,SAAOA;AACT;AAPS;AAST,IAAM,eAAe,oBAAI,QAA6B;AAE/C,SAAS,qBAAqB,MAAgB,UAA6B;AAChF,eAAa,IAAI,MAAgB,QAAQ;AAC3C;AAFgB;","names":["module"]}
1
+ {"version":3,"sources":["../src/hmr.ts"],"sourcesContent":["import type { VobsNode } from './fragment'\n\nexport type HmrComponent<Props extends object = Record<string, unknown>> =\n (props: Props) => VobsNode\n\nexport interface HmrStateStore {\n get<T>(key: string, initial: T | (() => T)): T\n set<T>(key: string, value: T): void\n has(key: string): boolean\n delete(key: string): void\n clear(): void\n}\n\nexport interface HmrInstance {\n node: VobsNode\n parent: Node | null\n refresh(): void\n}\n\ninterface HmrModuleState {\n readonly components: Map<string, HmrComponent>\n readonly state: Map<string, unknown>\n readonly instances: Set<HmrInstance>\n}\n\ninterface HmrGlobal {\n modules: Map<string, HmrModuleState>\n /** 编译器生成的 hmrStateRef 注册表:键为 `${moduleId}#${声明名}`,值跨模块重执行保活。 */\n states: Map<string, unknown>\n}\n\nconst globalTarget = globalThis as typeof globalThis & { __VOBS_HMR__?: HmrGlobal }\nconst hmrGlobal = globalTarget.__VOBS_HMR__ ?? { modules: new Map<string, HmrModuleState>(), states: new Map<string, unknown>() }\nglobalTarget.__VOBS_HMR__ = hmrGlobal\n\nexport function resolveComponent<Props extends object>(\n component: HmrComponent<Props>,\n moduleId: string,\n exportName: string\n): HmrComponent<Props> {\n const module = getModule(moduleId)\n const existing = module.components.get(exportName)\n if (existing) return existing as HmrComponent<Props>\n\n const proxy = ((props: Props) => {\n const current = (proxy as HmrComponent<Props> & { current: HmrComponent<Props> }).current\n return current(props)\n }) as HmrComponent<Props> & { current: HmrComponent<Props> }\n proxy.current = component\n Object.defineProperties(proxy, {\n displayName: { configurable: true, value: component.name || exportName },\n hmrKey: { configurable: false, value: `${moduleId}:${exportName}` }\n })\n module.components.set(exportName, proxy as HmrComponent)\n return proxy\n}\n\nexport function updateHmrModule(_moduleId: string, nextModule: Record<string, unknown>): void {\n const modules = [...hmrGlobal.modules.values()]\n for (const module of modules) {\n let changed = false\n for (const [name, proxy] of module.components) {\n const next = nextModule[name]\n if (typeof next !== 'function') continue\n const hmrProxy = proxy as HmrComponent & { current: HmrComponent; displayName?: string }\n hmrProxy.current = next as HmrComponent\n Object.defineProperty(hmrProxy, 'displayName', { configurable: true, value: next.name || name })\n changed = true\n }\n if (!changed) continue\n for (const instance of module.instances) {\n try {\n instance.refresh()\n } catch {\n // HMR failures remain application errors on the next normal render.\n }\n }\n }\n}\n\nexport function disposeHmrModule(_moduleId: string): void {\n // State and component proxies intentionally survive module disposal.\n}\n\nexport function createHmrStateStore(moduleId: string): HmrStateStore {\n const state = getModule(moduleId).state\n return {\n get<T>(key: string, initial: T | (() => T)): T {\n if (!state.has(key)) state.set(key, typeof initial === 'function' ? (initial as () => T)() : initial)\n return state.get(key) as T\n },\n set<T>(key: string, value: T): void {\n state.set(key, value)\n },\n has: key => state.has(key),\n delete: key => { state.delete(key) },\n clear: () => { state.clear() }\n }\n}\n\n/**\n * 编译器为模块顶层 state() 声明生成的取值入口。键为 `${moduleId}#${声明名}`,\n * 与手动 store 键空间隔离。模块热更新重执行时复用既有信号实例:旧导入方持有的\n * 实例与新模块实例共享同一份状态,消除\"两份模块、两份状态\"导致的编辑不生效/页面半边失灵。\n */\nexport function hmrStateRef<T>(key: string, create: () => T): T {\n const states = hmrGlobal.states\n if (states.has(key)) return states.get(key) as T\n const value = create()\n states.set(key, value)\n return value\n}\n\nexport function registerHmrInstance(moduleId: string, instance: HmrInstance): () => void {\n const instances = getModule(moduleId).instances\n instances.add(instance)\n return () => instances.delete(instance)\n}\n\nexport function markHmrInstanceMounted(node: VobsNode, parent: Node): void {\n const instance = hmrInstances.get(node as object)\n if (instance) instance.parent = parent\n}\n\nfunction getModule(moduleId: string): HmrModuleState {\n let module = hmrGlobal.modules.get(moduleId)\n if (!module) {\n module = { components: new Map(), state: new Map(), instances: new Set() }\n hmrGlobal.modules.set(moduleId, module)\n }\n return module\n}\n\nconst hmrInstances = new WeakMap<object, HmrInstance>()\n\nexport function associateHmrInstance(node: VobsNode, instance: HmrInstance): void {\n hmrInstances.set(node as object, instance)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA+BA,IAAM,eAAe;AACrB,IAAM,YAAY,aAAa,gBAAgB,EAAE,SAAS,oBAAI,IAA4B,GAAG,QAAQ,oBAAI,IAAqB,EAAE;AAChI,aAAa,eAA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}
package/dist/hmr.d.cts CHANGED
@@ -18,11 +18,11 @@ declare function updateHmrModule(_moduleId: string, nextModule: Record<string, u
18
18
  declare function disposeHmrModule(_moduleId: string): void;
19
19
  declare function createHmrStateStore(moduleId: string): HmrStateStore;
20
20
  /**
21
- * 编译器为模块顶层 state() 声明生成的取值入口(`#state:` 前缀与手动 store 键隔离)。
22
- * 模块热更新重执行时复用既有信号实例:旧导入方持有的实例与新模块实例共享同一份状态,
23
- * 消除"两份模块、两份状态"导致的编辑不生效/页面半边失灵。
21
+ * 编译器为模块顶层 state() 声明生成的取值入口。键为 `${moduleId}#${声明名}`,
22
+ * 与手动 store 键空间隔离。模块热更新重执行时复用既有信号实例:旧导入方持有的
23
+ * 实例与新模块实例共享同一份状态,消除"两份模块、两份状态"导致的编辑不生效/页面半边失灵。
24
24
  */
25
- declare function hmrStateRef<T>(moduleId: string, key: string, create: () => T): T;
25
+ declare function hmrStateRef<T>(key: string, create: () => T): T;
26
26
  declare function registerHmrInstance(moduleId: string, instance: HmrInstance): () => void;
27
27
  declare function markHmrInstanceMounted(node: VobsNode, parent: Node): void;
28
28
  declare function associateHmrInstance(node: VobsNode, instance: HmrInstance): void;
package/dist/hmr.d.ts CHANGED
@@ -18,11 +18,11 @@ declare function updateHmrModule(_moduleId: string, nextModule: Record<string, u
18
18
  declare function disposeHmrModule(_moduleId: string): void;
19
19
  declare function createHmrStateStore(moduleId: string): HmrStateStore;
20
20
  /**
21
- * 编译器为模块顶层 state() 声明生成的取值入口(`#state:` 前缀与手动 store 键隔离)。
22
- * 模块热更新重执行时复用既有信号实例:旧导入方持有的实例与新模块实例共享同一份状态,
23
- * 消除"两份模块、两份状态"导致的编辑不生效/页面半边失灵。
21
+ * 编译器为模块顶层 state() 声明生成的取值入口。键为 `${moduleId}#${声明名}`,
22
+ * 与手动 store 键空间隔离。模块热更新重执行时复用既有信号实例:旧导入方持有的
23
+ * 实例与新模块实例共享同一份状态,消除"两份模块、两份状态"导致的编辑不生效/页面半边失灵。
24
24
  */
25
- declare function hmrStateRef<T>(moduleId: string, key: string, create: () => T): T;
25
+ declare function hmrStateRef<T>(key: string, create: () => T): T;
26
26
  declare function registerHmrInstance(moduleId: string, instance: HmrInstance): () => void;
27
27
  declare function markHmrInstanceMounted(node: VobsNode, parent: Node): void;
28
28
  declare function associateHmrInstance(node: VobsNode, instance: HmrInstance): void;
package/dist/hmr.js CHANGED
@@ -3,7 +3,7 @@ var __name = (target, value) => __defProp(target, "name", { value, configurable:
3
3
 
4
4
  // packages/runtime/src/hmr.ts
5
5
  var globalTarget = globalThis;
6
- var hmrGlobal = globalTarget.__VOBS_HMR__ ?? { modules: /* @__PURE__ */ new Map() };
6
+ var hmrGlobal = globalTarget.__VOBS_HMR__ ?? { modules: /* @__PURE__ */ new Map(), states: /* @__PURE__ */ new Map() };
7
7
  globalTarget.__VOBS_HMR__ = hmrGlobal;
8
8
  function resolveComponent(component, moduleId, exportName) {
9
9
  const module = getModule(moduleId);
@@ -67,12 +67,11 @@ function createHmrStateStore(moduleId) {
67
67
  };
68
68
  }
69
69
  __name(createHmrStateStore, "createHmrStateStore");
70
- function hmrStateRef(moduleId, key, create) {
71
- const state = getModule(moduleId).state;
72
- const registryKey = `#state:${key}`;
73
- if (state.has(registryKey)) return state.get(registryKey);
70
+ function hmrStateRef(key, create) {
71
+ const states = hmrGlobal.states;
72
+ if (states.has(key)) return states.get(key);
74
73
  const value = create();
75
- state.set(registryKey, value);
74
+ states.set(key, value);
76
75
  return value;
77
76
  }
78
77
  __name(hmrStateRef, "hmrStateRef");
package/dist/hmr.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/hmr.ts"],"sourcesContent":["import type { VobsNode } from './fragment'\n\nexport type HmrComponent<Props extends object = Record<string, unknown>> =\n (props: Props) => VobsNode\n\nexport interface HmrStateStore {\n get<T>(key: string, initial: T | (() => T)): T\n set<T>(key: string, value: T): void\n has(key: string): boolean\n delete(key: string): void\n clear(): void\n}\n\nexport interface HmrInstance {\n node: VobsNode\n parent: Node | null\n refresh(): void\n}\n\ninterface HmrModuleState {\n readonly components: Map<string, HmrComponent>\n readonly state: Map<string, unknown>\n readonly instances: Set<HmrInstance>\n}\n\ninterface HmrGlobal {\n modules: Map<string, HmrModuleState>\n}\n\nconst globalTarget = globalThis as typeof globalThis & { __VOBS_HMR__?: HmrGlobal }\nconst hmrGlobal = globalTarget.__VOBS_HMR__ ?? { modules: new Map<string, HmrModuleState>() }\nglobalTarget.__VOBS_HMR__ = hmrGlobal\n\nexport function resolveComponent<Props extends object>(\n component: HmrComponent<Props>,\n moduleId: string,\n exportName: string\n): HmrComponent<Props> {\n const module = getModule(moduleId)\n const existing = module.components.get(exportName)\n if (existing) return existing as HmrComponent<Props>\n\n const proxy = ((props: Props) => {\n const current = (proxy as HmrComponent<Props> & { current: HmrComponent<Props> }).current\n return current(props)\n }) as HmrComponent<Props> & { current: HmrComponent<Props> }\n proxy.current = component\n Object.defineProperties(proxy, {\n displayName: { configurable: true, value: component.name || exportName },\n hmrKey: { configurable: false, value: `${moduleId}:${exportName}` }\n })\n module.components.set(exportName, proxy as HmrComponent)\n return proxy\n}\n\nexport function updateHmrModule(_moduleId: string, nextModule: Record<string, unknown>): void {\n const modules = [...hmrGlobal.modules.values()]\n for (const module of modules) {\n let changed = false\n for (const [name, proxy] of module.components) {\n const next = nextModule[name]\n if (typeof next !== 'function') continue\n const hmrProxy = proxy as HmrComponent & { current: HmrComponent; displayName?: string }\n hmrProxy.current = next as HmrComponent\n Object.defineProperty(hmrProxy, 'displayName', { configurable: true, value: next.name || name })\n changed = true\n }\n if (!changed) continue\n for (const instance of module.instances) {\n try {\n instance.refresh()\n } catch {\n // HMR failures remain application errors on the next normal render.\n }\n }\n }\n}\n\nexport function disposeHmrModule(_moduleId: string): void {\n // State and component proxies intentionally survive module disposal.\n}\n\nexport function createHmrStateStore(moduleId: string): HmrStateStore {\n const state = getModule(moduleId).state\n return {\n get<T>(key: string, initial: T | (() => T)): T {\n if (!state.has(key)) state.set(key, typeof initial === 'function' ? (initial as () => T)() : initial)\n return state.get(key) as T\n },\n set<T>(key: string, value: T): void {\n state.set(key, value)\n },\n has: key => state.has(key),\n delete: key => { state.delete(key) },\n clear: () => { state.clear() }\n }\n}\n\n/**\n * 编译器为模块顶层 state() 声明生成的取值入口(`#state:` 前缀与手动 store 键隔离)。\n * 模块热更新重执行时复用既有信号实例:旧导入方持有的实例与新模块实例共享同一份状态,\n * 消除\"两份模块、两份状态\"导致的编辑不生效/页面半边失灵。\n */\nexport function hmrStateRef<T>(moduleId: string, key: string, create: () => T): T {\n const state = getModule(moduleId).state\n const registryKey = `#state:${key}`\n if (state.has(registryKey)) return state.get(registryKey) as T\n const value = create()\n state.set(registryKey, value)\n return value\n}\n\nexport function registerHmrInstance(moduleId: string, instance: HmrInstance): () => void {\n const instances = getModule(moduleId).instances\n instances.add(instance)\n return () => instances.delete(instance)\n}\n\nexport function markHmrInstanceMounted(node: VobsNode, parent: Node): void {\n const instance = hmrInstances.get(node as object)\n if (instance) instance.parent = parent\n}\n\nfunction getModule(moduleId: string): HmrModuleState {\n let module = hmrGlobal.modules.get(moduleId)\n if (!module) {\n module = { components: new Map(), state: new Map(), instances: new Set() }\n hmrGlobal.modules.set(moduleId, module)\n }\n return module\n}\n\nconst hmrInstances = new WeakMap<object, HmrInstance>()\n\nexport function associateHmrInstance(node: VobsNode, instance: HmrInstance): void {\n hmrInstances.set(node as object, instance)\n}\n"],"mappings":";;;;AA6BA,IAAM,eAAe;AACrB,IAAM,YAAY,aAAa,gBAAgB,EAAE,SAAS,oBAAI,IAA4B,EAAE;AAC5F,aAAa,eAAe;AAErB,SAAS,iBACd,WACA,UACA,YACqB;AACrB,QAAM,SAAS,UAAU,QAAQ;AACjC,QAAM,WAAW,OAAO,WAAW,IAAI,UAAU;AACjD,MAAI,SAAU,QAAO;AAErB,QAAM,QAAS,yBAAC,UAAiB;AAC/B,UAAM,UAAW,MAAiE;AAClF,WAAO,QAAQ,KAAK;AAAA,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AAQ,gCAAO;AAAE,YAAM,OAAO,GAAG;AAAA,IAAE,GAA3B;AAAA,IACR,OAAO,6BAAM;AAAE,YAAM,MAAM;AAAA,IAAE,GAAtB;AAAA,EACT;AACF;AAdgB;AAqBT,SAAS,YAAe,UAAkB,KAAa,QAAoB;AAChF,QAAM,QAAQ,UAAU,QAAQ,EAAE;AAClC,QAAM,cAAc,UAAU,GAAG;AACjC,MAAI,MAAM,IAAI,WAAW,EAAG,QAAO,MAAM,IAAI,WAAW;AACxD,QAAM,QAAQ,OAAO;AACrB,QAAM,IAAI,aAAa,KAAK;AAC5B,SAAO;AACT;AAPgB;AAST,SAAS,oBAAoB,UAAkB,UAAmC;AACvF,QAAM,YAAY,UAAU,QAAQ,EAAE;AACtC,YAAU,IAAI,QAAQ;AACtB,SAAO,MAAM,UAAU,OAAO,QAAQ;AACxC;AAJgB;AAMT,SAAS,uBAAuB,MAAgB,QAAoB;AACzE,QAAM,WAAW,aAAa,IAAI,IAAc;AAChD,MAAI,SAAU,UAAS,SAAS;AAClC;AAHgB;AAKhB,SAAS,UAAU,UAAkC;AACnD,MAAI,SAAS,UAAU,QAAQ,IAAI,QAAQ;AAC3C,MAAI,CAAC,QAAQ;AACX,aAAS,EAAE,YAAY,oBAAI,IAAI,GAAG,OAAO,oBAAI,IAAI,GAAG,WAAW,oBAAI,IAAI,EAAE;AACzE,cAAU,QAAQ,IAAI,UAAU,MAAM;AAAA,EACxC;AACA,SAAO;AACT;AAPS;AAST,IAAM,eAAe,oBAAI,QAA6B;AAE/C,SAAS,qBAAqB,MAAgB,UAA6B;AAChF,eAAa,IAAI,MAAgB,QAAQ;AAC3C;AAFgB;","names":[]}
1
+ {"version":3,"sources":["../src/hmr.ts"],"sourcesContent":["import type { VobsNode } from './fragment'\n\nexport type HmrComponent<Props extends object = Record<string, unknown>> =\n (props: Props) => VobsNode\n\nexport interface HmrStateStore {\n get<T>(key: string, initial: T | (() => T)): T\n set<T>(key: string, value: T): void\n has(key: string): boolean\n delete(key: string): void\n clear(): void\n}\n\nexport interface HmrInstance {\n node: VobsNode\n parent: Node | null\n refresh(): void\n}\n\ninterface HmrModuleState {\n readonly components: Map<string, HmrComponent>\n readonly state: Map<string, unknown>\n readonly instances: Set<HmrInstance>\n}\n\ninterface HmrGlobal {\n modules: Map<string, HmrModuleState>\n /** 编译器生成的 hmrStateRef 注册表:键为 `${moduleId}#${声明名}`,值跨模块重执行保活。 */\n states: Map<string, unknown>\n}\n\nconst globalTarget = globalThis as typeof globalThis & { __VOBS_HMR__?: HmrGlobal }\nconst hmrGlobal = globalTarget.__VOBS_HMR__ ?? { modules: new Map<string, HmrModuleState>(), states: new Map<string, unknown>() }\nglobalTarget.__VOBS_HMR__ = hmrGlobal\n\nexport function resolveComponent<Props extends object>(\n component: HmrComponent<Props>,\n moduleId: string,\n exportName: string\n): HmrComponent<Props> {\n const module = getModule(moduleId)\n const existing = module.components.get(exportName)\n if (existing) return existing as HmrComponent<Props>\n\n const proxy = ((props: Props) => {\n const current = (proxy as HmrComponent<Props> & { current: HmrComponent<Props> }).current\n return current(props)\n }) as HmrComponent<Props> & { current: HmrComponent<Props> }\n proxy.current = component\n Object.defineProperties(proxy, {\n displayName: { configurable: true, value: component.name || exportName },\n hmrKey: { configurable: false, value: `${moduleId}:${exportName}` }\n })\n module.components.set(exportName, proxy as HmrComponent)\n return proxy\n}\n\nexport function updateHmrModule(_moduleId: string, nextModule: Record<string, unknown>): void {\n const modules = [...hmrGlobal.modules.values()]\n for (const module of modules) {\n let changed = false\n for (const [name, proxy] of module.components) {\n const next = nextModule[name]\n if (typeof next !== 'function') continue\n const hmrProxy = proxy as HmrComponent & { current: HmrComponent; displayName?: string }\n hmrProxy.current = next as HmrComponent\n Object.defineProperty(hmrProxy, 'displayName', { configurable: true, value: next.name || name })\n changed = true\n }\n if (!changed) continue\n for (const instance of module.instances) {\n try {\n instance.refresh()\n } catch {\n // HMR failures remain application errors on the next normal render.\n }\n }\n }\n}\n\nexport function disposeHmrModule(_moduleId: string): void {\n // State and component proxies intentionally survive module disposal.\n}\n\nexport function createHmrStateStore(moduleId: string): HmrStateStore {\n const state = getModule(moduleId).state\n return {\n get<T>(key: string, initial: T | (() => T)): T {\n if (!state.has(key)) state.set(key, typeof initial === 'function' ? (initial as () => T)() : initial)\n return state.get(key) as T\n },\n set<T>(key: string, value: T): void {\n state.set(key, value)\n },\n has: key => state.has(key),\n delete: key => { state.delete(key) },\n clear: () => { state.clear() }\n }\n}\n\n/**\n * 编译器为模块顶层 state() 声明生成的取值入口。键为 `${moduleId}#${声明名}`,\n * 与手动 store 键空间隔离。模块热更新重执行时复用既有信号实例:旧导入方持有的\n * 实例与新模块实例共享同一份状态,消除\"两份模块、两份状态\"导致的编辑不生效/页面半边失灵。\n */\nexport function hmrStateRef<T>(key: string, create: () => T): T {\n const states = hmrGlobal.states\n if (states.has(key)) return states.get(key) as T\n const value = create()\n states.set(key, value)\n return value\n}\n\nexport function registerHmrInstance(moduleId: string, instance: HmrInstance): () => void {\n const instances = getModule(moduleId).instances\n instances.add(instance)\n return () => instances.delete(instance)\n}\n\nexport function markHmrInstanceMounted(node: VobsNode, parent: Node): void {\n const instance = hmrInstances.get(node as object)\n if (instance) instance.parent = parent\n}\n\nfunction getModule(moduleId: string): HmrModuleState {\n let module = hmrGlobal.modules.get(moduleId)\n if (!module) {\n module = { components: new Map(), state: new Map(), instances: new Set() }\n hmrGlobal.modules.set(moduleId, module)\n }\n return module\n}\n\nconst hmrInstances = new WeakMap<object, HmrInstance>()\n\nexport function associateHmrInstance(node: VobsNode, instance: HmrInstance): void {\n hmrInstances.set(node as object, instance)\n}\n"],"mappings":";;;;AA+BA,IAAM,eAAe;AACrB,IAAM,YAAY,aAAa,gBAAgB,EAAE,SAAS,oBAAI,IAA4B,GAAG,QAAQ,oBAAI,IAAqB,EAAE;AAChI,aAAa,eAAe;AAErB,SAAS,iBACd,WACA,UACA,YACqB;AACrB,QAAM,SAAS,UAAU,QAAQ;AACjC,QAAM,WAAW,OA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tB;AAAA,IACA,IAAO,KAAa,OAAgB;AAClC,YAAM,IAAI,KAAK,KAAK;AAAA,IACtB;AAAA,IACA,KAAK,gCAAO,MAAM,IAAI,GAAG,GAApB;AAAA,IACL,QAAQ,gCAAO;AAAE,YAAM,OAAO,GAAG;AAAA,IAAE,GAA3B;AAAA,IACR,OAAO,6BAAM;AAAE,YAAM,MAAM;AAAA,IAAE,GAAtB;AAAA,EACT;AACF;AAdgB;AAqBT,SAAS,YAAe,KAAa,QAAoB;AAC9D,QAAM,SAAS,UAAU;AACzB,MAAI,OAAO,IAAI,GAAG,EAAG,QAAO,OAAO,IAAI,GAAG;AAC1C,QAAM,QAAQ,OAAO;AACrB,SAAO,IAAI,KAAK,KAAK;AACrB,SAAO;AACT;AANgB;AAQT,SAAS,oBAAoB,UAAkB,UAAmC;AACvF,QAAM,YAAY,UAAU,QAAQ,EAAE;AACtC,YAAU,IAAI,QAAQ;AACtB,SAAO,MAAM,UAAU,OAAO,QAAQ;AACxC;AAJgB;AAMT,SAAS,uBAAuB,MAAgB,QAAoB;AACzE,QAAM,WAAW,aAAa,IAAI,IAAc;AAChD,MAAI,SAAU,UAAS,SAAS;AAClC;AAHgB;AAKhB,SAAS,UAAU,UAAkC;AACnD,MAAI,SAAS,UAAU,QAAQ,IAAI,QAAQ;AAC3C,MAAI,CAAC,QAAQ;AACX,aAAS,EAAE,YAAY,oBAAI,IAAI,GAAG,OAAO,oBAAI,IAAI,GAAG,WAAW,oBAAI,IAAI,EAAE;AACzE,cAAU,QAAQ,IAAI,UAAU,MAAM;AAAA,EACxC;AACA,SAAO;AACT;AAPS;AAST,IAAM,eAAe,oBAAI,QAA6B;AAE/C,SAAS,qBAAqB,MAAgB,UAA6B;AAChF,eAAa,IAAI,MAAgB,QAAQ;AAC3C;AAFgB;","names":[]}
package/dist/index.cjs CHANGED
@@ -169,7 +169,7 @@ var import_reactivity2 = require("@vobs/reactivity");
169
169
 
170
170
  // packages/runtime/src/hmr.ts
171
171
  var globalTarget = globalThis;
172
- var hmrGlobal = globalTarget.__VOBS_HMR__ ?? { modules: /* @__PURE__ */ new Map() };
172
+ var hmrGlobal = globalTarget.__VOBS_HMR__ ?? { modules: /* @__PURE__ */ new Map(), states: /* @__PURE__ */ new Map() };
173
173
  globalTarget.__VOBS_HMR__ = hmrGlobal;
174
174
  function resolveComponent(component, moduleId, exportName) {
175
175
  const module2 = getModule(moduleId);
@@ -233,12 +233,11 @@ function createHmrStateStore(moduleId) {
233
233
  };
234
234
  }
235
235
  __name(createHmrStateStore, "createHmrStateStore");
236
- function hmrStateRef(moduleId, key, create) {
237
- const state4 = getModule(moduleId).state;
238
- const registryKey = `#state:${key}`;
239
- if (state4.has(registryKey)) return state4.get(registryKey);
236
+ function hmrStateRef(key, create) {
237
+ const states = hmrGlobal.states;
238
+ if (states.has(key)) return states.get(key);
240
239
  const value = create();
241
- state4.set(registryKey, value);
240
+ states.set(key, value);
242
241
  return value;
243
242
  }
244
243
  __name(hmrStateRef, "hmrStateRef");