-1

Updated:

I have typescript classes that have functions in them and I can pass those classes using postMessage fine. But the following class with a variable that is function fails.

For the code below calling doitSuccess() succeeds. Calling doitFails() throws a "can not serialize" exception. I have also successfully passed objects that are typescript classes that have functions in the class. What seems to be the problem is a member variable assigned a function.

Any idea why?

Update: It might be what is discussed in this post.

function defaultToString(item: any): string {
    return "dave";
}

export class WorkerApi {

    private worker:Worker;

    constructor () {
        worker = new Worker("my-worker.js");
    }

    public doitFails() : void {

        var ll = { name : "dave", toStr: defaultToString};

        this.worker.postMessage(ll);
    }

    public doitSuccesss() : void {

        var ll = { name : "dave"};

        this.worker.postMessage(ll);
    }
}
Community
  • 1
  • 1
David Thielen
  • 28,723
  • 34
  • 119
  • 193

1 Answers1

0

Probably the wrong this. Fix :

function defaultToString(item: any): string {
    return "dave";
}

export class WorkerApi {

    private worker:Worker;

    public doit = () => { // fix

        var ll = { name : "dave", toStr: defaultToString};

        this.worker.postMessage(ll);
    }
}

PS: a video on this https://www.youtube.com/watch?v=tvocUcbCupA&hd=1

basarat
  • 261,912
  • 58
  • 460
  • 511
  • I'm calling postMessage() fine if I don't have a member variable assigned a function so I know this is ok as is the rest of it. – David Thielen Apr 26 '14 at 12:06