Skip to content

Commit

Permalink
feat: 组件卸载添加resolve promise逻辑,防止内存泄漏
Browse files Browse the repository at this point in the history
  • Loading branch information
liuchuan committed Oct 20, 2023
1 parent d598623 commit bf42511
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 11 deletions.
Binary file added .parcel-cache/2ecb2ae785209a77
Binary file not shown.
Binary file added .parcel-cache/599de3244968e517
Binary file not shown.
2 changes: 2 additions & 0 deletions .parcel-cache/5ab0f72e1c03aae5.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
62893699
1697783164607299000
Binary file added .parcel-cache/a9f5484dda8bfe2c
Binary file not shown.
Binary file added .parcel-cache/c18ce38693a82912
Binary file not shown.
Binary file added .parcel-cache/data.mdb
Binary file not shown.
Binary file added .parcel-cache/lock.mdb
Binary file not shown.
2 changes: 1 addition & 1 deletion example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@

<body>
<div id="root"></div>
<script src="./index.tsx"></script>
<script type="module" src="./index.tsx"></script>
</body>
</html>
12 changes: 8 additions & 4 deletions example/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ const Count = () => {

const App = () => {
const [visible, setVisible] = React.useState(false);
const [destroy, setDestroy] = React.useState(false);
return (
<div>
<button onClick={() => setVisible(!visible)}>{visible}</button>
<Offscreen mode={visible ? 'visible' : 'hidden'}>
<Count />
</Offscreen>
<button onClick={() => setVisible(!visible)}>{visible ? 'hidden' : 'show'}</button>
<button onClick={() => setDestroy(true)}>destroy</button>
{!destroy && (
<Offscreen mode={visible ? 'visible' : 'hidden'}>
<Count />
</Offscreen>
)}
</div>
);
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.0.0",
"version": "1.0.1",
"license": "MIT",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
15 changes: 10 additions & 5 deletions src/Repeater.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useRef } from 'react';
import React, { useRef, useEffect } from 'react';
import type { FC, ReactNode } from 'react';

export const Repeater: FC<{
Expand All @@ -9,11 +9,16 @@ export const Repeater: FC<{
const { mode, children } = props;
// refs
const resolveRef = useRef<() => void>();
// destroy promise
if (resolveRef.current) {
resolveRef.current();
resolveRef.current = void 0;
// methods
const resolvePromise = () => {
if (typeof resolveRef.current === 'function') {
resolveRef.current();
resolveRef.current = void 0;
}
}
resolvePromise();
// effect
useEffect(() => resolvePromise, []);
if (mode === 'hidden') {
throw new Promise<void>((resolve) => (resolveRef.current = resolve));
}
Expand Down

0 comments on commit bf42511

Please sign in to comment.