Javascript Web Worker未返回执行操作的结果
我有一个web worker没有返回执行计算的结果,该方法如下所示:Javascript Web Worker未返回执行操作的结果,javascript,web-worker,lodash,turfjs,Javascript,Web Worker,Lodash,Turfjs,我有一个web worker没有返回执行计算的结果,该方法如下所示: self.checkIntersectionForLayerWith = function checkIntersectionForLayerWith(data) { var checkIntersectionForLayer = function checkIntersectionForLayer() { var checkEachConflict = function checkEachConflict(fe
self.checkIntersectionForLayerWith = function checkIntersectionForLayerWith(data) {
var checkIntersectionForLayer = function checkIntersectionForLayer() {
var checkEachConflict = function checkEachConflict(feature){
var conflict = turf.intersect(feature,data.rectFeature);
if(conflict != null) {
_.extend(conflict.properties,feature.properties);
conflict.id = feature.id;
}
return conflict;
};
var intersectiontest = self._(data.features)
.map(checkEachConflict)
.pull(void 0)
.thru(self.turf.featurecollection)
.value();
return {
layerLabel:data.layerLabel,
query:data.rectFeature,
result:data.intersectiontest
};
};
return checkIntersectionForLayer;
};
self.startQuery = function startQuery(data) {
if(data != null) {
var checkIntersectionForLayer = self.checkIntersectionForLayerWith(data);
var result = checkIntersectionForLayer();
var payload = {
type:'rectangle_query_worker_result',
result:result
};
self.postMessage(payload);
}
};
self.onmessage = function startQueryWorker(e) {
if(e.data.type === 'start') {
console.log("Inside the web worker,",e,e.data,e.data.payload);
self.startQuery(e.data.payload);
}
else if(e.data.type === 'setup') {
self.setup();
}
};
self.setup = function setup() {
importScripts('/MapErpMobileDev/leaflet/mobile/js/lib/turf.js');
importScripts('/MapErpMobileDev/leaflet/mobile/js/lib/lodash.compat.min.js');
};
我这样调用该方法:
self.checkIntersectionForLayerWith = function checkIntersectionForLayerWith(data) {
var checkIntersectionForLayer = function checkIntersectionForLayer() {
var checkEachConflict = function checkEachConflict(feature){
var conflict = turf.intersect(feature,data.rectFeature);
if(conflict != null) {
_.extend(conflict.properties,feature.properties);
conflict.id = feature.id;
}
return conflict;
};
var intersectiontest = self._(data.features)
.map(checkEachConflict)
.pull(void 0)
.thru(self.turf.featurecollection)
.value();
return {
layerLabel:data.layerLabel,
query:data.rectFeature,
result:data.intersectiontest
};
};
return checkIntersectionForLayer;
};
self.startQuery = function startQuery(data) {
if(data != null) {
var checkIntersectionForLayer = self.checkIntersectionForLayerWith(data);
var result = checkIntersectionForLayer();
var payload = {
type:'rectangle_query_worker_result',
result:result
};
self.postMessage(payload);
}
};
self.onmessage = function startQueryWorker(e) {
if(e.data.type === 'start') {
console.log("Inside the web worker,",e,e.data,e.data.payload);
self.startQuery(e.data.payload);
}
else if(e.data.type === 'setup') {
self.setup();
}
};
self.setup = function setup() {
importScripts('/MapErpMobileDev/leaflet/mobile/js/lib/turf.js');
importScripts('/MapErpMobileDev/leaflet/mobile/js/lib/lodash.compat.min.js');
};
我这样称呼nessecary库:
self.checkIntersectionForLayerWith = function checkIntersectionForLayerWith(data) {
var checkIntersectionForLayer = function checkIntersectionForLayer() {
var checkEachConflict = function checkEachConflict(feature){
var conflict = turf.intersect(feature,data.rectFeature);
if(conflict != null) {
_.extend(conflict.properties,feature.properties);
conflict.id = feature.id;
}
return conflict;
};
var intersectiontest = self._(data.features)
.map(checkEachConflict)
.pull(void 0)
.thru(self.turf.featurecollection)
.value();
return {
layerLabel:data.layerLabel,
query:data.rectFeature,
result:data.intersectiontest
};
};
return checkIntersectionForLayer;
};
self.startQuery = function startQuery(data) {
if(data != null) {
var checkIntersectionForLayer = self.checkIntersectionForLayerWith(data);
var result = checkIntersectionForLayer();
var payload = {
type:'rectangle_query_worker_result',
result:result
};
self.postMessage(payload);
}
};
self.onmessage = function startQueryWorker(e) {
if(e.data.type === 'start') {
console.log("Inside the web worker,",e,e.data,e.data.payload);
self.startQuery(e.data.payload);
}
else if(e.data.type === 'setup') {
self.setup();
}
};
self.setup = function setup() {
importScripts('/MapErpMobileDev/leaflet/mobile/js/lib/turf.js');
importScripts('/MapErpMobileDev/leaflet/mobile/js/lib/lodash.compat.min.js');
};
这和承诺有什么关系?什么是“不回来”?你的意思是说
e.data.payload
为null
并且从不调用postMessage
?或者只是intersectiontest
?另外,.pull(void 0)
相当于。pull(未定义)
但是.map()
似乎不太可能返回稀疏数组。空值更可能由checkeeAchConflict()
中的代码指示。试试.pull(null)
或者,皮带和背带,。pull(null,void 0)
。啊,好的。我会更乐意使用null,所以尝试var conflict=turp.intersect(feature,data.rectFeature)| null
和.pull(null)
。可能没什么区别,但更干净。