Skip to content

Commit

Permalink
[SpicesUpdate@claudiux] v7.5.1: Now uses mainloopTools to optimize lo…
Browse files Browse the repository at this point in the history
…op management (#6754)

* SpicesUpdate v7.5.1: Now use mainloopTools to optimize loop management

* SpicesUpdate v7.5.1: applet.js code clean-up

* SpicesUpdate v7.5.1: applet.js code clean-up 2
  • Loading branch information
claudiux authored Jan 12, 2025
1 parent 5ce5c6d commit f0f5587
Show file tree
Hide file tree
Showing 6 changed files with 266 additions and 117 deletions.
168 changes: 70 additions & 98 deletions SpicesUpdate@claudiux/files/SpicesUpdate@claudiux/6.0/applet.js

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const {
source_remove,
timeout_add_seconds,
timeout_add
} = imports.mainloop; //Mainloop
} = require("mainloopTools");

/**
* Usage of log and logError:
Expand Down
191 changes: 191 additions & 0 deletions SpicesUpdate@claudiux/files/SpicesUpdate@claudiux/6.0/mainloopTools.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
const GLib = imports.gi.GLib;
const Mainloop = imports.mainloop;

/**
* _sourceIds
* Array containing IDs of all looping loops.
*/
var _sourceIds = [];

/**
* timeout_add_seconds
*
* @callback (function) is executed every @sec (number) seconds
* with @params (dictionnary as {'key1': value1, 'key2': value2, ...}).
*
* @params is often null.
*
*/
function timeout_add_seconds(sec, callback, params=null) {
let id = Mainloop.timeout_add_seconds(sec, callback, params);
if (id && (_sourceIds.indexOf(id) === -1)) _sourceIds.push(id);
return id;
}

/**
* timeout_add_seconds
*
* @callback (function) is executed every @ms (number) milliseconds
* with @params (dictionnary as {'key1': value1, 'key2': value2, ...}).
*
* @params is often null.
*
*/
function timeout_add(ms, callback, params=null) {
let id = Mainloop.timeout_add_seconds(ms, callback, params);
if (id && (_sourceIds.indexOf(id) === -1)) _sourceIds.push(id);
return id;
}

/**
* setTimeout:
* @callback (function): Function to call at the end of the timeout.
* @ms (number): Milliseconds until the timeout expires.
*
* Convenience wrapper for a Mainloop.timeout_add loop that
* returns false.
*
* Returns (number): The ID of the loop.
*/
function setTimeout(callback, ms) {
let args = [];
if (arguments.length > 2) {
args = args.slice.call(arguments, 2);
}

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

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

return id;
}

/**
* clearTimeout:
* @id (number): The ID of the loop to remove.
*
* Convenience wrapper for Mainloop.source_remove.
*/
function clearTimeout(id) {
if (id) {
Mainloop.source_remove(id);
const pos = _sourceIds.indexOf(id);
if (pos > -1) _sourceIds.splice(pos, 1);
}
}


/**
* setInterval:
* @callback (function): Function to call on every interval.
* @ms (number): Milliseconds between invocations.
*
* Convenience wrapper for a Mainloop.timeout_add loop that
* returns true.
*
* Returns (number): The ID of the loop.
*/
function setInterval(callback, ms) {
let args = [];
if (arguments.length > 2) {
args = args.slice.call(arguments, 2);
}

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

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

return id;
}

/**
* clearInterval:
* @id (number): The ID of the loop to remove.
*
* Convenience wrapper for Mainloop.source_remove.
*/
function clearInterval(id) {
if (id) {
Mainloop.source_remove(id);
const pos = _sourceIds.indexOf(id);
if (pos > -1) _sourceIds.splice(pos, 1);
}
};

/**
* source_exists
*
* @id (number, or null)
*
* Checks if @id is well the ID of a loop.
*
*/
function source_exists(id) {
let _id = id;
if (!_id) return false;
return (GLib.MainContext.default().find_source_by_id(_id) != null);
}

//~ function timeout_exists(id) {
//~ if (!id) return false;
//~ if (!id.source_id) return false;
//~ return (GLib.MainContext.default().find_source_by_id(id.source_id) != null);
//~ }

//~ function interval_exists(id) {
//~ return timeout_exists(id);
//~ }

/**
* source_remove
*
* @id (number): The ID of the loop to stop.
* @remove_from_sourceIds (boolean): *true* (by default) when we want to
* remove @id from _sourceIds. May be *false* for internal functionning.
*
* Convenience wrapper for a Mainloop.source_remove(id) that returns a
* boolean.
*/
function source_remove(id, remove_from_sourceIds=true) {
if (source_exists(id)) {
Mainloop.source_remove(id);
if (remove_from_sourceIds) {
const pos = _sourceIds.indexOf(id);
if (pos > -1) _sourceIds.splice(pos, 1);
}
return true;
}
return false;
}

/**
* remove_all_sources
*
* Execute it when removing the spice.
* Tries to delete all remaining sources, in order to remove all loops.
*/
function remove_all_sources() {
while (_sourceIds.length > 0) {
let id = _sourceIds.pop();
source_remove(id, false);
}
}

module.exports = {
_sourceIds,
timeout_add_seconds,
timeout_add,
setTimeout,
clearTimeout,
setInterval,
clearInterval,
source_exists,
source_remove,
remove_all_sources
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### v7.5.1~20250112
* Now use mainloopTools to optimize loop management.

### v7.5.0~20250111
* Improved management of loops.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "Spices Update",
"max-instances": "1",
"hide-configuration": false,
"version": "7.5.0",
"version": "7.5.1",
"description": "Warns you when installed Spices (actions, applets, desklets, extensions, themes) require an update or new Spices are available.",
"multiversion": true,
"cinnamon-version": [
Expand Down

0 comments on commit f0f5587

Please sign in to comment.