-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathworker.js
80 lines (74 loc) · 1.78 KB
/
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
importScripts('rescomb.js');
var $rc=init$rc();
var savedTolerance=null;
onmessage=function(e) {
if(typeof(e.data)!="undefined") {
if("start"==e.data.cmd) {
// take the parameters and start
var numRes=e.data.numRes;
var target=e.data.target;
var tolerance=e.data.tolerance;
var combType=e.data.combType;
var srcVals;
if("E24"==e.data.srcVals) {
srcVals=$rc.E24Series;
numRes=Math.min(3, numRes);
}
else if("E12"==e.data.srcVals) {
srcVals=$rc.E12Series;
numRes=Math.min(4, numRes);
}
else if($rc.util.isArray(e.data.srcVals)) {
srcVals=e.data.srcVals;
}
start(numRes, target, tolerance, srcVals, combType);
}
}
}
/*
* the Serial/Parallel instances don't survive the transfer
* to the worker's master. Thus transferring the data
* as a map-object, with the combination specified as string
*/
function convert(vals) {
var valObjs=[];
vals.forEach(function(e) {
var c=e.v.toString();
var v=e.v.getValue();
var min=e.v.getMinValue(savedTolerance);
var max=e.v.getMaxValue(savedTolerance);
min=Math.round(min*100000)/100000;
max=Math.round(max*100000)/100000;
valObjs.push( {
c:c,
v:v,
min:min,
max:max
}
);
});
return valObjs;
}
function progress(numCombs, vals) {
postMessage({
type:"progress",
combs: numCombs,
vals: convert(vals)
}
);
}
function done(numCombs, vals) {
postMessage({
type:"done",
combs: numCombs,
vals: convert(vals)
}
);
}
function start(numResistors, targetValue, tolerance, srcVals, combType) {
savedTolerance=tolerance;
$rc.makeValues(
numResistors, targetValue, tolerance, srcVals,
64, combType, progress, done
);
}