简体   繁体   中英

How do I view the raw data posted in a Worker.postMessage( myObject ) call?

The nice thing about the web worker postMessage() when sending an object, is it serializes the entire object including all elements of the object that are themselves objects. All data that is a pointer to another object comes across and is rebuilt with everything pointing at the right thing.

The problem is... making sure it only brings across what I need. I am pulling a subset of a linked list of objects where there is a lot of pointers in object to other objects. ie, it's not just child elements that are an object pointed to by just the parent object, there's a lot of relationship pointers.

Is there a way to see what the postMessage() copies across? Like a way to see the raw JSON?

My mistake. I use Chrome and forgot that Chrome's console.log is different than most browsers. Try using console.dir(object).

I elaborated on the code a little too.

To log what your worker sends back to the main thread...

// will log what your worker is sending back to the main thread
onmessage = function (e) { console.dir(e.data); } 

To see what the main thread is sending to the worker...

// The worker
var worker = new Worker('worker.js');

// Prepare message for worker
var message = {
    colors: ['red', 'blue', 'green'],
    numbers: [1,2,3]
};

// log it
console.dir(message);

// send it
worker.sendMessage(message)

Also maybe check this answer out to see the difference between console.log and console.dir.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM