web: Mark TypeScript enums as const

Unlike regular enums, const enums are completely removed during
compilation, and as such are zero-cost in bundle size terms.
Since it is not possible to query the possible values of a const
enum at runtime, adapt the `autoplay` and `unmuteOverlay` validation
logic.
This commit is contained in:
relrelb 2021-09-21 13:04:08 +03:00 committed by relrelb
parent 0b4d6e6a1c
commit d27e29bd26
2 changed files with 12 additions and 20 deletions

View File

@ -1,4 +1,4 @@
export enum AutoPlay {
export const enum AutoPlay {
/**
* The player should automatically play the movie as soon as it is loaded.
*
@ -32,7 +32,7 @@ export enum AutoPlay {
* When letterboxed, black bars will be rendered around the exterior
* margins of the content.
*/
export enum Letterbox {
export const enum Letterbox {
/**
* The content will never be letterboxed.
*/
@ -53,7 +53,7 @@ export enum Letterbox {
* When the player is muted, this controls whether or not Ruffle will show a
* "click to unmute" overlay on top of the movie.
*/
export enum UnmuteOverlay {
export const enum UnmuteOverlay {
/**
* Show an overlay explaining that the movie is muted.
*/
@ -68,7 +68,7 @@ export enum UnmuteOverlay {
/**
* Console logging level.
*/
export enum LogLevel {
export const enum LogLevel {
Error = "error",
Warn = "warn",
Info = "info",

View File

@ -24,7 +24,7 @@ export const FLASH_ACTIVEX_CLASSID =
const RUFFLE_ORIGIN = "https://ruffle.rs";
const DIMENSION_REGEX = /^\s*(\d+(\.\d+)?(%)?)/;
enum PanicError {
const enum PanicError {
Unknown,
CSPConflict,
FileProtocol,
@ -427,25 +427,17 @@ export class RufflePlayer extends HTMLElement {
this.container.style.visibility = "";
}
const autoplay = Object.values(Object(AutoPlay)).includes(
config.autoplay
)
? config.autoplay
: AutoPlay.Auto;
const unmuteVisibility = Object.values(Object(UnmuteOverlay)).includes(
config.unmuteOverlay
)
? config.unmuteOverlay
: UnmuteOverlay.Visible;
// Treat unspecified and invalid values as `AutoPlay.Auto`.
if (
autoplay == AutoPlay.On ||
(autoplay == AutoPlay.Auto && this.audioState() === "running")
config.autoplay === AutoPlay.On ||
(config.autoplay !== AutoPlay.Off &&
this.audioState() === "running")
) {
this.play();
if (this.audioState() !== "running") {
if (unmuteVisibility === UnmuteOverlay.Visible) {
// Treat unspecified and invalid values as `UnmuteOverlay.Visible`.
if (config.unmuteOverlay !== UnmuteOverlay.Hidden) {
this.unmuteOverlay.style.display = "block";
}
@ -1251,7 +1243,7 @@ export class RufflePlayer extends HTMLElement {
/**
* Describes the loading state of an SWF movie.
*/
export enum ReadyState {
export const enum ReadyState {
/**
* No movie is loaded, or no information is yet available about the movie.
*/