-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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; | ||
|
@@ -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>; | ||
} | ||
|
@@ -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) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
@@ -191,7 +171,6 @@ export function profile< | |
), | ||
{ | ||
updateSnapshot, | ||
setSnapshot, | ||
} satisfies ProfiledComponentOnlyFields<Props, Snapshot>, | ||
{ | ||
renders: new Array< | ||
|
There was a problem hiding this comment.
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: