Skip to content

Commit

Permalink
adds isWebWorker
Browse files Browse the repository at this point in the history
  • Loading branch information
justinbmeyer committed Jan 4, 2018
1 parent 4ea9c8d commit 3ccf558
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
10 changes: 10 additions & 0 deletions 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");
});
35 changes: 35 additions & 0 deletions 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');
1 change: 1 addition & 0 deletions test.js
Expand Up @@ -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');

0 comments on commit 3ccf558

Please sign in to comment.