diff --git a/is-web-worker/is-web-worker-test.js b/is-web-worker/is-web-worker-test.js new file mode 100644 index 0000000..4e8df7b --- /dev/null +++ b/is-web-worker/is-web-worker-test.js @@ -0,0 +1,10 @@ +'use strict'; + +var QUnit = require('../../test-wrapper'); +var isWebWorker = require('./is-web-worker'); + +QUnit.module("can-globals/is-web-worker/is-web-worker"); + +QUnit.test("basics", function(){ + equal(typeof isWebWorker(), "boolean"); +}); diff --git a/is-web-worker/is-web-worker.js b/is-web-worker/is-web-worker.js new file mode 100644 index 0000000..698c100 --- /dev/null +++ b/is-web-worker/is-web-worker.js @@ -0,0 +1,35 @@ +'use strict'; + +var globals = require('can-globals/can-globals-instance'); + +/* globals WorkerGlobalScope */ +// A bit of weirdness to avoid complaining linters +var funcConstructor = Function; + + +/** + * @module {function} can-globals/is-browser-window/is-web-worker is-web-worker + * @parent can-globals/modules + * @signature `isWebWorker()` + * + * Returns `true` if the code is running within a [web worker](https://developer.mozilla.org/en-US/docs/Web/API/Worker). + * + * ```js + * var isWebWorker = require("can-globals/is-web-worker/is-web-worker"); + * var GLOBAL = require("can-globals/global/global"); + * + * if(isWebWorker()) { + * ... + * } + * ``` + * + * @return {Boolean} True if the environment is a web worker. + */ + +globals.define('isWebWorker', function(){ + var global = funcConstructor('return this')(); + return typeof WorkerGlobalScope !== "undefined" && + (global instanceof WorkerGlobalScope); +}); + +module.exports = globals.makeExport('isWebWorker'); diff --git a/test.js b/test.js index a353234..b89aef6 100644 --- a/test.js +++ b/test.js @@ -3,4 +3,5 @@ require('./can-globals-test'); require('./global/global-test'); require('./location/location-test'); require('./is-browser-window/is-browser-window-test'); +require('./is-web-worker/is-web-worker-test'); require('./is-node/is-node-test');