[gjs/ewlsh/bind-property-fields] Add API to bind class fields to GObject properties
- From: Evan Welsh <ewlsh src gnome org>
- To: commits-list gnome org
- Cc: 
- Subject: [gjs/ewlsh/bind-property-fields] Add API to bind class fields to GObject properties
- Date: Mon, 10 Jan 2022 00:12:46 +0000 (UTC)
commit 762ccf8fbaa57918e050aae2479aa566642c2be0
Author: Evan Welsh <contact evanwelsh com>
Date:   Thu Dec 30 12:16:01 2021 -0800
    Add API to bind class fields to GObject properties
 modules/core/_common.js           |  2 +-
 modules/core/overrides/GObject.js | 19 +++++++++++++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)
---
diff --git a/modules/core/_common.js b/modules/core/_common.js
index 780483b9..b36d5a80 100644
--- a/modules/core/_common.js
+++ b/modules/core/_common.js
@@ -81,7 +81,7 @@ function _checkAccessors(proto, pspec, GObject, {generateAccessors = true, acces
     const readable = flags & GObject.ParamFlags.READABLE;
     const writable = flags & GObject.ParamFlags.WRITABLE;
     if (!propdesc || (readable && !propdesc.get) || (writable && !propdesc.set))
-        propdesc = _generateAccessors(pspec, propdesc, GObject);
+        propdesc = _generateAccessors(pspec, propdesc, GObject, accessorMappingSymbol);
 
     if (generateAccessors) {
         if (!dashPropdesc)
diff --git a/modules/core/overrides/GObject.js b/modules/core/overrides/GObject.js
index 1b5fccdc..a29c098e 100644
--- a/modules/core/overrides/GObject.js
+++ b/modules/core/overrides/GObject.js
@@ -573,6 +573,25 @@ function _init() {
     const {toString} = GObject.Object.prototype;
 
     Object.assign(GObject.Object.prototype, {
+        bindPropertyFields() {
+            const accessorMap = this.constructor.prototype[_accessorMapping];
+
+            if (!accessorMap)
+                return;
+
+            Object.entries(accessorMap).forEach(([key, accessor]) => {
+                const descriptor = Object.getOwnPropertyDescriptor(this, key);
+
+                if (descriptor && 'value' in descriptor) {
+                    const value = descriptor.value;
+
+                    Object.defineProperty(this, key, accessor);
+                    // Don't override the default value with undefined
+                    if (value !== undefined)
+                        this[key] = value;
+                }
+            });
+        },
         toString() {
             if (!this.constructor)
                 return Object.prototype.toString.call(this);
[
Date Prev][
Date Next]   [
Thread Prev][
Thread Next]   
[
Thread Index]
[
Date Index]
[
Author Index]