Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
inokawa committed Jul 7, 2024
1 parent 2b94b98 commit 78ef60c
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 26 deletions.
6 changes: 3 additions & 3 deletions src/core/resizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
type VirtualStore,
} from "./store";
import { type ItemResize } from "./types";
import { exists, max } from "./utils";
import { max, NULL } from "./utils";

const createResizeObserver = (cb: ResizeObserverCallback) => {
let ro: ResizeObserver | undefined;
Expand Down Expand Up @@ -61,7 +61,7 @@ export const createResizer = (
store._update(ACTION_VIEWPORT_RESIZE, contentRect[sizeKey]);
} else {
const index = mountedIndexes.get(target);
if (exists(index)) {
if (index != NULL) {
resizes.push([index, contentRect[sizeKey]]);
}
}
Expand Down Expand Up @@ -112,7 +112,7 @@ export const createWindowResizer = (
if (!(target as HTMLElement).offsetParent) continue;

const index = mountedIndexes.get(target);
if (exists(index)) {
if (index != NULL) {
resizes.push([index, contentRect[sizeKey]]);
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/core/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import type {
ItemResize,
ItemsRange,
} from "./types";
import { abs, max, min } from "./utils";
import { abs, max, min, NULL } from "./utils";

/** @internal */
export const SCROLL_IDLE = 0;
Expand Down Expand Up @@ -155,7 +155,7 @@ export const createVirtualStore = (
let _scrollMode: ScrollMode = SCROLL_BY_NATIVE;
let _frozenRange: ItemsRange | null = isSSR
? [0, max(ssrCount - 1, 0)]
: null;
: NULL;
let _prevRange: ItemsRange = [0, 0];
let _totalMeasuredSize = 0;

Expand Down Expand Up @@ -302,7 +302,7 @@ export const createVirtualStore = (
// }

if (isSSR) {
_frozenRange = null;
_frozenRange = NULL;
isSSR = false;
}

Expand Down Expand Up @@ -331,7 +331,7 @@ export const createVirtualStore = (
}
_scrollDirection = SCROLL_IDLE;
_scrollMode = SCROLL_BY_NATIVE;
_frozenRange = null;
_frozenRange = NULL;
break;
}
case ACTION_ITEM_RESIZE: {
Expand Down
12 changes: 5 additions & 7 deletions src/core/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/** @internal */
export const NULL = null;

/** @internal */
export const min = Math.min;
/** @internal */
Expand All @@ -20,11 +23,6 @@ export const clamp = (
maxValue: number
): number => min(maxValue, max(minValue, value));

/**
* @internal
*/
export const exists = <T>(v: T): v is Exclude<T, null | undefined> => v != null;

/**
* @internal
*/
Expand Down Expand Up @@ -60,14 +58,14 @@ export const debounce = <T extends () => void>(fn: T, ms: number) => {
let id: ReturnType<typeof setTimeout> | undefined | null;

const cancel = () => {
if (exists(id)) {
if (id != NULL) {
clearTimeout(id);
}
};
const debouncedFn = () => {
cancel();
id = timeout(() => {
id = null;
id = NULL;
fn();
}, ms);
};
Expand Down
3 changes: 2 additions & 1 deletion src/react/ListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ItemResizeObserver } from "../core/resizer";
import { refKey } from "./utils";
import { isRTLDocument } from "../core/environment";
import { CustomItemComponent } from "./types";
import { NULL } from "../core/utils";

interface ListItemProps {
_children: ReactNode;
Expand All @@ -37,7 +38,7 @@ export const ListItem = memo(
_isHorizontal: isHorizontal,
_isSSR: isSSR,
}: ListItemProps): ReactElement => {
const ref = useRef<HTMLDivElement>(null);
const ref = useRef<HTMLDivElement>(NULL);

// The index may be changed if elements are inserted to or removed from the start of props.children
useIsomorphicLayoutEffect(() => resizer(ref[refKey]!, index), [index]);
Expand Down
5 changes: 3 additions & 2 deletions src/react/VGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { ViewportComponentAttributes } from "./types";
import { flushSync } from "react-dom";
import { isRTLDocument } from "../core/environment";
import { useRerender } from "./useRerender";
import { NULL } from "../core/utils";
const genKey = (i: number, j: number) => `${i}-${j}`;

/**
Expand Down Expand Up @@ -65,7 +66,7 @@ const Cell = memo(
_hide: hide,
_element: Element,
}: CellProps): ReactElement => {
const ref = useRef<HTMLDivElement>(null);
const ref = useRef<HTMLDivElement>(NULL);

// The index may be changed if elements are inserted to or removed from the start of props.children
useIsomorphicLayoutEffect(
Expand Down Expand Up @@ -249,7 +250,7 @@ export const VGrid = forwardRef<VGridHandle, VGridProps>(
const hJumpCount = hStore._getJumpCount();
const height = getScrollSize(vStore);
const width = getScrollSize(hStore);
const rootRef = useRef<HTMLDivElement>(null);
const rootRef = useRef<HTMLDivElement>(NULL);

useIsomorphicLayoutEffect(() => {
const root = rootRef[refKey]!;
Expand Down
3 changes: 2 additions & 1 deletion src/react/VList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
VirtualizerHandle,
VirtualizerProps,
} from "./Virtualizer";
import { NULL } from "../core/utils";

/**
* Methods of {@link VList}.
Expand Down Expand Up @@ -63,7 +64,7 @@ export const VList = forwardRef<VListHandle, VListProps>(
},
ref
): ReactElement => {
const scrollRef = useRef<HTMLDivElement>(null);
const scrollRef = useRef<HTMLDivElement>(NULL);
const shouldReverse = reverse && !horizontal;

let element = (
Expand Down
4 changes: 2 additions & 2 deletions src/react/Virtualizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { flushSync } from "react-dom";
import { useRerender } from "./useRerender";
import { useChildren } from "./useChildren";
import { CustomContainerComponent, CustomItemComponent } from "./types";
import { microtask, sort } from "../core/utils";
import { microtask, NULL, sort } from "../core/utils";

/**
* Methods of {@link Virtualizer}.
Expand Down Expand Up @@ -194,7 +194,7 @@ export const Virtualizer = forwardRef<VirtualizerHandle, VirtualizerProps>(

const [getElement, count] = useChildren(children, renderCountProp);

const containerRef = useRef<HTMLDivElement>(null);
const containerRef = useRef<HTMLDivElement>(NULL);

const isSSR = useRef(!!ssrCount);

Expand Down
3 changes: 2 additions & 1 deletion src/react/WindowVirtualizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { ListItem } from "./ListItem";
import { flushSync } from "react-dom";
import { useRerender } from "./useRerender";
import { useChildren } from "./useChildren";
import { NULL } from "../core/utils";

/**
* Methods of {@link WindowVirtualizer}.
Expand Down Expand Up @@ -138,7 +139,7 @@ export const WindowVirtualizer = forwardRef<

const [getElement, count] = useChildren(children, renderCountProp);

const containerRef = useRef<HTMLDivElement>(null);
const containerRef = useRef<HTMLDivElement>(NULL);

const onScrollEnd = useLatestRef(onScrollEndProp);

Expand Down
6 changes: 3 additions & 3 deletions src/react/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ReactElement, ReactFragment, ReactNode } from "react";
import { exists, isArray } from "../core/utils";
import { isArray, NULL } from "../core/utils";

/**
* @internal
Expand All @@ -16,7 +16,7 @@ const forEach = (children: ReactNode, elements: ItemElement[]) => {
for (const c of children) {
forEach(c, elements);
}
} else if (!exists(children) || typeof children === "boolean") {
} else if (children == NULL || typeof children === "boolean") {
// filter out, that is the same as React.Children.toArray
} else {
elements.push(children);
Expand Down Expand Up @@ -49,5 +49,5 @@ type MayHaveKey = { key?: React.Key };
*/
export const getKey = (e: ItemElement, i: number): React.Key => {
const key = (e as MayHaveKey).key;
return exists(key) ? key : "_" + i;
return key != NULL ? key : "_" + i;
};
4 changes: 2 additions & 2 deletions src/vue/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { VNode } from "vue";
import { exists } from "../core/utils";
import { NULL } from "../core/utils";

/**
* @internal
*/
export const getKey = (e: VNode, i: number): Exclude<VNode["key"], null> => {
const key = e.key;
return exists(key) ? key : "_" + i;
return key != NULL ? key : "_" + i;
};

0 comments on commit 78ef60c

Please sign in to comment.