Skip to content

Commit

Permalink
web: Mark TypeScript enums as const
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
relrelb committed Sep 24, 2021
1 parent 0b4d6e6 commit d27e29b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 20 deletions.
8 changes: 4 additions & 4 deletions web/packages/core/src/load-options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export enum AutoPlay {
export const enum AutoPlay {
/**
* The player should automatically play the movie as soon as it is loaded.
*
Expand Down Expand Up @@ -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.
*/
Expand All @@ -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.
*/
Expand All @@ -68,7 +68,7 @@ export enum UnmuteOverlay {
/**
* Console logging level.
*/
export enum LogLevel {
export const enum LogLevel {
Error = "error",
Warn = "warn",
Info = "info",
Expand Down
24 changes: 8 additions & 16 deletions web/packages/core/src/ruffle-player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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";
}

Expand Down Expand Up @@ -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.
*/
Expand Down

0 comments on commit d27e29b

Please sign in to comment.