在业力环境中使用Blob中的importsScripts [英] Using importsScripts within Blob in a karma environment

查看:397
本文介绍了在业力环境中使用Blob中的importsScripts的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用业力和茉莉花来开展我的一个小项目。我的目标浏览器是chrome 32。

I am working on a small project of mine using karma, and jasmine. My targeted browser is chrome 32.

我正在尝试在我通过blob实例化的web worker中导入脚本,如下所示:

I am trying to import scripts within a web worker whom I have instanciated through a blob as follows :

describeAsyncAppliPersephone("When the application project to DOM", function()
{
    it("it should call the function of DomProjection in the project associated with its event", function()
    {
        var eventSentBack = {
            eventType: 'testReceived',
            headers: { id: 14, version: 4 },
            payLoad: { textChanged: 'newText' }
        };
        var isRendered = false;
        var fnProjection = function(event, payload)
        {
            isRendered = true;
        }

        var bootstrap = [
            { eventType: 'testReceived', projection: fnProjection },
            { eventType: 'test2Received', projection: function() { } }
        ];

        runs(function()
        {
            var factory = new WorkerFactory();

        var worker = factory.CreateByScripts('importScripts("/base/SiteWeb/project/js/app/application.js"); var app = new application(self); app.projectOnDOM(' + JSON.stringify(eventSentBack) + '); ');

            console.log(worker.WorkerLocation);

            var applicationQueue = new queueAsync(worker);
            var projectQueue = new queueSync(worker);
            var p = new project(applicationQueue, persephoneQueue, bootstrap);

            applicationQueue.publish(eventSentBack);
        });

        waitsFor(function() { return isRendered }, "Projection called", 500);

        runs(function()
        {
            expect(isRendered).toBe(true);
        });


    });
});

workerFactory如下:

workerFactory is as follows :

this.CreateByScripts = function(scripts, fDefListener, fOnError)
    {
        var arrayScripts = scripts;

        if (!arrayScripts)
            throw "unable to load worker for undefined scripts";

        if (Object.prototype.toString.call(arrayScripts) !== '[object Array]')
            arrayScripts = [arrayScripts];

        var blob = new Blob(arrayScripts, { type: "text/javascript" });

        var w = createWorker(window.URL.createObjectURL(blob));

        return new QueryableWorker(w, fDefListener, fOnError);
    }

其中createWorker为:

where createWorker is :

createWorker = function(sUrl)
        {
            return new Worker(sUrl);
        }

但是importScripts会抛出以下错误:

But the importScripts throws me the following error :


未捕获的SyntaxError:无法执行'importScripts':URL
'/ base/SiteWeb/project/js/app/application.js'无效。

Uncaught SyntaxError: Failed to execute 'importScripts': the URL '/base/SiteWeb/project/js/app/application.js' is invalid.

我试过浏览器中的路径:

I have tried with the path within the browser :

http://mylocalhost:9876/base/SiteWeb/project/js/app/application.js

它运行良好。

我应该使用什么路径使importScripts成功运行?

What is the path I should use to make importScripts working successfully ?

谢谢,

推荐答案

您不能在使用Blob创建的worker中使用相对路径。

You can't use relative path in worker created with Blob.

今天遇到这个问题。解决方案在Web Workers的基础知识中进行了解释,但有点隐藏文章的长度:

Had this problem today. Solution is explained in "The Basics of Web Workers", but a little hidden in length of the article:


原因是:工人(现在从blob URL创建)将使用blob:前缀解析,而你的应用程序将从不同的(可能是http://)方案运行。因此,失败将归因于交叉来源限制。

The reason being: the worker (now created from a blob URL) will be resolved with a blob: prefix, while your app will be running from a different (presumably http://) scheme. Hence, the failure will be due to cross origin restrictions.

如果您决定避免在您的工作人员中硬编码域名,那么文章包含也是一个解决方案。在接收带有URL作为其参数之一的消息时导入脚本:

If you are determined to avoid hardcoding domain name in your workers the article has also a solution for this. Import your scripts on receiving a message with URL as one of its parameters:

self.onmessage = function(e) {
    importScripts(e.data.url + 'yourscript.js');
};

并通过发送该网址启动您的工作人员

and start your worker with sending that url

worker.postMessage({url: document.location.protocol + '//' + document.location.host});

为清楚起见,上面的代码已经过简化。

The code above is simplified for clarity.

这篇关于在业力环境中使用Blob中的importsScripts的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