[gjs] overrides: Implement Gio.ListStore[Symbol.iterator]



commit 9ba3adb63eb01c95538563c3b74371a4e1a1dbc9
Author: Patrick Griffis <tingping tingping se>
Date:   Sun May 7 22:06:59 2017 -0400

    overrides: Implement Gio.ListStore[Symbol.iterator]
    
    https://bugzilla.gnome.org/show_bug.cgi?id=782310

 Makefile-test.am              |    1 +
 installed-tests/js/testGio.js |   30 ++++++++++++++++++++++++++++++
 modules/overrides/Gio.js      |   11 +++++++++++
 3 files changed, 42 insertions(+), 0 deletions(-)
---
diff --git a/Makefile-test.am b/Makefile-test.am
index af2fd8f..31ac2d6 100644
--- a/Makefile-test.am
+++ b/Makefile-test.am
@@ -230,6 +230,7 @@ common_jstests_files =                                              \
        installed-tests/js/testGObjectClass.js                  \
        installed-tests/js/testGObjectInterface.js              \
        installed-tests/js/testGTypeClass.js                    \
+       installed-tests/js/testGio.js                           \
        installed-tests/js/testImporter.js                      \
        installed-tests/js/testInterface.js                     \
        installed-tests/js/testLang.js                          \
diff --git a/installed-tests/js/testGio.js b/installed-tests/js/testGio.js
new file mode 100644
index 0000000..107221d
--- /dev/null
+++ b/installed-tests/js/testGio.js
@@ -0,0 +1,30 @@
+const Gio = imports.gi.Gio;
+const GObject = imports.gi.GObject;
+const Lang = imports.lang;
+
+const Foo = new Lang.Class({
+    Name: 'Foo',
+    Extends: GObject.Object,
+    _init: function (value) {
+        this.parent();
+        this.value = value;
+    }
+});
+
+describe('ListStore iterator', function () {
+    let list;
+
+    beforeEach(function () {
+        list = new Gio.ListStore({item_type: Foo});
+        for (let i = 0; i < 100; i++) {
+            list.append(new Foo(i));
+        }
+    });
+
+    it('ListStore iterates', function () {
+        let i = 0;
+        for (let f of list) {
+            expect(f.value).toBe(i++);
+        }
+    });
+});
\ No newline at end of file
diff --git a/modules/overrides/Gio.js b/modules/overrides/Gio.js
index 328de0b..aa04712 100644
--- a/modules/overrides/Gio.js
+++ b/modules/overrides/Gio.js
@@ -340,6 +340,14 @@ function _wrapJSObject(interfaceInfo, jsObj) {
     return impl;
 }
 
+function* _listModelIterator() {
+    let _index = 0;
+    const _len = this.get_n_items();
+    while (_index < _len) {
+        yield this.get_item(_index++);
+    }
+}
+
 function _init() {
     Gio = this;
 
@@ -391,4 +399,7 @@ function _init() {
 
     Gio.DBusExportedObject = GjsPrivate.DBusImplementation;
     Gio.DBusExportedObject.wrapJSObject = _wrapJSObject;
+
+    // ListStore
+    Gio.ListStore.prototype[Symbol.iterator] = _listModelIterator;
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]