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

suggestion: use immer in updateSnapshot #11377

Closed
Closed
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
11 changes: 11 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
"glob": "8.1.0",
"graphql": "16.8.1",
"graphql-ws": "5.14.2",
"immer": "^10.0.3",
"jest": "29.7.0",
"jest-environment-jsdom": "29.7.0",
"jest-junit": "16.0.0",
Expand Down
20 changes: 10 additions & 10 deletions src/react/hooks/__tests__/useBackgroundQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -434,9 +434,9 @@ function renderPaginatedIntegrationTest({
}

function SuspenseFallback() {
ProfiledApp.setSnapshot(({ suspenseCount }) => ({
suspenseCount: suspenseCount + 1,
}));
ProfiledApp.updateSnapshot((snapshot) => {
snapshot.suspenseCount++;
});
Comment on lines +437 to +439
Copy link
Member Author

@phryneas phryneas Nov 21, 2023

Choose a reason for hiding this comment

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

The fact that this is safely updatable is highlighted through the types here:
image

return <div>loading</div>;
}

Expand All @@ -449,9 +449,9 @@ function renderPaginatedIntegrationTest({
}) {
const { data, error } = useReadQuery(queryRef);
// count renders in the child component
ProfiledApp.setSnapshot(({ count }) => ({
count: count + 1,
}));
ProfiledApp.updateSnapshot((snapshot) => {
snapshot.count++;
});
return (
<div>
{error ? <div>{error.message}</div> : null}
Expand Down Expand Up @@ -502,10 +502,10 @@ function renderPaginatedIntegrationTest({
<ErrorBoundary
fallback={<div>Error</div>}
onError={(error) => {
ProfiledApp.setSnapshot(({ errorCount, errors }) => ({
errorCount: errorCount + 1,
errors: errors.concat(error),
}));
ProfiledApp.updateSnapshot((snapshot) => {
snapshot.errorCount++;
snapshot.errors.push(error);
});
}}
>
<Suspense fallback={<SuspenseFallback />}>
Expand Down
37 changes: 8 additions & 29 deletions src/testing/internal/profile/profile.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as React from "react";

import type { Draft } from "immer";
import { produce } from "immer";
import { TextEncoder, TextDecoder } from "util";

global.TextEncoder ??= TextEncoder;
Expand Down Expand Up @@ -27,21 +28,10 @@ export interface ProfiledComponent<Props, Snapshot>

interface UpdateSnapshot<Snapshot> {
(newSnapshot: Snapshot): void;
(updateSnapshot: (lastSnapshot: Readonly<Snapshot>) => Snapshot): void;
}

interface SetSnapshot<Snapshot> {
(partialSnapshot: Partial<Snapshot>): void;
(
updatePartialSnapshot: (
lastSnapshot: Readonly<Snapshot>
) => Partial<Snapshot>
): void;
(updateSnapshot: (draft: Draft<Snapshot>) => Snapshot | void): void;
}

interface ProfiledComponentOnlyFields<Props, Snapshot> {
// Allows for partial updating of the snapshot by shallow merging the results
setSnapshot: SetSnapshot<Snapshot>;
// Performs a full replacement of the snapshot
updateSnapshot: UpdateSnapshot<Snapshot>;
}
Expand Down Expand Up @@ -112,26 +102,16 @@ export function profile<
"Cannot use a function to update the snapshot if no initial snapshot was provided."
);
}
snapshotRef.current = snap(
typeof snapshotRef.current === "object"
? // "cheap best effort" to prevent accidental mutation of the last snapshot
{ ...snapshotRef.current! }
: snapshotRef.current!
snapshotRef.current = produce<Snapshot>(
snapshotRef.current!,
snap as any
);
} else {
snapshotRef.current = snap;
// move the snapshot through `produce` to autofreeze it
snapshotRef.current = produce<Snapshot>(snap, (x) => x);
}
};

const setSnapshot: SetSnapshot<Snapshot> = (partialSnapshot) => {
Copy link
Member

@jerelmiller jerelmiller Nov 21, 2023

Choose a reason for hiding this comment

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

See #11376 (comment)

tl;dr; I'd still like to retain some way of doing a partial update without the need for a callback function to reduce noise for these kinds of updates. Immer may still make sense for this snapshot stuff, so I'm not opposed to introducing it. I just don't want to fully remove this partial update functionality.

Perhaps a different name for this function makes sense?

updateSnapshot((snapshot) => ({
...snapshot,
...(typeof partialSnapshot === "function"
? partialSnapshot(snapshot)
: partialSnapshot),
}));
};

const profilerOnRender: React.ProfilerOnRenderCallback = (
id,
phase,
Expand Down Expand Up @@ -191,7 +171,6 @@ export function profile<
),
{
updateSnapshot,
setSnapshot,
} satisfies ProfiledComponentOnlyFields<Props, Snapshot>,
{
renders: new Array<
Expand Down