fix: capacitor IPC

This commit is contained in:
ThaUnknown 2023-12-07 20:10:00 +01:00
parent df3fc91704
commit bc528c5cac
5 changed files with 22 additions and 41 deletions

View file

@ -3,18 +3,18 @@ import { StatusBar, Style } from '@capacitor/status-bar'
import { SafeArea } from 'capacitor-plugin-safe-area'
import { App } from '@capacitor/app'
import { Browser } from '@capacitor/browser'
// import { ipcRendererUI, main } from './ipc.js'
import IPC from './ipc.js'
// main.on('open', url => Browser.open({ url }))
IPC.on('open', url => Browser.open({ url }))
App.addListener('appUrlOpen', ({ url }) => handleProtocol(url))
// schema: miru://key/value
const protocolMap = {
auth: token => sendToken(token),
anime: id => ipcRendererUI.emit('open-anime', id),
w2g: link => ipcRendererUI.emit('w2glink', link),
schedule: () => ipcRendererUI.emit('schedule'),
anime: id => IPC.emit('open-anime', id),
w2g: link => IPC.emit('w2glink', link),
schedule: () => IPC.emit('schedule'),
donate: () => Browser.open({ url: 'https://github.com/sponsors/ThaUnknown/' })
}
@ -29,7 +29,7 @@ function sendToken (line) {
let token = line.split('access_token=')[1].split('&token_type')[0]
if (token) {
if (token.endsWith('/')) token = token.slice(0, -1)
ipcRendererUI.emit('altoken', token)
IPC.emit('altoken', token)
}
}

View file

@ -1,32 +1,13 @@
import { NodeJS } from 'capacitor-nodejs'
import EventEmitter from 'events'
let portListener
const ready = NodeJS.whenReady()
export default {
emit: async (event, data) => {
if (event === 'portRequest') return portRequest()
await ready
NodeJS.send({ eventName: event, args: [data] })
},
on: async (event, callback) => {
NodeJS.addListener(event, ({ args }) => callback(...args))
await ready
},
once: async (event, callback) => {
if (event === 'port') portListener = callback
await ready
const handle = NodeJS.addListener(event, ({ args }) => {
NodeJS.removeListener(handle)
callback(...args)
})
},
off: event => {
NodeJS.removeAllListeners(event)
}
}
const main = new EventEmitter()
async function portRequest (data) {
export default main
main.on('portRequest', async () => {
globalThis.port = {
onmessage: cb => {
NodeJS.addListener('ipc', ({ args }) => cb(args[0]))
@ -36,9 +17,9 @@ async function portRequest (data) {
}
}
await ready
portListener()
NodeJS.send({ eventName: 'port-init', args: [localStorage.getItem('settings')] })
}
main.emit('port')
})
const [_platform, arch] = navigator.platform.split(' ')

View file

@ -8,7 +8,7 @@ async function storageQuota (directory) {
if (typeof localStorage === 'undefined') {
const data = {}
globalThis.localStorage = {
setItem: (k,v) => { data[k] = v },
setItem: (k, v) => { data[k] = v },
getItem: (k) => data[k] || null
}
}
@ -21,7 +21,7 @@ channel.on('port-init', data => {
channel.send('ipc', { data })
}
}
channel.on('ipc', console.log)
channel.on('ipc', a => port.onmessage(a))
channel.emit('port', ({
ports: [port]

View file

@ -4,7 +4,7 @@ export const SUPPORTS = {
offscreenRender: false,
update: false,
angle: false,
doh: true,
doh: false,
dht: true,
discord: false,
torrentPort: true,

View file

@ -52,10 +52,6 @@
import AppSettings from './AppSettings.svelte'
import smoothScroll from '@/modules/scroll.js'
onDestroy(() => {
IPC.off('path')
})
const groups = {
player: {
name: 'Player',
@ -78,10 +74,14 @@
icon: 'description'
}
}
$: IPC.emit('show-discord-status', $settings.showDetailsInRPC)
IPC.on('path', data => {
function pathListener (data) {
$settings.torrentPath = data
}
onDestroy(() => {
IPC.off('path', pathListener)
})
$: IPC.emit('show-discord-status', $settings.showDetailsInRPC)
IPC.on('path', pathListener)
</script>
<Tabs>