Skip to content

Commit

Permalink
rename atk.debounce to atk.createDebouncedFx and fix wrong usage
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Oct 13, 2022
1 parent beb5a35 commit c7793dc
Show file tree
Hide file tree
Showing 18 changed files with 133 additions and 98 deletions.
10 changes: 5 additions & 5 deletions js/src/plugins/conditional-form.plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ export default class AtkConditionalFormPlugin extends AtkPlugin {
}
// add change listener to inputs according to selector
this.$el.find(':checkbox')
.on('change', this, atk.debounce(this.onInputChange, 100, true));
.on('change', this, atk.createDebouncedFx(this.onInputChange, 100, true));
this.$el.find(':radio')
.on('change', this, atk.debounce(this.onInputChange, 100, true));
.on('change', this, atk.createDebouncedFx(this.onInputChange, 100, true));
this.$el.find('input[type="hidden"]')
.on('change', this, atk.debounce(this.onInputChange, 100, true));
.on('change', this, atk.createDebouncedFx(this.onInputChange, 100, true));
this.$el.find('input')
.on(this.settings.validateEvent, this, atk.debounce(this.onInputChange, 250));
.on(this.settings.validateEvent, this, atk.createDebouncedFx(this.onInputChange, 250));
this.$el.find('select')
.on('change', this, atk.debounce(this.onInputChange, 100));
.on('change', this, atk.createDebouncedFx(this.onInputChange, 100));

