chore: extend Backpressure with `oneInTimeQueued`

This commit is contained in:
Patryk Osmaczko 2023-04-06 19:37:45 +02:00 committed by osmaczko
parent 3e3c9e7bdf
commit 4a92405444
1 changed files with 28 additions and 2 deletions

View File

@ -21,13 +21,13 @@ Item {
owner = backpressure; owner = backpressure;
} }
owner.Component.destruction.connect(cleanup); owner.Component.onDestruction.connect(cleanup);
var obj = Qt.createQmlObject('import QtQuick 2.0; Timer {running: false; repeat: false; interval: ' + timeout + '}', backpressure, "setTimeout"); var obj = Qt.createQmlObject('import QtQuick 2.0; Timer {running: false; repeat: false; interval: ' + timeout + '}', backpressure, "setTimeout");
obj.triggered.connect(function() { obj.triggered.connect(function() {
callback(); callback();
obj.destroy(); obj.destroy();
owner.Component.destruction.disconnect(cleanup); owner.Component.onDestruction.disconnect(cleanup);
delete _timers[tid]; delete _timers[tid];
}); });
obj.running = true; obj.running = true;
@ -62,6 +62,32 @@ Item {
} }
} }
// Same as `oneInTime` while also handling any queued calls
function oneInTimeQueued(owner, duration, callback) {
var pending = false;
var queued = false;
var timerId = null;
var proxy = function() {
if (pending) {
queued = true;
return;
}
pending = true;
queued = false;
var args = arguments;
callback.apply(null, args);
timerId = setTimeout(owner, duration , function() {
pending = false;
if (queued) {
proxy(owner, duration, callback);
}
}, duration);
}
return proxy
}
function promisedOneInTime(owner, callback) { function promisedOneInTime(owner, callback) {
var q = priv.loadPromiseLib(); var q = priv.loadPromiseLib();
var promise = null; var promise = null;