Skip to content

Commit

Permalink
chore: extend Backpressure with oneInTimeQueued
Browse files Browse the repository at this point in the history
  • Loading branch information
osmaczko committed Apr 7, 2023
1 parent 3e3c9e7 commit 4a92405
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions ui/imports/utils/Backpressure/Backpressure.qml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ Item {
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");
obj.triggered.connect(function() {
callback();
obj.destroy();
owner.Component.destruction.disconnect(cleanup);
owner.Component.onDestruction.disconnect(cleanup);
delete _timers[tid];
});
obj.running = true;
Expand Down Expand Up @@ -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) {
var q = priv.loadPromiseLib();
var promise = null;
Expand Down

0 comments on commit 4a92405

Please sign in to comment.