web: Changed installRuffle to always work with window.RufflePlayer, saves boilerplate

This commit is contained in:
Nathan Adams 2024-06-29 15:41:46 +02:00
parent 6827d4f086
commit 97396a1007
5 changed files with 24 additions and 34 deletions

View File

@ -15,39 +15,27 @@ export * from "./build-info";
export * from "./swf-utils"; export * from "./swf-utils";
export * from "./movie-metadata"; export * from "./movie-metadata";
import { PublicAPI, PublicAPILike } from "./public-api"; import { PublicAPI } from "./public-api";
import { SourceAPI } from "./source-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 * @param sourceName The name of this particular
* Ruffle source. Common convention is "local" for websites that bundle their own Ruffle, * Ruffle source. Common convention is "local" for websites that bundle their own Ruffle,
* "extension" for browser extensions, and something else for other use cases. * "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( export function installRuffle(sourceName?: string): void {
prevRuffle?: PublicAPILike | null,
sourceName?: string,
): PublicAPI {
let publicAPI: PublicAPI; let publicAPI: PublicAPI;
if (prevRuffle instanceof PublicAPI) { if (window.RufflePlayer instanceof PublicAPI) {
publicAPI = prevRuffle; publicAPI = window.RufflePlayer;
} else { } else {
publicAPI = new PublicAPI(prevRuffle); publicAPI = new PublicAPI(window.RufflePlayer);
window.RufflePlayer = publicAPI;
} }
if (sourceName !== undefined) { if (sourceName !== undefined) {
@ -63,6 +51,4 @@ export function installRuffle(
SourceAPI.pluginPolyfill(); SourceAPI.pluginPolyfill();
} }
} }
return publicAPI;
} }

View File

@ -12,7 +12,7 @@ import {
UnmuteOverlay, UnmuteOverlay,
} from "ruffle-core"; } from "ruffle-core";
window.RufflePlayer = installRuffle(window.RufflePlayer, "local"); installRuffle("local");
ReactDOM.createRoot(document.getElementById("root")!).render( ReactDOM.createRoot(document.getElementById("root")!).render(
<React.StrictMode> <React.StrictMode>

View File

@ -1,5 +1,5 @@
import * as utils from "./utils"; import * as utils from "./utils";
import { installRuffle } from "ruffle-core"; import { installRuffle, PublicAPI } from "ruffle-core";
import type { import type {
Letterbox, Letterbox,
RufflePlayer, RufflePlayer,
@ -17,9 +17,8 @@ declare global {
} }
} }
const api = installRuffle(window.RufflePlayer!, "local"); installRuffle("local");
window.RufflePlayer = api; const ruffle = (window.RufflePlayer as PublicAPI).newest()!;
const ruffle = api.newest()!;
let player: RufflePlayer; let player: RufflePlayer;
const playerContainer = document.getElementById("player-container")!; const playerContainer = document.getElementById("player-container")!;

View File

@ -4,13 +4,18 @@ import { Message } from "./messages";
function handleMessage(message: Message) { function handleMessage(message: Message) {
switch (message.type) { switch (message.type) {
case "load": { case "load": {
const api = window.RufflePlayer ?? {}; if (window.RufflePlayer === undefined) {
api.config = { window.RufflePlayer = {};
}
if (window.RufflePlayer.config === undefined) {
window.RufflePlayer.config = {};
}
window.RufflePlayer.config = {
...message.config, ...message.config,
...api.config, ...window.RufflePlayer.config,
openInNewTab, openInNewTab,
}; };
window.RufflePlayer = installRuffle(api, "extension"); installRuffle("extension");
return {}; return {};
} }
case "ping": case "ping":

View File

@ -1,3 +1,3 @@
import { installRuffle } from "ruffle-core"; import { installRuffle } from "ruffle-core";
window.RufflePlayer = installRuffle(window.RufflePlayer, "local"); installRuffle("local");