Files
pearcord-platform/state-poll-adaptive.js
T
Raven ScottandCursor 96f5893aad Complete Phase 409 platform perf, integrity, and mesh hardening.
Add adaptive poll helpers, discovery frame dedupe, poll DB read budget,
view integrity (draft prune, pin fallback, cross-ref audit, prefs checksum),
mesh quiet mode, topic merge counters, and discovery local-only guard.

Co-authored-by: Cursor <[email protected]>
2026-06-02 16:41:37 -04:00

68 lines
1.8 KiB
JavaScript

'use strict'
const FLOOR_MS = Math.max(
400,
Number(process.env.PEARCORD_STATE_POLL_FLOOR_MS) || 800
)
const CEIL_MS = Math.max(
FLOOR_MS + 500,
Number(process.env.PEARCORD_STATE_POLL_CEIL_MS) || 8000
)
/**
* Adaptive poll interval (Phase 409 P409-09).
* @param {{
* rendererFocused?: boolean,
* hasUnread?: boolean,
* guildLoading?: boolean,
* sessionReady?: boolean,
* mode?: string,
* composerTyping?: boolean,
* modalOpen?: boolean,
* activeMs?: number,
* idleMs?: number,
* loadingMs?: number,
* unfocusedMs?: number,
* unfocusedUnreadMs?: number
* }} opts
*/
function computeAdaptiveStatePollMs (opts = {}) {
const activeMs = Math.max(FLOOR_MS, Number(opts.activeMs) || 1500)
const idleMs = Math.min(CEIL_MS, Math.max(activeMs, Number(opts.idleMs) || 4000))
const loadingMs = Math.min(CEIL_MS, Math.max(activeMs, Number(opts.loadingMs) || 2500))
const unfocusedMs = Math.min(CEIL_MS, Math.max(idleMs, Number(opts.unfocusedMs) || 6000))
const unfocusedUnreadMs = Math.min(
CEIL_MS,
Math.max(FLOOR_MS, Number(opts.unfocusedUnreadMs) || 2500)
)
let tier = 'idle'
let intervalMs = idleMs
if (opts.composerTyping || opts.modalOpen) {
tier = opts.composerTyping ? 'typing' : 'modal'
intervalMs = FLOOR_MS
} else if (!opts.rendererFocused) {
tier = opts.hasUnread ? 'unfocused-unread' : 'unfocused'
intervalMs = opts.hasUnread ? unfocusedUnreadMs : unfocusedMs
} else if (opts.guildLoading) {
tier = 'loading'
intervalMs = loadingMs
} else if (
opts.sessionReady &&
(opts.mode === 'guild' || opts.mode === 'dm')
) {
tier = 'active'
intervalMs = activeMs
}
return {
intervalMs: Math.min(CEIL_MS, Math.max(FLOOR_MS, intervalMs)),
tier,
floorMs: FLOOR_MS,
ceilingMs: CEIL_MS
}
}
module.exports = { computeAdaptiveStatePollMs, FLOOR_MS, CEIL_MS }