Skip to content

Commit f8b9cce

Browse files
furybeanyyx990803
authored andcommitted
make directives sort stable. (vuejs#3149)
1 parent e7932f8 commit f8b9cce

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

src/compiler/compile.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,24 +105,40 @@ function linkAndCapture (linker, vm) {
105105
var originalDirCount = vm._directives.length
106106
linker()
107107
var dirs = vm._directives.slice(originalDirCount)
108-
dirs.sort(directiveComparator)
108+
sortDirectives(dirs)
109109
for (var i = 0, l = dirs.length; i < l; i++) {
110110
dirs[i]._bind()
111111
}
112112
return dirs
113113
}
114114

115115
/**
116-
* Directive priority sort comparator
116+
* sort directives by priority (stable sort)
117117
*
118-
* @param {Object} a
119-
* @param {Object} b
118+
* @param {Array} dirs
120119
*/
120+
function sortDirectives (dirs) {
121+
if (dirs.length === 0) return
122+
123+
var groupedMap = {}
124+
for (var i = 0, j = dirs.length; i < j; i++) {
125+
var dir = dirs[i]
126+
var priority = dir.descriptor.def.priority || DEFAULT_PRIORITY
127+
var array = groupedMap[priority]
128+
if (!array) {
129+
array = groupedMap[priority] = []
130+
}
131+
array.push(dir)
132+
}
121133

122-
function directiveComparator (a, b) {
123-
a = a.descriptor.def.priority || DEFAULT_PRIORITY
124-
b = b.descriptor.def.priority || DEFAULT_PRIORITY
125-
return a > b ? -1 : a === b ? 0 : 1
134+
var index = 0
135+
Object.keys(groupedMap).sort(function (a, b) {
136+
return a > b ? -1 : a === b ? 0 : 1
137+
}).forEach(function (priority) {
138+
groupedMap[priority].forEach(function (item) {
139+
dirs[index++] = item
140+
})
141+
})
126142
}
127143

128144
/**

0 commit comments

Comments
 (0)