-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaverage-colour.service.ts
159 lines (128 loc) · 4.53 KB
/
average-colour.service.ts
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
158
159
import { Injectable, NgZone, OnDestroy } from '@angular/core';
import { from, map, NEVER, Observable, ReplaySubject, switchMap, take, takeUntil } from 'rxjs';
import { FastAverageColor, FastAverageColorOptions } from 'fast-average-color';
import { AverageColourWorkerAction } from '../domain/average-colour-worker-action';
import { AverageColourWorkerMessage } from '../domain/average-colour-worker-message';
import { getDeferredPromise } from '../../utility/get-deferred-promise';
import { DeferredPromise } from '../domain/deferred-promise';
import { LogLevel } from '../domain/utility/log-level';
import { LogService } from './utility/log.service';
@Injectable({
providedIn: 'root'
})
export class AverageColourService implements OnDestroy {
private static readonly offscreenCanvasUnsupportedMsg =
'Offscreen canvas is not supported! Failing back to main thread average colour processing.';
private static readonly offscreenCanvasSupportedMsg =
'Offscreen canvas is supported on this device.';
private readonly ongoingTasks = new Map<string, DeferredPromise<string>>();
private worker?: Worker;
private fallbackFac?: FastAverageColor;
private facOptions: FastAverageColorOptions = {
mode: 'speed',
algorithm: 'simple'
};
private readonly offscreenCanvasSupportedS = new ReplaySubject<boolean>();
private readonly destroyedS = new ReplaySubject<boolean>(1);
constructor(private readonly logger: LogService, private readonly zone: NgZone) {
this.generateWorker();
}
ngOnDestroy() {
this.beginWorkerDestroy();
this.cleanUpObservables();
}
getAverageImageUrlRgba(url: string): Observable<string> {
return this.zone.runOutsideAngular(() =>
this.offscreenCanvasSupportedS.pipe(
take(1),
switchMap((isSupported) => this.getColourWithFallback(url, isSupported)),
takeUntil(this.destroyedS)
)
);
}
private getColourWithFallback(url: string, isSupported: boolean): Observable<string> {
if (isSupported) {
return from(this.assignNewPromise(url));
} else if (this.fallbackFac) {
return from(this.fallbackFac.getColorAsync(url, this.facOptions)).pipe(
map((result) => result.hex)
);
}
return NEVER;
}
private assignNewPromise(url: string): Promise<string> {
const deferred = getDeferredPromise<string>();
const preExisting = this.ongoingTasks.get(url);
if (preExisting) {
return preExisting.promise;
}
const msg: AverageColourWorkerMessage<[string, FastAverageColorOptions]> = [
AverageColourWorkerAction.colour,
url,
this.facOptions
];
this.ongoingTasks.set(url, deferred);
this.worker?.postMessage(msg);
return deferred.promise;
}
private onMessage(msg: AverageColourWorkerMessage<[(string | boolean)?, string?]>) {
const [action, mainProp, secondProp] = msg;
const url = mainProp as string;
const colour = secondProp as string;
const offscreenCanvasSupported = mainProp as boolean;
switch (action) {
case AverageColourWorkerAction.colour:
return this.onColourReceived(url, colour);
case AverageColourWorkerAction.checkedSupported:
return this.onOffscreenCanvasSupportChecked(offscreenCanvasSupported);
case AverageColourWorkerAction.destroyed:
return this.onWorkerDestroyed();
}
}
private onColourReceived(url?: string, colour?: string) {
if (!url || !colour) {
return;
}
const deferred = this.ongoingTasks.get(url);
if (deferred) {
this.ongoingTasks.delete(url);
deferred.resolve(colour);
}
}
private onOffscreenCanvasSupportChecked(supported: boolean) {
if (supported) {
this.logger.log(LogLevel.info, AverageColourService.offscreenCanvasSupportedMsg);
} else {
this.fallbackFac = new FastAverageColor();
this.logger.log(LogLevel.warn, AverageColourService.offscreenCanvasUnsupportedMsg);
}
this.offscreenCanvasSupportedS.next(supported);
}
private onWorkerDestroyed() {
const worker = this.worker;
if (worker) {
worker.onmessage = null;
worker.terminate();
}
this.fallbackFac?.destroy();
this.worker = undefined;
}
private generateWorker() {
const msg: AverageColourWorkerMessage<undefined[]> = [
AverageColourWorkerAction.checkSupported
];
const worker = (this.worker = new Worker(
new URL('../workers/average-colour.worker', import.meta.url)
));
worker.onmessage = (event) => this.onMessage(event.data);
worker.postMessage(msg);
}
private beginWorkerDestroy() {
const msg: AverageColourWorkerMessage<undefined[]> = [AverageColourWorkerAction.destroy];
this.worker?.postMessage(msg);
}
private cleanUpObservables() {
this.destroyedS.next(true);
this.destroyedS.complete();
}
}