Skip to content
uupaa edited this page Jun 11, 2015 · 3 revisions

Thread#post の第三引数(postbackMessageHandler) を指定し、ワーカースレッド側で、event.postback を使うとメッセージをメインスレッドに送り返す事ができます。この機能をポストバックと呼びます。

ワーカースレッドでメッセージを処理し、その結果をメインスレッドで簡単に受け取る事ができます。


以下は、ポストバックを使いワーカースレッドで value の値を2倍にする例です。

var thread = new Thread("worker.js");

thread.post([1, 1], null, postbackMessageHandler);
thread.post([2, 2], null, postbackMessageHandler);

function postbackMessageHandler(args, event) {
    console.log(args); // { source: 1, result: 2 }
                       // { source: 2, result: 4 }
});
/// worker.js
var proxy = new ThreadProxy(function(args, event) {
        var source = args[0];
        var result = args[1];

        event.postback( { source: source, result: result * 2 } ); // value の値を2倍にして返します
    });
Clone this wiki locally