forked from stimms/JavaScriptPatterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFanOutIn.js
56 lines (53 loc) · 2.05 KB
/
FanOutIn.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
var Westeros;
(function (Westeros) {
(function (Potion) {
var Ingredient = (function () {
function Ingredient() {
}
return Ingredient;
})();
Potion.Ingredient = Ingredient;
var CombinedIngredient = (function () {
function CombinedIngredient() {
}
CombinedIngredient.prototype.Add = function (ingredient) {
};
return CombinedIngredient;
})();
Potion.CombinedIngredient = CombinedIngredient;
var Step = (function () {
function Step() {
}
return Step;
})();
var Combiner = (function () {
function Combiner() {
this.waitingForChunks = 0;
}
Combiner.prototype.combine = function (ingredients) {
var _this = this;
console.log("Starting combination");
if (ingredients.length > 10) {
for (var i = 0; i < Math.ceil(ingredients.length / 2); i++) {
this.waitingForChunks++;
console.log("Dispatched chunks count at: " + this.waitingForChunks);
var worker = new Worker("FanOutInWebWorker.js");
worker.addEventListener('message', function (message) {
return _this.complete(message);
});
worker.postMessage({ ingredients: ingredients.slice(i, i * 2) });
}
}
};
Combiner.prototype.complete = function (message) {
this.waitingForChunks--;
console.log("Outstanding chunks count at: " + this.waitingForChunks);
if (this.waitingForChunks == 0)
console.log("All chunks received");
};
return Combiner;
})();
Potion.Combiner = Combiner;
})(Westeros.Potion || (Westeros.Potion = {}));
var Potion = Westeros.Potion;
})(Westeros || (Westeros = {}));