Skip to content

Commit be6c031

Browse files
committed
improve compile unlink memory retaining
1 parent ff66e0b commit be6c031

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

src/compiler/compile.js

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,8 @@ exports.compile = function (el, options, partial, host) {
6565
if (nodeLinkFn) nodeLinkFn(vm, el, host)
6666
if (childLinkFn) childLinkFn(vm, childNodes, host)
6767
}, vm)
68-
69-
/**
70-
* The linker function returns an unlink function that
71-
* tearsdown all directives instances generated during
72-
* the process.
73-
*
74-
* @param {Boolean} destroying
75-
*/
76-
return function unlink (destroying) {
77-
teardownDirs(vm, dirs, destroying)
78-
}
68+
//
69+
return makeUnlinkFn(vm, dirs)
7970
}
8071
}
8172

@@ -93,6 +84,30 @@ function linkAndCapture (linker, vm) {
9384
return vm._directives.slice(originalDirCount)
9485
}
9586

87+
/**
88+
* Linker functions return an unlink function that
89+
* tearsdown all directives instances generated during
90+
* the process.
91+
*
92+
* We create unlink functions with only the necessary
93+
* information to avoid retaining additional closures.
94+
*
95+
* @param {Vue} vm
96+
* @param {Array} dirs
97+
* @param {Vue} [parent]
98+
* @param {Array} [parentDirs]
99+
* @return {Function}
100+
*/
101+
102+
function makeUnlinkFn (vm, dirs, parent, parentDirs) {
103+
return function unlink (destroying) {
104+
teardownDirs(vm, dirs, destroying)
105+
if (parent && parentDirs) {
106+
teardownDirs(parent, parentDirs)
107+
}
108+
}
109+
}
110+
96111
/**
97112
* Teardown partial linked directives.
98113
*
@@ -183,10 +198,7 @@ function teardownDirs (vm, dirs, destroying) {
183198

184199
// return the unlink function that tearsdown parent
185200
// container directives.
186-
return function rootUnlinkFn () {
187-
teardownDirs(parent, parentDirs)
188-
teardownDirs(vm, selfDirs)
189-
}
201+
return makeUnlinkFn(vm, selfDirs, parent, parentDirs)
190202
}
191203

192204
/**

0 commit comments

Comments
 (0)