1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
# Mainloop
The `Mainloop` module is a convenience layer for some common event loop methods
in GLib, such as [`GLib.timeout_add()`][gtimeoutadd].
This module is not generally recommended, but is documented for the sake of
existing code. Each method below contains links to the corresponding GLib
method for reference.
For an introduction to the GLib event loop, see the
[Asynchronous Programming Tutorial][async-tutorial].
[async-tutorial]: https://gjs.guide/guides/gjs/asynchronous-programming.html
[gtimeoutadd]: https://gjs-docs.gnome.org/glib20/glib.timeout_add
#### Import
> Attention: This module is not available as an ECMAScript Module
The `Mainloop` module is available on the global `imports` object:
```js
const Mainloop = imports.mainloop
```
### Mainloop.idle_add(handler, priority)
> See also: [`GLib.idle_add()`][gidleadd]
Type:
* Static
Parameters:
* handler (`Function`) — The function to call
* priority (`Number`) — Optional priority
Returns:
* (`GLib.Source`) — The newly-created idle source
Adds a function to be called whenever there are no higher priority events
pending. If the function returns `false` it is automatically removed from the
list of event sources and will not be called again.
If not given, `priority` defaults to `GLib.PRIORITY_DEFAULT_IDLE`.
[gidleadd]: https://gjs-docs.gnome.org/glib20/glib.idle_add
### Mainloop.idle_source(handler, priority)
> See also: [`GLib.idle_source_new()`][gidlesourcenew]
Type:
* Static
Parameters:
* handler (`Function`) — The function to call
* priority (`Number`) — Optional priority
Returns:
* (`GLib.Source`) — The newly-created idle source
Creates a new idle source.
If not given, `priority` defaults to `GLib.PRIORITY_DEFAULT_IDLE`.
[gidlesourcenew]: https://gjs-docs.gnome.org/glib20/glib.idle_source_new
### Mainloop.quit(name)
> See also: [`GLib.MainLoop.quit()`][gmainloopquit]
Type:
* Static
Parameters:
* name (`String`) — Optional name
Stops a main loop from running. Any calls to `Mainloop.run(name)` for the loop
will return.
If `name` is given, this function will create a new [`GLib.MainLoop`][gmainloop]
if necessary.
[gmainloop]: https://gjs-docs.gnome.org/glib20/glib.mainloop
[gmainloopquit]: https://gjs-docs.gnome.org/glib20/glib.mainloop#method-quit
### Mainloop.run(name)
> See also: [`GLib.MainLoop.run()`][gmainlooprun]
Type:
* Static
Parameters:
* name (`String`) — Optional name
Runs a main loop until `Mainloop.quit()` is called on the loop.
If `name` is given, this function will create a new [`GLib.MainLoop`][gmainloop]
if necessary.
[gmainloop]: https://gjs-docs.gnome.org/glib20/glib.mainloop
[gmainlooprun]: https://gjs-docs.gnome.org/glib20/glib.mainloop#method-run
### Mainloop.source_remove(id)
> See also: [`GLib.Source.remove()`][gsourceremove]
Type:
* Static
Parameters:
* id (`Number`) — The ID of the source to remove
Returns:
* (`Boolean`) — For historical reasons, this function always returns `true`
Removes the source with the given ID from the default main context.
[gsourceremove]: https://gjs-docs.gnome.org/glib20/glib.source#function-remove
### Mainloop.timeout_add(timeout, handler, priority)
> See also: [`GLib.timeout_add()`][gtimeoutadd]
Type:
* Static
Parameters:
* timeout (`Number`) — The timeout interval in milliseconds
* handler (`Function`) — The function to call
* priority (`Number`) — Optional priority
Returns:
* (`GLib.Source`) — The newly-created timeout source
Sets a function to be called at regular intervals, with the given priority. The
function is called repeatedly until it returns `false`, at which point the
timeout is automatically destroyed and the function will not be called again.
The scheduling granularity/accuracy of this source will be in milliseconds. If
not given, `priority` defaults to `GLib.PRIORITY_DEFAULT`.
[gtimeoutadd]: https://gjs-docs.gnome.org/glib20/glib.timeout_add
### Mainloop.timeout_add_seconds(timeout, handler, priority)
> See also: [`GLib.timeout_add_seconds()`][gtimeoutaddseconds]
Type:
* Static
Parameters:
* timeout (`Number`) — The timeout interval in seconds
* handler (`Function`) — The function to call
* priority (`Number`) — Optional priority
Returns:
* (`GLib.Source`) — The newly-created timeout source
Sets a function to be called at regular intervals, with the given priority. The
function is called repeatedly until it returns `false`, at which point the
timeout is automatically destroyed and the function will not be called again.
The scheduling granularity/accuracy of this source will be in seconds. If not
given, `priority` defaults to `GLib.PRIORITY_DEFAULT`.
[gtimeoutaddseconds]: https://gjs-docs.gnome.org/glib20/glib.timeout_add_seconds
### Mainloop.timeout_source(timeout, handler, priority)
> See also: [`GLib.timeout_source_new()`][gtimeoutsourcenew]
Type:
* Static
Parameters:
* timeout (`Number`) — The timeout interval in milliseconds
* handler (`Function`) — The function to call
* priority (`Number`) — Optional priority
Returns:
* (`GLib.Source`) — The newly-created timeout source
Creates a new timeout source.
The scheduling granularity/accuracy of this source will be in milliseconds. If
not given, `priority` defaults to `GLib.PRIORITY_DEFAULT`.
[gtimeoutadd]: https://gjs-docs.gnome.org/glib20/glib.timeout_source_new
### Mainloop.timeout_seconds_source(timeout, handler, priority)
> See also: [`GLib.timeout_source_new_seconds()`][gtimeoutsourcenewseconds]
Type:
* Static
Parameters:
* timeout (`Number`) — The timeout interval in seconds
* handler (`Function`) — The function to call
* priority (`Number`) — Optional priority
Returns:
* (`GLib.Source`) — The newly-created timeout source
Creates a new timeout source.
The scheduling granularity/accuracy of this source will be in seconds. If not
given, `priority` defaults to `GLib.PRIORITY_DEFAULT`.
[gtimeoutsourcenewseconds]: https://gjs-docs.gnome.org/glib20/glib.timeout_source_new_seconds
|