diff --git a/.changeset/silent-bottles-beg.md b/.changeset/silent-bottles-beg.md new file mode 100644 index 0000000..f5e7bfe --- /dev/null +++ b/.changeset/silent-bottles-beg.md @@ -0,0 +1,6 @@ +--- +'@unisonjs/core': patch +'@unisonjs/vue': patch +--- + +Fix fast refresh on children components diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index af1c786..b21c8b1 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -1,13 +1,13 @@ export { $unison, createReactHook, usePlugin, type SetupComponent } from './management'; -export { type EventType } from './context'; +export { type EventType, isFastRefresh } from './context'; export { useUnison } from './use-unison'; -export { - HookManager, - toUnisonHook, - BaseSignal, - type UnisonHookOptions, - type HookOptions +export { + HookManager, + toUnisonHook, + BaseSignal, + type UnisonHookOptions, + type HookOptions } from './plugins/hook-manager/index'; export type * from './plugins/index'; diff --git a/packages/core/src/management.ts b/packages/core/src/management.ts index 3a77a27..ed7a9d7 100644 --- a/packages/core/src/management.ts +++ b/packages/core/src/management.ts @@ -42,7 +42,7 @@ export type SetupComponent> = (props: ShallowReact * @param name - component name */ export function $unison>(fn: SetupComponent, name?: string) { - if (!isFastRefresh() && typeof window !== "undefined") { + if (!isFastRefresh() && typeof window !== 'undefined') { window.__UNISON_REFRESH__ = { root: null }; } const component = React.forwardRef((props, ref) => { @@ -62,13 +62,15 @@ export function $unison>(fn: SetupComponent, na } instance.runEffects(); - useEffect(unset); + useEffect(() => { + unset(); + // reset if end of fast refresh + if (isFastRefresh() && window.__UNISON_REFRESH__.root === instance) { + window.__UNISON_REFRESH__ = undefined; + } + }); const result = instance.render(); - if (isFastRefresh() && window.__UNISON_REFRESH__.root === instance) { - window.__UNISON_REFRESH__ = undefined; - } - return result; }); if (name) { diff --git a/packages/vue/src/jsx-runtime.ts b/packages/vue/src/jsx-runtime.ts index ef350da..d275dd5 100644 --- a/packages/vue/src/jsx-runtime.ts +++ b/packages/vue/src/jsx-runtime.ts @@ -1,4 +1,4 @@ -import { usePlugin, getCurrentInstance, type UnisonPlugin, type ComponentInternalInstance } from '@unisonjs/core'; +import { isFastRefresh, usePlugin, getCurrentInstance, type UnisonPlugin, type ComponentInternalInstance } from '@unisonjs/core'; import { unref } from './index'; import { computed, type ComputedRefImpl } from './reactivity/computed'; @@ -6,6 +6,7 @@ type Callback = () => React.ReactNode; class ElementCache implements UnisonPlugin { + onInstanceFastRefresh(instance: ComponentInternalInstance): void {} #cache = new Map>(); /** @@ -13,7 +14,7 @@ class ElementCache implements UnisonPlugin { * @param key - The key associated with the element. * @returns The cached element or undefined if not found. */ - getElement(key) { + getElement(key: Callback) { return this.#cache.get(key); } @@ -39,8 +40,8 @@ export function rsx(callback: Callback) { if (!cache) return callback(); let element = cache.getElement(callback); - if (!element) { - const ref: ComputedRefImpl = computed(callback); + if (!element || isFastRefresh()) { + const ref: ComputedRefImpl = computed(callback) as unknown as ComputedRefImpl; const result = ref.value; if (ref.dep.subs) {