fix(mobile): ipc

This commit is contained in:
ThaUnknown 2023-12-06 18:59:59 +01:00
parent bffcc306f4
commit b7eef76204
5 changed files with 28 additions and 18 deletions

View file

@ -27,18 +27,17 @@ export default {
}
async function portRequest (data) {
const { port1, port2 } = new MessageChannel()
globalThis.port = {
onmessage: cb => {
NodeJS.addListener('ipc', ({ args }) => cb(args[0]))
},
postMessage: (a, b) => {
NodeJS.send({ eventName: 'ipc', args: [a] })
postMessage: (data, b) => {
NodeJS.send({ eventName: 'ipc', args: [{ data }] })
}
}
await ready
portListener()
NodeJS.send({ eventName: 'port-init' })
NodeJS.send({ eventName: 'port-init', args: [localStorage.getItem('settings')] })
}
const [_platform, arch] = navigator.platform.split(' ')

View file

@ -1,20 +1,28 @@
import TorrentClient from 'common/modules/webtorrent.js'
import { statfs } from 'fs/promises'
import { channel } from 'bridge'
async function storageQuota (directory) {
const { bsize, bavail } = await statfs(directory)
return bsize * bavail
return Infinity
}
channel.on('port-init', () => {
if (typeof localStorage === 'undefined') {
const data = {}
globalThis.localStorage = {
setItem: (k,v) => { data[k] = v },
getItem: (k) => data[k] || null
}
}
channel.on('port-init', data => {
localStorage.setItem('settings', data)
const port = {
onmessage: () => {},
postMessage: data=> {
channel.send('ipc', data)
onmessage: _ => {},
postMessage: data => {
channel.send('ipc', { data })
}
}
channel.on('ipc', port.onmessage)
channel.on('ipc', console.log)
channel.on('ipc', a => port.onmessage(a))
channel.emit('port', ({
ports: [port]
}))

View file

@ -35,7 +35,7 @@ const capacitorConfig = {
client: {
overlay: { errors: true, warnings: false, runtimeErrors: false }
},
port: 5000
port: 5001
},
plugins: [
new CopyWebpackPlugin({

View file

@ -49,7 +49,7 @@ class TorrentWorker extends EventTarget {
export const client = new TorrentWorker()
client.send('load')
client.send('load', localStorage.getItem('torrent'))
client.on('files', ({ detail }) => {
files.set(detail)
@ -68,6 +68,7 @@ client.on('warn', ({ detail }) => {
export async function add (torrentID, hide) {
if (torrentID) {
console.info('Torrent: adding torrent', { torrentID })
localStorage.setItem('torrent', torrentID)
files.set([])
if (!hide) page.set('player')
client.send('torrent', torrentID)

View file

@ -46,12 +46,12 @@ export default class TorrentClient extends WebTorrent {
this._ready = new Promise(resolve => {
ipc.on('port', ({ ports }) => {
this.message = ports[0].postMessage.bind(ports[0])
resolve()
ports[0].onmessage = ({ data }) => {
if (data.type === 'load') this.loadLastTorrent()
if (data.type === 'load') this.loadLastTorrent(data.data)
if (data.type === 'destroy') this.destroy()
this.handleMessage({ data })
}
resolve()
})
ipc.on('destroy', this.destroy.bind(this))
})
@ -93,8 +93,8 @@ export default class TorrentClient extends WebTorrent {
this.on('error', this.dispatchError.bind(this))
}
loadLastTorrent () {
const torrent = localStorage.getItem('torrent')
loadLastTorrent (t) {
const torrent = localStorage.getItem('torrent') || t
if (torrent) this.addTorrent(new Uint8Array(JSON.parse(torrent)), JSON.parse(localStorage.getItem('lastFinished')))
}
@ -209,6 +209,8 @@ export default class TorrentClient extends WebTorrent {
switch (data.type) {
case 'current': {
if (data.data) {
console.log('adding torrent')
console.log(data.data)
const torrent = await this.get(data.data.infoHash)
const found = torrent?.files.find(file => file.path === data.data.path)
if (!found) return