簡體   English   中英

在使用GJS進行一系列異步任務之后,我該如何安排某些事情發生?

[英]How can I schedule something to happen after a series of asynchronous tasks with GJS?

我正在使用JavaScript與GJS和GNOME平台編寫一個簡單的桌面應用程序:GTK +,GLib,Gio,GObject。 下面的代碼說明了我所面臨的情況,並且由於不需要訪問應用程序使用的文件,因此更易於復制。 簡而言之,我想完成一系列異步任務(加載一系列文件的內容) 之后運行指定的代碼行。 如何在GJS中做到這一點,最好使用Gio或JavaScript本身提供的東西?

 #!/usr/bin/gjs const Lang = imports.lang; const Gio = imports.gi.Gio; const GLib = imports.gi.GLib; const GObject = imports.gi.GObject; const Gtk = imports.gi.Gtk; const Home = new Lang.Class({ Name: "Home", // Start snippet 1 _enumerateChildrenAsyncCallback: function(dir, result) { let fileEnumerator = dir.enumerate_children_finish(result); let displayName, file, fileInfo, fileType, iter; while ((fileInfo = fileEnumerator.next_file(null))) { iter = this.model.append(); displayName = fileInfo.get_display_name(); this.model.set(iter, [0], [displayName], 1); file = dir.get_child(fileInfo.get_name()); fileType = file.query_file_type(Gio.FileQueryInfoFlags.NONE, null); if (fileType != Gio.FileType.REGULAR) continue; file.load_contents_async(null, function(file, result) { let [success, contents, etag] = file.load_contents_finish(result); let message = ""; if (success) { message = "Finished loading file %s"; } else { message = "Couldn't load file %s"; } log(message.replace("%s", file.get_basename())); }); } }, _init: function() { this.application = new Gtk.Application(); this.application.connect("activate", Lang.bind(this, this._onActivate)); this.application.connect("startup", Lang.bind(this, this._onStartup)); }, _onActivate: function() { this._window.show_all(); }, _onStartup: function() { this.model = new Gtk.ListStore(); this.model.set_column_types([GObject.TYPE_STRING]); let renderer = new Gtk.CellRendererText(); let dir = Gio.file_new_for_path(GLib.get_home_dir()); dir.enumerate_children_async("standard::*", Gio.FileQueryInfoFlags.NONE, GLib.PRIORITY_DEFAULT, null, Lang.bind(this, this._enumerateChildrenAsyncCallback), null); /* * I would like this line to be run after all files have been read. * */ this.model.set_sort_column_id(0, Gtk.SortType.ASCENDING); let column = new Gtk.TreeViewColumn({ title: "Files" }); column.pack_start(renderer, true); column.add_attribute(renderer, "text", 0); let view = new Gtk.TreeView({ model: this.model }); view.append_column(column); let scrolled = new Gtk.ScrolledWindow(); scrolled.hscrollbar_policy = Gtk.PolicyType.AUTOMATIC; scrolled.Vscrollbar_policy = Gtk.PolicyType.AUTOMATIC; scrolled.add(view); this._window = new Gtk.ApplicationWindow({ application: this.application, default_height: 300, default_width: 400, title: "Home, sweet home" }); this._window.add(scrolled); } }); let home = new Home(); home.application.run(ARGV); 

PS:在提供的代碼中,在完成所有異步任務之前運行指定的代碼行不會阻止應用程序正常運行。 但是,我想在應用程序啟動時對列表進行一次排序,而不是每次讀取文件時對列表進行排序。 而且,當然,知道如何做也可以在其他情況下有所幫助。

如果我正確理解您要做什么,則需要類似Promise.all()東西。 不幸的是,GJS還沒有一個Promise庫。

我建議在枚舉子級回調中執行類似的操作(我已經編輯了一些與我要說明的內容無關的部分):

_enumerateChildrenAsyncCallback: function(dir, result) {
  let fileEnumerator = dir.enumerate_children_finish(result);
  let displayName, file, fileInfo, fileType, iter;

  let pendingOperations = new Set();

  while ((fileInfo = fileEnumerator.next_file(null))) {
    file = dir.get_child(fileInfo.get_name());
    fileType = file.query_file_type(Gio.FileQueryInfoFlags.NONE, null);
    if (fileType != Gio.FileType.REGULAR) continue;

    pendingOperations.add(file);

    file.load_contents_async(null, (file, result) => {
      let [success, contents, etag] = file.load_contents_finish(result);

      pendingOperations.delete(file);
      if (pendingOperations.size === 0)
        this.model.set_sort_column_id(0, Gtk.SortType.ASCENDING);
    });
  }
},

乍一看,這似乎在pendingOperations清除第一個元素和添加第二個元素之間pendingOperations競爭條件,但是直到下一次主循環迭代時, load_contents_async回調才會開始被調用,因此在那里您應該很安全。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM