Search code examples
javascriptweb-worker

Web Workers are throwing exceptions when window is checked for existance


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.


Solution

  • 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
    }