Skip to content

Commit 2e4c17f

Browse files
committed
Add current value tracking to prevent duplicate event dispatch
1 parent ea06bdf commit 2e4c17f

File tree

1 file changed

+37
-24
lines changed

1 file changed

+37
-24
lines changed

src/slider/SliderWebcomponent.ts

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ export class SliderWebcomponent extends BaseCustomWebComponentConstructorAppend
8181
private _rangeInputs: HTMLInputElement[];
8282
private _valuesGap: number = 1;
8383
private _suppressAttributeChange: boolean = false;
84+
private _currentMinVal: number;
85+
private _currentMaxVal: number;
8486

8587
attributeChangedCallback(name: string, oldValue: string, newValue: string) {
8688
if (this._suppressAttributeChange) return;
@@ -99,15 +101,17 @@ export class SliderWebcomponent extends BaseCustomWebComponentConstructorAppend
99101
this._restoreCachedInititalValues();
100102
}
101103

102-
connectedCallback() {
103-
this._rangeInputs = Array.from(this._getDomElements(".range-input input"));
104-
}
105-
106104
disconnectedCallback() { }
107105

108106
ready() {
107+
this._rangeInputs = Array.from(this._getDomElements(".range-input input"));
108+
109109
this._parseAttributesToProperties();
110110

111+
// Initialize current values
112+
this._currentMinVal = parseInt(this.getAttribute('value-min'));
113+
this._currentMaxVal = parseInt(this.getAttribute('value-max'));
114+
111115
// Add event listeners to range input elements
112116
for (let i = 0; i < this._rangeInputs.length; i++) {
113117
this._rangeInputs[i].addEventListener("input", e => {
@@ -121,8 +125,8 @@ export class SliderWebcomponent extends BaseCustomWebComponentConstructorAppend
121125

122126
this._updateRangeInputsMinMax();
123127
this._updateRangeInputsValues();
124-
this._updateSliderPosition(parseInt(this.getAttribute('value-min')), parseInt(this.getAttribute('max')), true);
125-
this._updateSliderPosition(parseInt(this.getAttribute('value-max')), parseInt(this.getAttribute('max')), false);
128+
this._updateSliderPosition(this._currentMinVal, parseInt(this.getAttribute('max')), true);
129+
this._updateSliderPosition(this._currentMaxVal, parseInt(this.getAttribute('max')), false);
126130
}
127131

128132
private _validateValues(changedAttr: string, newValue: string) {
@@ -141,8 +145,12 @@ export class SliderWebcomponent extends BaseCustomWebComponentConstructorAppend
141145
const oldValueMax = parseInt(this.getAttribute('value-max'));
142146

143147
this._suppressAttributeChange = true;
144-
this.setAttribute('value-min', valueMin.toString());
145-
this.setAttribute('value-max', valueMax.toString());
148+
if (valueMin !== oldValueMin) {
149+
this.setAttribute('value-min', valueMin.toString());
150+
}
151+
if (valueMax !== oldValueMax) {
152+
this.setAttribute('value-max', valueMax.toString());
153+
}
146154
this._suppressAttributeChange = false;
147155

148156
this._updateRangeInputsValues();
@@ -157,15 +165,19 @@ export class SliderWebcomponent extends BaseCustomWebComponentConstructorAppend
157165
}
158166

159167
private _updateRangeInputsMinMax() {
160-
this._rangeInputs.forEach(rangeInput => {
161-
rangeInput.min = this.getAttribute('min');
162-
rangeInput.max = this.getAttribute('max');
163-
});
168+
if (this._rangeInputs) {
169+
this._rangeInputs.forEach(rangeInput => {
170+
rangeInput.min = this.getAttribute('min');
171+
rangeInput.max = this.getAttribute('max');
172+
});
173+
}
164174
}
165175

166176
private _updateRangeInputsValues() {
167-
this._rangeInputs[0].value = this.getAttribute('value-min');
168-
this._rangeInputs[1].value = this.getAttribute('value-max');
177+
if (this._rangeInputs) {
178+
this._rangeInputs[0].value = this.getAttribute('value-min');
179+
this._rangeInputs[1].value = this.getAttribute('value-max');
180+
}
169181
}
170182

171183
private _handleRangeInput(e: Event) {
@@ -185,8 +197,12 @@ export class SliderWebcomponent extends BaseCustomWebComponentConstructorAppend
185197
} else {
186198
// Update input values and range progress
187199
this._suppressAttributeChange = true;
188-
this.setAttribute('value-min', minVal.toString());
189-
this.setAttribute('value-max', maxVal.toString());
200+
if (parseInt(this.getAttribute('value-min')) !== minVal) {
201+
this.setAttribute('value-min', minVal.toString());
202+
}
203+
if (parseInt(this.getAttribute('value-max')) !== maxVal) {
204+
this.setAttribute('value-max', maxVal.toString());
205+
}
190206
this._suppressAttributeChange = false;
191207
this._updateSliderPosition(minVal, parseInt(this.getAttribute('max')), true);
192208
this._updateSliderPosition(maxVal, parseInt(this.getAttribute('max')), false);
@@ -197,21 +213,18 @@ export class SliderWebcomponent extends BaseCustomWebComponentConstructorAppend
197213
let minVal = parseInt(this._rangeInputs[0].value);
198214
let maxVal = parseInt(this._rangeInputs[1].value);
199215

200-
// Prevent duplicate event firing
201-
const currentMinVal = parseInt(this.getAttribute('value-min'));
202-
const currentMaxVal = parseInt(this.getAttribute('value-max'));
203-
204-
this._suppressAttributeChange = true;
205-
this.setAttribute('value-min', minVal.toString());
206-
this.setAttribute('value-max', maxVal.toString());
207-
this._suppressAttributeChange = false;
216+
// Check if values actually changed
217+
if (minVal !== this._currentMinVal || maxVal !== this._currentMaxVal) {
218+
this._currentMinVal = minVal;
219+
this._currentMaxVal = maxVal;
208220

209221
// Dispatch the appropriate event
210222
if ((e.target as HTMLInputElement).className === "min-range") {
211223
this._dispatchChangeEvent('value-min-changed', minVal);
212224
} else {
213225
this._dispatchChangeEvent('value-max-changed', maxVal);
214226
}
227+
}
215228
}
216229

217230
private _updateSliderPosition(value: number, max: number, isMin: boolean) {

0 commit comments

Comments
 (0)