Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 165x 165x 165x 60x 59x 3x 3x 60x 63x 7x 22x 22x 22x 19x 19x 16x 16x 3x 3x 1x 1x 22x 22x 3x 3x 3x 3x | import { runWithConcurrency } from '@be-music/shared-utils/core';
/**
* Minimal slice of `FileSystemFileEntry` the deferred handle needs. Kept structural so tests can hand in a stub.
*/
export interface DroppedFileEntryLike {
readonly name: string;
file(successCallback: (file: File) => void, errorCallback?: (error: unknown) => void): void;
}
/**
* A dropped file whose `File` object has not been opened yet. Calling `FileSystemFileEntry.file()` costs a browser
* round trip per file (tens of microseconds each, serialised), which dominates a multi-thousand-file drop — so the drop
* walk hands back this handle for song-bundle assets and only opens the entry when somebody actually reads the bytes.
*
* It satisfies the same `{ name, webkitRelativePath, arrayBuffer() }` shape the song loader and `loadAssetBytes`
* already accept for lazy `File` references.
*/
export class DeferredDroppedFile {
readonly name: string;
readonly webkitRelativePath: string;
private readonly entry: DroppedFileEntryLike;
private pending: Promise<File> | undefined;
constructor(entry: DroppedFileEntryLike, relativePath: string) {
this.entry = entry;
this.name = entry.name;
this.webkitRelativePath = relativePath;
}
/** Opens the underlying entry once and caches the result; a failed open is retried on the next call. */
getFile(): Promise<File> {
this.pending ??= new Promise<File>((resolve, reject) => {
this.entry.file(resolve, reject);
}).catch((error: unknown) => {
this.pending = undefined;
throw error;
});
return this.pending;
}
async arrayBuffer(): Promise<ArrayBuffer> {
return (await this.getFile()).arrayBuffer();
}
async text(): Promise<string> {
return (await this.getFile()).text();
}
}
/** A file handed back by the drop walk: either an opened `File` or a handle that opens on first read. */
export type BrowserDroppedFile = File | DeferredDroppedFile;
/**
* Opens every deferred handle in `files` (bounded concurrency) and returns plain `File`s in the same order. Handles that
* fail to open are dropped from the result and reported through `onError`. Use it before passing dropped files to an
* API that needs real `File`s, such as the theme loaders.
*/
export async function materializeDroppedFiles(
files: readonly BrowserDroppedFile[],
options: {
concurrency?: number;
signal?: AbortSignal;
onError?: (file: DeferredDroppedFile, error: unknown) => void;
} = {},
): Promise<File[]> {
const opened: Array<File | undefined> = Array.from({ length: files.length });
const indices = files.map((_, index) => index);
await runWithConcurrency(
indices,
options.concurrency ?? 16,
async (index) => {
const file = files[index]!;
if (!(file instanceof DeferredDroppedFile)) {
opened[index] = file;
return;
}
try {
opened[index] = withRelativePath(await file.getFile(), file.webkitRelativePath);
} catch (error) {
options.signal?.throwIfAborted();
options.onError?.(file, error);
}
},
options.signal === undefined ? {} : { signal: options.signal },
);
options.signal?.throwIfAborted();
return opened.filter((file): file is File => file !== undefined);
}
/**
* Pins `webkitRelativePath` on a `File` opened from a `FileSystemEntry` walk. The browser leaves that property empty
* for entry-sourced files, but every loader keys the dropped tree by it.
*/
export function withRelativePath(file: File, relativePath: string): File {
Iif (file.webkitRelativePath === relativePath) {
return file;
}
try {
Object.defineProperty(file, 'webkitRelativePath', {
configurable: true,
enumerable: true,
value: relativePath,
writable: false,
});
return file;
} catch {
return new Proxy(file, {
get(target, property) {
if (property === 'webkitRelativePath') {
return relativePath;
}
const value = Reflect.get(target, property, target);
return typeof value === 'function' ? value.bind(target) : value;
},
});
}
}
|