From 97396a1007586d705d64548fbc76890f16f831e9 Mon Sep 17 00:00:00 2001 From: Nathan Adams Date: Sat, 29 Jun 2024 15:41:46 +0200 Subject: [PATCH] web: Changed installRuffle to always work with window.RufflePlayer, saves boilerplate --- web/packages/core/src/index.ts | 34 ++++++++-------------------- web/packages/demo/src/main.tsx | 2 +- web/packages/extension/src/player.ts | 7 +++--- web/packages/extension/src/ruffle.ts | 13 +++++++---- web/packages/selfhosted/js/ruffle.js | 2 +- 5 files changed, 24 insertions(+), 34 deletions(-) diff --git a/web/packages/core/src/index.ts b/web/packages/core/src/index.ts index 24e11b50a..113b586b8 100644 --- a/web/packages/core/src/index.ts +++ b/web/packages/core/src/index.ts @@ -15,39 +15,27 @@ export * from "./build-info"; export * from "./swf-utils"; export * from "./movie-metadata"; -import { PublicAPI, PublicAPILike } from "./public-api"; +import { PublicAPI } from "./public-api"; import { SourceAPI } from "./source-api"; /** - * Join a source into the public API, if it doesn't already exist. + * Install this version of Ruffle into the current page. * - * @param prevRuffle The previous iteration of the Ruffle API. + * Multiple (or zero) versions of Ruffle may be installed at the same time, + * and you should use `window.RufflePlayer.newest()` or similar to access the appropriate + * installation at time of use. * - * The `prevRuffle` param lists the previous object in the RufflePlayer - * slot. We perform some checks to see if this is a Ruffle public API or a - * conflicting object. If this is conflicting, then a new public API will - * be constructed (see the constructor information for what happens to - * `prevRuffle`). - * - * Note that Public API upgrades are deliberately not enabled in this - * version of Ruffle, since there is no Public API to upgrade from. * @param sourceName The name of this particular * Ruffle source. Common convention is "local" for websites that bundle their own Ruffle, * "extension" for browser extensions, and something else for other use cases. - * - * If both parameters are provided they will be used to define a new Ruffle - * source to register with the public API. - * @returns The Ruffle Public API. */ -export function installRuffle( - prevRuffle?: PublicAPILike | null, - sourceName?: string, -): PublicAPI { +export function installRuffle(sourceName?: string): void { let publicAPI: PublicAPI; - if (prevRuffle instanceof PublicAPI) { - publicAPI = prevRuffle; + if (window.RufflePlayer instanceof PublicAPI) { + publicAPI = window.RufflePlayer; } else { - publicAPI = new PublicAPI(prevRuffle); + publicAPI = new PublicAPI(window.RufflePlayer); + window.RufflePlayer = publicAPI; } if (sourceName !== undefined) { @@ -63,6 +51,4 @@ export function installRuffle( SourceAPI.pluginPolyfill(); } } - - return publicAPI; } diff --git a/web/packages/demo/src/main.tsx b/web/packages/demo/src/main.tsx index 6f7ce860b..846c45ad3 100644 --- a/web/packages/demo/src/main.tsx +++ b/web/packages/demo/src/main.tsx @@ -12,7 +12,7 @@ import { UnmuteOverlay, } from "ruffle-core"; -window.RufflePlayer = installRuffle(window.RufflePlayer, "local"); +installRuffle("local"); ReactDOM.createRoot(document.getElementById("root")!).render( diff --git a/web/packages/extension/src/player.ts b/web/packages/extension/src/player.ts index 7eaa223f1..3af2ce38b 100644 --- a/web/packages/extension/src/player.ts +++ b/web/packages/extension/src/player.ts @@ -1,5 +1,5 @@ import * as utils from "./utils"; -import { installRuffle } from "ruffle-core"; +import { installRuffle, PublicAPI } from "ruffle-core"; import type { Letterbox, RufflePlayer, @@ -17,9 +17,8 @@ declare global { } } -const api = installRuffle(window.RufflePlayer!, "local"); -window.RufflePlayer = api; -const ruffle = api.newest()!; +installRuffle("local"); +const ruffle = (window.RufflePlayer as PublicAPI).newest()!; let player: RufflePlayer; const playerContainer = document.getElementById("player-container")!; diff --git a/web/packages/extension/src/ruffle.ts b/web/packages/extension/src/ruffle.ts index 35abc173d..ed518da3b 100644 --- a/web/packages/extension/src/ruffle.ts +++ b/web/packages/extension/src/ruffle.ts @@ -4,13 +4,18 @@ import { Message } from "./messages"; function handleMessage(message: Message) { switch (message.type) { case "load": { - const api = window.RufflePlayer ?? {}; - api.config = { + if (window.RufflePlayer === undefined) { + window.RufflePlayer = {}; + } + if (window.RufflePlayer.config === undefined) { + window.RufflePlayer.config = {}; + } + window.RufflePlayer.config = { ...message.config, - ...api.config, + ...window.RufflePlayer.config, openInNewTab, }; - window.RufflePlayer = installRuffle(api, "extension"); + installRuffle("extension"); return {}; } case "ping": diff --git a/web/packages/selfhosted/js/ruffle.js b/web/packages/selfhosted/js/ruffle.js index 04d1749cd..6445cb4db 100644 --- a/web/packages/selfhosted/js/ruffle.js +++ b/web/packages/selfhosted/js/ruffle.js @@ -1,3 +1,3 @@ import { installRuffle } from "ruffle-core"; -window.RufflePlayer = installRuffle(window.RufflePlayer, "local"); +installRuffle("local");