From c199c427d4ae586339822320f20f512a7a19dc3f Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 18 Oct 2018 14:35:31 +0200 Subject: Better typings --- client/src/assets/player/peertube-chunk-store.ts | 12 ++++++------ client/src/assets/player/peertube-link-button.ts | 5 ++++- .../src/assets/player/peertube-load-progress-bar.ts | 5 ++++- client/src/assets/player/peertube-player.ts | 20 ++++++++++++-------- client/src/assets/player/peertube-videojs-plugin.ts | 20 ++++++++++++-------- client/src/assets/player/peertube-videojs-typings.ts | 5 ++++- client/src/assets/player/resolution-menu-button.ts | 6 +++++- client/src/assets/player/resolution-menu-item.ts | 6 +++++- client/src/assets/player/settings-menu-button.ts | 17 ++++++++++------- client/src/assets/player/settings-menu-item.ts | 8 ++++++-- client/src/assets/player/theater-button.ts | 6 +++++- client/src/assets/player/utils.ts | 2 +- client/src/assets/player/webtorrent-info-button.ts | 2 +- 13 files changed, 75 insertions(+), 39 deletions(-) (limited to 'client/src/assets/player') diff --git a/client/src/assets/player/peertube-chunk-store.ts b/client/src/assets/player/peertube-chunk-store.ts index ac3f9e654..54cc0ea64 100644 --- a/client/src/assets/player/peertube-chunk-store.ts +++ b/client/src/assets/player/peertube-chunk-store.ts @@ -76,7 +76,7 @@ export class PeertubeChunkStore extends EventEmitter { this.runCleaner() } - put (index: number, buf: Buffer, cb: Function) { + put (index: number, buf: Buffer, cb: (err?: Error) => void) { const isLastChunk = (index === this.lastChunkIndex) if (isLastChunk && buf.length !== this.lastChunkLength) { return this.nextTick(cb, new Error('Last chunk length must be ' + this.lastChunkLength)) @@ -113,7 +113,7 @@ export class PeertubeChunkStore extends EventEmitter { }, PeertubeChunkStore.BUFFERING_PUT_MS) } - get (index: number, opts: any, cb: any): any { + get (index: number, opts: any, cb: (err?: Error, buf?: Buffer) => void): void { if (typeof opts === 'function') return this.get(index, null, opts) // IndexDB could be slow, use our memory index first @@ -146,11 +146,11 @@ export class PeertubeChunkStore extends EventEmitter { }) } - close (db: any) { - return this.destroy(db) + close (cb: (err?: Error) => void) { + return this.destroy(cb) } - async destroy (cb: any) { + async destroy (cb: (err?: Error) => void) { try { if (this.pendingPut) { clearTimeout(this.putBulkTimeout) @@ -225,7 +225,7 @@ export class PeertubeChunkStore extends EventEmitter { } } - private nextTick (cb: any, err: Error, val?: any) { + private nextTick (cb: (err?: Error, val?: T) => void, err: Error, val?: T) { process.nextTick(() => cb(err, val), undefined) } } diff --git a/client/src/assets/player/peertube-link-button.ts b/client/src/assets/player/peertube-link-button.ts index b03952b47..de9a49de9 100644 --- a/client/src/assets/player/peertube-link-button.ts +++ b/client/src/assets/player/peertube-link-button.ts @@ -1,10 +1,13 @@ import { VideoJSComponentInterface, videojsUntyped } from './peertube-videojs-typings' import { buildVideoLink } from './utils' +// FIXME: something weird with our path definition in tsconfig and typings +// @ts-ignore +import { Player } from 'video.js' const Button: VideoJSComponentInterface = videojsUntyped.getComponent('Button') class PeerTubeLinkButton extends Button { - constructor (player: any, options: any) { + constructor (player: Player, options: any) { super(player, options) } diff --git a/client/src/assets/player/peertube-load-progress-bar.ts b/client/src/assets/player/peertube-load-progress-bar.ts index ee8a6cd81..af276d1b2 100644 --- a/client/src/assets/player/peertube-load-progress-bar.ts +++ b/client/src/assets/player/peertube-load-progress-bar.ts @@ -1,10 +1,13 @@ import { VideoJSComponentInterface, videojsUntyped } from './peertube-videojs-typings' +// FIXME: something weird with our path definition in tsconfig and typings +// @ts-ignore +import { Player } from 'video.js' const Component: VideoJSComponentInterface = videojsUntyped.getComponent('Component') class PeerTubeLoadProgressBar extends Component { - constructor (player: any, options: any) { + constructor (player: Player, options: any) { super(player, options) this.partEls_ = [] this.on(player, 'progress', this.update) diff --git a/client/src/assets/player/peertube-player.ts b/client/src/assets/player/peertube-player.ts index ef9e7fcc0..db63071cb 100644 --- a/client/src/assets/player/peertube-player.ts +++ b/client/src/assets/player/peertube-player.ts @@ -14,6 +14,10 @@ import { UserWatching, VideoJSCaption, videojsUntyped } from './peertube-videojs import { buildVideoEmbed, buildVideoLink, copyToClipboard } from './utils' import { getCompleteLocale, getShortLocale, is18nLocale, isDefaultLocale } from '../../../../shared/models/i18n/i18n' +// FIXME: something weird with our path definition in tsconfig and typings +// @ts-ignore +import { Player } from 'video.js' + // Change 'Playback Rate' to 'Speed' (smaller for our settings menu) videojsUntyped.getComponent('PlaybackRateMenuButton').prototype.controlText_ = 'Speed' // Change Captions to Subtitles/CC @@ -75,12 +79,12 @@ function getVideojsOptions (options: { enableVolumeScroll: false, enableModifiersForNumbers: false, - fullscreenKey: function (event: any) { + fullscreenKey: function (event: KeyboardEvent) { // fullscreen with the f key or Ctrl+Enter return event.key === 'f' || (event.ctrlKey && event.key === 'Enter') }, - seekStep: function (event: any) { + seekStep: function (event: KeyboardEvent) { // mimic VLC seek behavior, and default to 5 (original value is 5). if (event.ctrlKey && event.altKey) { return 5 * 60 @@ -95,26 +99,26 @@ function getVideojsOptions (options: { customKeys: { increasePlaybackRateKey: { - key: function (event: any) { + key: function (event: KeyboardEvent) { return event.key === '>' }, - handler: function (player: any) { + handler: function (player: Player) { player.playbackRate((player.playbackRate() + 0.1).toFixed(2)) } }, decreasePlaybackRateKey: { - key: function (event: any) { + key: function (event: KeyboardEvent) { return event.key === '<' }, - handler: function (player: any) { + handler: function (player: Player) { player.playbackRate((player.playbackRate() - 0.1).toFixed(2)) } }, frameByFrame: { - key: function (event: any) { + key: function (event: KeyboardEvent) { return event.key === '.' }, - handler: function (player: any) { + handler: function (player: Player) { player.pause() // Calculate movement distance (assuming 30 fps) const dist = 1 / 30 diff --git a/client/src/assets/player/peertube-videojs-plugin.ts b/client/src/assets/player/peertube-videojs-plugin.ts index 03def186e..40da5f1f7 100644 --- a/client/src/assets/player/peertube-videojs-plugin.ts +++ b/client/src/assets/player/peertube-videojs-plugin.ts @@ -1,11 +1,13 @@ -const videojs = require('video.js') +// FIXME: something weird with our path definition in tsconfig and typings +// @ts-ignore +import * as videojs from 'video.js' + import * as WebTorrent from 'webtorrent' import { VideoFile } from '../../../../shared/models/videos/video.model' import { renderVideo } from './video-renderer' import './settings-menu-button' import { PeertubePluginOptions, UserWatching, VideoJSCaption, VideoJSComponentInterface, videojsUntyped } from './peertube-videojs-typings' import { isMobile, timeToInt, videoFileMaxByResolution, videoFileMinByResolution } from './utils' -const CacheChunkStore = require('cache-chunk-store') import { PeertubeChunkStore } from './peertube-chunk-store' import { getAverageBandwidthInStore, @@ -17,6 +19,8 @@ import { saveVolumeInStore } from './peertube-player-local-storage' +const CacheChunkStore = require('cache-chunk-store') + type PlayOptions = { forcePlay?: boolean, seek?: number, @@ -61,7 +65,7 @@ class PeerTubePlugin extends Plugin { private player: any private currentVideoFile: VideoFile - private torrent: any + private torrent: WebTorrent.Torrent private videoCaptions: VideoJSCaption[] private renderer: any @@ -83,7 +87,7 @@ class PeerTubePlugin extends Plugin { private downloadSpeeds: number[] = [] - constructor (player: any, options: PeertubePluginOptions) { + constructor (player: videojs.Player, options: PeertubePluginOptions) { super(player, options) // Disable auto play on iOS @@ -273,7 +277,7 @@ class PeerTubePlugin extends Plugin { const oldTorrent = this.torrent const torrentOptions = { - store: (chunkLength: any, storeOpts: any) => new CacheChunkStore(new PeertubeChunkStore(chunkLength, storeOpts), { + store: (chunkLength: number, storeOpts: any) => new CacheChunkStore(new PeertubeChunkStore(chunkLength, storeOpts), { max: 100 }) } @@ -304,7 +308,7 @@ class PeerTubePlugin extends Plugin { if (err) return this.fallbackToHttp(options, done) - return this.tryToPlay((err: Error) => { + return this.tryToPlay(err => { if (err) return done(err) if (options.seek) this.seek(options.seek) @@ -344,7 +348,7 @@ class PeerTubePlugin extends Plugin { }) } - private tryToPlay (done?: Function) { + private tryToPlay (done?: (err?: Error) => void) { if (!done) done = function () { /* empty */ } const playPromise = this.player.play() @@ -641,7 +645,7 @@ class PeerTubePlugin extends Plugin { return this.videoFiles[Math.floor(this.videoFiles.length / 2)] } - private stopTorrent (torrent: any) { + private stopTorrent (torrent: WebTorrent.Torrent) { torrent.pause() // Pause does not remove actual peers (in particular the webseed peer) torrent.removePeer(torrent[ 'ws' ]) diff --git a/client/src/assets/player/peertube-videojs-typings.ts b/client/src/assets/player/peertube-videojs-typings.ts index 98a33077d..d127230fa 100644 --- a/client/src/assets/player/peertube-videojs-typings.ts +++ b/client/src/assets/player/peertube-videojs-typings.ts @@ -1,4 +1,7 @@ -const videojs = require('video.js') +// FIXME: something weird with our path definition in tsconfig and typings +// @ts-ignore +import * as videojs from 'video.js' + import { VideoFile } from '../../../../shared/models/videos/video.model' import { PeerTubePlugin } from './peertube-videojs-plugin' diff --git a/client/src/assets/player/resolution-menu-button.ts b/client/src/assets/player/resolution-menu-button.ts index 91818efc9..a3c1108ca 100644 --- a/client/src/assets/player/resolution-menu-button.ts +++ b/client/src/assets/player/resolution-menu-button.ts @@ -1,3 +1,7 @@ +// FIXME: something weird with our path definition in tsconfig and typings +// @ts-ignore +import { Player } from 'video.js' + import { VideoJSComponentInterface, videojsUntyped } from './peertube-videojs-typings' import { ResolutionMenuItem } from './resolution-menu-item' @@ -6,7 +10,7 @@ const MenuButton: VideoJSComponentInterface = videojsUntyped.getComponent('MenuB class ResolutionMenuButton extends MenuButton { label: HTMLElement - constructor (player: any, options: any) { + constructor (player: Player, options: any) { super(player, options) this.player = player diff --git a/client/src/assets/player/resolution-menu-item.ts b/client/src/assets/player/resolution-menu-item.ts index afe490abb..b54fd91ef 100644 --- a/client/src/assets/player/resolution-menu-item.ts +++ b/client/src/assets/player/resolution-menu-item.ts @@ -1,9 +1,13 @@ +// FIXME: something weird with our path definition in tsconfig and typings +// @ts-ignore +import { Player } from 'video.js' + import { VideoJSComponentInterface, videojsUntyped } from './peertube-videojs-typings' const MenuItem: VideoJSComponentInterface = videojsUntyped.getComponent('MenuItem') class ResolutionMenuItem extends MenuItem { - constructor (player: any, options: any) { + constructor (player: Player, options: any) { const currentResolutionId = player.peertube().getCurrentResolutionId() options.selectable = true options.selected = options.id === currentResolutionId diff --git a/client/src/assets/player/settings-menu-button.ts b/client/src/assets/player/settings-menu-button.ts index f0ccb5862..aa7281727 100644 --- a/client/src/assets/player/settings-menu-button.ts +++ b/client/src/assets/player/settings-menu-button.ts @@ -1,7 +1,10 @@ // Author: Yanko Shterev // Thanks https://github.com/yshterev/videojs-settings-menu -const videojs = require('video.js') +// FIXME: something weird with our path definition in tsconfig and typings +// @ts-ignore +import * as videojs from 'video.js' + import { SettingsMenuItem } from './settings-menu-item' import { VideoJSComponentInterface, videojsUntyped } from './peertube-videojs-typings' import { toTitleCase } from './utils' @@ -11,7 +14,7 @@ const Menu: VideoJSComponentInterface = videojsUntyped.getComponent('Menu') const Component: VideoJSComponentInterface = videojsUntyped.getComponent('Component') class SettingsButton extends Button { - constructor (player: any, options: any) { + constructor (player: videojs.Player, options: any) { super(player, options) this.playerComponent = player @@ -48,7 +51,7 @@ class SettingsButton extends Button { } } - onDisposeSettingsItem (name: string) { + onDisposeSettingsItem (event: any, name: string) { if (name === undefined) { let children = this.menu.children() @@ -74,7 +77,7 @@ class SettingsButton extends Button { } } - onAddSettingsItem (data: any) { + onAddSettingsItem (event: any, data: any) { const [ entry, options ] = data this.addMenuItem(entry, options) @@ -218,7 +221,7 @@ class SettingsButton extends Button { } class SettingsPanel extends Component { - constructor (player: any, options: any) { + constructor (player: videojs.Player, options: any) { super(player, options) } @@ -232,7 +235,7 @@ class SettingsPanel extends Component { } class SettingsPanelChild extends Component { - constructor (player: any, options: any) { + constructor (player: videojs.Player, options: any) { super(player, options) } @@ -246,7 +249,7 @@ class SettingsPanelChild extends Component { } class SettingsDialog extends Component { - constructor (player: any, options: any) { + constructor (player: videojs.Player, options: any) { super(player, options) this.hide() } diff --git a/client/src/assets/player/settings-menu-item.ts b/client/src/assets/player/settings-menu-item.ts index 2d752b62e..698f4627a 100644 --- a/client/src/assets/player/settings-menu-item.ts +++ b/client/src/assets/player/settings-menu-item.ts @@ -1,6 +1,10 @@ // Author: Yanko Shterev // Thanks https://github.com/yshterev/videojs-settings-menu +// FIXME: something weird with our path definition in tsconfig and typings +// @ts-ignore +import * as videojs from 'video.js' + import { toTitleCase } from './utils' import { VideoJSComponentInterface, videojsUntyped } from './peertube-videojs-typings' @@ -9,7 +13,7 @@ const component: VideoJSComponentInterface = videojsUntyped.getComponent('Compon class SettingsMenuItem extends MenuItem { - constructor (player: any, options: any, entry: string, menuButton: VideoJSComponentInterface) { + constructor (player: videojs.Player, options: any, entry: string, menuButton: VideoJSComponentInterface) { super(player, options) this.settingsButton = menuButton @@ -229,7 +233,7 @@ class SettingsMenuItem extends MenuItem { } update (event?: any) { - let target = null + let target: HTMLElement = null let subMenu = this.subMenu.name() if (event && event.type === 'tap') { diff --git a/client/src/assets/player/theater-button.ts b/client/src/assets/player/theater-button.ts index b761f6030..4f8fede3d 100644 --- a/client/src/assets/player/theater-button.ts +++ b/client/src/assets/player/theater-button.ts @@ -1,3 +1,7 @@ +// FIXME: something weird with our path definition in tsconfig and typings +// @ts-ignore +import * as videojs from 'video.js' + import { VideoJSComponentInterface, videojsUntyped } from './peertube-videojs-typings' import { saveTheaterInStore, getStoredTheater } from './peertube-player-local-storage' @@ -6,7 +10,7 @@ class TheaterButton extends Button { private static readonly THEATER_MODE_CLASS = 'vjs-theater-enabled' - constructor (player: any, options: any) { + constructor (player: videojs.Player, options: any) { super(player, options) const enabled = getStoredTheater() diff --git a/client/src/assets/player/utils.ts b/client/src/assets/player/utils.ts index 46081c0d2..c87287482 100644 --- a/client/src/assets/player/utils.ts +++ b/client/src/assets/player/utils.ts @@ -12,7 +12,7 @@ const dictionaryBytes: Array<{max: number, type: string}> = [ { max: 1073741824, type: 'MB' }, { max: 1.0995116e12, type: 'GB' } ] -function bytes (value: any) { +function bytes (value: number) { const format = dictionaryBytes.find(d => value < d.max) || dictionaryBytes[dictionaryBytes.length - 1] const calc = Math.floor(value / (format.max / 1024)).toString() diff --git a/client/src/assets/player/webtorrent-info-button.ts b/client/src/assets/player/webtorrent-info-button.ts index 5b9d0a401..c3c1af951 100644 --- a/client/src/assets/player/webtorrent-info-button.ts +++ b/client/src/assets/player/webtorrent-info-button.ts @@ -65,7 +65,7 @@ class WebtorrentInfoButton extends Button { subDivHttp.appendChild(subDivHttpText) div.appendChild(subDivHttp) - this.player_.peertube().on('torrentInfo', (data: any) => { + this.player_.peertube().on('torrentInfo', (event: any, data: any) => { // We are in HTTP fallback if (!data) { subDivHttp.className = 'vjs-peertube-displayed' -- cgit v1.2.3