RandomWallpaperGnome3/randomwallpaper@iflow.space/timer.js

149 lines
3.1 KiB
JavaScript
Raw Permalink Normal View History

2017-02-24 22:31:35 +00:00
const Lang = imports.lang;
const GLib = imports.gi.GLib;
const Self = imports.misc.extensionUtils.getCurrentExtension();
const Prefs = Self.imports.settings;
const LoggerModule = Self.imports.logger;
2017-02-24 22:31:35 +00:00
let _afTimerInstance = null;
// Singleton implementation of _AFTimer
2018-07-27 15:36:47 +00:00
var AFTimer = function () {
if (!_afTimerInstance) {
_afTimerInstance = new _AFTimer();
}
return _afTimerInstance;
2017-02-24 22:31:35 +00:00
};
/**
* Timer for the auto fetch feature.
*
* @type {Lang}
*/
var _AFTimer = new Lang.Class({
2018-07-27 15:36:47 +00:00
Name: 'AFTimer',
logger: null,
2017-02-24 22:31:35 +00:00
2018-07-27 15:36:47 +00:00
_timeout: null,
_timoutEndCallback: null,
_minutes: 30,
2017-02-24 22:31:35 +00:00
2018-07-27 15:36:47 +00:00
_init: function () {
this.logger = new LoggerModule.Logger('RWG3', 'Timer');
2018-07-27 15:36:47 +00:00
this._settings = new Prefs.Settings();
},
2017-02-24 22:31:35 +00:00
2018-07-27 15:36:47 +00:00
isActive: function () {
return this._settings.get('auto-fetch', 'boolean');
},
2017-02-24 22:31:35 +00:00
2018-07-27 15:36:47 +00:00
remainingMinutes: function () {
let minutesElapsed = this._minutesElapsed();
let remainder = minutesElapsed % this._minutes;
return Math.max(this._minutes - remainder, 0);
2018-07-27 15:36:47 +00:00
},
2017-02-24 22:31:35 +00:00
2018-07-27 15:36:47 +00:00
registerCallback: function (callback) {
this._timoutEndCallback = callback;
},
2017-02-24 22:31:35 +00:00
/**
2018-07-27 15:36:47 +00:00
* Sets the minutes of the timer.
*
* @param minutes
*/
2018-07-27 15:36:47 +00:00
setMinutes: function (minutes) {
this._minutes = minutes;
},
2018-07-27 15:36:47 +00:00
/**
* Start the timer.
*
* @return void
*/
start: function () {
this.cleanup();
2017-02-24 22:31:35 +00:00
2018-07-27 15:36:47 +00:00
let last = this._settings.get('timer-last-trigger', 'int64');
if (last === 0) {
this.reset();
}
2017-02-24 22:31:35 +00:00
let millisRemaining = this.remainingMinutes() * 60 * 1000;
// set new wallpaper if the interval was surpassed and set the timestamp to when it should have been updated
if (this._surpassedInterval()) {
if (this._timoutEndCallback) {
this._timoutEndCallback();
}
let millisOverdue = (this._minutes * 60 * 1000) - millisRemaining;
this._settings.set('timer-last-trigger', 'int64', Date.now() - millisOverdue);
}
// actual timer function
this._timeout = GLib.timeout_add(GLib.PRIORITY_DEFAULT, millisRemaining, () => {
2018-07-27 15:36:47 +00:00
if (this._timoutEndCallback) {
this._timoutEndCallback();
}
2018-07-28 20:49:58 +00:00
this.reset(); // reset timer
2018-07-27 15:36:47 +00:00
this.start(); // restart timer
});
},
/**
* Stop the timer.
*
* @return void
*/
stop: function () {
this._settings.set('timer-last-trigger', 'int64', 0);
this.cleanup();
},
/**
* Cleanup the timeout callback if it exists.
*
* @return void
*/
cleanup: function () {
if (this._timeout) { // only remove if a timeout is active
GLib.source_remove(this._timeout);
this._timeout = null;
}
},
/**
* Reset the timer.
*
* @return void
*/
reset: function () {
this._settings.set('timer-last-trigger', 'int64', new Date().getTime());
this.cleanup();
},
_minutesElapsed: function () {
let now = Date.now();
let last = this._settings.get('timer-last-trigger', 'int64');
2018-07-27 15:36:47 +00:00
if (last === 0) {
return 0;
}
2018-07-27 15:36:47 +00:00
let elapsed = Math.max(now - last, 0);
return Math.floor(elapsed / (60 * 1000));
},
_surpassedInterval: function () {
let now = Date.now();
let last = this._settings.get('timer-last-trigger', 'int64');
let diff = now - last;
let intervalLength = this._minutes * 60 * 1000;
2018-07-27 15:36:47 +00:00
return diff > intervalLength;
}
2017-02-24 22:31:35 +00:00
});