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

Organize core imports #559

Merged
merged 1 commit into from
Nov 30, 2024
Merged
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
8 changes: 4 additions & 4 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,19 @@ export default [
},
{
target: ["./src/react/**/*"],
from: ["./src/!(core|react)/**/*"],
from: ["./src/!(core|react)/**/*", "./src/core/!(index.ts)"],
},
{
target: ["./src/solid/**/*"],
from: ["./src/!(core|solid)/**/*"],
from: ["./src/!(core|solid)/**/*", "./src/core/!(index.ts)"],
},
{
target: ["./src/svelte/**/*"],
from: ["./src/!(core|svelte)/**/*"],
from: ["./src/!(core|svelte)/**/*", "./src/core/!(index.ts)"],
},
{
target: ["./src/vue/**/*"],
from: ["./src/!(core|vue)/**/*"],
from: ["./src/!(core|vue)/**/*", "./src/core/!(index.ts)"],
},
],
},
Expand Down
26 changes: 26 additions & 0 deletions src/core/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export {
ACTION_ITEMS_LENGTH_CHANGE,
ACTION_START_OFFSET_CHANGE,
UPDATE_VIRTUAL_STATE,
UPDATE_SCROLL_END_EVENT,
UPDATE_SCROLL_EVENT,
createVirtualStore,
type VirtualStore,
getScrollSize,
type StateVersion,
} from "./store";
export {
createScroller,
createWindowScroller,
createGridScroller,
} from "./scroller";
export {
createResizer,
createWindowResizer,
type ItemResizeObserver,
createGridResizer,
type GridResizer,
} from "./resizer";
export { isRTLDocument, isBrowser } from "./environment";
export { microtask, sort } from "./utils";
export type { CacheSnapshot, ScrollToIndexOpts, ItemsRange } from "./types";
2 changes: 0 additions & 2 deletions src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ export const { min, max, abs, floor } = Math;
/** @internal */
export const values = Object.values;
/** @internal */
export const isArray = Array.isArray;
/** @internal */
export const timeout = setTimeout;

