Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/silent-bottles-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@unisonjs/core': patch
'@unisonjs/vue': patch
---

Fix fast refresh on children components
14 changes: 7 additions & 7 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
14 changes: 8 additions & 6 deletions packages/core/src/management.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export type SetupComponent<T extends Record<string, any>> = (props: ShallowReact
* @param name - component name
*/
export function $unison<T extends Record<string, any>>(fn: SetupComponent<T>, name?: string) {
if (!isFastRefresh() && typeof window !== "undefined") {
if (!isFastRefresh() && typeof window !== 'undefined') {
window.__UNISON_REFRESH__ = { root: null };
}
const component = React.forwardRef<T['ref'], T>((props, ref) => {
Expand All @@ -62,13 +62,15 @@ export function $unison<T extends Record<string, any>>(fn: SetupComponent<T>, 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) {
Expand Down
9 changes: 5 additions & 4 deletions packages/vue/src/jsx-runtime.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
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';

type Callback = () => React.ReactNode;


class ElementCache implements UnisonPlugin {
onInstanceFastRefresh(instance: ComponentInternalInstance): void {}
#cache = new Map<Callback, React.ReactNode | ComputedRefImpl<React.ReactNode>>();

/**
* Retrieves an element from the cache by its key.
* @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);
}

Expand All @@ -39,8 +40,8 @@ export function rsx(callback: Callback) {
if (!cache) return callback();

let element = cache.getElement(callback);
if (!element) {
const ref: ComputedRefImpl<React.ReactNode> = computed(callback);
if (!element || isFastRefresh()) {
const ref: ComputedRefImpl<React.ReactNode> = computed(callback) as unknown as ComputedRefImpl<React.ReactNode>;
const result = ref.value;

if (ref.dep.subs) {
Expand Down