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 : : // eslint-disable-next-line camelcase
36 : 6 : function idle_source(handler, priority) {
37 : 6 : let s = GLib.idle_source_new();
38 : 6 : GObject.source_set_closure(s, handler);
39 [ + - ]: 6 : if (priority !== undefined)
40 : 0 : s.set_priority(priority);
41 : 6 : return s;
42 : 6 : }
43 : :
44 : : // eslint-disable-next-line camelcase
45 : 6 : function idle_add(handler, priority) {
46 : 6 : return idle_source(handler, priority).attach(null);
47 : : }
48 : :
49 : : // eslint-disable-next-line camelcase
50 : 3 : function timeout_source(timeout, handler, priority) {
51 : 3 : let s = GLib.timeout_source_new(timeout);
52 : 3 : GObject.source_set_closure(s, handler);
53 [ + - ]: 3 : if (priority !== undefined)
54 : 0 : s.set_priority(priority);
55 : 3 : return s;
56 : 3 : }
57 : :
58 : : // eslint-disable-next-line camelcase
59 : 0 : function timeout_seconds_source(timeout, handler, priority) {
60 : 0 : let s = GLib.timeout_source_new_seconds(timeout);
61 : 0 : GObject.source_set_closure(s, handler);
62 [ # # ]: 0 : if (priority !== undefined)
63 : 0 : s.set_priority(priority);
64 : 0 : return s;
65 : 0 : }
66 : :
67 : : // eslint-disable-next-line camelcase
68 : 3 : function timeout_add(timeout, handler, priority) {
69 : 3 : return timeout_source(timeout, handler, priority).attach(null);
70 : : }
71 : :
72 : : // eslint-disable-next-line camelcase
73 : 0 : function timeout_add_seconds(timeout, handler, priority) {
74 : 0 : return timeout_seconds_source(timeout, handler, priority).attach(null);
75 : : }
76 : :
77 : : // eslint-disable-next-line camelcase
78 : 3 : function source_remove(id) {
79 : 3 : return GLib.source_remove(id);
80 : : }
|