-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathcomponent-will-unmount.js
More file actions
45 lines (37 loc) · 1.3 KB
/
component-will-unmount.js
File metadata and controls
45 lines (37 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { InstanceCache, VTree } from '../util/types';
import diff from '../util/binding';
import { $$hooks } from '../util/symbols';
const { release, Internals } = diff;
/**
* This is called whenever a component is removed or in the case of a function
* component is called whenever a component is updated.
*
* @param {VTree} vTree - The respecting tree pointing to the component
*/
export default function componentWillUnmount(vTree) {
const domNode = Internals.NodeCache.get(vTree);
// Clean up attached Shadow DOM.
if (domNode && /** @type {any} */ (domNode).shadowRoot) {
release(/** @type {any} */ (domNode).shadowRoot);
}
else {
Internals.memory.unprotectVTree(vTree);
}
for (let i = 0; i < vTree.childNodes.length; i++) {
componentWillUnmount(vTree.childNodes[i]);
}
if (!InstanceCache.has(vTree)) {
return;
}
const instance = InstanceCache.get(vTree);
InstanceCache.delete(vTree);
// Empty out all hooks for gc. If using a stateless class or function, they
// may not have this value set.
if (instance[$$hooks]) {
instance[$$hooks].fns.length = 0;
instance[$$hooks].i = 0;
}
// Ensure this is a stateful component. Stateless components do not get
// lifecycle events yet.
instance && instance.componentWillUnmount && instance.componentWillUnmount();
}