|
| 1 | +import { createDeferred } from '@ember-data/request'; |
| 2 | +import type { Deferred } from '@ember-data/request/-private/types'; |
| 3 | + |
| 4 | +export type SuccessResponseEventData = { |
| 5 | + type: 'success-response'; |
| 6 | + thread: string; |
| 7 | + url: string; |
| 8 | +}; |
| 9 | +export type ErrorResponseEventData = { |
| 10 | + type: 'error-response'; |
| 11 | + thread: string; |
| 12 | + url: string; |
| 13 | +}; |
| 14 | + |
| 15 | +export type RequestEventData = { |
| 16 | + type: 'load'; |
| 17 | + thread: string; |
| 18 | + url: string; |
| 19 | +}; |
| 20 | + |
| 21 | +export type ThreadInitEventData = { |
| 22 | + type: 'connect'; |
| 23 | + thread: string; |
| 24 | +}; |
| 25 | + |
| 26 | +export type MainThreadEvent = MessageEvent< |
| 27 | + SuccessResponseEventData | ErrorResponseEventData |
| 28 | +>; |
| 29 | +export type WorkerThreadEvent = |
| 30 | + | MessageEvent<RequestEventData> |
| 31 | + | MessageEvent<ThreadInitEventData>; |
| 32 | + |
| 33 | +import { assert } from '@ember/debug'; |
| 34 | + |
| 35 | +export interface FastBoot { |
| 36 | + // eslint-disable-next-line no-unused-vars |
| 37 | + require(_moduleName: string): unknown; |
| 38 | + isFastBoot: boolean; |
| 39 | + request: Request; |
| 40 | +} |
| 41 | + |
| 42 | +const isServerEnv = typeof FastBoot !== 'undefined'; |
| 43 | + |
| 44 | +// This is a copy of WarpDrive's ImageFetch class with |
| 45 | +// a temporary fix for an issue with which url is returned. |
| 46 | +export class ImageFetch { |
| 47 | + declare worker: Worker | SharedWorker; |
| 48 | + declare threadId: string; |
| 49 | + declare pending: Map<string, Deferred<string>>; |
| 50 | + declare channel: MessageChannel; |
| 51 | + declare cache: Map<string, string>; |
| 52 | + declare queue: MainThreadEvent[]; |
| 53 | + |
| 54 | + constructor(worker: Worker | SharedWorker | null) { |
| 55 | + this.threadId = isServerEnv ? '' : crypto.randomUUID(); |
| 56 | + this.pending = new Map(); |
| 57 | + this.cache = new Map(); |
| 58 | + this.queue = []; |
| 59 | + |
| 60 | + const isTesting = false; |
| 61 | + assert( |
| 62 | + `Expected a SharedWorker instance`, |
| 63 | + isTesting || isServerEnv || worker instanceof SharedWorker, |
| 64 | + ); |
| 65 | + this.worker = worker as SharedWorker; |
| 66 | + |
| 67 | + if (!isServerEnv) { |
| 68 | + const fn = (event: MainThreadEvent) => { |
| 69 | + this.queue.push(event); |
| 70 | + if (this.queue.length === 1) { |
| 71 | + setTimeout(() => { |
| 72 | + this.processQueue(); |
| 73 | + }, 0); |
| 74 | + } |
| 75 | + }; |
| 76 | + |
| 77 | + if (worker instanceof SharedWorker) { |
| 78 | + worker.port.postMessage({ type: 'connect', thread: this.threadId }); |
| 79 | + worker.port.onmessage = fn; |
| 80 | + } else if (worker) { |
| 81 | + this.channel = new MessageChannel(); |
| 82 | + worker.postMessage({ type: 'connect', thread: this.threadId }, [ |
| 83 | + this.channel.port2, |
| 84 | + ]); |
| 85 | + |
| 86 | + this.channel.port1.onmessage = fn; |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + processQueue() { |
| 92 | + const queue = this.queue; |
| 93 | + this.queue = []; |
| 94 | + |
| 95 | + queue.forEach((event) => { |
| 96 | + const { type, url, objectUrl } = event.data; |
| 97 | + const deferred = this.cleanupRequest(url); |
| 98 | + if (!deferred) { |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + if (type === 'success-response') { |
| 103 | + deferred.resolve(objectUrl); |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + if (type === 'error-response') { |
| 108 | + deferred.reject(null); |
| 109 | + return; |
| 110 | + } |
| 111 | + }); |
| 112 | + } |
| 113 | + |
| 114 | + cleanupRequest(url: string) { |
| 115 | + const deferred = this.pending.get(url); |
| 116 | + this.pending.delete(url); |
| 117 | + |
| 118 | + return deferred; |
| 119 | + } |
| 120 | + |
| 121 | + _send(event: RequestEventData) { |
| 122 | + this.worker instanceof SharedWorker |
| 123 | + ? this.worker.port.postMessage(event) |
| 124 | + : this.channel.port1.postMessage(event); |
| 125 | + } |
| 126 | + |
| 127 | + load(url: string) { |
| 128 | + if (isServerEnv) { |
| 129 | + return Promise.resolve(url); |
| 130 | + } |
| 131 | + |
| 132 | + const objectUrl = this.cache.get(url); |
| 133 | + if (objectUrl) { |
| 134 | + return Promise.resolve(objectUrl); |
| 135 | + } |
| 136 | + |
| 137 | + const deferred = createDeferred<string>(); |
| 138 | + this.pending.set(url, deferred); |
| 139 | + this._send({ type: 'load', thread: this.threadId, url }); |
| 140 | + return deferred.promise; |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +export default { |
| 145 | + create() { |
| 146 | + return new ImageFetch( |
| 147 | + new SharedWorker( |
| 148 | + new URL('../data-worker/image-worker.ts', import.meta.url), |
| 149 | + { |
| 150 | + name: 'ImageWorker', |
| 151 | + type: 'module', |
| 152 | + }, |
| 153 | + ), |
| 154 | + ); |
| 155 | + }, |
| 156 | +}; |
0 commit comments