Skip to content

Draft: Gio iterators

Sonny Piers requested to merge (removed):gio-promise-iterators into master

Example usage

const {
  Gio: {File, createIterator, createAsyncIterator},
  GLib: {PRIORITY_DEFAULT}
} = imports.gi;

const file = File.new_for_path('/tmp/some-large-file');
const inputStream = file.read(null);

// synchronous iteration on a GInputStream
let bytes_read = 0
for (const chunk of createItator(inputStream, 8192)) {
  bytes_read += chunk.get_size();
}
log(['done', bytes_read]);

// asynchronous iteration on a GInputStream
let bytes_read = 0
for await (const chunk of createAsyncIterator(inputStream, 8192, PRIORITY_DEFAULT)) {
  bytes_read += chunk.get_size();
}
log(['done', bytes_read]);

Ideally Gio.InputStream would be both iterable and async iterable directly.

One way to do that would be to use a default and customizable byte count for reading, for example source.read_count.

To be honest I'm not even sure this belongs in Gio overrides or even in gjs.

WDYT?

Edited by Sonny Piers

Merge request reports