refactor(web): move events to folder, fix error codes

This commit is contained in:
Kamil Moskała
2026-03-25 16:02:21 +01:00
parent 8f7eeea5bf
commit 7800719e90
9 changed files with 31 additions and 11 deletions
@@ -15,7 +15,7 @@ import type { VideoPlayerBase } from "./types/VideoPlayerBase";
import type { VideoPlayerStatus } from "./types/VideoPlayerStatus";
import { createPlayer } from "./utils/playerFactory";
import { createSource } from "./utils/sourceFactory";
import { VideoPlayerEvents } from "./VideoPlayerEvents";
import { VideoPlayerEvents } from "./events/VideoPlayerEvents";
class VideoPlayer extends VideoPlayerEvents implements VideoPlayerBase {
private _player: VideoPlayerImpl | undefined;
@@ -10,7 +10,7 @@ import type {
import type { VideoPlayerBase } from "./types/VideoPlayerBase";
import type { VideoPlayerSourceBase } from "./types/VideoPlayerSourceBase";
import type { VideoPlayerStatus } from "./types/VideoPlayerStatus";
import { VideoPlayerEvents } from "./VideoPlayerEvents";
import { VideoPlayerEvents } from "./events/VideoPlayerEvents";
import { MediaSessionHandler } from "./web/MediaSession";
import { WebEventEmitter, type VideoStore } from "./web/WebEventEmitter";
@@ -1,3 +0,0 @@
import { VideoPlayerEventsBase } from "./VideoPlayerEventsBase";
export class VideoPlayerEvents extends VideoPlayerEventsBase {}
@@ -1,5 +1,5 @@
import type { AllPlayerEvents as PlayerEvents } from "./types/Events";
import type { ListenerSubscription } from "./types/EventEmitter";
import type { AllPlayerEvents as PlayerEvents } from "../types/Events";
import type { ListenerSubscription } from "../types/EventEmitter";
import { VideoPlayerEventsBase } from "./VideoPlayerEventsBase";
export class VideoPlayerEvents extends VideoPlayerEventsBase {
@@ -0,0 +1,16 @@
import type { AllPlayerEvents as PlayerEvents } from "../types/Events";
import type { ListenerSubscription } from "../types/EventEmitter";
import { VideoPlayerEventsBase } from "./VideoPlayerEventsBase";
export class VideoPlayerEvents extends VideoPlayerEventsBase {
addEventListener<Event extends keyof PlayerEvents>(
event: Event,
callback: PlayerEvents[Event],
): ListenerSubscription {
switch (event) {
// Web-only events will be added here
default:
return super.addEventListener(event, callback);
}
}
}
@@ -2,11 +2,11 @@ import {
ALL_PLAYER_EVENTS,
type JSVideoPlayerEvents,
type AllPlayerEvents as PlayerEvents,
} from "./types/Events";
} from "../types/Events";
import type {
ListenerSubscription,
VideoPlayerEventEmitterBase,
} from "./types/EventEmitter";
} from "../types/EventEmitter";
export class VideoPlayerEventsBase {
protected eventEmitter: VideoPlayerEventEmitterBase;
@@ -14,6 +14,7 @@ import type { VideoViewProps, VideoViewRef } from "./VideoViewProps";
import { createPlayer, videoFeatures, usePlayerContext, useMediaAttach } from "@videojs/react";
import { VideoSkin } from "@videojs/react/video";
import "@videojs/react/video/skin.css";
import type { VideoStore } from "../web/WebEventEmitter";
const Player = createPlayer({ features: videoFeatures });
@@ -22,7 +23,8 @@ const Player = createPlayer({ features: videoFeatures });
* then passes the ready store to the adapter.
*/
function PlayerBridge({ player }: { player: VideoPlayer }) {
const { store, container } = usePlayerContext();
const { store: rawStore, container } = usePlayerContext();
const store = rawStore as unknown as VideoStore;
const setMedia = useMediaAttach();
useEffect(() => {
@@ -40,7 +40,11 @@ export interface VideoStore {
readonly buffered: [number, number][];
readonly error: { code: number; message: string } | null;
readonly textTrackList: Array<{ kind: string; label: string; language: string; mode: string }>;
readonly destroyed: boolean;
readonly target: unknown;
subscribe(callback: () => void): () => void;
attach(target: { media: HTMLVideoElement; container: HTMLElement | null }): () => void;
destroy(): void;
loadSource(src: string): string;
play(): Promise<void>;
pause(): void;
@@ -51,6 +55,7 @@ export interface VideoStore {
exitFullscreen(): Promise<void>;
requestPictureInPicture(): Promise<void>;
exitPictureInPicture(): Promise<void>;
readonly pipAvailability: string;
}
/**
@@ -213,7 +218,7 @@ export class WebEventEmitter implements VideoPlayerEventEmitterBase {
}
const codeMap: Record<number, LibraryError | PlayerError | SourceError | UnknownError> = {
1: "player/asset-not-initialized",
2: "player/network",
2: "player/not-initialized",
3: "player/invalid-source",
4: "source/unsupported-content-type",
};