diff options
author | Sonny Piers <sonny@fastmail.net> | 2022-09-20 21:41:32 +0200 |
---|---|---|
committer | Philip Chimento <philip.chimento@gmail.com> | 2022-11-02 20:02:44 -0700 |
commit | de7b3776767802196325c4c783895e9e57c6ffe6 (patch) | |
tree | ec76f8db7ba87d4ab87ef5fd9498fde87c6f53ec /modules | |
parent | ee333b81b0d37b60d34a5dc23a8421c802b7a5f0 (diff) | |
download | gjs-de7b3776767802196325c4c783895e9e57c6ffe6.tar.gz |
Make GInputStream iterable and async iterable
Diffstat (limited to 'modules')
-rw-r--r-- | modules/core/overrides/Gio.js | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/modules/core/overrides/Gio.js b/modules/core/overrides/Gio.js index a9ba5aa7..94fa8188 100644 --- a/modules/core/overrides/Gio.js +++ b/modules/core/overrides/Gio.js @@ -598,6 +598,40 @@ function _init() { } }; + Gio.InputStream.prototype.createSyncIterator = function* createSyncIterator(count) { + while (true) { + const bytes = this.read_bytes(count, null); + if (bytes.get_size() === 0) + return; + yield bytes; + } + }; + + Gio.InputStream.prototype.createAsyncIterator = async function* createAsyncIterator(count, ioPriority = Gio.PRIORITY_DEFAULT) { + const self = this; + + function next() { + return new Promise((resolve, reject) => { + self.read_bytes_async(count, ioPriority, null, (_self, res) => { + try { + const bytes = self.read_bytes_finish(res); + resolve(bytes); + } catch (err) { + reject(err); + } + }); + }); + } + + while (true) { + // eslint-disable-next-line no-await-in-loop + const bytes = await next(count); + if (bytes.get_size() === 0) + return; + yield bytes; + } + }; + Gio.FileEnumerator.prototype[Symbol.iterator] = function* FileEnumeratorIterator() { while (true) { try { |