From f54465e5feeef6c9036447f940de4047275bc788 Mon Sep 17 00:00:00 2001 From: chylex Date: Wed, 8 May 2024 21:08:24 +0200 Subject: [PATCH] Fix viewer compatibility with older browsers and (hopefully) Safari Closes #260 --- app/Resources/Viewer/scripts/bootstrap.mjs | 1 + app/Resources/Viewer/scripts/polyfills.mjs | 35 ++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 app/Resources/Viewer/scripts/polyfills.mjs diff --git a/app/Resources/Viewer/scripts/bootstrap.mjs b/app/Resources/Viewer/scripts/bootstrap.mjs index d0c58ed..4934a0b 100644 --- a/app/Resources/Viewer/scripts/bootstrap.mjs +++ b/app/Resources/Viewer/scripts/bootstrap.mjs @@ -1,6 +1,7 @@ import discord from "./discord.mjs"; import gui from "./gui.mjs"; import state from "./state.mjs"; +import "./polyfills.mjs"; window.DISCORD = discord; diff --git a/app/Resources/Viewer/scripts/polyfills.mjs b/app/Resources/Viewer/scripts/polyfills.mjs new file mode 100644 index 0000000..1692b31 --- /dev/null +++ b/app/Resources/Viewer/scripts/polyfills.mjs @@ -0,0 +1,35 @@ +// https://gist.github.com/MattiasBuelens/496fc1d37adb50a733edd43853f2f60e/088f061ab79b296f29225467ae9ba86ff990195d + +ReadableStream.prototype.values ??= function({ preventCancel = false } = {}) { + const reader = this.getReader(); + return { + async next() { + try { + const result = await reader.read(); + if (result.done) { + reader.releaseLock(); + } + return result; + } catch (e) { + reader.releaseLock(); + throw e; + } + }, + async return(value) { + if (!preventCancel) { + const cancelPromise = reader.cancel(value); + reader.releaseLock(); + await cancelPromise; + } + else { + reader.releaseLock(); + } + return { done: true, value }; + }, + [Symbol.asyncIterator]() { + return this; + } + }; +}; + +ReadableStream.prototype[Symbol.asyncIterator] ??= ReadableStream.prototype.values;