Timer inside a gnome-shell extension


I want to share a little extension for gnome-shell to test implementation of a timer into itself; the extension simply executes the count the seconds passed after clicking on the indicator and shows the time spent in the notification bar; the more interesting file into the project is timer.js that simple regulates the operation of counting time:

/* -*- mode: js; js-basic-offset: 4; indent-tabs-mode: nil -*- */

const Lang = imports.lang;
const GLib = imports.gi.GLib;

let interval_id= null;
let CallbackFunc= null;

let isRunning = false;
let secpassed=0;


const TimerCounting = new Lang.Class({
    Name: "TimerCounting",
    /*
     * Create a new timer
     */
    _init: function(callback){
        global.log('Test #12 > init TimerCounting called');
        
        this.setCallback(callback);
        secpassed=0;
    },
    /*
     * Set the callback-function 
     */
    setCallback: function(callback){
        global.log('Test #12 > setcallback TimerCounting called');
    
        if (callback === undefined || callback === null || typeof callback !== "function"){
            throw TypeError("'callback' needs to be a function.");
        }
        CallbackFunc = callback;
    },
    /**
     * Start or restart a new timer
     */
    begin: function(){
        global.log('Test #12 > start TimerCounting called');
        
        if(isRunning){
            this.stop();
        }
        isRunning=true;
        
        interval_id = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 1000, 
                                        Lang.bind(this, this._callbackInternal));
    },
    /**
     * Stop the current timer
     */
    stop: function(){
        global.log('Test #12 > stop TimerCounting called');
        
        isRunning=false;
        
        if (interval_id !== null){
            if (GLib.source_remove(interval_id) ){
                interval_id = null;
            }
        }
    },
    /**
     * A convenient way to stop timer
     */
    halt: function(){
        isRunning=false;
    },
    /**
     * The internal callback-function.
     */
    _callbackInternal: function(){
                
        if(isRunning===false){
            global.log('Test #12 > finish TimerCounting ');
            
            CallbackFunc(secpassed,true);
            secpassed=0;
            
            this.stop();
            
            return false;
        } else {
            secpassed++;
            
            global.log('Test #12 > continued TimerCounting | sec: ' + secpassed); 
            
            CallbackFunc(secpassed,false);
            
            return true;
        }
    }
});

for better debug use this comand with super user power:

# journalctl -a | grep JS

here the complete project: Download PRJ that’s all.8y3

Lascia un commento

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...