Files
v10/dist/dev/media/dom/mse/buffer-flusher.js
T

55 lines
1.6 KiB
JavaScript

//#region src/media/dom/mse/buffer-flusher.ts
/**
* Buffer flusher helper (P12)
*
* Removes a time range from a SourceBuffer to manage memory.
*/
/**
* Remove a time range from a SourceBuffer.
*
* Waits for the SourceBuffer to be ready (not updating), then removes
* the specified range. Returns a promise that resolves when removal completes.
*
* @param sourceBuffer - The SourceBuffer to remove data from
* @param start - Start of the time range to remove (seconds)
* @param end - End of the time range to remove (seconds)
* @returns Promise that resolves when removal completes
*
* @example
* await flushBuffer(videoSourceBuffer, 0, 30);
*/
async function flushBuffer(sourceBuffer, start, end) {
if (sourceBuffer.updating) await new Promise((resolve) => {
const onUpdateEnd = () => {
sourceBuffer.removeEventListener("updateend", onUpdateEnd);
resolve();
};
sourceBuffer.addEventListener("updateend", onUpdateEnd);
});
return new Promise((resolve, reject) => {
const onUpdateEnd = () => {
cleanup();
resolve();
};
const onError = (event) => {
cleanup();
reject(/* @__PURE__ */ new Error(`SourceBuffer remove error: ${event.type}`));
};
const cleanup = () => {
sourceBuffer.removeEventListener("updateend", onUpdateEnd);
sourceBuffer.removeEventListener("error", onError);
};
sourceBuffer.addEventListener("updateend", onUpdateEnd);
sourceBuffer.addEventListener("error", onError);
try {
sourceBuffer.remove(start, end);
} catch (error) {
cleanup();
reject(error);
}
});
}
//#endregion
export { flushBuffer };
//# sourceMappingURL=buffer-flusher.js.map