web: Changed installRuffle to always work with window.RufflePlayer, saves boilerplate
This commit is contained in:
parent
6827d4f086
commit
97396a1007
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ import {
|
|||
UnmuteOverlay,
|
||||
} from "ruffle-core";
|
||||
|
||||
window.RufflePlayer = installRuffle(window.RufflePlayer, "local");
|
||||
installRuffle("local");
|
||||
|
||||
ReactDOM.createRoot(document.getElementById("root")!).render(
|
||||
<React.StrictMode>
|
||||
|
|
|
@ -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")!;
|
||||
|
|
|
@ -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":
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
import { installRuffle } from "ruffle-core";
|
||||
|
||||
window.RufflePlayer = installRuffle(window.RufflePlayer, "local");
|
||||
installRuffle("local");
|
||||
|
|
Loading…
Reference in New Issue