Skip to content

Commit

Permalink
makes event listener callbacks execute asynchronously
Browse files Browse the repository at this point in the history
  • Loading branch information
rovolution committed Dec 22, 2018
1 parent a5fc012 commit b725613
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 148 deletions.
13 changes: 9 additions & 4 deletions src/js/bootstrap-slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const windowIsDefined = (typeof window === "object");
const NAMESPACE_MAIN = 'slider';
const NAMESPACE_ALTERNATE = 'bootstrapSlider';

// Polyfill console methods
// Polyfills
if (windowIsDefined && !window.console) {
window.console = {};
}
Expand All @@ -78,6 +78,9 @@ const windowIsDefined = (typeof window === "object");
if (windowIsDefined && !window.console.warn) {
window.console.warn = function () { };
}
if (windowIsDefined && !window.setTimeout) {
window.setTimeout = function(fn) { fn(); };
}

// Reference to Slider constructor
var Slider;
Expand Down Expand Up @@ -1862,9 +1865,11 @@ const windowIsDefined = (typeof window === "object");

var callbackFnArray = this.eventToCallbackMap[evt];
if(callbackFnArray && callbackFnArray.length) {
for(var i = 0; i < callbackFnArray.length; i++) {
var callbackFn = callbackFnArray[i];
callbackFn(val);
for(let i = 0; i < callbackFnArray.length; i++) {
let callbackFn = callbackFnArray[i];
window.setTimeout(() => {
callbackFn(val);
}, 0);
}
}

Expand Down
11 changes: 3 additions & 8 deletions test/specs/DraggingHandlesSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,28 +221,23 @@ describe("Dragging handles tests", function() {
expect(testSlider.getValue()).toEqual([2, 5]);
});

it("Should trigger change on mouseup", function() {
var ups = 0;
it("Should trigger change on mouseup", function(done) {
var changes = 0;

testSlider.on('slideStop', function(){
expect(changes).toBe(1);
ups++;
expect(testSlider.getValue()).toEqual([2, 5]);
done();
});

testSlider.mousedown(mouseEvent('mousedown', tickOffsets[1]));
expect(testSlider.getValue()).toEqual([1, 5]);
expect(ups).toBe(0);

testSlider.on('change', function(){
expect(ups).toBe(0);
changes++;
});

testSlider.mouseup(mouseEvent('mouseup', tickOffsets[2]));
expect(testSlider.getValue()).toEqual([2, 5]);
expect(changes).toBe(1);
expect(ups).toBe(1);
});

describe("Test 'mousemove' and 'mouseup' produces correct results", function() {
Expand Down
Loading

0 comments on commit b725613

Please sign in to comment.