@@ -29,13 +29,23 @@ export class Data {
29
29
init ( ) {
30
30
this . _getAttributesVariables ( )
31
31
this . _getEventVariables ( )
32
+ this . _removeDuplicateVariables ( )
32
33
this . _initVariables ( )
33
34
}
34
35
35
36
initAttributeVariables ( attr ) {
36
37
const expr = this . entity . element . getAttribute ( attr )
37
38
if ( ! expr ) return
38
39
40
+ const variables = this . getAttributeVariables ( )
41
+
42
+ if ( variables . length ) {
43
+ variables . forEach ( ( variable ) => {
44
+ this . remove ( variable , attr )
45
+ } )
46
+ this . _attributes . set ( attr , [ ] )
47
+ }
48
+
39
49
if ( attr === 'each' ) {
40
50
const [ _ , variable ] = expr . split ( ' in ' )
41
51
@@ -85,21 +95,32 @@ export class Data {
85
95
} )
86
96
}
87
97
98
+ _removeDuplicateVariables ( ) {
99
+ this . _variables . forEach ( ( attributes , variable ) => {
100
+ this . _variables . set ( variable , [ ...new Set ( attributes ) ] )
101
+ } )
102
+
103
+ this . _attributes . forEach ( ( variables , attribute ) => {
104
+ this . _attributes . set ( attribute , [ ...new Set ( variables ) ] )
105
+ } )
106
+ }
107
+
88
108
_initVariables ( ) {
89
109
const entityID = this . entity . id
110
+ const state = this . entity . base . state
90
111
91
112
this . variables . forEach ( ( variable ) => {
92
113
if ( State . isElState ( variable ) ) {
93
114
this . entity . setAsGroup ( )
94
115
95
116
if ( window [ entityID ] == null )
96
- window [ entityID ] = this . entity . base . state . create ( { } , entityID )
117
+ window [ entityID ] = state . create ( { } , entityID )
97
118
98
- this . entity . base . state . addVariable ( entityID , entityID )
119
+ state . addVariable ( entityID , entityID )
99
120
100
121
if ( variable !== State . EL_STATE ) {
101
122
const [ _ , varName ] = variable . split ( '.' )
102
- this . entity . base . state . addEntityVariable ( entityID , varName , entityID )
123
+ state . addEntityVariable ( entityID , varName , entityID )
103
124
}
104
125
} else if ( State . isGroupState ( variable ) ) {
105
126
if ( this . entity . group == null )
@@ -112,39 +133,37 @@ export class Data {
112
133
const groupID = this . entity . group ?. id
113
134
114
135
if ( window [ groupID ] == null ) {
115
- window [ groupID ] = this . entity . base . state . create ( { } , groupID )
136
+ window [ groupID ] = state . create ( { } , groupID )
116
137
}
117
138
118
- this . entity . base . state . addVariable ( groupID , entityID )
139
+ state . addVariable ( groupID , entityID )
119
140
120
141
if ( variable !== State . GROUP_STATE ) {
121
142
const [ _ , varName ] = variable . split ( '.' )
122
- this . entity . base . state . addEntityVariable ( groupID , varName , entityID )
143
+ state . addEntityVariable ( groupID , varName , entityID )
123
144
}
124
145
} else if ( typeof window [ variable ] === 'function' ) {
125
146
this . deleteVariable ( variable )
126
147
} else {
127
148
try {
128
149
const [ identifier ] = variable . split ( '.' )
129
- window [ identifier ] = this . entity . base . state . getState ( identifier )
130
- this . entity . base . state . addVariable ( identifier , entityID )
150
+ window [ identifier ] = state . getState ( identifier )
151
+ state . addVariable ( identifier , entityID )
131
152
} catch ( error ) {
132
153
console . error ( 'Failed to initialize variable:' , variable , error )
133
154
}
134
155
}
135
156
} )
157
+
158
+ state . removeDuplicateVariables ( )
136
159
}
137
160
138
161
add ( variable , attribute ) {
139
162
const currentAttributes = this . _variables . get ( variable ) ?? [ ]
140
- this . _variables . set ( variable , [
141
- ...new Set ( currentAttributes . concat ( [ attribute ] ) ) ,
142
- ] )
163
+ this . _variables . set ( variable , [ ...currentAttributes , attribute ] )
143
164
144
165
const currentVariables = this . _attributes . get ( attribute ) ?? [ ]
145
- this . _attributes . set ( attribute , [
146
- ...new Set ( currentVariables . concat ( [ variable ] ) ) ,
147
- ] )
166
+ this . _attributes . set ( attribute , [ ...currentVariables , variable ] )
148
167
}
149
168
150
169
remove ( variable , attributes ) {
0 commit comments