{"size": 3185, "ext": "js", "max_stars_repo_path": "Worker-Promise.js", "max_stars_repo_name": "Gioyik/Worker-Promise.js", "max_stars_repo_stars_event_min_datetime": "2015-03-08T22:00:21.000Z", "max_stars_repo_stars_event_max_datetime": "2015-03-08T22:00:21.000Z", "max_issues_repo_path": "Worker-Promise.js", "max_issues_repo_name": "Gioyik/Worker-Promise.js", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "Worker-Promise.js", "max_forks_repo_name": "Gioyik/Worker-Promise.js", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 33.5263157895, "max_line_length": 116, "alphanum_fraction": 0.4558869702}
(function (r, f) {
    var NAME = 'WorkerPromise';

    if (typeof define === 'function' && define.amd) {
        define(['q'], function (Q) {
            return (r[NAME] = f(Q));
        });
    } else {
        r[NAME] = f(r.Q);
    }
}(this, function (Q) {
    var source = (function __workerEnv() {
            var RegExp = /^function\s*([a-zA-Z0-9_$]*)\s*\(([a-zA-Z0-9_$,\s]*)\)\s*[^{]*{\s*([\d\D]*)\s*}[^}]*$/gim;

            function deserialize(functionText) {
                var functionParts = RegExp.exec(functionText),
                    functionArgs = functionParts[2].split(/\s*,\s*/),
                    functionBody = functionParts[3];

                return Function.apply(null, functionArgs.concat(functionBody));
            }

            function post (result) {
                self.postMessage(result);
                self.close();
            }

            self.onmessage = function (evt) {
                var commandObject = evt.data,
                    invokeFn,
                    invokeArguments,
                    invokeResult;
                
                switch (commandObject.command.toLowerCase()) {
                    case "import":
                        importScripts.apply(null, commandObject.arguments);
                    break;
                    case "invoke":
                        invokeFn = deserialize(commandObject.arguments[0]);
                        invokeArguments = commandObject.arguments.slice(1);

                        invokeResult = invokeFn.apply(this, invokeArguments);

                        if (invokeResult != null && typeof invokeResult.then == 'function') {
                            invokeResult.then (post)
                        }
                        else {
                            post(invokeResult)
                        }
                    break;
                }
            };
        }).toString().replace(/^[^{]*{\s*([\d\D]*)\s*}[^}]*$/,'$1'),
        wBlob = new Blob([Source]),
        wUrl = window.URL.createObjectURL(wBlob);

    function getUrl(scriptPath) {
        if (scriptPath.toLowerCase().indexOf("http") == 0) {
            return scriptPath;
        }

        return location.protocol + "//" + location.hostname + 
            (location.port && ":" + location.port) + "/" + scriptPath;
    }

    return function (workerFunction, imports) {
        var importUrls = (imports || []).map(getUrl);

        return function () {
            var worker = new Worker(wUrl),
                deferred = Q.defer();

            worker.addEventListener('message', function (evt) {
                deferred.resolve(evt.data);
            }, false);

            worker.addEventListener('error', function (errorEvent) {
                deferred.reject(errorEvent);
            }, false);

            worker.postMessage({
                command: "import",
                arguments: importUrls
            });

            worker.postMessage({
                command: "invoke",
                arguments: [workerFunction.toString()]
                    .concat([].slice.call(arguments))
            });

            return deferred.promise;
        };
    };
}));