diff options
author | Jose <jose@zeroc.com> | 2016-08-19 21:25:18 +0200 |
---|---|---|
committer | Jose <jose@zeroc.com> | 2016-08-19 21:25:18 +0200 |
commit | c8d32e04873be7938915c606027c84c8ab832f6b (patch) | |
tree | f862751cfaddcf5bb7b82a7c04a299d471f948d4 /js/src/Ice/browser/TimerUtil.js | |
parent | Fix ICE-7278 (diff) | |
download | ice-c8d32e04873be7938915c606027c84c8ab832f6b.tar.bz2 ice-c8d32e04873be7938915c606027c84c8ab832f6b.tar.xz ice-c8d32e04873be7938915c606027c84c8ab832f6b.zip |
ES6 mapping updates
Diffstat (limited to 'js/src/Ice/browser/TimerUtil.js')
-rw-r--r-- | js/src/Ice/browser/TimerUtil.js | 179 |
1 files changed, 101 insertions, 78 deletions
diff --git a/js/src/Ice/browser/TimerUtil.js b/js/src/Ice/browser/TimerUtil.js index 99c7b3f2608..970f2a877b5 100644 --- a/js/src/Ice/browser/TimerUtil.js +++ b/js/src/Ice/browser/TimerUtil.js @@ -14,7 +14,7 @@ /* global WorkerGlobalScope */ -var Ice = require("../Ice/ModuleRegistry").Ice; +const Ice = require("../Ice/ModuleRegistry").Ice; // // Create a timer object that uses the default browser methods. Note that we also @@ -24,36 +24,57 @@ var Ice = require("../Ice/ModuleRegistry").Ice; // function createTimerObject() { - var Timer = {}; - Timer.setTimeout = function () { setTimeout.apply(null, arguments); }; - Timer.clearTimeout = function () { clearTimeout.apply(null, arguments); }; - Timer.setInterval = function () { setInterval.apply(null, arguments); }; - Timer.clearInterval = function () { clearInterval.apply(null, arguments); }; - Timer.setImmediate = typeof(setImmediate) == "function" ? - function () { setImmediate.apply(null, arguments); } : - function () { setTimeout.apply(null, arguments); }; - return Timer; -} + let Timer = class + { + static setTimeout() + { + setTimeout.apply(null, arguments); + } + + static clearTimeout() + { + clearTimeout.apply(null, arguments); + } + static setInterval() + { + setInterval.apply(null, arguments); + } -Ice.__M.require(module, - [ - "../Ice/HashMap", - ]); + static clearInterval() + { + clearInterval.apply(null, arguments); + } + }; + + if(typeof(setImmediate) == "function") + { + Timer.setImmediate = function() + { + setImmediate.apply(null, arguments); + }; + } + else + { + Timer.setImmediate = function() + { + setTimeout.apply(null, arguments); + }; + } -var HashMap = Ice.HashMap; + return Timer; +} -var MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991; +const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991; -var _timers = new HashMap(); +const _timers = new Map(); -var _SetTimeoutType = 0, - _SetIntervalType = 1, - _SetImmediateType = 2, - _ClearTimeoutType = 3, - _ClearIntervalType = 4; +const _SetTimeoutType = 0, + _SetIntervalType = 1, + _SetImmediateType = 2, + _ClearTimeoutType = 3, + _ClearIntervalType = 4; -var Timer = {}; var worker; var _nextId = 0; @@ -67,59 +88,62 @@ var nextId = function() return _nextId++; }; -Timer.setTimeout = function(cb, ms) -{ - var id = nextId(); - _timers.set(id, cb); - worker.postMessage({type: _SetTimeoutType, id: id, ms: ms}); - return id; -}; - -Timer.clearTimeout = function(id) +class Timer { - _timers.delete(id); - worker.postMessage({type: _ClearTimeoutType, id: id}); -}; - -Timer.setInterval = function(cb, ms) -{ - var id = nextId(); - _timers.set(id, cb); - worker.postMessage({type: _SetIntervalType, id: id, ms: ms}); - return id; -}; + static setTimeout(cb, ms) + { + var id = nextId(); + _timers.set(id, cb); + worker.postMessage({type: _SetTimeoutType, id: id, ms: ms}); + return id; + } -Timer.clearInterval = function(id) -{ - _timers.delete(id); - worker.postMessage({type: _ClearIntervalType, id: id}); -}; + static clearTimeout(id) + { + _timers.delete(id); + worker.postMessage({type: _ClearTimeoutType, id: id}); + } -Timer.setImmediate = function(cb) -{ - var id = nextId(); - _timers.set(id, cb); - worker.postMessage({type: _SetImmediateType, id: id}); - return id; -}; + static setInterval(cb, ms) + { + var id = nextId(); + _timers.set(id, cb); + worker.postMessage({type: _SetIntervalType, id: id, ms: ms}); + return id; + } -Timer.onmessage = function(e) -{ - var cb; - if(e.data.type === _SetIntervalType) + static clearInterval(id) { - cb = _timers.get(e.data.id); + _timers.delete(id); + worker.postMessage({type: _ClearIntervalType, id: id}); } - else + + static setImmediate(cb) { - cb = _timers.delete(e.data.id); + var id = nextId(); + _timers.set(id, cb); + worker.postMessage({type: _SetImmediateType, id: id}); + return id; } - if(cb !== undefined) + static onmessage(e) { - cb.call(); + var cb; + if(e.data.type === _SetIntervalType) + { + cb = _timers.get(e.data.id); + } + else + { + cb = _timers.delete(e.data.id); + } + + if(cb !== undefined) + { + cb.call(); + } } -}; +} var workerCode = function() { @@ -137,23 +161,15 @@ var workerCode = function() var timers = {}; - self.onmessage = function(e) + self.onmessage = e => { if(e.data.type == _wSetTimeoutType) { - timers[e.data.id] = setTimeout(function() - { - self.postMessage(e.data); - }, - e.data.ms); + timers[e.data.id] = setTimeout(() => self.postMessage(e.data), e.data.ms); } else if(e.data.type == _wSetIntervalType) { - timers[e.data.id] = setInterval(function() - { - self.postMessage(e.data); - }, - e.data.ms); + timers[e.data.id] = setInterval(() => self.postMessage(e.data), e.data.ms); } else if(e.data.type == _wSetImmediateType) { @@ -177,7 +193,14 @@ var workerCode = function() }.toString() + "());"; }; -if(worker === undefined) +if(self == this) +{ + // + // If we are running in a worker don't spawn a separate worker for the timer + // + Ice.Timer = createTimerObject(); +} +else if(worker === undefined) { var url; try |