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

Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
e969116
fix error with hot module replacement
PatrykWalach Nov 12, 2024
25ba84c
update tests
PatrykWalach Nov 12, 2024
7d4628d
fix cleanup
PatrykWalach Nov 14, 2024
1ddc73b
don't use stale itemCleanupPairRef
PatrykWalach Nov 14, 2024
07de136
Rename TextSource -> RelativeTextSource and SourceFileName to Relativ…
rbalicki2 Jan 3, 2025
760b2c1
rename more structs to include Embedded in their name
rbalicki2 Jan 3, 2025
1169c86
Add AbsoluteEmbeddedLocation wrapper.
rbalicki2 Jan 3, 2025
bdd06db
add comment for guards added in `useEffects`
PatrykWalach Jan 3, 2025
e1a897d
fix error with hot module replacement
PatrykWalach Nov 12, 2024
ad1282a
update tests
PatrykWalach Nov 12, 2024
5bc585f
fix cleanup
PatrykWalach Nov 14, 2024
ef89d4d
don't use stale itemCleanupPairRef
PatrykWalach Nov 14, 2024
c8d6a76
add comment for guards added in `useEffects`
PatrykWalach Jan 3, 2025
f8e6e1b
Merge branch 'bandaid-error-on-hot-module-replacement' of github.com:…
PatrykWalach Jan 3, 2025
b18976d
run cargo fmt
rbalicki2 Jan 3, 2025
db10124
AbsoluteEmbeddedLocation doesnt need to implement Copy
rbalicki2 Jan 4, 2025
8a2ecd0
Create AbsoluteLocation struct
rbalicki2 Jan 4, 2025
b9ec7bd
std::fmt::Display the directive name directly, not the WithEmbededRel…
rbalicki2 Jan 4, 2025
0a63b51
remove unneeded impls
rbalicki2 Jan 4, 2025
7970dd3
Merge branch 'main' into bandaid-error-on-hot-module-replacement
PatrykWalach Jan 4, 2025
fbd0172
Merge branch 'main' into bandaid-error-on-hot-module-replacement
PatrykWalach Jan 6, 2025
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) {
rbalicki2 marked this conversation as resolved.
Show resolved Hide resolved
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;
// capture last set pair in a variable
const current = itemCleanupPairRef.current;
return () => {
// current is a stale variable
current?.[1]();
PatrykWalach marked this conversation as resolved.
Show resolved Hide resolved
};
},
[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
14 changes: 10 additions & 4 deletions libs/isograph-react-disposable-state/src/useLazyDisposableState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,24 @@ 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;
// capture last set pair in a variable
const current = itemCleanupPairRef.current;
return () => {
const cleanupFn = itemCleanupPairRef.current?.[1];
// current is a stale variable
const cleanupFn = current?.[1];
// TODO confirm useEffect is called in order.
if (cleanupFn == null) {
throw new Error(
Expand All @@ -43,7 +49,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