]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
wip: vapor hmr reload
authorEvan You <evan@vuejs.org>
Sun, 8 Dec 2024 15:37:40 +0000 (23:37 +0800)
committerEvan You <evan@vuejs.org>
Sun, 8 Dec 2024 15:37:40 +0000 (23:37 +0800)
packages/runtime-core/src/component.ts
packages/runtime-core/src/componentCurrentInstance.ts
packages/runtime-core/src/hmr.ts
packages/runtime-vapor/src/component.ts
packages/runtime-vapor/src/hmr.ts

index 8a8129ffd38e141b70b8893fcdc065cb33ed5967..58b1bd6428f38a48059616c6158313245040e1c3 100644 (file)
@@ -491,7 +491,7 @@ export interface GenericComponentInstance {
   /**
    * @internal vapor only
    */
-  hmrReload?: () => void
+  hmrReload?: (newComp: any) => void
 }
 
 /**
index e0322e0c90dc48435abc6f1c7ccc6c9d74b7047e..c091b9c693dbb437d4e2e73a85872c533966566f 100644 (file)
@@ -96,7 +96,7 @@ export const unsetCurrentInstance = (): void => {
  */
 export const simpleSetCurrentInstance = (
   i: GenericComponentInstance | null,
-  unset?: GenericComponentInstance,
+  unset?: GenericComponentInstance | null,
 ): void => {
   currentInstance = i
   if (unset) {
index b2c52ab3effede57bf893a0223ff6a19cf6dcbae..42f671e8167768b9442212a450b87170e717c9aa 100644 (file)
@@ -119,7 +119,7 @@ function reload(id: string, newComp: HMRComponent): void {
 
   if (newComp.vapor) {
     for (const instance of instances) {
-      instance.hmrReload!()
+      instance.hmrReload!(newComp)
     }
   } else {
     for (const instance of instances as ComponentInternalInstance[]) {
index 74c2fa2d3b022b04bafaae03af3a022d7ea43454..7334bbecfa547d2517cffcf2b7caf84dbcae49fb 100644 (file)
@@ -150,6 +150,7 @@ export function createComponent(
       // HMR
       if (component.__hmrId) {
         registerHMR(instance)
+        instance.isSingleRoot = isSingleRoot
         instance.hmrRerender = hmrRerender.bind(null, instance)
         instance.hmrReload = hmrReload.bind(null, instance)
       }
@@ -260,9 +261,10 @@ export class VaporComponentInstance implements GenericComponentInstance {
   setupState?: Record<string, any>
   devtoolsRawSetupState?: any
   hmrRerender?: () => void
-  hmrReload?: () => void
+  hmrReload?: (newComp: VaporComponent) => void
   propsOptions?: NormalizedPropsOptions
   emitsOptions?: ObjectEmitsOptions | null
+  isSingleRoot?: boolean
 
   constructor(
     comp: VaporComponent,
index 5e9616b39ca9d4b4bd91e66a97574a5dc4d50f56..f01b337bdeeef1d3dff4e7149e7a56dae9573ded 100644 (file)
@@ -5,7 +5,14 @@ import {
   simpleSetCurrentInstance,
 } from '@vue/runtime-core'
 import { normalizeBlock } from './block'
-import { type VaporComponentInstance, devRender } from './component'
+import {
+  type VaporComponent,
+  type VaporComponentInstance,
+  createComponent,
+  devRender,
+  mountComponent,
+  unmountComponent,
+} from './component'
 import { insert, remove } from './dom/node'
 
 export function hmrRerender(instance: VaporComponentInstance): void {
@@ -22,7 +29,22 @@ export function hmrRerender(instance: VaporComponentInstance): void {
   insert(instance.block, parent, anchor)
 }
 
-export function hmrReload(instance: VaporComponentInstance): void {
-  // in parent block, find the corresponding block of this instance
-  // create new instance, replace
+export function hmrReload(
+  instance: VaporComponentInstance,
+  newComp: VaporComponent,
+): void {
+  const normalized = normalizeBlock(instance.block)
+  const parent = normalized[0].parentNode!
+  const anchor = normalized[normalized.length - 1].nextSibling
+  unmountComponent(instance, parent)
+  const prev = currentInstance
+  simpleSetCurrentInstance(instance.parent)
+  const newInstance = createComponent(
+    newComp,
+    instance.rawProps,
+    instance.rawSlots,
+    instance.isSingleRoot,
+  )
+  simpleSetCurrentInstance(prev, instance.parent)
+  mountComponent(newInstance, parent, anchor)
 }