Skip to content
This repository has been archived by the owner on Jun 9, 2019. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
1.1.0
  • Loading branch information
mohayonao committed Jun 6, 2016
1 parent 1a2a7f3 commit 03f479e
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
73 changes: 73 additions & 0 deletions build/worker-timer.js
@@ -0,0 +1,73 @@
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.WorkerTimer = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
(function (global){
"use strict";

if (global === global.window && global.URL && global.Blob && global.Worker) {
module.exports = (function() {
var TIMER_WORKER_SOURCE = [
"var timerIds = {}, _ = {};",
"_.setInterval = function(args) {",
" timerIds[args.timerId] = setInterval(function() { postMessage(args.timerId); }, args.delay);",
"};",
"_.clearInterval = function(args) {",
" clearInterval(timerIds[args.timerId]);",
"};",
"_.setTimeout = function(args) {",
" timerIds[args.timerId] = setTimeout(function() { postMessage(args.timerId); }, args.delay);",
"};",
"_.clearTimeout = function(args) {",
" clearTimeout(timerIds[args.timerId]);",
"};",
"onmessage = function(e) { _[e.data.type](e.data) };"
].join("");

var _timerId = 0;
var _callbacks = {};
var _timer = new global.Worker(global.URL.createObjectURL(
new global.Blob([ TIMER_WORKER_SOURCE ], { type: "text/javascript" })
));

_timer.onmessage = function(e) {
if (_callbacks[e.data]) {
_callbacks[e.data].callback.apply(null, _callbacks[e.data].params);
}
};

return {
setInterval: function(callback, delay) {
var params = Array.prototype.slice.call(arguments, 2);

_timerId += 1;

_timer.postMessage({ type: "setInterval", timerId: _timerId, delay: delay });
_callbacks[_timerId] = { callback: callback, params: params };

return _timerId;
},
setTimeout: function(callback, delay) {
var params = Array.prototype.slice.call(arguments, 2);

_timerId += 1;

_timer.postMessage({ type: "setTimeout", timerId: _timerId, delay: delay });
_callbacks[_timerId] = { callback: callback, params: params };

return _timerId;
},
clearInterval: function(timerId) {
_timer.postMessage({ type: "clearInterval", timerId: timerId });
_callbacks[timerId] = null;
},
clearTimeout: function(timerId) {
_timer.postMessage({ type: "clearTimeout", timerId: timerId });
_callbacks[timerId] = null;
}
};
})();
} else {
module.exports = global;
}

}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
},{}]},{},[1])(1)
});
1 change: 1 addition & 0 deletions build/worker-timer.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "worker-timer",
"description": "Timer API that works stable in background tabs",
"version": "1.0.0",
"version": "1.1.0",
"author": "Nao Yonamine <mohayonao@gmail.com>",
"bugs": {
"url": "https://github.com/mohayonao/worker-timer/issues"
Expand Down

0 comments on commit 03f479e

Please sign in to comment.