Skip to content

Commit

Permalink
feat: 格式化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
IVLIU committed Feb 22, 2024
1 parent 82e587b commit 7c3bfdc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
20 changes: 12 additions & 8 deletions src/Activity.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import React, { Suspense } from 'react';
import { Repeater } from './Repeater';
import type { FC, ExoticComponent } from 'react';
import type { IProps } from './type';
import React, { Suspense } from "react";
import { Repeater } from "./Repeater";
import type { FC, ExoticComponent } from "react";
import type { IProps } from "./type";

const NativeActivity = ('Activity' in React ? React.Activity : 'unstable_Activity' in React ? React.unstable_Activity : null) as ExoticComponent<IProps> | null;
const NativeActivity = (
"Activity" in React
? React.Activity
: "unstable_Activity" in React
? React.unstable_Activity
: null
) as ExoticComponent<IProps> | null;

export const Activity: FC<IProps> = (props) => {
const { mode, children } = props;

if (NativeActivity) {
return (
<NativeActivity mode={mode}>{children}</NativeActivity>
)
return <NativeActivity mode={mode}>{children}</NativeActivity>;
}

return (
Expand Down
33 changes: 20 additions & 13 deletions src/Repeater.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useRef, useEffect } from 'react';
import type { FC } from 'react';
import type { IProps } from './type';
import React, { useRef, useEffect } from "react";
import type { FC } from "react";
import type { IProps } from "./type";

export const Repeater: FC<IProps> = (props) => {
// props
Expand All @@ -9,34 +9,41 @@ export const Repeater: FC<IProps> = (props) => {
const resolveRef = useRef<() => void>();
// methods
const resolvePromise = (ignoreMode?: boolean) => {
if ((ignoreMode || mode === 'visible') && typeof resolveRef.current === 'function') {
if (
(ignoreMode || mode === "visible") &&
typeof resolveRef.current === "function"
) {
resolveRef.current();
resolveRef.current = void 0;
}
}
};
// effect
useEffect(() => () => resolvePromise(true), []);

if (mode === 'hidden' && typeof resolveRef.current === 'undefined') {
const promise = new Promise<void>((resolve) => (resolveRef.current = resolve));
if('use' in React && typeof React.use === 'function') {
if (mode === "hidden" && typeof resolveRef.current === "undefined") {
const promise = new Promise<void>(
(resolve) => (resolveRef.current = resolve),
);
if ("use" in React && typeof React.use === "function") {
(React.use as <T>(primise: Promise<T>) => T)(promise);
} else {
throw promise;
}
}

// warning
if(mode === 'hidden') {
if (mode === "hidden") {
console.error(
navigator.language === 'zh-CN' ? `
navigator.language === "zh-CN"
? `
由于react的限制,由startTransition或者useDeferredValue触发的更新引起的组件挂起不会渲染回退,具体可以参考
https://zh-hans.react.dev/reference/react/Suspense#preventing-already-revealed-content-from-hiding
` : `
`
: `
Due to the limitations of react, component suspension caused by updates triggered by startTransition or useDeferredValue will not render the rollback. For details, please refer to
https://zh-hans.react.dev/reference/react/Suspense#preventing-already-revealed-content-from-hiding
`
)
`,
);
}

resolvePromise();
Expand Down

0 comments on commit 7c3bfdc

Please sign in to comment.