/**
Expand Down
6 changes: 2 additions & 4 deletions src/react/ListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ import {
ReactNode,
} from "react";
import { useIsomorphicLayoutEffect } from "./useIsomorphicLayoutEffect";
import { ItemResizeObserver } from "../core/resizer";
import { isRTLDocument, ItemResizeObserver } from "../core";
import { refKey } from "./utils";
import { isRTLDocument } from "../core/environment";
import { CustomItemComponent } from "./types";
import { NULL } from "../core/utils";

interface ListItemProps {
_children: ReactNode;
Expand All @@ -38,7 +36,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
15 changes: 8 additions & 7 deletions src/react/VGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@
createVirtualStore,
getScrollSize,
UPDATE_VIRTUAL_STATE,
} from "../core/store";
createGridScroller,
createGridResizer,
GridResizer,
isRTLDocument,
} from "../core";
import { useIsomorphicLayoutEffect } from "./useIsomorphicLayoutEffect";
import { createGridScroller } from "../core/scroller";
import { refKey } from "./utils";
import { useStatic } from "./useStatic";
import { createGridResizer, GridResizer } from "../core/resizer";
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 @@
_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 @@ -257,7 +258,7 @@
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 Expand Up @@ -320,7 +321,7 @@
scrollTo: scroller._scrollTo,
scrollBy: scroller._scrollBy,
};
}, []);

Check warning on line 324 in src/react/VGrid.tsx

View workflow job for this annotation

GitHub Actions / check

React Hook useImperativeHandle has missing dependencies: 'hStore', 'scroller._scrollBy', 'scroller._scrollTo', 'scroller._scrollToIndex', and 'vStore'. Either include them or remove the dependency array

const render = useMemo(() => {
const cache = new Map<string, ReactNode>();
Expand Down
3 changes: 1 addition & 2 deletions src/react/VList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
VirtualizerHandle,
VirtualizerProps,
} from "./Virtualizer";
import { NULL } from "../core/utils";

/**
* Methods of {@link VList}.
Expand Down Expand Up @@ -62,7 +61,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
14 changes: 8 additions & 6 deletions src/react/Virtualizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,22 @@
UPDATE_SCROLL_END_EVENT,
getScrollSize,
ACTION_START_OFFSET_CHANGE,
} from "../core/store";
createScroller,
createResizer,
CacheSnapshot,
ScrollToIndexOpts,
microtask,
sort,
} from "../core";
import { useIsomorphicLayoutEffect } from "./useIsomorphicLayoutEffect";
import { createScroller } from "../core/scroller";
import { getKey, refKey } from "./utils";
import { useStatic } from "./useStatic";
import { useLatestRef } from "./useLatestRef";
import { createResizer } from "../core/resizer";
import { ListItem } from "./ListItem";
import { CacheSnapshot, ScrollToIndexOpts } from "../core/types";
import { flushSync } from "react-dom";
import { useRerender } from "./useRerender";
import { useChildren } from "./useChildren";
import { CustomContainerComponent, CustomItemComponent } from "./types";
import { microtask, NULL, sort } from "../core/utils";

/**
* Methods of {@link Virtualizer}.
Expand Down Expand Up @@ -191,7 +193,7 @@

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

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

const isSSR = useRef(!!ssrCount);

Expand Down Expand Up @@ -320,7 +322,7 @@
scrollTo: scroller._scrollTo,
scrollBy: scroller._scrollBy,
};
}, []);

Check warning on line 325 in src/react/Virtualizer.tsx

View workflow job for this annotation

GitHub Actions / check

React Hook useImperativeHandle has missing dependencies: 'scroller._scrollBy', 'scroller._scrollTo', 'scroller._scrollToIndex', and 'store'. Either include them or remove the dependency array

for (let i = startIndex, j = endIndex; i <= j; i++) {
items.push(getListItem(i));
Expand Down
12 changes: 6 additions & 6 deletions src/react/WindowVirtualizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@
createVirtualStore,
UPDATE_SCROLL_END_EVENT,
UPDATE_SCROLL_EVENT,
} from "../core/store";
createWindowScroller,
createWindowResizer,
CacheSnapshot,
ScrollToIndexOpts,
} from "../core";
import { useIsomorphicLayoutEffect } from "./useIsomorphicLayoutEffect";
import { createWindowScroller } from "../core/scroller";
import { getKey, refKey } from "./utils";
import { useStatic } from "./useStatic";
import { useLatestRef } from "./useLatestRef";
import { createWindowResizer } from "../core/resizer";
import { CacheSnapshot, ScrollToIndexOpts } from "../core/types";
import { CustomContainerComponent, CustomItemComponent } from "./types";
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 @@ -144,7 +144,7 @@

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

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

const onScroll = useLatestRef(onScrollProp);
const onScrollEnd = useLatestRef(onScrollEndProp);
Expand Down Expand Up @@ -232,7 +232,7 @@
findEndIndex: store._findEndIndex,
scrollToIndex: scroller._scrollToIndex,
};
}, []);

Check warning on line 235 in src/react/WindowVirtualizer.tsx

View workflow job for this annotation

GitHub Actions / check

React Hook useImperativeHandle has missing dependencies: 'scroller._scrollToIndex' and 'store'. Either include them or remove the dependency array

for (let i = startIndex, j = endIndex; i <= j; i++) {
const e = getElement(i);
Expand Down
2 changes: 1 addition & 1 deletion src/react/useIsomorphicLayoutEffect.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect, useLayoutEffect } from "react";
import { isBrowser } from "../core/environment";
import { isBrowser } from "../core";

/**
* https://gist.github.com/gaearon/e7d97cdf38a2907924ea12e4ebdf3c85
Expand Down
2 changes: 1 addition & 1 deletion src/react/useRerender.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useReducer } from "react";
import { VirtualStore } from "../core/store";
import { VirtualStore } from "../core";

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

/**
* @internal
Expand All @@ -12,11 +11,11 @@ export const refKey = "current";
export type ItemElement = Exclude<ReactNode, null | boolean | Array<any>>;

const forEach = (children: ReactNode, elements: ItemElement[]) => {
if (isArray(children)) {
if (Array.isArray(children)) {
for (const c of children) {
forEach(c, elements);
}
} else if (children == NULL || 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 +48,5 @@ type MayHaveKey = { key?: React.Key };
*/
export const getKey = (e: ItemElement, i: number): React.Key => {
const key = (e as MayHaveKey).key;
return key != NULL ? key : "_" + i;
return key != null ? key : "_" + i;
};
3 changes: 1 addition & 2 deletions src/solid/ListItem.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/**
* @jsxImportSource solid-js
*/
import { ItemResizeObserver } from "../core/resizer";
import { isRTLDocument } from "../core/environment";
import { ItemResizeObserver, isRTLDocument } from "../core";
import {
Component,
JSX,
Expand All @@ -29,7 +28,7 @@
*/
export const ListItem: Component<ListItemProps> = (props) => {
let elementRef: HTMLDivElement | undefined;
props = mergeProps<[Partial<ListItemProps>, ListItemProps]>(

Check warning on line 31 in src/solid/ListItem.tsx

View workflow job for this annotation

GitHub Actions / check

The reactive variable 'props' should not be reassigned or altered directly

Check warning on line 31 in src/solid/ListItem.tsx

View workflow job for this annotation

GitHub Actions / check

The reactive variable 'props' should not be reassigned or altered directly
{ _as: "div" },
props
);
Expand Down
2 changes: 1 addition & 1 deletion src/solid/RangedFor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Signal,
Accessor,
} from "solid-js";
import { ItemsRange } from "../core/types";
import { ItemsRange } from "../core";

interface RenderedNode<T> {
_data: Signal<T>;
Expand All @@ -37,7 +37,7 @@
}
});

