1

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?

admdrew
  • 3,790
  • 4
  • 27
  • 39
David Thielen
  • 28,723
  • 34
  • 119
  • 193
  • It's not JSON. The engine will use some internal serialisation format. – Bergi Jun 30 '14 at 22:56
  • Try `onmessage = console.log.bind(console);` in the Worker? – Bergi Jun 30 '14 at 22:57
  • @Bergi logging it just gives me the outer variables in the object - "Object {message: 9, subMessage: 1, msgGuid: 1, pages: Array[1], success: undefined…}" – David Thielen Jul 10 '14 at 22:24
  • Just click on the `Array [1]` in the debugger to expand its view. – Bergi Jul 10 '14 at 22:25
  • @Bergi - I understand that. My problem is the object has a tree of sub objects including possible links across to other objects. If I can get some JSON type text that makes it easy to walk through the 500+ objects in the message to see if it's just what I want. – David Thielen Jul 10 '14 at 22:33
  • For that, just call `JSON.stringify()` on it (or a non-cyclic part of it) before logging. Or try [`console.table`](https://getfirebug.com/wiki/index.php/Console.table) in Firebug. – Bergi Jul 10 '14 at 22:39
  • @Bergi - I tried that and get an exception that it's cyclic. I tried removing the parts that I think cause the cycle, and still get that. It's frustrating because if I could get the string, then I could see what I need to do to get the string... – David Thielen Jul 10 '14 at 22:41

1 Answers1

0

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.

Community
  • 1
  • 1
Shawn Whinnery
  • 3,519
  • 3
  • 16
  • 16
  • I tried that but it's just showing me the same object so I have to do the same thing, walk through all pointers. – David Thielen Jul 01 '14 at 17:00
  • Can you share your code? Also try to test it in chrome and let me know what happens. – Shawn Whinnery Jul 10 '14 at 19:43
  • I tried in Chrome and got "Object {message: 9, subMessage: 1, msgGuid: 1, pages: Array[1], success: undefined…}" for console.log(msg) and "Object" for console.dir(). The code's not sharable because there's a ton and it's proprietary. I tried to JSONify it but that gave me a no circular references error. – David Thielen Jul 10 '14 at 22:23