Upload packaging folder
[platform/upstream/iotjs.git] / src / js / timers.js
1 /* Copyright 2015-present Samsung Electronics Co., Ltd. and other contributors
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 var Timer = process.binding(process.binding.timer);
17
18 var util = require('util');
19
20 var TIMEOUT_MAX = 2147483647; // 2^31-1
21
22
23 function Timeout(after) {
24   this.after = after;
25   this.isrepeat = false;
26   this.callback = null;
27   this.handler = null;
28 }
29
30
31 Timer.prototype.handleTimeout = function() {
32   var timeout = this.timeoutObj; // 'this' is Timer object
33   if (timeout && timeout.callback) {
34     timeout.callback();
35     if (!timeout.isrepeat) {
36       timeout.close();
37     }
38   }
39 };
40
41
42 Timeout.prototype.activate = function() {
43   var repeat = 0;
44   var handler = new Timer();
45
46   if (this.isrepeat) {
47     repeat = this.after;
48
49   }
50
51   handler.timeoutObj = this;
52   this.handler = handler;
53
54   handler.start(this.after, repeat);
55 };
56
57
58 Timeout.prototype.close = function() {
59   this.callback = undefined;
60   if (this.handler) {
61     this.handler.timeoutObj = undefined;
62     this.handler.stop();
63     this.handler = undefined;
64   }
65 };
66
67
68 exports.setTimeout = function(callback, delay) {
69   if (!util.isFunction(callback)) {
70     throw new TypeError('Bad arguments: callback must be a Function');
71   }
72
73   delay *= 1;
74   if (delay < 1 || delay > TIMEOUT_MAX) {
75     delay = 1;
76   }
77
78   var timeout = new Timeout(delay);
79
80   // set timeout handler.
81   if (arguments.length <= 2) {
82     timeout.callback = callback;
83   } else {
84     var args = Array.prototype.slice.call(arguments, 2);
85     timeout.callback = function() {
86       callback.apply(timeout, args);
87     };
88   }
89
90   timeout.activate();
91
92   return timeout;
93 };
94
95
96 exports.clearTimeout = function(timeout) {
97   if (timeout && timeout.callback && (timeout instanceof Timeout))
98     timeout.close();
99   else
100     throw new Error('clearTimeout() - invalid timeout');
101 };
102
103
104 exports.setInterval = function(callback, repeat) {
105   if (!util.isFunction(callback)) {
106     throw new TypeError('Bad arguments: callback must be a Function');
107   }
108
109   repeat *= 1;
110   if (repeat < 1 || repeat > TIMEOUT_MAX) {
111     repeat = 1;
112   }
113   var timeout = new Timeout(repeat);
114
115   // set interval timeout handler.
116   if (arguments.length <= 2) {
117     timeout.callback = callback;
118   } else {
119     var args = Array.prototype.slice.call(arguments, 2);
120     timeout.callback = function() {
121       callback.apply(timeout, args);
122     };
123   }
124   timeout.isrepeat = true;
125   timeout.activate();
126
127   return timeout;
128 };
129
130
131 exports.clearInterval = function(timeout) {
132   if (timeout && timeout.isrepeat)
133     timeout.close();
134   else
135     throw new Error('clearInterval() - invalid interval');
136 };