This commit is contained in:
ThaUnknown 2023-11-23 23:34:25 +01:00
parent 31226fbf89
commit 3e9e015945
5 changed files with 64 additions and 80 deletions

View file

@ -14,14 +14,10 @@ const capacitorConfig = {
process: 'webtorrent/polyfills/process-fast.js',
Buffer: ['buffer', 'Buffer']
}),
new webpack.DefinePlugin({
global: 'globalThis'
})
new webpack.DefinePlugin({ global: 'globalThis' })
],
devServer: {
devMiddleware: {
writeToDisk: true
},
devMiddleware: { writeToDisk: true },
hot: true,
client: {
overlay: { errors: true, warnings: false, runtimeErrors: false }

View file

@ -45,9 +45,9 @@ export function fastPrettyBytes (num) {
/**
* @type {DOMParser['parseFromString']}
*/
export const DOMPARSER = DOMParser.prototype.parseFromString.bind(new DOMParser())
export const DOMPARSER = (typeof DOMParser !== 'undefined') && DOMParser.prototype.parseFromString.bind(new DOMParser())
export const sleep = t => new Promise(resolve => setTimeout(resolve, t))
export const sleep = t => new Promise(resolve => setTimeout(resolve, t).unref?.())
export function toTS (sec, full) {
if (isNaN(sec) || sec < 0) {
@ -106,7 +106,7 @@ export function throttle (fn, time) {
setTimeout(() => {
fn()
wait = false
}, time)
}, time).unref?.()
}
}
}
@ -119,7 +119,7 @@ export function debounce (fn, time) {
fn(...args)
}
clearTimeout(timeout)
timeout = setTimeout(later, time)
timeout = setTimeout(later, time).unref?.()
}
}

View file

@ -40,7 +40,7 @@
const json = await res.json()
return json.map(({ body, tag_name: version }) => ({ body, version }))
})()
IPC.emit('discord_status', settings.value.showDetailsInRPC)
IPC.emit('show-discord-status', settings.value.showDetailsInRPC)
</script>
<script>
@ -78,7 +78,7 @@
icon: 'description'
}
}
$: IPC.emit('discord_status', $settings.showDetailsInRPC)
$: IPC.emit('show-discord-status', $settings.showDetailsInRPC)
IPC.on('path', data => {
$settings.torrentPath = data
})

View file

@ -1,63 +1,12 @@
import { Client } from 'discord-rpc'
import { ipcMain } from 'electron'
import { debounce } from '@/modules/util.js'
export default class {
window
status
discord
requestedDiscordDetails
allowDiscordDetails
rpcStarted
cachedPresence
/**
* @param {import('electron').BrowserWindow} window
*/
constructor (window) {
this.window = window
this.discord = new Client({
transport: 'ipc'
})
ipcMain.on('discord_status', (event, data) => {
this.requestedDiscordDetails = data
if (!this.rpcStarted) {
this.handleRPC()
setInterval(this.handleRPC.bind(this), 5000) // According to Discord documentation, clients can only update their presence 5 times per 20 seconds. We will add an extra second to be safe.
this.rpcStarted = true
}
})
ipcMain.on('discord', (event, data) => {
this.cachedPresence = data
if (this.allowDiscordDetails) {
this.setDiscordRPC(data)
}
})
this.discord.on('ready', async () => {
this.setDiscordRPC(this.status)
this.discord.subscribe('ACTIVITY_JOIN_REQUEST')
this.discord.subscribe('ACTIVITY_JOIN')
this.discord.subscribe('ACTIVITY_SPECTATE')
})
this.discord.on('ACTIVITY_JOIN', ({ secret }) => {
this.window.webContents.send('w2glink', secret)
})
this.loginRPC()
}
loginRPC () {
this.discord.login({ clientId: '954855428355915797' }).catch(() => {
setTimeout(this.loginRPC.bind(this), 5000).unref()
})
}
setDiscordRPC (data = {
defaultStatus = {
activity: {
timestamps: {
start: Date.now()
},
timestamps: { start: Date.now() },
details: 'Stream anime torrents, real-time.',
state: 'Watching anime',
assets: {
@ -73,22 +22,58 @@ export default class {
instance: true,
type: 3
}
}) {
this.status = data
if (this.discord.user && this.status) {
this.status.pid = process.pid
this.discord.request('SET_ACTIVITY', this.status)
}
}
handleRPC () {
if (this.allowDiscordDetails === this.requestedDiscordDetails) return
discord
requestedDiscordDetails
allowDiscordDetails
cachedPresence
this.allowDiscordDetails = this.requestedDiscordDetails
if (!this.allowDiscordDetails) {
this.setDiscordRPC(null)
} else if (this.cachedPresence) {
this.setDiscordRPC(this.cachedPresence)
/**
* @param {import('electron').BrowserWindow} window
*/
constructor (window) {
this.window = window
this.discord = new Client({
transport: 'ipc'
})
ipcMain.on('show-discord-status', (event, data) => {
this.allowDiscordDetails = data
this.debouncedDiscordRPC(this.allowDiscordDetails ? this.cachedPresence : undefined)
})
ipcMain.on('discord', (event, data) => {
this.cachedPresence = data
this.debouncedDiscordRPC(this.allowDiscordDetails ? this.cachedPresence : undefined)
})
this.discord.on('ready', async () => {
this.setDiscordRPC(this.cachedPresence || this.defaultStatus)
this.discord.subscribe('ACTIVITY_JOIN_REQUEST')
this.discord.subscribe('ACTIVITY_JOIN')
this.discord.subscribe('ACTIVITY_SPECTATE')
})
this.discord.on('ACTIVITY_JOIN', ({ secret }) => {
this.window.webContents.send('w2glink', secret)
})
this.loginRPC()
this.debouncedDiscordRPC = debounce(status => this.setDiscordRPC(status), 4500)
}
loginRPC () {
this.discord.login({ clientId: '954855428355915797' }).catch(() => {
setTimeout(() => this.loginRPC(), 5000).unref()
})
}
setDiscordRPC (data = this.defaultStatus) {
if (this.discord.user && data) {
data.pid = process.pid
this.discord.request('SET_ACTIVITY', data)
}
}
}

View file

@ -62,7 +62,10 @@ module.exports = [
filename: 'main.js'
},
resolve: {
aliasFields: []
aliasFields: [],
alias: {
'@': resolve(__dirname, '..', 'common')
}
},
mode,
target: 'electron20.0-main'