[gnome-weather] Modernize JavaScript



commit d23c49c06543bd6339db5d38da892483d58779e9
Author: Giovanni Campagna <gcampagna src gnome org>
Date:   Sun Aug 20 15:53:48 2017 +0200

    Modernize JavaScript
    
    Remove Lang.bind in favor of arrow functions or Function.prototype.bind;
    Replace Lang.Class with ES6 classes where possible

 src/app/city.js                      |   27 +++++++---------
 src/app/currentLocationController.js |   46 +++++++++++++--------------
 src/app/main.js                      |    4 +-
 src/app/window.js                    |    2 +-
 src/app/world.js                     |   30 +++++++++---------
 src/misc/params.js                   |    4 +-
 src/service/searchProvider.js        |   58 ++++++++++++++++-----------------
 src/shared/world.js                  |   15 ++++-----
 8 files changed, 89 insertions(+), 97 deletions(-)
---
diff --git a/src/app/city.js b/src/app/city.js
index 1d13653..d444ccc 100644
--- a/src/app/city.js
+++ b/src/app/city.js
@@ -66,11 +66,11 @@ var WeatherWidget = new Lang.Class({
             hscrollbar.set_opacity(0.0);
             hscrollbar.hide();
             let hadjustment = fsw.get_hadjustment();
-            hadjustment.connect('changed', Lang.bind(this, this._syncLeftRightButtons));
-            hadjustment.connect('value-changed', Lang.bind(this, this._syncLeftRightButtons));
+            hadjustment.connect('changed', this._syncLeftRightButtons.bind(this));
+            hadjustment.connect('value-changed', this._syncLeftRightButtons.bind(this));
         }
 
-        this._forecastStack.connect('notify::visible-child', Lang.bind(this, function() {
+        this._forecastStack.connect('notify::visible-child', () => {
             let visible_child = this._forecastStack.visible_child;
             if (visible_child == null)
                 return; // can happen at destruction
@@ -83,23 +83,23 @@ var WeatherWidget = new Lang.Class({
                 this.remove_tick_callback(this._tickId);
                 this._tickId = 0;
             }
-        }));
+        });
 
         this._tickId = 0;
 
-        this._leftButton.connect('clicked', Lang.bind(this, function() {
+        this._leftButton.connect('clicked', () => {
             let hadjustment = this._forecastStack.visible_child.get_hadjustment();
             let target = hadjustment.value - hadjustment.page_size;
 
             this._beginScrollAnimation(target);
-        }));
+        });
 
-        this._rightButton.connect('clicked', Lang.bind(this, function() {
+        this._rightButton.connect('clicked', () => {
             let hadjustment = this._forecastStack.visible_child.get_hadjustment();
             let target = hadjustment.value + hadjustment.page_size;
 
             this._beginScrollAnimation(target);
-        }));
+        });
     },
 
     _syncLeftRightButtons: function() {
@@ -126,9 +126,7 @@ var WeatherWidget = new Lang.Class({
         if (this._tickId != 0)
             this.remove_tick_callback(this._tickId);
 
-        this._tickId = this.add_tick_callback(Lang.bind(this, function() {
-            return this._animate(target, start, end);
-        }));
+        this._tickId = this.add_tick_callback(() => this._animate(target, start, end));
     },
 
     _animate: function(target, start, end) {
@@ -214,7 +212,7 @@ var WeatherView = new Lang.Class({
         this._info = null;
         this._updateId = 0;
 
-        this.connect('destroy', Lang.bind(this, this._onDestroy));
+        this.connect('destroy', this._onDestroy.bind(this));
 
         this._wallClock = new Gnome.WallClock();
         this._clockHandlerId = 0;
@@ -237,8 +235,7 @@ var WeatherView = new Lang.Class({
         this._info = info;
 
         if (info) {
-            this._updateId = this._info.connect('updated',
-                                                Lang.bind(this, this._onUpdate));
+            this._updateId = this._info.connect('updated', this._onUpdate.bind(this));
             if (info.is_valid())
                 this._onUpdate(info);
         }
@@ -274,7 +271,7 @@ var WeatherView = new Lang.Class({
         }
 
         if (!this._clockHandlerId && visible) {
-            this._clockHandlerId = this._wallClock.connect('notify::clock', Lang.bind(this, 
this._updateTime));
+            this._clockHandlerId = this._wallClock.connect('notify::clock', this._updateTime.bind(this));
         }
 
         this._infoPage.setTimeVisible(visible);
diff --git a/src/app/currentLocationController.js b/src/app/currentLocationController.js
index a5238b3..ea88800 100644
--- a/src/app/currentLocationController.js
+++ b/src/app/currentLocationController.js
@@ -30,10 +30,8 @@ var AutoLocation = {
     NOT_AVAILABLE: 2
 };
 
-var CurrentLocationController = new Lang.Class({
-    Name: 'CurrentLocationController',
-
-    _init: function(world) {
+var CurrentLocationController = class CurrentLocationController {
+    constructor(world) {
         this._world = world;
         this._processStarted = false;
         this._settings = Util.getSettings('org.gnome.Weather.Application');
@@ -42,25 +40,25 @@ var CurrentLocationController = new Lang.Class({
         if (this.autoLocation == AutoLocation.ENABLED)
             this._startGeolocationService();
         this.currentLocation = null;
-    },
+    }
 
-    _startGeolocationService: function() {
+    _startGeolocationService() {
         this._processStarted = true;
         Geoclue.Simple.new(pkg.name,
                            Geoclue.AccuracyLevel.CITY,
                            null,
-                           Lang.bind (this, this._onSimpleReady));
-    },
+                           this._onSimpleReady.bind(this));
+    }
 
-    _geoLocationFailed: function(e) {
+    _geoLocationFailed(e) {
         log ("Failed to connect to GeoClue2 service: " + e.message);
         this.autoLocation = AutoLocation.NOT_AVAILABLE;
-        GLib.idle_add(GLib.PRIORITY_DEFAULT, Lang.bind(this, function() {
+        GLib.idle_add(GLib.PRIORITY_DEFAULT, () => {
             this._world.currentLocationChanged(null);
-        }));
-    },
+        });
+    }
 
-    _onSimpleReady: function(object, result) {
+    _onSimpleReady(object, result) {
         try {
             this._simple = Geoclue.Simple.new_finish(result);
         }
@@ -73,17 +71,17 @@ var CurrentLocationController = new Lang.Class({
         client.distance_threshold = 100;
 
         this._findLocation();
-    },
+    }
 
-    _findLocation: function() {
+    _findLocation() {
         this._locationUpdatedId =
                     this._simple.connect("notify::location",
                                          this._onLocationUpdated.bind(this));
 
         this._onLocationUpdated(this._simple);
-    },
+    }
 
-    _onLocationUpdated: function(simple) {
+    _onLocationUpdated(simple) {
         let geoclueLocation = simple.get_location();
 
         this.currentLocation = GWeather.Location.new_detached(geoclueLocation.description,
@@ -91,25 +89,25 @@ var CurrentLocationController = new Lang.Class({
                                                               geoclueLocation.latitude,
                                                               geoclueLocation.longitude);
         this._world.currentLocationChanged(this.currentLocation);
-    },
+    }
 
-    setAutoLocation: function(active) {
+    setAutoLocation(active) {
         this._settings.set_value('automatic-location', new GLib.Variant('b', active));
 
         if (this.autoLocation == AutoLocation.NOT_AVAILABLE)
             return;
         this._autoLocationChanged(active);
         this._syncAutoLocation(active);
-    },
+    }
 
-    _syncAutoLocation: function(autoLocation) {
+    _syncAutoLocation(autoLocation) {
         if (autoLocation)
             this.autoLocation = AutoLocation.ENABLED;
         else
             this.autoLocation = AutoLocation.DISABLED;
-    },
+    }
 
-    _autoLocationChanged: function(active) {
+    _autoLocationChanged(active) {
         if (active) {
             if (!this._processStarted) {
                 this._startGeolocationService();
@@ -122,4 +120,4 @@ var CurrentLocationController = new Lang.Class({
             this._simple.disconnect(this._locationUpdatedId);
         }
     }
-});
+}
diff --git a/src/app/main.js b/src/app/main.js
index b122663..adf3ac2 100644
--- a/src/app/main.js
+++ b/src/app/main.js
@@ -98,12 +98,12 @@ const Application = new Lang.Class({
         this.model.load();
         this.currentLocationController = new CurrentLocationController.CurrentLocationController(this.model);
 
-        this.model.connect('notify::loading', Lang.bind(this, function() {
+        this.model.connect('notify::loading', () => {
             if (this.model.loading)
                 this.mark_busy();
             else
                 this.unmark_busy();
-        }));
+        });
         if (this.model.loading)
             this.mark_busy();
 
diff --git a/src/app/window.js b/src/app/window.js
index 6700adf..877eed0 100644
--- a/src/app/window.js
+++ b/src/app/window.js
@@ -72,7 +72,7 @@ var MainWindow = new Lang.Class({
         this._searchView = builder.get_object('initial-grid');
 
         this._searchEntry = builder.get_object('initial-grid-location-entry');
-        this._searchEntry.connect('notify::location', Lang.bind(this, this._searchLocationChanged));
+        this._searchEntry.connect('notify::location', this._searchLocationChanged.bind(this));
 
         let placesButton = builder.get_object('places-button');
         this._pageWidgets[Page.CITY].push(placesButton);
diff --git a/src/app/world.js b/src/app/world.js
index 49b65bd..11219f8 100644
--- a/src/app/world.js
+++ b/src/app/world.js
@@ -48,7 +48,7 @@ var WorldContentView = new Lang.Class({
         this._window = window;
 
         this._listbox = builder.get_object('locations-list-box');
-        this._listbox.set_header_func(function (row, previous) {
+        this._listbox.set_header_func((row, previous) => {
             let hasHeader = row.get_header() != null;
             let shouldHaveHeader = previous != null;
             if (hasHeader != shouldHaveHeader) {
@@ -60,11 +60,11 @@ var WorldContentView = new Lang.Class({
         });
 
         let locationEntry = builder.get_object('location-entry');
-        locationEntry.connect('notify::location', Lang.bind(this, this._locationChanged));
+        locationEntry.connect('notify::location', this._locationChanged.bind(this));
 
-        this.connect('show', Lang.bind(this, function() {
+        this.connect('show', () => {
             locationEntry.grab_focus();
-        }));
+        });
 
         let autoLocStack = builder.get_object('auto-location-stack');
         let autoLocSwitch = builder.get_object('auto-location-switch');
@@ -78,22 +78,22 @@ var WorldContentView = new Lang.Class({
             autoLocSwitch.sensitive = (currentLocationController.autoLocation != 
CurrentLocationController.AutoLocation.NOT_AVAILABLE);
         }
 
-        let handlerId = autoLocSwitch.connect('notify::active', Lang.bind(this, function() {
+        let handlerId = autoLocSwitch.connect('notify::active', () => {
             currentLocationController.setAutoLocation(autoLocSwitch.active);
 
             if (autoLocSwitch.active && !this.model.addedCurrentLocation)
                 autoLocStack.visible_child_name = 'locating-label';
 
             this.hide();
-        }));
+        });
 
-        this._listbox.connect('row-activated', Lang.bind(this, function(listbox, row) {
+        this._listbox.connect('row-activated', (listbox, row) => {
             this.hide();
             this.model.moveLocationToFront(row._info);
             this._window.showInfo(row._info, false);
-        }));
+        });
 
-        this.model.connect('current-location-changed', Lang.bind(this, function(model, info) {
+        this.model.connect('current-location-changed', (model, info) => {
             autoLocStack.visible_child_name = 'auto-location-switch-grid';
             GObject.signal_handler_block(autoLocSwitch, handlerId);
             autoLocSwitch.active = (currentLocationController.autoLocation == 
CurrentLocationController.AutoLocation.ENABLED);
@@ -101,13 +101,13 @@ var WorldContentView = new Lang.Class({
             GObject.signal_handler_unblock(autoLocSwitch, handlerId);
 
             this._window.showInfo(info, true);
-        }));
+        });
 
         this._stackPopover = builder.get_object('popover-stack');
-        this._listbox.set_filter_func(Lang.bind(this, this._filterListbox));
+        this._listbox.set_filter_func(this._filterListbox.bind(this));
 
-        this.model.connect('location-added', Lang.bind(this, this._onLocationAdded));
-        this.model.connect('location-removed', Lang.bind(this, this._onLocationRemoved));
+        this.model.connect('location-added', this._onLocationAdded.bind(this));
+        this.model.connect('location-removed', this._onLocationRemoved.bind(this));
 
         this._currentLocationAdded = false;
         let list = this.model.getAll();
@@ -206,10 +206,10 @@ var WorldContentView = new Lang.Class({
         if (info._updatedId)
             return;
 
-        info._updatedId = info.connect('updated', Lang.bind(this, function(info) {
+        info._updatedId = info.connect('updated', (info) => {
             tempLabel.label = info.get_temp_summary();
             image.icon_name = info.get_symbolic_icon_name();
-        }));
+        });
 
         this._syncStackPopover();
     },
diff --git a/src/misc/params.js b/src/misc/params.js
index ee45c81..4468508 100644
--- a/src/misc/params.js
+++ b/src/misc/params.js
@@ -20,9 +20,9 @@
 // Extend a method to allow more params in a subclass
 // The superclass can safely use Params.parse(), it won't see
 // the extensions.
-// const MyClass = new Lang.Class({
+// const MyClass = class {
 //       ...
-//       method: function(params) {
+//       method(params) {
 //           let mine = Params.filter(params, { anInt: 42 });
 //           this.parent(params);
 //           ... mine.anInt ...
diff --git a/src/service/searchProvider.js b/src/service/searchProvider.js
index efc9055..a841951 100644
--- a/src/service/searchProvider.js
+++ b/src/service/searchProvider.js
@@ -34,42 +34,40 @@ function getCountryName(location) {
     return location.get_name();
 }
 
-var SearchProvider = new Lang.Class({
-    Name: 'WeatherSearchProvider',
-
-    _init: function(application) {
+var SearchProvider = class WeatherSearchProvider {
+    constructor(application) {
         this._app = application;
 
         this._impl = Gio.DBusExportedObject.wrapJSObject(SearchProviderInterface, this);
-    },
+    }
 
-    export: function(connection, path) {
+    export(connection, path) {
         return this._impl.export(connection, path);
-    },
+    }
 
-    unexport: function(connection) {
+    unexport(connection) {
         return this._impl.unexport_from_connection(connection);
-    },
+    }
 
-    GetInitialResultSetAsync: function(params, invocation) {
+    GetInitialResultSetAsync(params, invocation) {
         this._app.hold();
 
         let terms = params[0];
         let model = this._app.model;
 
         if (model.loading) {
-            let notifyId = model.connect('notify::loading', Lang.bind(this, function(model) {
+            let notifyId = model.connect('notify::loading', (model) => {
                 if (!model.loading) {
                     model.disconnect(notifyId);
                     this._runQuery(terms, invocation);
                 }
-            }));
+            });
         } else {
             this._runQuery(terms, invocation);
         }
-    },
+    }
 
-    _runQuery: function(terms, invocation) {
+    _runQuery(terms, invocation) {
         let nameRet = [];
         let cityRet = [];
         let countryRet = [];
@@ -123,9 +121,9 @@ var SearchProvider = new Lang.Class({
 
         let result = nameRet.concat(cityRet).concat(countryRet);
         invocation.return_value(new GLib.Variant('(as)', [result]));
-    },
+    }
 
-    GetSubsearchResultSet: function(previous, terms) {
+    GetSubsearchResultSet(previous, terms) {
         this._app.hold();
 
         let model = this._app.model;
@@ -160,9 +158,9 @@ var SearchProvider = new Lang.Class({
         this._app.release();
 
         return ret;
-    },
+    }
 
-    GetResultMetas: function(identifiers) {
+    GetResultMetas(identifiers) {
         this._app.hold();
 
         let model = this._app.model;
@@ -191,13 +189,13 @@ var SearchProvider = new Lang.Class({
         this._app.release();
 
         return ret;
-    },
+    }
 
-    _getPlatformData: function(timestamp) {
+    _getPlatformData(timestamp) {
         return {'desktop-startup-id': new GLib.Variant('s', '_TIME' + timestamp) };
-    },
+    }
 
-    _activateAction: function(action, parameter, timestamp) {
+    _activateAction(action, parameter, timestamp) {
         let wrappedParam;
         if (parameter)
             wrappedParam = [parameter];
@@ -212,7 +210,7 @@ var SearchProvider = new Lang.Class({
                                                               this._getPlatformData(timestamp)]),
                               null,
                               Gio.DBusCallFlags.NONE,
-                              -1, null, Lang.bind(this, function(connection, result) {
+                              -1, null, (connection, result) => {
                                   try {
                                       connection.call_finish(result);
                                   } catch(e) {
@@ -220,10 +218,10 @@ var SearchProvider = new Lang.Class({
                                   }
 
                                   this._app.release();
-                              }));
-    },
+                              });
+    }
 
-    ActivateResult: function(id, terms, timestamp) {
+    ActivateResult(id, terms, timestamp) {
         this._app.hold();
 
         //log('Activating ' + id);
@@ -239,11 +237,11 @@ var SearchProvider = new Lang.Class({
 
         let location = info.location.serialize();
         this._activateAction('show-location', new GLib.Variant('v', location), timestamp);
-    },
+    }
 
-    LaunchSearch: function(terms, timestamp) {
+    LaunchSearch(terms, timestamp) {
         this._app.hold();
 
         this._activateAction('show-search', new GLib.Variant('s', terms.join(' ')), timestamp);
-    },
-});
+    }
+}
diff --git a/src/shared/world.js b/src/shared/world.js
index be342fc..5752b2c 100644
--- a/src/shared/world.js
+++ b/src/shared/world.js
@@ -125,12 +125,12 @@ var WorldModel = new Lang.Class({
         if (info._loadingId)
             return;
 
-        info._loadingId = info.connect('updated', Lang.bind(this, function(info) {
+        info._loadingId = info.connect('updated', (info) => {
             info.disconnect(info._loadingId);
             info._loadingId = 0;
 
             this._updateLoadingCount(-1);
-        }));
+        });
 
         info.update();
         this._updateLoadingCount(+1);
@@ -163,12 +163,11 @@ var WorldModel = new Lang.Class({
         if (this._queueSaveSettingsId)
             return;
 
-        let id = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, 10,
-                                          Lang.bind(this, function() {
-                                              this._queueSaveSettingsId = 0;
-                                              this._saveSettingsInternal();
-                                              return false;
-                                          }));
+        let id = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, 10, () => {
+            this._queueSaveSettingsId = 0;
+            this._saveSettingsInternal();
+            return false;
+        });
         this._queueSaveSettingsId = id;
     },
 


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