在JavaScript中创建动态重新计划GSource [英] Create a dynamic rescheduling GSource in JavaScript

查看:83
本文介绍了在JavaScript中创建动态重新计划GSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GLib的主循环支持调度使用

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.

我现在想以动态间隔修改此过程.回调不仅应返回truefalse,还应能够返回一个时间值,该值应在下一次调用之前传递.

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.

在C中执行此操作非常简单:可以创建一个新的GSource类型,该类型仅与

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.

不幸的是,我正在为GNOME Shell编写扩展,所以我坚持使用JavaScript.将上述策略移植到JavaScript的主要关键点似乎等同于 g_source_new function new GLib.Source.首先,它需要初始化struct类型的长度,这将由C中的sizeof运算符计算.我不知道如何在JavaScript中获取此值.另外,尝试创建

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

如何使用JavaScript创建新的GSource?

How can I create a new GSource in JavaScript?

推荐答案

g_source_new()并不是真正为语言绑定而设计的,应该在为JS或Python生成绑定时标记为跳过.

g_source_new() was not really designed for language bindings and should probably be marked to be skipped when generating bindings for JS or Python.

正如您在其他问题中所建议的那样,包括您自己的私有C库(可以通过GObject自省访问),这就是我通常在应用程序中要做的事情.但是,我不知道是否可以为shell扩展做这件事.

Including your own private C library, accessed via GObject introspection, as you suggest in your other question, is what I would usually do in an app. However, I have no idea if you can do it for a shell extension.

不过,您应该很容易就能在JS中实现所需的功能.这是我从内存中写的一个简单示例,看起来它可以满足您的要求:

You should quite easily be able to implement what you want in JS, though. Here's a simple example I wrote from memory that seems like it might do what you want:

const Scheduler = new Lang.Class({
    Name: 'Scheduler',
    schedule: function (timeMs, callback, priority=GLib.PRIORITY_DEFAULT) {
        this._callback = callback;
        this._priority = priority;
        GLib.timeout_add(priority, timeMs, this._onTimeout.bind(this));
    },
    _onTimeout: function (
        let nextTimeoutMs = this._callback();
        this.schedule(nextTimeoutMs, this._callback, this._priority);
        return GLib.SOURCE_REMOVE;
    },
});

这篇关于在JavaScript中创建动态重新计划GSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