[Service] Provide iteration API setServiceInterval 62/218062/4
authorYoungsoo Choi <kenshin.choi@samsung.com>
Tue, 19 Nov 2019 02:43:03 +0000 (18:43 -0800)
committerSangYong Park <sy302.park@samsung.com>
Tue, 31 Dec 2019 00:35:04 +0000 (00:35 +0000)
This provides the use of service iteration and usage is like below:

* API:
setServiceInterval(function, after, repeat);

* Parameters:
{func} The function |func| will run.
{repeat} The number of iteration is |repeat|.
         The value 0 means infinite and default.
{after} The iteration interval is |after| ms and default is 1000 ms.

Change-Id: I5e50ddf9c11f6b7a49cf48aa4c526698c044794f
Signed-off-by: Youngsoo Choi <kenshin.choi@samsung.com>
wrt_app/service/timer_manager.js
wrt_app/src/runtime.js

index 1f04a72..e17cd68 100644 (file)
@@ -20,6 +20,20 @@ class TimerManager {
     this.timer_api.setTimeout = function(func, delay) {
       _this.timeout_handlers.push(setTimeout(func, delay));
     }
+    this.timer_api.setServiceInterval = function(func, after = 1000, repeat = 0) {
+      if (typeof func !== "function") {
+        console.log("Use function as the first parameter.");
+        return;
+      }
+      let count = 1;
+      let handler = this.timer_api.setInterval(function() {
+        func();
+        count++;
+        if (count > repeat && repeat !== 0) {
+          this.timer_api.clearInterval(handler);
+        }
+      }.bind(this), after);
+    }.bind(this);
   }
   getTimerAPI() {
     return this.timer_api;
index dac4b09..0ef19b0 100644 (file)
@@ -229,8 +229,8 @@ class Runtime {
                     for(let key in global) {
                         _this.sandbox[app_id][key] = global[key];
                     }
-                    _this.sandbox[app_id]['timer_manager'] = new TimerManager();
-                    const timer_api = _this.sandbox[app_id]['timer_manager'].getTimerAPI();
+                    _this.sandbox[app_id].timer_manager = new TimerManager();
+                    const timer_api = _this.sandbox[app_id].timer_manager.getTimerAPI();
                     for(let key in timer_api) {
                         _this.sandbox[app_id][key] = timer_api[key];
                     }