1
- const stylesInDom = [ ] ;
1
+ const stylesInDOM = [ ] ;
2
2
3
3
function getIndexByIdentifier ( identifier ) {
4
4
let result = - 1 ;
5
5
6
- for ( let i = 0 ; i < stylesInDom . length ; i ++ ) {
7
- if ( stylesInDom [ i ] . identifier === identifier ) {
6
+ for ( let i = 0 ; i < stylesInDOM . length ; i ++ ) {
7
+ if ( stylesInDOM [ i ] . identifier === identifier ) {
8
8
result = i ;
9
9
break ;
10
10
}
@@ -25,7 +25,7 @@ function modulesToDom(list, options) {
25
25
26
26
idCountMap [ id ] = count + 1 ;
27
27
28
- const index = getIndexByIdentifier ( identifier ) ;
28
+ const indexByIdentifier = getIndexByIdentifier ( identifier ) ;
29
29
const obj = {
30
30
css : item [ 1 ] ,
31
31
media : item [ 2 ] ,
@@ -34,13 +34,17 @@ function modulesToDom(list, options) {
34
34
layer : item [ 5 ] ,
35
35
} ;
36
36
37
- if ( index !== - 1 ) {
38
- stylesInDom [ index ] . references ++ ;
39
- stylesInDom [ index ] . updater ( obj ) ;
37
+ if ( indexByIdentifier !== - 1 ) {
38
+ stylesInDOM [ indexByIdentifier ] . references ++ ;
39
+ stylesInDOM [ indexByIdentifier ] . updater ( obj ) ;
40
40
} else {
41
- stylesInDom . push ( {
41
+ const updater = addElementStyle ( obj , options ) ;
42
+
43
+ options . byIndex = i ;
44
+
45
+ stylesInDOM . splice ( i , 0 , {
42
46
identifier,
43
- updater : addStyle ( obj , options ) ,
47
+ updater,
44
48
references : 1 ,
45
49
} ) ;
46
50
}
@@ -51,12 +55,12 @@ function modulesToDom(list, options) {
51
55
return identifiers ;
52
56
}
53
57
54
- function addStyle ( obj , options ) {
58
+ function addElementStyle ( obj , options ) {
55
59
const api = options . domAPI ( options ) ;
56
60
57
61
api . update ( obj ) ;
58
62
59
- return function updateStyle ( newObj ) {
63
+ const updater = ( newObj ) => {
60
64
if ( newObj ) {
61
65
if (
62
66
newObj . css === obj . css &&
@@ -73,6 +77,8 @@ function addStyle(obj, options) {
73
77
api . remove ( ) ;
74
78
}
75
79
} ;
80
+
81
+ return updater ;
76
82
}
77
83
78
84
module . exports = ( list , options ) => {
@@ -89,7 +95,7 @@ module.exports = (list, options) => {
89
95
const identifier = lastIdentifiers [ i ] ;
90
96
const index = getIndexByIdentifier ( identifier ) ;
91
97
92
- stylesInDom [ index ] . references -- ;
98
+ stylesInDOM [ index ] . references -- ;
93
99
}
94
100
95
101
const newLastIdentifiers = modulesToDom ( newList , options ) ;
@@ -98,9 +104,9 @@ module.exports = (list, options) => {
98
104
const identifier = lastIdentifiers [ i ] ;
99
105
const index = getIndexByIdentifier ( identifier ) ;
100
106
101
- if ( stylesInDom [ index ] . references === 0 ) {
102
- stylesInDom [ index ] . updater ( ) ;
103
- stylesInDom . splice ( index , 1 ) ;
107
+ if ( stylesInDOM [ index ] . references === 0 ) {
108
+ stylesInDOM [ index ] . updater ( ) ;
109
+ stylesInDOM . splice ( index , 1 ) ;
104
110
}
105
111
}
106
112
0 commit comments