return createMemo(() => {

Check warning on line 40 in src/solid/RangedFor.tsx

View workflow job for this annotation

GitHub Actions / check

For proper analysis, a variable should be used to capture the result of this function call
const list = props._each;
const [start, end] = props._range;
const current = new Map<number, RenderedNode<T>>();
Expand All @@ -50,7 +50,7 @@
lookup
? lookup._element
: createRoot((dispose) => {
const data = createSignal(newData);

Check warning on line 53 in src/solid/RangedFor.tsx

View workflow job for this annotation

GitHub Actions / check

For proper analysis, array destructuring should be used to capture the first result of this function call
const result = props._render(data[0], i);
current.set(i, {
_data: data,
Expand Down
9 changes: 5 additions & 4 deletions src/solid/Virtualizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ import {
ACTION_ITEMS_LENGTH_CHANGE,
getScrollSize,
ACTION_START_OFFSET_CHANGE,
} from "../core/store";
import { createResizer } from "../core/resizer";
import { createScroller } from "../core/scroller";
import { ItemsRange, ScrollToIndexOpts } from "../core/types";
createResizer,
createScroller,
ItemsRange,
ScrollToIndexOpts,
} from "../core";
import { ListItem } from "./ListItem";
import { RangedFor } from "./RangedFor";
import { isSameRange } from "./utils";
Expand Down
9 changes: 5 additions & 4 deletions src/solid/WindowVirtualizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ import {
createVirtualStore,
ACTION_ITEMS_LENGTH_CHANGE,
UPDATE_SCROLL_EVENT,
} from "../core/store";
import { createWindowResizer } from "../core/resizer";
import { createWindowScroller } from "../core/scroller";
createWindowResizer,
createWindowScroller,
ItemsRange,
ScrollToIndexOpts,
} from "../core";
import { ListItem } from "./ListItem";
import { RangedFor } from "./RangedFor";
import { isSameRange } from "./utils";
import { ItemsRange, ScrollToIndexOpts } from "../core/types";

/**
* Methods of {@link WindowVirtualizer}.
Expand Down
2 changes: 1 addition & 1 deletion src/solid/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ItemsRange } from "../core/types";
import { ItemsRange } from "../core";

/**
* @internal
Expand Down
2 changes: 1 addition & 1 deletion src/svelte/Virtualizer.type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type Snippet } from "svelte";
import type { SvelteHTMLElements } from "svelte/elements";
import { type ScrollToIndexOpts } from "../core/types";
import { type ScrollToIndexOpts } from "../core";

/**
* Props of {@link Virtualizer}.
Expand Down
2 changes: 1 addition & 1 deletion src/svelte/WindowVirtualizer.type.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type Snippet } from "svelte";
import { type ScrollToIndexOpts } from "../core/types";
import { type ScrollToIndexOpts } from "../core";

/**
* Props of {@link WindowVirtualizer}.
Expand Down
16 changes: 10 additions & 6 deletions src/svelte/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ import {
type StateVersion,
getScrollSize,
ACTION_START_OFFSET_CHANGE,
} from "../core/store";
import { createResizer, createWindowResizer } from "../core/resizer";
import { createScroller, createWindowScroller } from "../core/scroller";
createResizer,
createWindowResizer,
createScroller,
createWindowScroller,
} from "../core";

export { type StateVersion } from "../core/store";
export { isRTLDocument } from "../core/environment";
export type { ItemResizeObserver } from "../core/resizer";
export {
type StateVersion,
type ItemResizeObserver,
isRTLDocument,
} from "../core";

export const ON_MOUNT = 0;
export const ON_UN_MOUNT = 1;
Expand Down
2 changes: 1 addition & 1 deletion src/svelte/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ItemsRange } from "../core/types";
import type { ItemsRange } from "../core";

export const styleToString = (
obj: Record<string, string | undefined>
Expand Down
9 changes: 6 additions & 3 deletions src/vue/ListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ import {
computed,
Ref,
} from "vue";
import { ItemResizeObserver } from "../core/resizer";
import { isRTLDocument } from "../core/environment";
import { StateVersion, VirtualStore } from "../core/store";
import {
ItemResizeObserver,
isRTLDocument,
StateVersion,
VirtualStore,
} from "../core";

/**
* @internal
Expand Down
11 changes: 6 additions & 5 deletions src/vue/Virtualizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ import {
ACTION_ITEMS_LENGTH_CHANGE,
getScrollSize,
ACTION_START_OFFSET_CHANGE,
} from "../core/store";
import { createResizer } from "../core/resizer";
import { createScroller } from "../core/scroller";
import { ItemsRange, ScrollToIndexOpts } from "../core/types";
createResizer,
createScroller,
ItemsRange,
ScrollToIndexOpts,
microtask,
} from "../core";
import { ListItem } from "./ListItem";
import { getKey, isSameRange } from "./utils";
import { microtask } from "../core/utils";

export interface VirtualizerHandle {
/**
Expand Down
Loading
Loading