Skip to content

Commit

Permalink
mainloopTools library: improvements (#1387)
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiux authored Jan 17, 2025
1 parent 19a46c8 commit 7061652
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions [email protected]/files/[email protected]/lib/mainloopTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ var _sourceIds = [];
*
*/
function timeout_add_seconds(sec, callback, params=null) {
let id = Mainloop.timeout_add_seconds(sec, callback, params);
//~ let id = Mainloop.timeout_add_seconds(sec, callback, params);
//~ let id = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, sec, callback, params);
let id = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, sec, callback);
if (id && (_sourceIds.indexOf(id) === -1)) _sourceIds.push(id);
return id;
}
Expand All @@ -32,7 +34,9 @@ function timeout_add_seconds(sec, callback, params=null) {
*
*/
function timeout_add(ms, callback, params=null) {
let id = Mainloop.timeout_add_seconds(ms, callback, params);
//~ let id = Mainloop.timeout_add_seconds(ms, callback, params);
//~ let id = GLib.timeout_add(GLib.PRIORITY_DEFAULT, ms, callback, params);
let id = GLib.timeout_add(GLib.PRIORITY_DEFAULT, ms, callback);
if (id && (_sourceIds.indexOf(id) === -1)) _sourceIds.push(id);
return id;
}
Expand All @@ -53,10 +57,14 @@ function setTimeout(callback, ms) {
args = args.slice.call(arguments, 2);
}

let id = Mainloop.timeout_add(ms, () => {
//~ let id = Mainloop.timeout_add(ms, () => {
let id = GLib.timeout_add(GLib.PRIORITY_DEFAULT,
ms,
() => {
callback.call(null, ...args);
return false; // Stop repeating
}, null);
}
);

if (id && (_sourceIds.indexOf(id) === -1)) _sourceIds.push(id);

Expand All @@ -71,7 +79,8 @@ function setTimeout(callback, ms) {
*/
function clearTimeout(id) {
if (id) {
Mainloop.source_remove(id);
//~ Mainloop.source_remove(id);
GLib.source_remove(id);
const pos = _sourceIds.indexOf(id);
if (pos > -1) _sourceIds.splice(pos, 1);
}
Expand All @@ -94,10 +103,11 @@ function setInterval(callback, ms) {
args = args.slice.call(arguments, 2);
}

let id = Mainloop.timeout_add(ms, () => {
//~ let id = Mainloop.timeout_add(ms, () => {
let id = GLib.timeout_add(GLib.PRIORITY_DEFAULT, ms, () => {
callback.call(null, ...args);
return true; // Repeat
}, null);
});

if (id && (_sourceIds.indexOf(id) === -1)) _sourceIds.push(id);

Expand All @@ -112,7 +122,8 @@ function setInterval(callback, ms) {
*/
function clearInterval(id) {
if (id) {
Mainloop.source_remove(id);
//~ Mainloop.source_remove(id);
GLib.source_remove(id);
const pos = _sourceIds.indexOf(id);
if (pos > -1) _sourceIds.splice(pos, 1);
}
Expand Down Expand Up @@ -154,7 +165,8 @@ function source_exists(id) {
*/
function source_remove(id, remove_from_sourceIds=true) {
if (source_exists(id)) {
Mainloop.source_remove(id);
//~ Mainloop.source_remove(id);
GLib.source_remove(id);
if (remove_from_sourceIds) {
const pos = _sourceIds.indexOf(id);
if (pos > -1) _sourceIds.splice(pos, 1);
Expand Down

0 comments on commit 7061652

Please sign in to comment.