Add debounce logic.

This commit is contained in:
David Bau
2022-03-17 20:40:29 -04:00
parent 23524aa811
commit 16c9317de3
+21 -1
View File
@@ -635,6 +635,9 @@ class Range(Widget):
# Note that the 'input' event would enable during-drag feedback,
# but this is pretty slow on google colab.
return minify('''
element.addEventListener('input', (e) => {
model.debounce('value', parseFloat(element.value));
});
element.addEventListener('change', (e) => {
model.set('value', parseFloat(element.value));
});
@@ -1075,13 +1078,22 @@ class Model {
constructor(obj_id, init) {
this._id = obj_id;
this._listeners = {};
this._data = Object.assign({}, init)
this._data = Object.assign({}, init);
this._sent = {};
recvFromPython(this._id, (name, value) => {
this._data[name] = value;
var e = new Event(name); e.value = value;
if (this._listeners.hasOwnProperty(name)) {
this._listeners[name].forEach((fn) => { fn(e); });
}
if (this._sent[name]) {
value = this._sent[name];
delete this._sent[name];
if (value.length) {
value = value[0];
this.debounce(name, value);
}
}
})
}
trigger(name, value) {
@@ -1093,6 +1105,14 @@ class Model {
set(name, value) {
this.trigger(name, value);
}
debounce(name, value) {
if (!this._sent.hasOwnProperty(name)) {
this._sent[name] = [];
this.trigger(name, value);
} else {
this._sent[name] = [value];
}
}
on(name, fn) {
name.split(/\s+/).forEach((n) => {
if (!this._listeners.hasOwnProperty(n)) {