Skip to content

Commit

Permalink
refactor: Improve types
Browse files Browse the repository at this point in the history
  • Loading branch information
vlazh committed Oct 8, 2024
1 parent f9da3f5 commit 9322d7e
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 54 deletions.
2 changes: 1 addition & 1 deletion src/loader.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/prefer-promise-reject-errors */
/* eslint-disable @typescript-eslint/no-use-before-define */
import './types';

function isScriptAdded(src: string): boolean {
const url = src.startsWith('//') ? window.location.protocol + src : src;
for (let i = 0; i < document.scripts.length; i += 1) {
Expand Down
138 changes: 85 additions & 53 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,63 @@
namespace Kinescope {
type VideoQuality = number | 'auto';

declare enum PlayerEvents {
Ready = 'Ready',
CurrentTrackChanged = 'CurrentTrackChanged',
SizeChanged = 'SizeChanged',
QualityChanged = 'QualityChanged',
Play = 'Play',
Playing = 'Playing',
Pause = 'Pause',
Ended = 'Ended',
TimeUpdate = 'TimeUpdate',
Waiting = 'Waiting',
Progress = 'Progress',
DurationChange = 'DurationChange',
VolumeChange = 'VolumeChange',
PlaybackRateChange = 'PlaybackRateChange',
Seeked = 'Seeked',
SeekChapter = 'SeekChapter',
FullscreenChange = 'FullscreenChange',
PipChange = 'PipChange',
CallAction = 'CallAction',
CallBookmark = 'CallBookmark',
Error = 'Error',
Destroy = 'Destroy',
}
type DefineAll<Enum extends string | number | symbol, T extends Record<Enum, unknown>> = T;

type PlayerEventKeys =
| 'Ready'
| 'CurrentTrackChanged'
| 'SizeChanged'
| 'QualityChanged'
| 'Play'
| 'Playing'
| 'Pause'
| 'Ended'
| 'TimeUpdate'
| 'Waiting'
| 'Progress'
| 'DurationChange'
| 'VolumeChange'
| 'PlaybackRateChange'
| 'Seeked'
| 'SeekChapter'
| 'FullscreenChange'
| 'PipChange'
| 'CallAction'
| 'CallBookmark'
| 'Error'
| 'Destroy';

type PlayerEvents = DefineAll<
PlayerEventKeys,
{
Ready: 'ready';
CurrentTrackChanged: 'currenttrackchanged';
SizeChanged: 'sizechanged';
QualityChanged: 'qualitychanged';
Play: 'play';
Playing: 'playing';
Pause: 'pause';
Ended: 'ended';
TimeUpdate: 'timeupdate';
Waiting: 'waiting';
Progress: 'progress';
DurationChange: 'durationchange';
VolumeChange: 'volumechange';
PlaybackRateChange: 'playbackratechange';
Seeked: 'seeked';
SeekChapter: 'seekchapter';
FullscreenChange: 'fullscreenchange';
PipChange: 'pipchange';
CallAction: 'callaction';
CallBookmark: 'callbookmark';
Error: 'error';
Destroy: 'destroy';
}
>;

declare const PlayerEvents: PlayerEvents;

export declare namespace IframePlayer {
export type VideoQuality = number | 'auto';

export interface PlaylistItemOptions {
/** Заголовок видео-ролика. Отображается в верхней части плеера. */
title?: string;
Expand Down Expand Up @@ -111,24 +142,7 @@ namespace Kinescope {
}

export interface UpdatablePlayerOptions {
/** Настройки UI */
ui?: {
/** Водяной знак. */
watermark?: {
/** Текст */
text: string;
/**
* - `stripes` - линиями;
* - `random` - в случайных местах;
* По умолчанию `random`.
*/
mode?: 'stripes' | 'random';
/** Коэффициент масштабирования размера текста в зависимости от размера плеера. По умолчанию `0.25`. */
scale?: number;
/** Длительность показа/скрытия (мс). Если не указано, текст показывается постоянно. */
displayTimeout?: number | { visible: number; hidden: number };
};
};
ui?: Pick<NonNullable<CreateOptions['ui']>, 'watermark'>;
}

export interface CreateOptions {
Expand Down Expand Up @@ -207,6 +221,21 @@ namespace Kinescope {
mainPlayButton?: boolean;
/** Показывать ли кнопку выбора скорости воспроизведения. */
playbackRateButton?: boolean;
/** Водяной знак. */
watermark?: {
/** Текст */
text: string;
/**
* - `stripes` - линиями;
* - `random` - в случайных местах;
* По умолчанию `random`.
*/
mode?: 'stripes' | 'random';
/** Коэффициент масштабирования размера текста в зависимости от размера плеера. По умолчанию `0.25`. */
scale?: number;
/** Длительность показа/скрытия (мс). Если не указано, текст показывается постоянно. */
displayTimeout?: number | { visible: number; hidden: number };
};
};

/** Настройки темы. */
Expand Down Expand Up @@ -235,9 +264,9 @@ namespace Kinescope {
export interface Player {
readonly Events: Player.Events;

on<T extends PlayerEvents>(event: T, handler: Player.EventHandler<T>): this;
once<T extends PlayerEvents>(event: T, handler: Player.EventHandler<T>): this;
off<T extends PlayerEvents>(event: T, handler: Player.EventHandler<T>): this;
on<T extends Player.EventType>(event: T, handler: Player.EventHandler<T>): this;
once<T extends Player.EventType>(event: T, handler: Player.EventHandler<T>): this;
off<T extends Player.EventType>(event: T, handler: Player.EventHandler<T>): this;

isPaused(): Promise<boolean>;

Expand Down Expand Up @@ -305,12 +334,11 @@ namespace Kinescope {
}

export namespace Player {
export type Events = typeof PlayerEvents;

type DefineAll<Enum extends string | number | symbol, T extends Record<Enum, unknown>> = T;
export type EventType = PlayerEvents[keyof PlayerEvents];
export type Events = PlayerEvents;

type EventMap = DefineAll<
PlayerEvents,
export type EventMap = DefineAll<
EventType,
{
[PlayerEvents.Ready]: {
quality: VideoQuality;
Expand Down Expand Up @@ -373,7 +401,7 @@ namespace Kinescope {
}
>;

export type EventHandler<T extends keyof EventMap = keyof EventMap> = (event: {
export type EventHandler<T extends EventType = EventType> = (event: {
readonly type: T;
readonly target: Player;
readonly data: EventMap[T];
Expand All @@ -388,4 +416,8 @@ namespace Kinescope {
/** Returns all created players. */
export function getAll(): readonly IframePlayer.Player[];
}

// export type IframePlayer = typeof IframePlayer;
}

// type Kinescope = typeof Kinescope;

0 comments on commit 9322d7e

Please sign in to comment.