Branch data Line data Source code
1 : 2 : /* -*- mode: js; indent-tabs-mode: nil; -*- */
2 : : // SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
3 : : // SPDX-FileCopyrightText: 2012 Giovanni Campagna <scampa.giovanni@gmail.com>
4 : :
5 : : /* exported idle_add, idle_source, quit, run, source_remove, timeout_add,
6 : : timeout_add_seconds, timeout_seconds_source, timeout_source */
7 : :
8 : : // A layer of convenience and backwards-compatibility over GLib MainLoop facilities
9 : :
10 : 2 : const GLib = imports.gi.GLib;
11 : 2 : const GObject = imports.gi.GObject;
12 : :
13 : 2 : var _mainLoops = {};
14 : :
15 : 2 : function run(name) {
16 [ - + ]: 2 : if (!_mainLoops[name])
17 : 2 : _mainLoops[name] = GLib.MainLoop.new(null, false);
18 : :
19 : 2 : _mainLoops[name].run();
20 : : }
21 : :
22 : 2 : function quit(name) {
23 [ + - ]: 2 : if (!_mainLoops[name])
24 : 0 : throw new Error('No main loop with this id');
25 : :
26 : 2 : let loop = _mainLoops[name];
27 : 2 : _mainLoops[name] = null;
28 : :
29 [ + - ]: 2 : if (!loop.is_running())
30 : 0 : throw new Error('Main loop was stopped already');
31 : :
32 : 2 : loop.quit();
33 : : }
34 : :
35 : 6 : function idle_source(handler, priority) {
36 : 6 : let s = GLib.idle_source_new();
37 : 6 : GObject.source_set_closure(s, handler);
38 [ + - ]: 6 : if (priority !== undefined)
39 : 0 : s.set_priority(priority);
40 : 6 : return s;
41 : 6 : }
42 : :
43 : 6 : function idle_add(handler, priority) {
44 : 6 : return idle_source(handler, priority).attach(null);
45 : : }
46 : :
47 : 3 : function timeout_source(timeout, handler, priority) {
48 : 3 : let s = GLib.timeout_source_new(timeout);
49 : 3 : GObject.source_set_closure(s, handler);
50 [ + - ]: 3 : if (priority !== undefined)
51 : 0 : s.set_priority(priority);
52 : 3 : return s;
53 : 3 : }
54 : :
55 : 0 : function timeout_seconds_source(timeout, handler, priority) {
56 : 0 : let s = GLib.timeout_source_new_seconds(timeout);
57 : 0 : GObject.source_set_closure(s, handler);
58 [ # # ]: 0 : if (priority !== undefined)
59 : 0 : s.set_priority(priority);
60 : 0 : return s;
61 : 0 : }
62 : :
63 : 3 : function timeout_add(timeout, handler, priority) {
64 : 3 : return timeout_source(timeout, handler, priority).attach(null);
65 : : }
66 : :
67 : 0 : function timeout_add_seconds(timeout, handler, priority) {
68 : 0 : return timeout_seconds_source(timeout, handler, priority).attach(null);
69 : : }
70 : :
71 : 3 : function source_remove(id) {
72 : 3 : return GLib.source_remove(id);
73 : : }
|