forked from OfficeDev/Office-Add-in-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions-worker.js
93 lines (85 loc) · 2.48 KB
/
functions-worker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
self.addEventListener('message',
function(event) {
let job = event.data;
if (typeof(job) == "string") {
job = JSON.parse(job);
}
const jobId = job.jobId;
try {
const result = invokeFunction(job.name, job.parameters);
// check whether the result is a promise.
if (typeof(result) == "function" || typeof(result) == "object" && typeof(result.then) == "function") {
result.then(function(realResult) {
postMessage(
{
jobId: jobId,
result: realResult
}
);
})
.catch(function(ex) {
postMessage(
{
jobId: jobId,
error: true
}
)
});
}
else {
postMessage({
jobId: jobId,
result: result
});
}
}
catch(ex) {
postMessage({
jobId: jobId,
error: true
});
}
}
);
function invokeFunction(name, parameters) {
if (name == "TEST") {
return test.apply(null, parameters);
}
else if (name == "TEST_PROMISE") {
return test_promise.apply(null, parameters);
}
else if (name == "TEST_ERROR") {
return test_error.apply(null, parameters);
}
else if (name == "TEST_ERROR_PROMISE") {
return test_error_promise.apply(null, parameters);
}
else {
throw new Error("not supported");
}
}
function test(n) {
let ret = 0;
for (let i = 0; i < n; i++) {
ret += Math.tan(Math.atan(Math.tan(Math.atan(Math.tan(Math.atan(Math.tan(Math.atan(Math.tan(Math.atan(50))))))))));
for (let l = 0; l < n; l++) {
ret -= Math.tan(Math.atan(Math.tan(Math.atan(Math.tan(Math.atan(Math.tan(Math.atan(Math.tan(Math.atan(50))))))))));
}
}
return ret;
}
function test_promise(n) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve(test(n));
}, 1000);
});
}
function test_error(n) {
throw new Error();
}
function test_error_promise(n) {
return Promise.reject(new Error());
}