feat: light view() polls with cached devices list
view(opts) accepts { light: true } to reuse deviceSync.listDevices()
for up to PEARCORD_VIEW_DEVICES_CACHE_MS on hot poll paths.
Co-authored-by: Cursor <[email protected]>
This commit is contained in:
@@ -9137,7 +9137,8 @@ class PearcordPlatform extends EventEmitter {
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
async view () {
|
async view (opts = {}) {
|
||||||
|
const light = !!opts.light
|
||||||
const user = this.identity.user
|
const user = this.identity.user
|
||||||
const guild = this.mode === 'guild' ? (this.guild?.guild || null) : null
|
const guild = this.mode === 'guild' ? (this.guild?.guild || null) : null
|
||||||
let channels = []
|
let channels = []
|
||||||
@@ -9989,13 +9990,28 @@ class PearcordPlatform extends EventEmitter {
|
|||||||
guildPresenceSummary: this._guildPresenceSummaryForView(),
|
guildPresenceSummary: this._guildPresenceSummaryForView(),
|
||||||
readOnly: this._readOnly,
|
readOnly: this._readOnly,
|
||||||
contacts: contactsForView,
|
contacts: contactsForView,
|
||||||
devices: this.deviceSync
|
devices: await this._devicesForView(light),
|
||||||
? await this.deviceSync.listDevices()
|
|
||||||
: [],
|
|
||||||
deviceId: this.identity?.deviceId || null
|
deviceId: this.identity?.deviceId || null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async _devicesForView (light = false) {
|
||||||
|
if (!this.deviceSync) return []
|
||||||
|
const ttlMs = Number(process.env.PEARCORD_VIEW_DEVICES_CACHE_MS) || 30000
|
||||||
|
const now = Date.now()
|
||||||
|
if (
|
||||||
|
light &&
|
||||||
|
this._cachedViewDevices &&
|
||||||
|
now - (this._cachedViewDevicesAt || 0) < ttlMs
|
||||||
|
) {
|
||||||
|
return this._cachedViewDevices
|
||||||
|
}
|
||||||
|
const devices = await this.deviceSync.listDevices()
|
||||||
|
this._cachedViewDevices = devices
|
||||||
|
this._cachedViewDevicesAt = now
|
||||||
|
return devices
|
||||||
|
}
|
||||||
|
|
||||||
async _enrichPresenceActivity (activity) {
|
async _enrichPresenceActivity (activity) {
|
||||||
if (!activity?.assets || !this.attachments) return activity
|
if (!activity?.assets || !this.attachments) return activity
|
||||||
const keys = [
|
const keys = [
|
||||||
|
|||||||
Reference in New Issue
Block a user