forked from EasyScreenCast/EasyScreenCast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.js
211 lines (179 loc) · 5.2 KB
/
timer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* -*- mode: js; js-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
Copyright (C) 2013 Borsato Ivano
The JavaScript code in this page is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
*/
const Lang = imports.lang;
const GLib = imports.gi.GLib;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Lib = Me.imports.convenience;
/*
DELAY TIMER
*/
let DelaySec = 0;
let ID_TimerDelay = null;
let CallbackFuncDelay = null;
let ElapsedSec;
const TimerDelay = new Lang.Class({
Name: "TimerDelay",
/*
* Create a new timer
*/
_init: function (delay, callback, scope) {
if (isNaN(delay)) {
Lib.TalkativeLog('delay is NOT a number :' + delay);
} else {
Lib.TalkativeLog('init TimerDelay called - sec : ' + delay);
DelaySec = delay;
ElapsedSec = 1;
this.setCallback(callback);
this.Scope = scope;
}
},
/*
* Set the callback-function
*/
setCallback: function (callback) {
Lib.TalkativeLog('setcallback TimerDelay called');
if (callback === undefined || callback === null ||
typeof callback !== "function") {
throw TypeError("'callback' needs to be a function.");
}
CallbackFuncDelay = callback;
},
/*
* Set the delay time
*/
setDelay: function (delay) {
Lib.TalkativeLog('setdelay TimerDelay called');
DelaySec = delay;
},
/**
* Start or restart a new timer
*/
begin: function () {
Lib.TalkativeLog('start TimerDelay called');
this.stop();
ID_TimerDelay = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 1000,
Lang.bind(this, this._callbackInternal));
},
/**
* Stop the current timer
*/
stop: function () {
Lib.TalkativeLog('stop TimerDelay called');
if (ID_TimerDelay !== null) {
if (GLib.source_remove(ID_TimerDelay)) {
ID_TimerDelay = null;
ElapsedSec = 1;
}
}
},
/**
* A convenient way to restart the timer.
*/
restart: function () {
this.stop();
this.begin();
},
/**
* The internal callback-function.
* @private
*/
_callbackInternal: function () {
Lib.TalkativeLog('internalFunction TimerDelay called | Sec = ' +
ElapsedSec + ' Sec delay = ' + DelaySec);
if (ElapsedSec >= DelaySec) {
CallbackFuncDelay.apply(this.Scope, []);
ElapsedSec = 1;
return false;
} else {
ElapsedSec++;
return true;
}
}
});
/*
COUNTING TIMER
*/
let ID_TimerCounting = null;
let CallbackFuncCounting = null;
let isRunning = false;
let secpassed = 0;
const TimerCounting = new Lang.Class({
Name: "TimerCounting",
/*
* Create a new timer
*/
_init: function (callback, scope) {
Lib.TalkativeLog('init TimerCounting called');
this.setCallback(callback);
secpassed = 0;
this.Scope = scope;
},
/*
* Set the callback-function
*/
setCallback: function (callback) {
Lib.TalkativeLog('setcallback TimerCounting called');
if (callback === undefined || callback === null ||
typeof callback !== "function") {
throw TypeError("'callback' needs to be a function.");
}
CallbackFuncCounting = callback;
},
/**
* Start or restart a new timer
*/
begin: function () {
Lib.TalkativeLog('start TimerCounting called');
if (isRunning) {
this.stop();
}
isRunning = true;
ID_TimerCounting = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 1000,
Lang.bind(this, this._callbackInternal));
},
/**
* Stop the current timer
*/
stop: function () {
Lib.TalkativeLog('stop TimerCounting called');
isRunning = false;
if (ID_TimerCounting !== null) {
if (GLib.source_remove(ID_TimerCounting)) {
ID_TimerCounting = null;
}
}
},
/**
* A convenient way to stop timer
*/
halt: function () {
isRunning = false;
},
/**
* The internal callback-function.
*/
_callbackInternal: function () {
if (isRunning === false) {
Lib.TalkativeLog('finish TimerCounting ');
CallbackFuncCounting.apply(this.Scope, [secpassed, true]);
secpassed = 0;
this.stop();
return false;
} else {
secpassed++;
Lib.TalkativeLog('continued TimerCounting | sec: ' + secpassed);
CallbackFuncCounting.apply(this.Scope, [secpassed, false]);
return true;
}
}
});