This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState } from "react"; | |
import { wait } from "./wait"; | |
export function FunctionalUpdates() { | |
const [counter, setCounter] = useState(0); | |
function handleIncrement() { | |
setCounter(counter + 1); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState } from "react"; | |
import { wait } from "./wait"; | |
export function FunctionalUpdates() { | |
const [counter, setCounter] = useState(0); | |
function handleIncrement() { | |
setCounter(counter + 1); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState } from "react"; | |
export function FunctionalUpdates() { | |
const [counter, setCounter] = useState(0); | |
function handleIncrement() { | |
setCounter(counter + 1); | |
} | |
return ( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useCallback, useState } from "react"; | |
import { wait } from "./src/FunctionalUpdates/wait"; | |
export function FunctionalUpdates() { | |
const [counter, setCounter] = useState(0); | |
function handleIncrement() { | |
setCounter(counter + 1); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function wait({ miliseconds }) { | |
return new Promise((resolve) => setTimeout(resolve, miliseconds)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const updatePhotos = useCallback( | |
(itemsUpdates) => { | |
const { toReset, toUpdate } = getItemsToResetAndUpdate( | |
itemsUpdates, | |
photos | |
); | |
setPendingUpdates( | |
update(pendingUpdates, { | |
$unset: toReset, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useCallback, useMemo, useState } from "react"; | |
import { useDebouncedCallback } from "use-debounce/lib"; | |
export const DEBOUNCED_BATCH_TIMEOUT = 500; | |
function hasPendingUpdates(batchUpdates) { | |
return Object.keys(batchUpdates).length > 0; | |
} | |
export function usePhotos({ photos: initialPhotos = [], onUpdate }) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function updatePhotos(itemsUpdates) { | |
const { toReset, toUpdate } = getItemsToResetAndUpdate(itemsUpdates, photos); | |
setPendingUpdates(toReset, toUpdate); | |
performUpdates(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function PhotosList() { | |
const onUpdate = useCallback(() => { | |
return new Promise((resolve) => | |
setTimeout(resolve, getRandomBetween(1500, 2000)) | |
); | |
}, []); | |
const { photos, updatePhotos } = usePhotos({ | |
photos: DEFAULT_PHOTOS, | |
onUpdate, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function isUpdateNeeded(original, itemUpdate) { | |
const fieldsToOmit = [LOCKED_FLAG_KEY]; | |
const keysToCompare = Object.keys(itemUpdate).filter( | |
(key) => ![...fieldsToOmit, "id"].includes(key) | |
); | |
return !isEqual( | |
pick(original, keysToCompare), | |
pick(itemUpdate, keysToCompare) | |
); |
NewerOlder