this.initialize();
}
Expand Down
4 changes: 2 additions & 2 deletions js/src/plugins/js-search.plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default class AtkJsSearchPlugin extends AtkPlugin {
* Query server on each keystroke after proper timeout.
*/
onAutoQueryAction() {
this.textInput.on('keyup', atk.debounce((e) => {
this.textInput.on('keyup', atk.createDebouncedFx((e) => {
const options = $.extend({}, this.urlArgs, this.settings.urlOptions);
if (e.target.value === '' || e.keyCode === 27) {
this.doSearch(this.settings.url, null, options, () => {
Expand Down Expand Up @@ -205,6 +205,6 @@ AtkJsSearchPlugin.DEFAULTS = {
urlQueryKey: null,
q: null,
autoQuery: false,
timeOut: 300,
timeOut: 250,
useAjax: true,
};
4 changes: 2 additions & 2 deletions js/src/services/panel.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ class PanelService {
*/
addClickAwayEvent(id) {
// clicking anywhere in main tag will close panel.
$('main').on('click.atkPanel', atk.debounce((evt) => {
$('main').on('click.atkPanel', atk.createDebouncedFx((evt) => {
this.closePanel(id);
}, 250));
}
Expand All @@ -290,7 +290,7 @@ class PanelService {
*/
addEscAwayEvent(id) {
// pressing esc key will close panel.
$(document).on('keyup.atkPanel', atk.debounce((evt) => {
$(document).on('keyup.atkPanel', atk.createDebouncedFx((evt) => {
if (evt.keyCode === 27) {
this.closePanel(id);
}
Expand Down
20 changes: 10 additions & 10 deletions js/src/setup-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ atk.eventBus = (function () {
};
}());

atk.debounce = function (func, wait, options) {
atk.createDebouncedFx = function (func, wait, options) {
let timerId = null;
let debouncedInner;
let lodashDebouncedFx;

function createTimer() {
timerId = setInterval(() => {
if (!debouncedInner.pending()) {
if (!lodashDebouncedFx.pending()) {
clearInterval(timerId);
timerId = null;
$.active--;
Expand All @@ -54,20 +54,20 @@ atk.debounce = function (func, wait, options) {
$.active++;
}

debouncedInner = lodashDebounce(func, wait, options);
lodashDebouncedFx = lodashDebounce(func, wait, options);

function debounced(...args) {
function debouncedFx(...args) {
if (timerId === null) {
createTimer();
}

return debouncedInner(...args);
return lodashDebouncedFx(...args);
}
debounced.cancel = debouncedInner.cancel;
debounced.flush = debouncedInner.flush;
debounced.pending = debouncedInner.pending;
debouncedFx.cancel = lodashDebouncedFx.cancel;
debouncedFx.flush = lodashDebouncedFx.flush;
debouncedFx.pending = lodashDebouncedFx.pending;

return debounced;
return debouncedFx;
};

/*
Expand Down
20 changes: 12 additions & 8 deletions js/src/vue-components/item-search.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,19 @@ export default {
},
methods: {
onKeyup: function () {
atk.debounce((e) => {
if (this.query !== this.temp) {
if (this.query === '') {
this.query = null;
if (!this.onKeyup.debouncedFx) {
this.onKeyup.debouncedFx = atk.createDebouncedFx((e) => {
this.onKeyup.debouncedFx = null;
if (this.query !== this.temp) {
if (this.query === '') {
this.query = null;
}
this.sendQuery();
this.temp = this.query;
}
this.sendQuery();
this.temp = this.query;
}
}, this.options.inputTimeOut).call(this);
}, this.options.inputTimeOut);
}
this.onKeyup.debouncedFx.call(this);
},
onEscape: function () {
if (this.query !== null) {
Expand Down
12 changes: 8 additions & 4 deletions js/src/vue-components/multiline/multiline.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,14 @@ export default {
this.clearError(atkmlId, fieldName);
this.updateInputValue();

atk.debounce(() => {
this.fetchExpression(atkmlId);
this.fetchOnChangeAction(fieldName);
}, 300).call(this);
if (!this.onUpdate.debouncedFx) {
this.onUpdate.debouncedFx = atk.createDebouncedFx(() => {
this.onUpdate.debouncedFx = null;
this.fetchExpression(atkmlId);
this.fetchOnChangeAction(fieldName);
}, 250);
}
this.onUpdate.debouncedFx.call(this);
},
/**
* Creates a new row of data and
Expand Down
23 changes: 14 additions & 9 deletions js/src/vue-components/share/atk-lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,20 @@ export default {
if (inputValue) {
this.isLoading = true;
}
this.temp = inputValue;
atk.debounce(() => {
if (this.query !== this.temp) {
this.query = this.temp;
if (this.query) {
this.fetchItems(this.query);

if (!this.onFiltered.debouncedFx) {
this.onFiltered.debouncedFx = atk.createDebouncedFx(() => {
this.onFiltered.debouncedFx = null;
if (this.query !== this.temp) {
this.query = this.temp;
if (this.query) {
this.fetchItems(this.query);
}
}
}
}, 300).call(this);
}, 250);
}
this.temp = inputValue;
this.onFiltered.debouncedFx(this);
},
/**
* Fetch new data from server.
Expand All @@ -80,9 +85,9 @@ export default {
if (response.success) {
this.dropdownProps.options = response.results;
}
this.isLoading = false;
} catch (e) {
console.error(e);
} finally {
this.isLoading = false;
}
},
Expand Down
22 changes: 14 additions & 8 deletions public/js/atk-vue-item-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,22 @@ const template = `<div class="atk-item-search" :class="inputCss">
},
methods: {
onKeyup: function () {
atk__WEBPACK_IMPORTED_MODULE_1__["default"].debounce(e => {
if (this.query !== this.temp) {
if (this.query === '') {
this.query = null;
if (!this.onKeyup.debouncedFx) {
this.onKeyup.debouncedFx = atk__WEBPACK_IMPORTED_MODULE_1__["default"].createDebouncedFx(e => {
this.onKeyup.debouncedFx = null;

if (this.query !== this.temp) {
if (this.query === '') {
this.query = null;
}

this.sendQuery();
this.temp = this.query;
}
}, this.options.inputTimeOut);
}

this.sendQuery();
this.temp = this.query;
}
}, this.options.inputTimeOut).call(this);
this.onKeyup.debouncedFx.call(this);
},
onEscape: function () {
if (this.query !== null) {
Expand Down
2 changes: 1 addition & 1 deletion public/js/atk-vue-item-search.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c7793dc

Please sign in to comment.