Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix error with hot module replacement #264

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions libs/isograph-react-disposable-state/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
},
"devDependencies": {
"@types/react": "18.3.1",
"@types/react-test-renderer": "^18.3.0",
"react-test-renderer": "^18.2.0",
"typescript": "5.6.3"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { describe, test, vi, expect, assert } from 'vitest';
import { ParentCache } from './ParentCache';
import { ItemCleanupPair } from '@isograph/disposable-types';
import { useCachedResponsivePrecommitValue } from './useCachedResponsivePrecommitValue';
import React from 'react';
import React, { StrictMode, type ReactElement } from 'react';
import { create } from 'react-test-renderer';
import { CacheItem, CacheItemState } from './CacheItem';

Expand Down Expand Up @@ -51,9 +51,10 @@ function promiseAndResolver() {

// The fact that sometimes we need to render in concurrent mode and sometimes
// not is a bit worrisome.
async function awaitableCreate(Component, isConcurrent) {
async function awaitableCreate(Component: ReactElement, isConcurrent) {
const element = create(
Component,
<StrictMode>{Component}</StrictMode>,
// @ts-expect-error
isConcurrent ? { unstable_isConcurrent: true } : undefined,
);
await shortPromise();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ export function useCachedResponsivePrecommitValue<T>(
const lastCommittedParentCache = useRef<ParentCache<T> | null>(null);

useEffect(() => {
if (lastCommittedParentCache.current === parentCache) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot 2024-11-13 at 6 10 17 PM
I now see this, btw... I'll investigate what is going on.

My suspicion is that the component cache is caching the old component. I haven't looked into this.

I think in terms of strict mode, we're still likely incompatible. Basically, useLazyReference will uncommit, dispose, and re-commit, which does not re-issue the query.

I am philosophically opposed to that — queries aren't always re-issuable! But we can probably warn loudly and reissue queries. Just sucks if you're lazily doing a mutation, which is possible :-P

Once #252 lands, there probably won't be a huge need for lazy loaded queries though

return;
}

lastCommittedParentCache.current = parentCache;
// On commit, cacheItem may be disposed, because during the render phase,
// we only temporarily retained the item, and the temporary retain could have
Expand Down
24 changes: 16 additions & 8 deletions libs/isograph-react-disposable-state/src/useDisposableState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export function useDisposableState<T = never>(
const preCommitItem = useCachedResponsivePrecommitValue(
parentCache,
(pair) => {
itemCleanupPairRef.current?.[1]();
itemCleanupPairRef.current = pair;
},
);
Expand All @@ -47,14 +46,23 @@ export function useDisposableState<T = never>(
[stateFromDisposableStateHook],
);

useEffect(function cleanupItemCleanupPairRefIfSetStateNotCalled() {
return () => {
if (itemCleanupPairRef.current !== null) {
itemCleanupPairRef.current[1]();
itemCleanupPairRef.current = null;
const lastCommittedParentCache = useRef<ParentCache<T> | null>(null);

useEffect(
function cleanupItemCleanupPairRefIfSetStateNotCalled() {
if (lastCommittedParentCache.current === parentCache) {
return;
}
};
}, []);
lastCommittedParentCache.current = parentCache;

return () => {
if (itemCleanupPairRef.current !== null) {
itemCleanupPairRef.current[1]();
}
};
},
[parentCache],
);

// Safety: we can be in one of three states. Pre-commit, in which case
// preCommitItem is assigned, post-commit but before setState has been
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ItemCleanupPair } from '@isograph/disposable-types';
import React, { useEffect, useState } from 'react';
import React, { StrictMode, useEffect, useState } from 'react';
import { create } from 'react-test-renderer';
import { describe, expect, test, vi } from 'vitest';
import { ParentCache } from './ParentCache';
Expand Down Expand Up @@ -55,7 +55,12 @@ describe('useLazyDisposableState', async () => {
return null;
}

const root = create(<TestComponent />, { unstable_isConcurrent: true });
const root = create(
<StrictMode>
<TestComponent />
</StrictMode>,
{ unstable_isConcurrent: true },
);
await committed.promise;
expect(cache1.disposeItem).toHaveBeenCalled();
expect(cache1.cache.factory).toHaveBeenCalledOnce();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,20 @@ export function useLazyDisposableState<T>(
state: T;
} {
const itemCleanupPairRef = useRef<ItemCleanupPair<T> | null>(null);

const preCommitItem = useCachedResponsivePrecommitValue(
parentCache,
(pair) => {
itemCleanupPairRef.current?.[1]();
itemCleanupPairRef.current = pair;
},
);

const lastCommittedParentCache = useRef<ParentCache<T> | null>(null);
useEffect(() => {
if (lastCommittedParentCache.current === parentCache) {
return;
}
lastCommittedParentCache.current = parentCache;

return () => {
const cleanupFn = itemCleanupPairRef.current?.[1];
// TODO confirm useEffect is called in order.
Expand All @@ -43,7 +47,7 @@ export function useLazyDisposableState<T>(
}
return cleanupFn();
};
}, []);
}, [parentCache]);

const returnedItem = preCommitItem?.state ?? itemCleanupPairRef.current?.[0];

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, test, vi, expect } from 'vitest';
import React from 'react';
import React, { StrictMode } from 'react';
import { create } from 'react-test-renderer';
import {
useUpdatableDisposableState,
Expand Down Expand Up @@ -45,7 +45,8 @@ function promiseAndResolver() {
// not is a bit worrisome.
async function awaitableCreate(Component, isConcurrent) {
const element = create(
Component,
<StrictMode>{Component}</StrictMode>,

isConcurrent ? { unstable_isConcurrent: true } : undefined,
);
await shortPromise();
Expand Down
16 changes: 13 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading