From 7ca38d7eeb3d4c462b048692757a92c46f14d75b Mon Sep 17 00:00:00 2001 From: Dustin Falgout Date: Sat, 1 Oct 2016 20:29:53 -0500 Subject: [PATCH] add base class for auto-binding this (es6). --- .../greeter-resources.gresource.xml | 0 src/js/auto-binding-object.js | 73 +++++++++++++++++++ {data => src/js}/heartbeat.js | 37 +++++----- 3 files changed, 93 insertions(+), 17 deletions(-) rename {data => src/gresource}/greeter-resources.gresource.xml (100%) create mode 100644 src/js/auto-binding-object.js rename {data => src/js}/heartbeat.js (77%) diff --git a/data/greeter-resources.gresource.xml b/src/gresource/greeter-resources.gresource.xml similarity index 100% rename from data/greeter-resources.gresource.xml rename to src/gresource/greeter-resources.gresource.xml diff --git a/src/js/auto-binding-object.js b/src/js/auto-binding-object.js new file mode 100644 index 0000000..567e75f --- /dev/null +++ b/src/js/auto-binding-object.js @@ -0,0 +1,73 @@ +/* + * auto-binding-object.js + * + * Copyright © 2016 Antergos Developers + * + * This file is part of lightdm-webkit2-greeter. + * + * lightdm-webkit2-greeter is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * lightdm-webkit2-greeter is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * The following additional terms are in effect as per Section 7 of the license: + * + * The preservation of all legal notices and author attributions in + * the material or in the Appropriate Legal Notices displayed + * by works containing it is required. + * + * You should have received a copy of the GNU General Public License + * along with lightdm-webkit2-greeter; If not, see . + */ + + +/** + * Generic base class that automatically binds `this` to the instance for all class methods. + * It is made available in the global scope and can be used in greeter themes if needed/wanted. + */ +class AutoBindingObject { + /** + * Creates a new {@link AutoBindingObject} instance. + */ + constructor() { + this.__bind_this(); + } + + /** + * Binds `this` to the class for all class methods. + * + * @private + */ + __bind_this() { + let excluded_methods = ['constructor', '__bind_this']; + + function not_excluded( method, context ) { + let _is_excluded = excluded_methods.findIndex( excluded_method => method === excluded_method ) > -1, + is_method = 'function' === typeof context[method]; + + return is_method && !_is_excluded; + } + + for ( let obj = this; obj; obj = Object.getPrototypeOf( obj ) ) { + // Handle only our methods + if ( 'Object' === obj.constructor.name ) { + break; + } + + for ( let method of Object.getOwnPropertyNames( obj ) ) { + if ( not_excluded( method, this ) ) { + this[method] = this[method].bind( this ); + } + } + } + + } +} + + +window.AutoBindingObject = AutoBindingObject; diff --git a/data/heartbeat.js b/src/js/heartbeat.js similarity index 77% rename from data/heartbeat.js rename to src/js/heartbeat.js index 8e70948..18aab82 100644 --- a/data/heartbeat.js +++ b/src/js/heartbeat.js @@ -37,22 +37,23 @@ * subsequent checks run, it will assume that there has been an error in the theme's script * execution and fall back to the simple theme. */ -class LightDMGreeterHeartbeat { +class LightDMGreeterHeartbeat extends AutoBindingObject { constructor() { - if ( '_heartbeat' in window ) { - return _heartbeat; + if ( '__heartbeat' in window ) { + return __heartbeat; } - window._heartbeat = this; + super(); + + window.__heartbeat = this; this.heartbeat = ''; + this.heartbeats = 0; this.initialize_theme_heartbeat(); } initialize_theme_heartbeat() { - let heartbeats = 0; - if ( '' !== this.heartbeat ) { console.log( 'Heartbeat has already been initialized!' ); return; @@ -60,21 +61,23 @@ class LightDMGreeterHeartbeat { console.log('Initializing theme heartbeat.'); - this.heartbeat = setInterval(() => { - ++heartbeats; + this.send_heartbeat(); + this.heartbeat = setInterval(this.send_heartbeat, 5000); + } - if ( true === lightdm.session_starting ) { - clearInterval( this.heartbeat ); - return; - } + send_heartbeat() { + ++this.heartbeats; - window.webkit.messageHandlers.GreeterBridge.postMessage('Heartbeat'); + if ( true === lightdm.session_starting ) { + clearInterval( this.heartbeat ); + return; + } - if ( heartbeats < 5 ) { - console.log('Sending heartbeat...'); - } + if ( this.heartbeats < 5 ) { + console.log('Sending heartbeat...'); + } - }, 5000); + window.webkit.messageHandlers.GreeterBridge.postMessage('Heartbeat'); } }