-
Notifications
You must be signed in to change notification settings - Fork 526
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SpicesUpdate@claudiux] v7.5.1: Now uses mainloopTools to optimize lo…
…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
Showing
6 changed files
with
266 additions
and
117 deletions.
There are no files selected for viewing
168 changes: 70 additions & 98 deletions
168
SpicesUpdate@claudiux/files/SpicesUpdate@claudiux/6.0/applet.js
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
191 changes: 191 additions & 0 deletions
191
SpicesUpdate@claudiux/files/SpicesUpdate@claudiux/6.0/mainloopTools.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
17 changes: 0 additions & 17 deletions
17
SpicesUpdate@claudiux/files/SpicesUpdate@claudiux/6.0/sourceExists.js
This file was deleted.
Oops, something went wrong.
3 changes: 3 additions & 0 deletions
3
SpicesUpdate@claudiux/files/SpicesUpdate@claudiux/CHANGELOG.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters