feat: added robust and more decoupled ways on init

This commit is contained in:
2026-04-24 00:28:09 +03:30
parent c1788b0e3e
commit 60e16940f3

View File

@@ -1,5 +1,5 @@
/** /**
* main player * main player and orchest
* *
* author: db123 at db * author: db123 at db
* creation: 21/4/2026 * creation: 21/4/2026
@@ -9,7 +9,7 @@ import { NativeAdapter } from "../adapters";
import { AdapterAPI, PlayerConfig } from "../types/index"; import { AdapterAPI, PlayerConfig } from "../types/index";
export class VP { export class VP {
private video: HTMLVideoElement | null; private video: HTMLVideoElement;
private videoSrc?: NodeListOf<HTMLSourceElement>; private videoSrc?: NodeListOf<HTMLSourceElement>;
private config?: PlayerConfig; private config?: PlayerConfig;
private adapter: AdapterAPI; private adapter: AdapterAPI;
@@ -21,17 +21,55 @@ export class VP {
constructor(video: string | HTMLVideoElement, config: PlayerConfig) { constructor(video: string | HTMLVideoElement, config: PlayerConfig) {
this.config = config; this.config = config;
this.video = this.videoElement(video);
this.videoSrc = this.video?.querySelectorAll<HTMLSourceElement>("source");
this.adapter = this.adapterInit(config.type);
this.info();
}
private info(): void {
console.info(`
====================================
VP = VideoPlayer by db
https://git.db123.ir/db/VideoPlayer
====================================
`);
}
private videoElement(video: string | HTMLVideoElement): HTMLVideoElement {
let videoEl;
if (video instanceof HTMLVideoElement) { if (video instanceof HTMLVideoElement) {
this.video = video; videoEl = video;
} else { } else {
this.video = document.querySelector(video); videoEl = document.querySelector<HTMLVideoElement>(video);
} }
if (!this.video) { if (!videoEl) {
throw new Error(`Video element not found: ${video}`); throw new Error(`Video element not found: ${video}`);
} }
return videoEl;
}
this.videoSrc = this.video?.querySelectorAll("source"); private adapterInit(type: string = "native"): AdapterAPI {
this.adapter = new NativeAdapter(this.video); switch (type) {
case "native": {
return new NativeAdapter(this.video);
}
case "hls": {
throw new Error(`Adapter not implemented: ${type}`);
}
case "dash": {
throw new Error(`Adapter not implemented: ${type}`);
}
default: {
throw new Error(`Adapter not found: ${type}`);
}
}
} }
load(): void { load(): void {