mirror of
https://github.com/NoCrypt/migu.git
synced 2026-03-11 17:45:32 +00:00
50 lines
1.2 KiB
JavaScript
50 lines
1.2 KiB
JavaScript
import { files } from '../views/Player/MediaHandler.svelte'
|
|
import { page } from '@/App.svelte'
|
|
import { toast } from 'svelte-sonner'
|
|
import 'browser-event-target-emitter'
|
|
|
|
class TorrentWorker extends EventTarget {
|
|
constructor () {
|
|
super()
|
|
this.ready = new Promise((resolve) => {
|
|
window.IPC.once('port', () => {
|
|
this.port = window.port
|
|
this.port.onmessage((...args) => this.handleMessage(...args))
|
|
resolve()
|
|
})
|
|
window.IPC.emit('portRequest')
|
|
})
|
|
}
|
|
|
|
handleMessage ({ data }) {
|
|
this.emit(data.type, data.data)
|
|
}
|
|
|
|
async send (type, data, transfer) {
|
|
await this.ready
|
|
console.info('Torrent: sending message', { type, data })
|
|
this.port.postMessage({ type, data }, transfer)
|
|
}
|
|
}
|
|
|
|
export const client = new TorrentWorker()
|
|
|
|
client.send('load')
|
|
|
|
client.on('files', ({ detail }) => {
|
|
files.set(detail)
|
|
})
|
|
|
|
client.on('error', ({ detail }) => {
|
|
console.error(detail)
|
|
toast.error('Torrent Error', { description: detail.message || detail })
|
|
})
|
|
|
|
export async function add (torrentID, hide) {
|
|
if (torrentID) {
|
|
console.info('Torrent: adding torrent', { torrentID })
|
|
files.set([])
|
|
if (!hide) page.set('player')
|
|
client.send('torrent', torrentID)
|
|
}
|
|
}
|