2017-04-24 22 views
0

Webワーカーでdefiant.jsを使用しようとしています.JSON.searchに加えて計算量が多いためです。 は、しかし、私は、私は反抗的なデモコードの一部を使用して簡単な例を作成しimportScripts(defiant.min.js)を使用しているWebワーカーでエラーが発生しました

Uncaught Error: Uncaught ReferenceError: Defiant is not defined

を得続けます。

これはdefiant.jsの問題であるかどうか、私は間違ってスクリプトをインポートするだけですか? これを行う方法についての別の解決方法がありますか?

dandavisによって提案されたコメントのようにimportScriptsの位置を()変更main.htmlを

var obj = { 
    "car": [ 
     {"id": 10, "color": "silver", "name": "Volvo"}, 
     {"id": 11, "color": "red", "name": "Saab"}, 
     {"id": 12, "color": "red", "name": "Peugeot"}, 
     {"id": 13, "color": "yellow", "name": "Porsche"} 
    ], 
    "bike": [ 
     {"id": 20, "color": "black", "name": "Cannondale"}, 
     {"id": 21, "color": "red", "name": "Shimano"} 
    ] 
} 
    var worker = new Worker('defiantWW.js'); 


    worker.addEventListener('message', function(e) { 
     console.log(e.data); 
    }, false); 
    worker.postMessage(obj); 

ウェブワーカーファイル

self.addEventListener('message', function(e) { 
    importScripts('defiant.min.js') 
    var obj=e.data; 
    var search = JSON.search(obj, '//car[color="yellow"]/name'); 
    self.postMessage(search); 
}, false); 

EDIT でJS - しかし、同じ結果が

ウェブワーカーファイルv2

importScripts('defiant.min.js') 
self.addEventListener('message', function(e) { 
    var obj=e.data; 
    var search = JSON.search(obj, '//car[color="yellow"]/name'); 
    self.postMessage(search); 
}, false); 
+1

あなたはアップフロント負荷時ではなく、各メッセージ時にインポートする必要がありますが、物事を台無しに可能性がある – dandavis

+0

をありがとう - importScriptsの位置を変更()質問を編集しましたが、結果は変わりません。 – Kristian

答えて

0

悲しいことに、十分に難しいのはWebワーカーでの使用をサポートしていません。 が問題にfeedback from Hakan Bilginを手に入れた:

There is already support for web workers in Defiant.js

<script src="defiant.min.js"></script> 
<script> 

var obj = { 
    "car": [ 
     {"id": 10, "color": "silver", "name": "Volvo"}, 
     {"id": 11, "color": "red", "name": "Saab"}, 
     {"id": 12, "color": "red", "name": "Peugeot"}, 
     {"id": 13, "color": "yellow", "name": "Porsche"} 
    ], 
    "bike": [ 
     {"id": 20, "color": "black", "name": "Cannondale"}, 
     {"id": 21, "color": "red", "name": "Shimano"} 
    ] 
}; 

Defiant.getSnapshot(obj, function(snapshot) { 
    var found = JSON.search(snapshot, '//car'); 
    console.log(found); 
}); 

</script> 

[Snapshot] doesn't give me access to defiant from within my own web-worker. I can execute the Snapshot in the main thread and then post the result to my web-worker, however the large JSON (5-15mb) is handled only in the web-worker. I would have to pass it over to the main thread, run the Snapshot (which is executed in another web-worker) and then pass the result back to my WW. Since I'm using the JSON.search quite often that would induce a lot of unnecessary overhead on the main thread.

No...you can not access Defiant from within a web worker

関連する問題