-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathapp.component.ts
More file actions
157 lines (140 loc) · 4.85 KB
/
app.component.ts
File metadata and controls
157 lines (140 loc) · 4.85 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { Component, OnInit } from '@angular/core';
import { WebWorkerService } from '../web-worker.service';
import { Result } from './result';
@Component({
selector: 'my-app',
template: `
<h2>calculating fibonacci using web worker</h2>
<form (ngSubmit)='startWebWorkerCalculation()'>
<div>
<span>calculate fib(n) from</span>
<input type='number' [(ngModel)]='webWorkerStart' name='webWorkerStart' />
<span>to:</span>
<input type='number' [(ngModel)]='webWorkerEnd' name='webWorkerEnd' />
</div>
<button type='submit'>Start</button>
<button type='button' (click)='stopWebWorkerCalculation()'>Stop</button>
</form>
<div>
<p *ngFor='let result of webWorkerResults'>
fib({{ result.number }}) =
<span *ngIf='result.loading' class='spin-me-baby'>... calculating ...</span>
<span *ngIf='!result.loading'>{{ result.result }}</span>
</p>
</div>
<h2>calculating fibonacci using main UI thread</h2>
<form (ngSubmit)='startSynchronousCalculation()'>
<div>
<span>calculate fib(n) from</span>
<input type='number' [(ngModel)]='synchronousStart' name='synchronousStart' />
<span>to:</span>
<input type='number' [(ngModel)]='synchronousEnd' name='synchronousEnd' />
</div>
<button type='submit'>Start (might lock up your browser for large numbers)</button>
</form>
<div>
<span *ngIf='synchronousDuration' [class.zoom-me-baby]='true'>took {{ synchronousDuration }} seconds</span>
<p *ngFor='let result of synchronousResults'>
fib({{ result.number }}) = {{ result.result }}
</p>
</div>
`,
styles: [
`
.spin-me-baby {
animation: spin 4s linear infinite;
}
@keyframes spin {
100% {
transform: rotate(360deg);
}
}
.zoom-me-baby {
animation: zoom 4s linear infinite;
}
@keyframes zoom {
50% {
font-size: 2em;
}
100% {
font-size: 1em;
}
}
`
],
providers: [WebWorkerService]
})
export class AppComponent implements OnInit {
public webWorkerResults: any[] = [];
public webWorkerStart = 35;
public webWorkerEnd = 45;
public synchronousStart = 35;
public synchronousEnd = 38;
public synchronousResults: any[] = [];
public synchronousDuration = 0;
private promises: Promise<any>[] = [];
constructor(private _webWorkerService: WebWorkerService) {
}
startWebWorkerCalculation() {
let pointer = this.webWorkerStart;
const end = this.webWorkerEnd;
this.stopWebWorkerCalculation();
while (pointer <= end) {
this.webWorkerCalculate(pointer);
pointer++;
}
}
stopWebWorkerCalculation() {
this.promises.forEach(promise => {
this._webWorkerService.terminate(promise);
});
this.promises.length = 0;
this.webWorkerResults.length = 0;
}
startSynchronousCalculation() {
let pointer = this.synchronousStart;
const end = this.synchronousEnd;
this.synchronousResults.length = 0;
const start = new Date();
while (pointer <= end) {
const result = new Result(pointer, this.fib(pointer), false);
this.synchronousResults.push(result);
pointer++;
}
this.synchronousDuration = ((new Date()).getTime() - start.getTime()) / 1000;
}
startExternalRequest() {
const promises = [];
promises.push(this._webWorkerService.runUrl('dist/app/echo.js', 'marco'));
promises.push(this._webWorkerService.run(() => 'polo', 0));
promises.forEach(promise => {
let worker = this._webWorkerService.getWorker(promise);
worker.addEventListener('message', event => {
console.log('getWorker', event.data);
});
});
Promise.all(promises)
.then(response => console.log(response))
.catch(error => console.error(error));
}
ngOnInit() {
this.startExternalRequest();
}
private webWorkerCalculate(n: number) {
const promise = this._webWorkerService.run(this.fib, n);
const result = new Result(n, 0, true);
this.webWorkerResults.push(result);
this.promises.push(promise);
promise.then(function (response) {
result.result = response;
result.loading = false;
});
}
private fib(n: number) {
const fib = (n: number) : number => {
if (n < 2) return 1;
return fib(n - 1) + fib(n - 2);
};
return fib(n);
}
}