Problem
const blob = new Blob([`
  self.onmessage = function() {
    const processor = {
      cache: new Map(),
      process(data) {
        return data.map(x => x * 2);
      }
    };
    
    processor.cache.set('results', processor.process([1,2,3]));
    processor.cache.set('processFn', processor.process);
    
    self.postMessage({
      type: 'DONE',
      state: processor.cache
    });
  };
`], { type: 'application/javascript' });

const worker = new Worker(URL.createObjectURL(blob));
worker.postMessage('start');
worker.onerror = (e) => console.error('Worker error:', e);
worker.onmessage = (e) => console.log(e.data);
Solution

The processor.cache is a Map object, which is not serializable. To fix this, you can convert the Map to a serializable object before posting it to the main thread.

const blob = new Blob([`
  self.onmessage = function() {
    const processor = {
      cache: new Map(),
      process(data) {
        return data.map(x => x * 2);
      }
    };
    
    processor.cache.set('results', processor.process([1,2,3]));
    
    // Convert Map to serializable object, skip functions
    const serializableState = {
      type: 'DONE',
      state: Object.fromEntries(processor.cache)
    };
    
    self.postMessage(serializableState);
  };
`], { type: 'application/javascript' });

const worker = new Worker(URL.createObjectURL(blob));
worker.postMessage('start');
worker.onmessage = (e) => console.log(e.data);