summaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorSonny Piers <sonny@fastmail.net>2022-09-20 21:41:32 +0200
committerPhilip Chimento <philip.chimento@gmail.com>2022-11-02 20:02:44 -0700
commitde7b3776767802196325c4c783895e9e57c6ffe6 (patch)
treeec76f8db7ba87d4ab87ef5fd9498fde87c6f53ec /modules
parentee333b81b0d37b60d34a5dc23a8421c802b7a5f0 (diff)
downloadgjs-de7b3776767802196325c4c783895e9e57c6ffe6.tar.gz
Make GInputStream iterable and async iterable
Diffstat (limited to 'modules')
-rw-r--r--modules/core/overrides/Gio.js34
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 {