Skip to content

Commit

Permalink
Test better equal checks
Browse files Browse the repository at this point in the history
  • Loading branch information
cheeaun committed Jan 6, 2024
1 parent 1574be2 commit 16e2ac9
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
18 changes: 13 additions & 5 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"dayjs": "~1.11.10",
"dayjs-twitter": "~0.5.0",
"fast-blurhash": "~1.1.2",
"fast-deep-equal": "~3.1.3",
"fast-equals": "~5.0.1",
"idb-keyval": "~6.2.1",
"just-debounce-it": "~3.2.0",
"lz-string": "~1.5.0",
Expand Down
7 changes: 5 additions & 2 deletions src/components/compose.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import './compose.css';

import '@github/text-expander-element';
import { MenuItem } from '@szhsin/react-menu';
import equal from 'fast-deep-equal';
import { deepEqual } from 'fast-equals';
import { forwardRef } from 'preact/compat';
import { useEffect, useMemo, useRef, useState } from 'preact/hooks';
import { useHotkeys } from 'react-hotkeys-hook';
Expand Down Expand Up @@ -502,7 +502,10 @@ function Compose({
mediaAttachments,
},
};
if (!equal(backgroundDraft, prevBackgroundDraft.current) && !canClose()) {
if (
!deepEqual(backgroundDraft, prevBackgroundDraft.current) &&
!canClose()
) {
console.debug('not equal', backgroundDraft, prevBackgroundDraft.current);
db.drafts
.set(key, {
Expand Down
11 changes: 10 additions & 1 deletion src/components/status.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
MenuItem,
} from '@szhsin/react-menu';
import { decodeBlurHash, getBlurHashAverageColor } from 'fast-blurhash';
import { shallowEqual } from 'fast-equals';
import { memo } from 'preact/compat';
import {
useCallback,
Expand Down Expand Up @@ -2460,4 +2461,12 @@ const QuoteStatuses = memo(({ id, instance, level = 0 }) => {
});
});

export default memo(Status);
export default memo(Status, (oldProps, newProps) => {
// Shallow equal all props except 'status'
// This will be pure static until status ID changes
const { status, ...restOldProps } = oldProps;
const { status: newStatus, ...restNewProps } = newProps;
return (
status?.id === newStatus?.id && shallowEqual(restOldProps, restNewProps)
);
});
2 changes: 2 additions & 0 deletions src/utils/states.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { deepEqual } from 'fast-equals';
import { proxy, subscribe } from 'valtio';
import { subscribeKey } from 'valtio/utils';

Expand Down Expand Up @@ -178,6 +179,7 @@ export function saveStatus(status, instance, opts) {
if (!status) return;
const oldStatus = getStatus(status.id, instance);
if (!override && oldStatus) return;
if (deepEqual(status, oldStatus)) return;
queueMicrotask(() => {
const key = statusKey(status.id, instance);
if (oldStatus?._pinned) status._pinned = oldStatus._pinned;
Expand Down

0 comments on commit 16e2ac9

Please sign in to comment.