Skip to content
This repository has been archived by the owner on Oct 26, 2022. It is now read-only.

Commit

Permalink
use event emitter
Browse files Browse the repository at this point in the history
  • Loading branch information
ThaUnknown committed Aug 1, 2021
1 parent a0820ec commit 349f268
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 61 deletions.
2 changes: 1 addition & 1 deletion dist/bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/bundle.js.map

Large diffs are not rendered by default.

30 changes: 10 additions & 20 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,7 @@ Options:
seekTime: Number, // Time in seconds for the player to seek
streamedDownload: Boolean, // If true, the player will only download pieces directly needed for playback
destroyStore: Boolean, // If false, the files will be kept in storage even after playback finishes
WebTorrentOpts: Object, // Object of opts for WebTorrent
// events documented below
onWatched: Function,
onPlaylist: Function,
onNext: Function,
onPrev: Function,
onError: Function,
onWarn: Function,
onVideoFiles: Function,
onDownloadDone: Function,
onOfflineTorrent: Function
WebTorrentOpts: Object // Object of opts for WebTorrent
}
```
Most of these options can be also **changed** after creating the WebTorrentPlayer instance, but some of them will need to be declared on init [optimisation ¯\\\_(ツ)\_]
Expand Down Expand Up @@ -134,12 +124,12 @@ You can ofc modify the UI I made, but it might be painful.

## `on Events`

- `onWatched(File, FileMedia)` triggers when the user watches most of the video
- `onPlaylist()` triggers when the user clicks the playlist button [TLDR make your own UI for playlist]
- `onNext(File, FileMedia)` - triggers when the player cant find the next file to play
- `onPrev(File, FileMedia)` - triggers when the player cant find the last file to play
- `onError(String, Torrent)` - triggers when the player encounters an error // TODO
- `onWarn(String, Torrent)` - triggers when the player encounters an issue
- `onVideoFiles([File])` - triggers when the player finds video files [for creating playlist UI, maybe download progress etc]
- `onDownloadDone(File)` - triggers when a file finishes downloading
- `onOfflineTorrent(Torrent)` - triggers when an offline torrent loads [for creating UI]
- `client.on('watched', { file, filemedia } => {})` triggers when the user watches most of the video
- `client.on('playlist', { files } => {})` triggers when the user clicks the playlist button [TLDR make your own UI for playlist]
- `client.on('next', { file, filemedia } => {})` - triggers when the player cant find the next file to play
- `client.on('prev', { file, filemedia } => {})` - triggers when the player cant find the last file to play
- `client.on('video-files', { files } => {})` - triggers when the player finds video files [for creating playlist UI, maybe download progress etc]
- `client.on('download-done', { file } => {})` - triggers when a file finishes downloading
- `client.on('offline-torrent', { torrent } => {})` - triggers when an offline torrent loads [for creating UI]
- `client.on('no-files', { torrent } => {})` - triggers when the player cant find a video file to play back
- `client.on('no-peers', { torrent } => {})` - triggers when the player cant find peers to connect to
26 changes: 10 additions & 16 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -139,23 +139,17 @@
burnIn: true,
seekTime: 2,
immerseTime: 10,
generateThumbnails: true,
onDownloadDone: (name) => {
console.log(`${name} has finished downloading. Now seeding.`)
},
onWatched: () => {
console.log('Watched current video!')
},
onPlaylist: () => {
console.log('User wants to open playlist!')
},
onNext: () => {
console.log('User wants to play next video!')
},
onPrev: () => {
console.log('User wants to play last video!')
}
generateThumbnails: true
})
client.on('prev', params => console.log('User wants to play previous video!', params))
client.on('next', params => console.log('User wants to play next video!', params))
client.on('playlist', params => console.log('User wants to open playlist!', params))
client.on('watched', params => console.log('User watched current video!', params))
client.on('download-done', params => console.log('Player finished downloading a file!', params))
client.on('video-files', params => console.log('Player found video files!', params))
client.on('offline-torrent', params => console.log('Player loaded an offline stored torrent!', params))
client.on('no-files', params => console.error('Player couldnt find any playable video files!', params))
client.on('no-peers', params => console.error('Player couldnt find any peers!', params))

function d(a) {
switch (a) {
Expand Down
34 changes: 11 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,23 +125,14 @@ Style: Default,${options.defaultSSAStyles || 'Roboto Medium,26,&H00FFFFFF,&H0000
}

this.completed = false
this.onWatched = options.onWatched
if (this.onWatched) this.video.addEventListener('timeupdate', () => this.checkCompletion())
this.video.addEventListener('timeupdate', () => this.checkCompletion())

this.onPlaylist = options.onPlaylist

this.onNext = options.onNext
this.onPrev = options.onPrev
this.nextTimeout = undefined
if (this.onNext && options.autoNext) this.video.addEventListener('ended', () => this.playNext())

this.onError = options.onError
this.onWarn = options.onWarn
if (options.autoNext) this.video.addEventListener('ended', () => this.playNext())

this.resolveFileMedia = options.resolveFileMedia
this.currentFile = undefined
this.videoFile = undefined
this.onVideoFiles = options.onVideoFiles

if (this.controls.thumbnail) {
this.generateThumbnails = options.generateThumbnails
Expand All @@ -164,11 +155,8 @@ Style: Default,${options.defaultSSAStyles || 'Roboto Medium,26,&H00FFFFFF,&H0000
})
}

this.onDownloadDone = options.onDownloadDone
this.onDone = undefined

this.onOfflineTorrent = options.onOfflineTorrent

this.destroyStore = options.destroyStore || true

this.immerseTimeout = undefined
Expand Down Expand Up @@ -530,7 +518,7 @@ Style: Default,${options.defaultSSAStyles || 'Roboto Medium,26,&H00FFFFFF,&H0000
}

openPlaylist () {
if (this.onPlaylist) this.onPlaylist()
this.emit('playlist', { files: this.videoFiles })
}

playNext () {
Expand All @@ -542,7 +530,7 @@ Style: Default,${options.defaultSSAStyles || 'Roboto Medium,26,&H00FFFFFF,&H0000
const torrent = this.currentFile._torrent
this.buildVideo(torrent, { media: nowPlaying, file: this.videoFiles[this.videoFiles.indexOf(this.currentFile) + 1] })
} else {
if (this.onNext) this.onNext(this.currentFile, this.nowPlaying)
this.emit('next', { file: this.currentFile, filemedia: this.nowPlaying })
}
}, 200)
}
Expand All @@ -556,7 +544,7 @@ Style: Default,${options.defaultSSAStyles || 'Roboto Medium,26,&H00FFFFFF,&H0000
const torrent = this.currentFile._torrent
this.buildVideo(torrent, { media: nowPlaying, file: this.videoFiles[this.videoFiles.indexOf(this.currentFile) - 1] })
} else {
if (this.onPrev) this.onPrev(this.currentFile, this.nowPlaying)
this.emit('prev', { file: this.currentFile, filemedia: this.nowPlaying })
}
}, 200)
}
Expand Down Expand Up @@ -685,7 +673,7 @@ Style: Default,${options.defaultSSAStyles || 'Roboto Medium,26,&H00FFFFFF,&H0000
checkCompletion () {
if (!this.completed && this.video.duration - 180 < this.video.currentTime) {
this.completed = true
this.onWatched(this.currentFile, this.nowPlaying)
this.emit('watched', { file: this.currentFile, filemedia: this.nowPlaying })
}
}

Expand Down Expand Up @@ -1034,7 +1022,7 @@ Style: Default,${options.defaultSSAStyles || 'Roboto Medium,26,&H00FFFFFF,&H0000
}

postDownload () {
this.onDownloadDone(this.currentFile)
this.emit('download-done', { file: this.currentFile })
this.parseSubtitles(this.currentFile).then(() => {
if (this.generateThumbnails) {
this.finishThumbnails(this.video.src)
Expand All @@ -1045,21 +1033,21 @@ Style: Default,${options.defaultSSAStyles || 'Roboto Medium,26,&H00FFFFFF,&H0000
playTorrent (torrentID, opts = {}) { // TODO: clean this up
const handleTorrent = (torrent, opts) => {
torrent.on('noPeers', () => {
if (this.onWarn) this.onWarn('no peers', torrent)
this.emit('no-peers', torrent)
})
if (this.streamedDownload) {
torrent.files.forEach(file => file.deselect())
torrent.deselect(0, torrent.pieces.length - 1, false)
}
this.videoFiles = torrent.files.filter(file => this.videoExtensions.some(ext => file.name.endsWith(ext)))
if (this.onVideoFiles) this.onVideoFiles(this.videoFiles)
this.emit('video-files', { files: this.videoFiles })
if (this.videoFiles.length > 1) {
torrent.files.forEach(file => file.deselect())
}
if (this.videoFiles) {
this.buildVideo(torrent, opts)
} else {
if (this.onWarn) this.onWarn('no file', torrent)
this.emit('no-file', torrent)
this.cleanupTorrents()
}
}
Expand Down Expand Up @@ -1107,7 +1095,7 @@ Style: Default,${options.defaultSSAStyles || 'Roboto Medium,26,&H00FFFFFF,&H0000
this.offlineTorrents[torrent.infoHash] = Array.from(torrent.torrentFile)
localStorage.setItem('offlineTorrents', JSON.stringify(this.offlineTorrents))
}
if (this.onOfflineTorrent) this.onOfflineTorrent(torrent)
this.emit('offline-torrent', torrent)
})
}
}

0 comments on commit 349f268

Please sign in to comment.