Skip to content
This repository was archived by the owner on Nov 9, 2024. It is now read-only.

Commit db1aa47

Browse files
committed
fix: singleton cleanup
1 parent 1c1cd28 commit db1aa47

File tree

3 files changed

+56
-24
lines changed

3 files changed

+56
-24
lines changed

demo/index.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,18 +123,31 @@ function Singleton() {
123123

124124
function SingletonHeadless() {
125125
const [source, target] = useSingletonHeadless({overrides: ['placement']});
126+
const [enabled, setEnabled] = useState(false);
127+
128+
useEffect(() => {
129+
setInterval(() => {
130+
setEnabled(e => !e);
131+
}, 2000);
132+
}, []);
126133

127134
return (
128135
<>
129-
<TippyHeadless
130-
render={(attrs, content) => (
131-
<ReactSpringBox {...attrs}>{content}</ReactSpringBox>
132-
)}
133-
singleton={source}
134-
delay={500}
135-
/>
136-
137-
<TippyHeadless content="Hello" singleton={target}>
136+
{enabled && (
137+
<TippyHeadless
138+
render={(attrs, content) => (
139+
<ReactSpringBox {...attrs}>{content}</ReactSpringBox>
140+
)}
141+
singleton={source}
142+
/>
143+
)}
144+
145+
{enabled && (
146+
<TippyHeadless content="Hello" singleton={target}>
147+
<button>Reference</button>
148+
</TippyHeadless>
149+
)}
150+
<TippyHeadless placement="right" content="Bye" singleton={target}>
138151
<button>Reference</button>
139152
</TippyHeadless>
140153
<TippyHeadless placement="right" content="Bye" singleton={target}>

src/Tippy.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ export default function TippyGenerator(tippy) {
123123

124124
return () => {
125125
instance.destroy();
126+
singleton?.cleanup(instance);
126127
};
127128
}, deps);
128129

src/useSingleton.js

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ export default function useSingletonGenerator(createSingleton) {
99
renders: 1,
1010
});
1111

12-
const deps = [mutableBox.children.length];
12+
const deps = [
13+
mutableBox.children.length,
14+
mutableBox.sourceData,
15+
...overrides,
16+
];
1317

1418
useIsomorphicLayoutEffect(() => {
1519
const {children, sourceData} = mutableBox;
@@ -39,7 +43,7 @@ export default function useSingletonGenerator(createSingleton) {
3943
({instance}) => !instance.state.isDestroyed,
4044
);
4145
};
42-
}, [...overrides, ...deps]);
46+
}, deps);
4347

4448
useIsomorphicLayoutEffect(() => {
4549
if (mutableBox.renders === 1) {
@@ -62,21 +66,35 @@ export default function useSingletonGenerator(createSingleton) {
6266
}
6367
});
6468

65-
return useMemo(
66-
() => [
67-
{
68-
data: mutableBox,
69-
hook(data) {
70-
mutableBox.sourceData = data;
71-
},
69+
return useMemo(() => {
70+
const source = {
71+
data: mutableBox,
72+
hook(data) {
73+
mutableBox.sourceData = data;
7274
},
73-
{
74-
hook(data) {
75+
cleanup() {
76+
mutableBox.sourceData = null;
77+
},
78+
};
79+
80+
const target = {
81+
hook(data) {
82+
if (
83+
!mutableBox.children.find(
84+
({instance}) => data.instance === instance,
85+
)
86+
) {
7587
mutableBox.children.push(data);
76-
},
88+
}
89+
},
90+
cleanup(instance) {
91+
mutableBox.children = mutableBox.children.filter(
92+
data => data.instance !== instance,
93+
);
7794
},
78-
],
79-
[],
80-
);
95+
};
96+
97+
return [source, target];
98+
}, []);
8199
};
82100
}

0 commit comments

Comments
 (0)