GLib's main loop supports scheduling callback functions for periodic intervals, using g_timemout_source_new and related functions. The callback will repeatedly be called after the scheduled interval, until it returns false
.
I now want to modify this process with a dynamic interval. Instead of just true
or false
, the callback should be able to return a time value that should pass until its next invocation.
Doing this in C is quite straightforward: A new GSource Type can be created, that only differs from the timeout source in its dispatch function, which then takes into account the return value when setting the next expiration.
Unfortunately, I am programming an extension for the GNOME Shell, so I'm stuck to JavaScript. The main critical point to porting the above strategy to JavaScript seems to be the equivalent of the g_source_new function
, new GLib.Source
. First, it requires the length of the struct type to initialize, which would be computed by the sizeof
operator in C. I do not know how to get this value in JavaScript. In addition, it is an error to attempt the creation of a GSourceFuncs Struct, the second argument to this constructor, which is needed to hold the dispatch function.
gjs> new imports.gi.GLib.SourceFuncs()
Error: Unable to construct struct type SourceFuncs since it has no default constructor and cannot be allocated directly
How can I create a new GSource in JavaScript?