aboutsummaryrefslogtreecommitdiffstats
path: root/examples/quick/threading/workerscript/workerscript.js
blob: 67fbd8af9a9aee797356247b07e2e14116c3afd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//Will be initialized when WorkerScript{} is instantiated
var cache = new Array(64);
for (var i = 0; i < 64; i++)
    cache[i] = new Array(64);

function triangle(row, column) {
    if (cache[row][column])
        return cache[row][column]
    if (column < 0 || column > row)
        return -1;
    if (column == 0 || column == row)
        return 1;
    return triangle(row-1, column-1) + triangle(row-1, column);
}
//! [0]
WorkerScript.onMessage = function(message) {
    //Calculate result (may take a while, using a naive algorithm)
    var calculatedResult = triangle(message.row, message.column);
    //Send result back to main thread
    WorkerScript.sendMessage( { row: message.row,
                                column: message.column,
                                result: calculatedResult} );
}
//! [0]