Skip to content

Commit 3e55e51

Browse files
committed
fix(Demo): Fix the demo timings so that the graph makes sense
1 parent 8253341 commit 3e55e51

File tree

5 files changed

+33
-20
lines changed

5 files changed

+33
-20
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -245,4 +245,5 @@ Note here that the worker class `implements DoWorkUnit<File, string>`. This is d
245245
`DoWork` which had the slightly more complex signature of inputting an observable and outputting one.
246246

247247
If using the `fromWorkerPool` strategy, you must only implement `DoWorkUnit` as it relies on the completion of the
248-
returned observable to indicate that the unit of work is finished processing.
248+
returned observable to indicate that the unit of work is finished processing, and the next unit of work can be
249+
transferred to the worker.

src/app/app.component.html

-9
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,6 @@ <h1>
1414

1515
<app-single-worker></app-single-worker>
1616

17-
<p>
18-
Note if you select a particularly large file (>1GB) you will have time to select another file and see that the
19-
original worker is terminated and a new worker is created for the new file. This is because the main thread uses a
20-
<code>switchMap</code>
21-
, but if this behaviour was not desired, simply swapping in a
22-
<code>mergeMap</code>
23-
would allow both files to complete hashing.
24-
</p>
25-
2617
<hr />
2718

2819
<app-multiple-worker-pool></app-multiple-worker-pool>

src/app/multiple-worker-pool/multiple-worker-pool.component.html

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ <h3 *ngIf="status$ | async as status">({{ status }})</h3>
33

44
<p>Select multiple files of varying sizes to compute MD5 sum of, in pool of webworkers:</p>
55

6+
<input type="file" multiple (change)="calculateMD5Multiple($event)" />
67
<section>
78
<small>(No files are uploaded; they're kept entirely within your browser)</small>
9+
<p>
10+
ℹ️ large files (>10MB) gives the best results otherwise the timing starts to be dominated by the UI updates rather
11+
than the computation of hashes
12+
</p>
813
</section>
9-
<input type="file" multiple (change)="calculateMD5Multiple($event)" />
1014

1115
<h3 [attr.data]="chartObserver$ | async">Timeline</h3>
1216
<div #timeline></div>

src/app/multiple-worker-pool/multiple-worker-pool.component.ts

+17-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, ElementRef, ViewChild } from '@angular/core';
1+
import { ChangeDetectionStrategy, Component, ElementRef, ViewChild } from '@angular/core';
22
import {
33
animationFrameScheduler,
44
asyncScheduler,
@@ -11,6 +11,7 @@ import {
1111
Subject,
1212
} from 'rxjs';
1313
import {
14+
delay,
1415
filter,
1516
groupBy,
1617
map,
@@ -34,6 +35,7 @@ import TimelineOptions = google.visualization.TimelineOptions;
3435
selector: 'app-multiple-worker-pool',
3536
templateUrl: './multiple-worker-pool.component.html',
3637
styleUrls: ['./multiple-worker-pool.component.scss'],
38+
changeDetection: ChangeDetectionStrategy.OnPush,
3739
})
3840
export class MultipleWorkerPoolComponent {
3941
@ViewChild('timeline', { read: ElementRef }) private timelineComponent: ElementRef;
@@ -135,13 +137,10 @@ export class MultipleWorkerPoolComponent {
135137
return;
136138
}
137139

138-
if (lastRow.has(event.file)) {
139-
dataTable.setCell(lastRow.get(event.file), 3, event.timestamp);
140-
}
140+
const timestamp = event.timestamp;
141141

142-
if (event.fileEventType === FileHashEvent.HASH_RECEIVED) {
143-
lastRow.delete(event.file);
144-
return;
142+
if (lastRow.has(event.file)) {
143+
dataTable.setCell(lastRow.get(event.file), 3, timestamp);
145144
}
146145

147146
let durationName: string;
@@ -164,10 +163,18 @@ export class MultipleWorkerPoolComponent {
164163
case FileHashEvent.HASH_COMPUTED:
165164
durationName = 'Returning hash result to main thread';
166165
break;
166+
case FileHashEvent.HASH_RECEIVED:
167+
durationName = 'Main thread received hash';
168+
break;
167169
}
168170

169-
const row = dataTable.addRow([event.file, durationName, event.timestamp, event.timestamp]);
170-
lastRow.set(event.file, row);
171+
const row = dataTable.addRow([event.file, durationName, timestamp, timestamp]);
172+
173+
if (event.fileEventType === FileHashEvent.HASH_RECEIVED) {
174+
lastRow.delete(event.file);
175+
} else {
176+
lastRow.set(event.file, row);
177+
}
171178

172179
chartOptions.height = filenames.length * 41 + 50;
173180

@@ -200,6 +207,7 @@ export class MultipleWorkerPoolComponent {
200207
this.complete$.pipe(
201208
filter(c => c),
202209
take(1),
210+
delay(0),
203211
),
204212
),
205213
);

src/app/single-worker/single-worker.component.html

+9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ <h2>Single Worker</h2>
33
Select file to compute MD5 sum of, in webworker:
44
<input type="file" (change)="calculateMD5($event)" />
55

6+
<p>
7+
ℹ️ If you select a particularly large file (>1GB) you will have time to select another file and see that the original
8+
worker is terminated and a new worker is created for the new file. This is because the main thread uses a
9+
<code>switchMap</code>
10+
, but if this behaviour was not desired, simply swapping in a
11+
<code>mergeMap</code>
12+
would allow both files to complete hashing.
13+
</p>
14+
615
<h3>Events:</h3>
716
<ol [attr.data]="hashResult$ | async">
817
<li *ngFor="let event of eventList$ | async">{{ event }}</li>

0 commit comments

Comments
 (0)