-1

I have been messing around with web workers and im trying to create a class that when called determines if it can execute its work in a worker or if isnt implemented in the browser. So far I have the following which functions as I would expect.

var workerClass = {
    getInstance: function()  {
        console.log("-- INSTANCE REQUESTED --")
        // If we have a worker we have an instance return this.
        if (this.worker !== undefined) return this.worker;
        console.log("-- INSTANCE NOT CACHED --")

        //var 
        //    iAmWorker = window === undefined,
        //    iCanBeAWorker = iAmWorker ? true : window.Worker !== undefined;

        var 
            iAmWorker = false,
            iCanBeAWorker = false;
        try{
            iAmWorker = window === undefined
            iCanBeAWorker = iAmWorker ? true : window.Worker !== undefined;
        }
        catch(e){
            iAmWorker = true;
            iCanBeAWorker = true;
        }

        if (!iAmWorker && iCanBeAWorker){
            console.log("-- I AM NOT IN A THREAD - SPAWNING ONE --")
            this.worker = {
                obj: new Worker("worker.js"),
                callFunc: function(funcName, Args){
                    // MAKE REQUEST
                    this.obj.postMessage([funcName,Args]);
                    this.obj.onmessage = function(e) {  
                        console.log('-- MESSAGE RECEIVED FROM WORKER THREAD --');
                        console.log(e.data);
                        // COMPLETE DEFFERED OBJ
                    };  
                    // RETURN DEFFERED OBJ
                }
            }
            console.log("-- I AM IN A THREAD --")
        }else{
            // Actual Worker object regardless of in a thread or not
            this.worker = {
                obj: this,
                callFunc: function(funcName, Args){
                    return this.obj[funcName](Args);
                }
            }
        }

        return this.worker;
    },

    // Return a Deferred Object
    workRequest: function(content){
        return this.getInstance().callFunc("actualWork",[content]);
    },

    // Actual Work
    actualWork: function(content){
        console.log("-- SIMULATING SOME WORK --")

        var dt = new Date();
        while ((new Date()) - dt <= 3000) { /* Do nothing */ }
        console.log("-- SIMULATED WORK DONE --");

        return content + " world";
    }
}

// NOTE ONLY EVER TO BE USED IN A WORKER 
onmessage = function(args) {
    postMessage(workerClass.getInstance().callFunc(
        args.data[0],
        args.data[1]
    ));
} 

Called like this

workerClass.workRequest("Hello");

My question relates to the following. I know in the worker the window doesnt exist. Hence my attempt of checking its existance.

var 
    iAmWorker = window === undefined,
    iCanBeAWorker = iAmWorker ? true : window.Worker !== undefined;

This throws an exception which I need to catch, I find this ugly and stupid. Is there a better way of implemented this test?

Cheers and thanks.

Spaceman
  • 1,319
  • 13
  • 42
  • @Xufox it throws the exception on the window === undefined check as well as the child accessor – Spaceman Apr 18 '16 at 00:33
  • 1
    Possible duplicate of [How to check a not-defined variable in JavaScript](http://stackoverflow.com/questions/858181/how-to-check-a-not-defined-variable-in-javascript) – Sebastian Simon Apr 18 '16 at 00:36
  • @Xufox i dont think its a dup if you look at the code im checking for undefined as in the post you linked... – Spaceman Apr 18 '16 at 00:41
  • 1
    It doesn’t sound like you’ve tried all suggestions there. Think of it this way: why does `if(xyz == 3){`…`}` throw a `ReferenceError` with “`xyz` is not defined”? Isn’t it the same here but with “`window` is not defined”? So you probably want to check if a variable — `window` in this case — even exists… – Sebastian Simon Apr 18 '16 at 00:46
  • @Xufox I see what your saying I am checking for undefined but doing it incorrectly. typeof(window) === 'undefined' vs window === undefined works... My fault i always thought they did the same thing if you want to put it in as awnser ill accept it or we can close as a dupe if you want.. my mistake apollogies – Spaceman Apr 18 '16 at 00:50

1 Answers1

1

The safe way to check to see if a variable is defined in the current scope without any risk of an exception is using typeof:

if (typeof window !== "undefined") {
    // code that references window
} else {
    // code that does not reference window
}

Or, perhaps check for a specific type:

if (typeof window === "object") {
    // code that references window
} else {
    // code that does not reference window
}
jfriend00
  • 683,504
  • 96
  • 985
  • 979