2015-06-28 21:54:41 +00:00
|
|
|
(function (workerScript) {
|
|
|
|
var worker,
|
2015-07-10 14:34:14 +00:00
|
|
|
fakeIdToCallback = {},
|
2015-07-10 15:11:09 +00:00
|
|
|
lastFakeId = 0,
|
|
|
|
logPrefix = 'HackTimer.js by turuslan: ';
|
2015-06-28 21:54:41 +00:00
|
|
|
if (typeof (Worker) !== 'undefined') {
|
2015-07-10 14:34:14 +00:00
|
|
|
function getFakeId () {
|
|
|
|
lastFakeId ++;
|
|
|
|
return lastFakeId;
|
2015-06-28 21:54:41 +00:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
worker = new Worker (workerScript);
|
|
|
|
window.setInterval = function (callback, time) {
|
2015-07-10 14:34:14 +00:00
|
|
|
var fakeId = getFakeId ();
|
2015-06-28 21:54:41 +00:00
|
|
|
fakeIdToCallback[fakeId] = callback;
|
|
|
|
worker.postMessage ({
|
|
|
|
name: 'setInterval',
|
|
|
|
fakeId: fakeId,
|
|
|
|
time: time
|
|
|
|
});
|
|
|
|
return fakeId;
|
|
|
|
};
|
|
|
|
window.clearInterval = function (fakeId) {
|
2015-07-10 14:19:19 +00:00
|
|
|
if (fakeIdToCallback.hasOwnProperty(fakeId)) {
|
|
|
|
delete fakeIdToCallback[fakeId];
|
|
|
|
worker.postMessage ({
|
|
|
|
name: 'clearInterval',
|
|
|
|
fakeId: fakeId
|
|
|
|
});
|
|
|
|
}
|
2015-06-28 21:54:41 +00:00
|
|
|
};
|
|
|
|
window.setTimeout = function (callback, time) {
|
2015-07-10 14:34:14 +00:00
|
|
|
var fakeId = getFakeId ();
|
2015-06-28 21:54:41 +00:00
|
|
|
fakeIdToCallback[fakeId] = callback;
|
|
|
|
worker.postMessage ({
|
|
|
|
name: 'setTimeout',
|
|
|
|
fakeId: fakeId,
|
|
|
|
time: time
|
|
|
|
});
|
|
|
|
return fakeId;
|
|
|
|
};
|
|
|
|
window.clearTimeout = function (fakeId) {
|
2015-07-10 14:19:19 +00:00
|
|
|
if (fakeIdToCallback.hasOwnProperty(fakeId)) {
|
|
|
|
delete fakeIdToCallback[fakeId];
|
|
|
|
worker.postMessage ({
|
|
|
|
name: 'clearTimeout',
|
|
|
|
fakeId: fakeId
|
|
|
|
});
|
|
|
|
}
|
2015-06-28 21:54:41 +00:00
|
|
|
};
|
|
|
|
worker.onmessage = function (event) {
|
|
|
|
var data = event.data,
|
|
|
|
fakeId = data.fakeId,
|
2015-07-10 14:19:19 +00:00
|
|
|
callback;
|
|
|
|
if (fakeIdToCallback.hasOwnProperty(fakeId)) {
|
2015-06-28 21:54:41 +00:00
|
|
|
callback = fakeIdToCallback[fakeId];
|
2015-07-10 14:19:19 +00:00
|
|
|
}
|
2015-07-10 14:34:14 +00:00
|
|
|
if (typeof (callback) === 'string') {
|
|
|
|
try {
|
|
|
|
callback = new Function (callback);
|
2015-07-10 15:11:09 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.log (logPrefix + 'Error parsing callback code string: ', error);
|
2015-07-10 14:34:14 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-10 14:19:19 +00:00
|
|
|
if (typeof (callback) === 'function') {
|
|
|
|
callback.call (window);
|
|
|
|
}
|
2015-06-28 21:54:41 +00:00
|
|
|
};
|
|
|
|
worker.onerror = function (event) {
|
2015-07-10 15:11:09 +00:00
|
|
|
console.log (event);
|
2015-06-28 21:54:41 +00:00
|
|
|
};
|
2015-07-10 15:11:09 +00:00
|
|
|
console.log (logPrefix + 'Initialisation succeeded');
|
2015-06-28 21:54:41 +00:00
|
|
|
} catch (error) {
|
2015-07-10 15:11:09 +00:00
|
|
|
console.log (logPrefix + 'Initialisation failed');
|
|
|
|
console.error (error);
|
2015-06-28 21:54:41 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-07-10 15:11:09 +00:00
|
|
|
console.log (logPrefix + 'Initialisation failed - HTML5 Web Worker is not supported');
|
2015-06-28 21:54:41 +00:00
|
|
|
}
|
|
|
|
})('HackTimerWorker.js');
|