久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 karma 環境中使用 Blob 中的 importsScripts

Using importsScripts within Blob in a karma environment(在 karma 環境中使用 Blob 中的 importsScripts)
本文介紹了在 karma 環境中使用 Blob 中的 importsScripts的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

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

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 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);
    }

where createWorker is :

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

But the importScripts throws me the following error :

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

and it does work well.

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

Thanks,

解決方案

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

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

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.

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.

這篇關于在 karma 環境中使用 Blob 中的 importsScripts的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I get my jasmine tests fixtures to load before the javascript considers the document to be quot;readyquot;?(在 javascript 認為文檔“準備好之前,如何讓我的 jasmine 測試裝置加載?) - IT屋-程序員軟件開發技術
What do jasmine runs and waitsFor actually do?(jasmine 運行和等待實際上是做什么的?)
How to provide mock files to change event of lt;input type=#39;file#39;gt; for unit testing(如何提供模擬文件來更改 lt;input type=filegt; 的事件用于單元測試)
How to unit test a chained method using Jasmine(如何使用 Jasmine 對鏈式方法進行單元測試)
How do I inject $rootScope into an AngularJS unit test?(如何將 $rootScope 注入 AngularJS 單元測試?)
Jasmine - How to spy on a function call within a function?(Jasmine - 如何監視函數中的函數調用?)
主站蜘蛛池模板: 亚洲精选一区二区 | 成人在线精品视频 | 精品欧美一区二区精品久久久 | 精品国产乱码久久久久久蜜柚 | 欧美精品一区三区 | 九九国产在线观看 | 欧美群妇大交群中文字幕 | 久草中文网 | 美女爽到呻吟久久久久 | 久久九九99 | 亚洲国产aⅴ精品一区二区 免费观看av | 久久午夜精品福利一区二区 | 91资源在线观看 | 久久丝袜 | 激情久久久久 | 成人欧美一区二区三区黑人孕妇 | 一区二区三区四区视频 | 成人不卡一区二区 | 曰韩一二三区 | 天天综合网天天综合 | 久久午夜视频 | 一区二区av | 97人人澡人人爽91综合色 | 伊人二区 | 一区在线视频 | 欧美视频一区二区三区 | 91在线电影| 天天摸天天干 | 一本一道久久a久久精品蜜桃 | av超碰 | 亚洲免费观看视频网站 | 日韩免费一区二区 | 欧美色综合天天久久综合精品 | 日本中出视频 | 在线91| 国产精品视频偷伦精品视频 | 国产成人免费在线观看 | 久久国产精品亚洲 | 日日夜夜操天天干 | 国产精品射 | 成人毛片视频免费 |