Skip to content

Commit

Permalink
Move to requestPollInterval handling (#3914)
Browse files Browse the repository at this point in the history
  • Loading branch information
estrattonbailey authored May 8, 2024
1 parent f3515e2 commit 7d06fb9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 23 deletions.
52 changes: 35 additions & 17 deletions src/state/messages/events/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import {

const LOGGER_CONTEXT = 'MessagesEventBus'

const ACTIVE_POLL_INTERVAL = 60e3
const BACKGROUND_POLL_INTERVAL = 60e3
const DEFAULT_POLL_INTERVAL = 60e3

export class MessagesEventBus {
private id: string
Expand All @@ -26,9 +25,10 @@ export class MessagesEventBus {
private emitter = new EventEmitter()

private status: MessagesEventBusStatus = MessagesEventBusStatus.Uninitialized
private pollInterval = ACTIVE_POLL_INTERVAL
private error: MessagesEventBusError | undefined
private latestRev: string | undefined = undefined
private pollInterval = DEFAULT_POLL_INTERVAL
private requestedPollIntervals: Map<string, number> = new Map()

snapshot: MessagesEventBusState | undefined

Expand All @@ -42,7 +42,7 @@ export class MessagesEventBus {
this.init = this.init.bind(this)
this.suspend = this.suspend.bind(this)
this.resume = this.resume.bind(this)
this.setPollInterval = this.setPollInterval.bind(this)
this.requestPollInterval = this.requestPollInterval.bind(this)
this.trail = this.trail.bind(this)
this.trailConvo = this.trailConvo.bind(this)
}
Expand Down Expand Up @@ -78,7 +78,7 @@ export class MessagesEventBus {
status: MessagesEventBusStatus.Initializing,
rev: undefined,
error: undefined,
setPollInterval: this.setPollInterval,
requestPollInterval: this.requestPollInterval,
trail: this.trail,
trailConvo: this.trailConvo,
}
Expand All @@ -88,7 +88,7 @@ export class MessagesEventBus {
status: this.status,
rev: this.latestRev!,
error: undefined,
setPollInterval: this.setPollInterval,
requestPollInterval: this.requestPollInterval,
trail: this.trail,
trailConvo: this.trailConvo,
}
Expand All @@ -98,7 +98,7 @@ export class MessagesEventBus {
status: this.status,
rev: this.latestRev,
error: undefined,
setPollInterval: this.setPollInterval,
requestPollInterval: this.requestPollInterval,
trail: this.trail,
trailConvo: this.trailConvo,
}
Expand All @@ -113,7 +113,7 @@ export class MessagesEventBus {
this.init()
},
},
setPollInterval: this.setPollInterval,
requestPollInterval: this.requestPollInterval,
trail: this.trail,
trailConvo: this.trailConvo,
}
Expand All @@ -123,7 +123,7 @@ export class MessagesEventBus {
status: MessagesEventBusStatus.Uninitialized,
rev: undefined,
error: undefined,
setPollInterval: this.setPollInterval,
requestPollInterval: this.requestPollInterval,
trail: this.trail,
trailConvo: this.trailConvo,
}
Expand All @@ -149,12 +149,12 @@ export class MessagesEventBus {
switch (action.event) {
case MessagesEventBusDispatchEvent.Ready: {
this.status = MessagesEventBusStatus.Ready
this.setPollInterval(ACTIVE_POLL_INTERVAL)
this.resetPoll()
break
}
case MessagesEventBusDispatchEvent.Background: {
this.status = MessagesEventBusStatus.Backgrounded
this.setPollInterval(BACKGROUND_POLL_INTERVAL)
this.resetPoll()
break
}
case MessagesEventBusDispatchEvent.Suspend: {
Expand All @@ -173,7 +173,7 @@ export class MessagesEventBus {
switch (action.event) {
case MessagesEventBusDispatchEvent.Background: {
this.status = MessagesEventBusStatus.Backgrounded
this.setPollInterval(BACKGROUND_POLL_INTERVAL)
this.resetPoll()
break
}
case MessagesEventBusDispatchEvent.Suspend: {
Expand All @@ -194,7 +194,7 @@ export class MessagesEventBus {
switch (action.event) {
case MessagesEventBusDispatchEvent.Resume: {
this.status = MessagesEventBusStatus.Ready
this.setPollInterval(ACTIVE_POLL_INTERVAL)
this.resetPoll()
break
}
case MessagesEventBusDispatchEvent.Suspend: {
Expand All @@ -215,12 +215,12 @@ export class MessagesEventBus {
switch (action.event) {
case MessagesEventBusDispatchEvent.Resume: {
this.status = MessagesEventBusStatus.Ready
this.setPollInterval(ACTIVE_POLL_INTERVAL)
this.resetPoll()
break
}
case MessagesEventBusDispatchEvent.Background: {
this.status = MessagesEventBusStatus.Backgrounded
this.setPollInterval(BACKGROUND_POLL_INTERVAL)
this.resetPoll()
break
}
case MessagesEventBusDispatchEvent.Error: {
Expand Down Expand Up @@ -306,9 +306,14 @@ export class MessagesEventBus {
this.dispatch({event: MessagesEventBusDispatchEvent.Resume})
}

setPollInterval(interval: number) {
this.pollInterval = interval
requestPollInterval(interval: number) {
const id = nanoid()
this.requestedPollIntervals.set(id, interval)
this.resetPoll()
return () => {
this.requestedPollIntervals.delete(id)
this.resetPoll()
}
}

trail(handler: (events: ChatBskyConvoGetLog.OutputSchema['logs']) => void) {
Expand Down Expand Up @@ -375,7 +380,20 @@ export class MessagesEventBus {
private isPolling = false
private pollIntervalRef: NodeJS.Timeout | undefined

private getPollInterval() {
switch (this.status) {
case MessagesEventBusStatus.Ready: {
const requested = Array.from(this.requestedPollIntervals.values())
const lowest = Math.min(DEFAULT_POLL_INTERVAL, ...requested)
return lowest
}
default:
return DEFAULT_POLL_INTERVAL
}
}

private resetPoll() {
this.pollInterval = this.getPollInterval()
this.stopPoll()
this.startPoll()
}
Expand Down
14 changes: 8 additions & 6 deletions src/state/messages/events/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,52 +60,54 @@ export type TrailHandler = (
events: ChatBskyConvoGetLog.OutputSchema['logs'],
) => void

export type RequestPollIntervalHandler = (interval: number) => () => void

export type MessagesEventBusState =
| {
status: MessagesEventBusStatus.Uninitialized
rev: undefined
error: undefined
setPollInterval: (interval: number) => void
requestPollInterval: RequestPollIntervalHandler
trail: (handler: TrailHandler) => () => void
trailConvo: (convoId: string, handler: TrailHandler) => () => void
}
| {
status: MessagesEventBusStatus.Initializing
rev: undefined
error: undefined
setPollInterval: (interval: number) => void
requestPollInterval: RequestPollIntervalHandler
trail: (handler: TrailHandler) => () => void
trailConvo: (convoId: string, handler: TrailHandler) => () => void
}
| {
status: MessagesEventBusStatus.Ready
rev: string
error: undefined
setPollInterval: (interval: number) => void
requestPollInterval: RequestPollIntervalHandler
trail: (handler: TrailHandler) => () => void
trailConvo: (convoId: string, handler: TrailHandler) => () => void
}
| {
status: MessagesEventBusStatus.Backgrounded
rev: string | undefined
error: undefined
setPollInterval: (interval: number) => void
requestPollInterval: RequestPollIntervalHandler
trail: (handler: TrailHandler) => () => void
trailConvo: (convoId: string, handler: TrailHandler) => () => void
}
| {
status: MessagesEventBusStatus.Suspended
rev: string | undefined
error: undefined
setPollInterval: (interval: number) => void
requestPollInterval: RequestPollIntervalHandler
trail: (handler: TrailHandler) => () => void
trailConvo: (convoId: string, handler: TrailHandler) => () => void
}
| {
status: MessagesEventBusStatus.Error
rev: string | undefined
error: MessagesEventBusError
setPollInterval: (interval: number) => void
requestPollInterval: RequestPollIntervalHandler
trail: (handler: TrailHandler) => () => void
trailConvo: (convoId: string, handler: TrailHandler) => () => void
}

0 comments on commit 7d06fb9

Please sign in to comment.