--- /dev/null
+class TimerManager {
+ constructor() {
+ this.interval_handlers = [];
+ this.timeout_handlers = [];
+ // Prevent contaminating global APIs
+ this.timer_api = {};
+ const _this = this;
+
+ this.timer_api.clearInterval = function(handler) {
+ const index = Object.keys(_this.interval_handlers)[Object.values(_this.interval_handlers).indexOf(handler)]
+ clearInterval(_this.interval_handlers.splice(index, 1)[0]);
+ }
+ this.timer_api.clearTimeout = function(handler) {
+ const index = Object.keys(_this.timeout_handlers)[Object.values(_this.timeout_handlers).indexOf(handler)]
+ clearTimeout(_this.timeout_handlers.splice(index, 1)[0]);
+ }
+ this.timer_api.setInterval = function(func, delay) {
+ _this.interval_handlers.push(setInterval(func, delay));
+ }
+ this.timer_api.setTimeout = function(func, delay) {
+ _this.timeout_handlers.push(setTimeout(func, delay));
+ }
+ }
+ getTimerAPI() {
+ return this.timer_api;
+ }
+ releaseRemainingTimers() {
+ console.log('Remaining interval(s) : ' + this.interval_handlers.length);
+ for (let index in this.interval_handlers) {
+ const handler = this.interval_handlers.splice(index, 1)[0];
+ clearInterval(handler);
+ }
+ console.log('Remaining timer(s) : ' + this.timeout_handlers.length);
+ for (let index in this.timeout_handlers) {
+ const handler = this.timeout_handlers.splice(index, 1)[0];
+ clearTimeout(handler);
+ }
+ }
+}
+module.exports = TimerManager;
const AddonManager = require('./addon_manager');
const {app, ipcMain} = require('electron');
const IPC_MESSAGE = require('./ipc_message');
+const TimerManager = require('../service/timer_manager');
const WAS_EVENT = require('./was_event');
const WebApplication = require('./web_application');
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();
+ for(let key in timer_api) {
+ _this.sandbox[app_id][key] = timer_api[key];
+ }
let options = {};
let code = fs.readFileSync(params[1]);
vm.runInNewContext(code, _this.sandbox[app_id], options);
_this.sandbox[app_id]['started'] = undefined;
const stop_callback_string = 'if (module.exports.onStop !== undefined) { module.exports.onStop(); }';
vm.runInContext(stop_callback_string, _this.sandbox[app_id]);
+ _this.sandbox[app_id]['timer_manager'].releaseRemainingTimers();
_this.sandbox[app_id] = undefined;
} else {
console.log('UI service has been stopped.');