Move to requestPollInterval handling (#3914)
parent
f3515e2673
commit
7d06fb9432
|
@ -15,8 +15,7 @@ import {
|
||||||
|
|
||||||
const LOGGER_CONTEXT = 'MessagesEventBus'
|
const LOGGER_CONTEXT = 'MessagesEventBus'
|
||||||
|
|
||||||
const ACTIVE_POLL_INTERVAL = 60e3
|
const DEFAULT_POLL_INTERVAL = 60e3
|
||||||
const BACKGROUND_POLL_INTERVAL = 60e3
|
|
||||||
|
|
||||||
export class MessagesEventBus {
|
export class MessagesEventBus {
|
||||||
private id: string
|
private id: string
|
||||||
|
@ -26,9 +25,10 @@ export class MessagesEventBus {
|
||||||
private emitter = new EventEmitter()
|
private emitter = new EventEmitter()
|
||||||
|
|
||||||
private status: MessagesEventBusStatus = MessagesEventBusStatus.Uninitialized
|
private status: MessagesEventBusStatus = MessagesEventBusStatus.Uninitialized
|
||||||
private pollInterval = ACTIVE_POLL_INTERVAL
|
|
||||||
private error: MessagesEventBusError | undefined
|
private error: MessagesEventBusError | undefined
|
||||||
private latestRev: string | undefined = undefined
|
private latestRev: string | undefined = undefined
|
||||||
|
private pollInterval = DEFAULT_POLL_INTERVAL
|
||||||
|
private requestedPollIntervals: Map<string, number> = new Map()
|
||||||
|
|
||||||
snapshot: MessagesEventBusState | undefined
|
snapshot: MessagesEventBusState | undefined
|
||||||
|
|
||||||
|
@ -42,7 +42,7 @@ export class MessagesEventBus {
|
||||||
this.init = this.init.bind(this)
|
this.init = this.init.bind(this)
|
||||||
this.suspend = this.suspend.bind(this)
|
this.suspend = this.suspend.bind(this)
|
||||||
this.resume = this.resume.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.trail = this.trail.bind(this)
|
||||||
this.trailConvo = this.trailConvo.bind(this)
|
this.trailConvo = this.trailConvo.bind(this)
|
||||||
}
|
}
|
||||||
|
@ -78,7 +78,7 @@ export class MessagesEventBus {
|
||||||
status: MessagesEventBusStatus.Initializing,
|
status: MessagesEventBusStatus.Initializing,
|
||||||
rev: undefined,
|
rev: undefined,
|
||||||
error: undefined,
|
error: undefined,
|
||||||
setPollInterval: this.setPollInterval,
|
requestPollInterval: this.requestPollInterval,
|
||||||
trail: this.trail,
|
trail: this.trail,
|
||||||
trailConvo: this.trailConvo,
|
trailConvo: this.trailConvo,
|
||||||
}
|
}
|
||||||
|
@ -88,7 +88,7 @@ export class MessagesEventBus {
|
||||||
status: this.status,
|
status: this.status,
|
||||||
rev: this.latestRev!,
|
rev: this.latestRev!,
|
||||||
error: undefined,
|
error: undefined,
|
||||||
setPollInterval: this.setPollInterval,
|
requestPollInterval: this.requestPollInterval,
|
||||||
trail: this.trail,
|
trail: this.trail,
|
||||||
trailConvo: this.trailConvo,
|
trailConvo: this.trailConvo,
|
||||||
}
|
}
|
||||||
|
@ -98,7 +98,7 @@ export class MessagesEventBus {
|
||||||
status: this.status,
|
status: this.status,
|
||||||
rev: this.latestRev,
|
rev: this.latestRev,
|
||||||
error: undefined,
|
error: undefined,
|
||||||
setPollInterval: this.setPollInterval,
|
requestPollInterval: this.requestPollInterval,
|
||||||
trail: this.trail,
|
trail: this.trail,
|
||||||
trailConvo: this.trailConvo,
|
trailConvo: this.trailConvo,
|
||||||
}
|
}
|
||||||
|
@ -113,7 +113,7 @@ export class MessagesEventBus {
|
||||||
this.init()
|
this.init()
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
setPollInterval: this.setPollInterval,
|
requestPollInterval: this.requestPollInterval,
|
||||||
trail: this.trail,
|
trail: this.trail,
|
||||||
trailConvo: this.trailConvo,
|
trailConvo: this.trailConvo,
|
||||||
}
|
}
|
||||||
|
@ -123,7 +123,7 @@ export class MessagesEventBus {
|
||||||
status: MessagesEventBusStatus.Uninitialized,
|
status: MessagesEventBusStatus.Uninitialized,
|
||||||
rev: undefined,
|
rev: undefined,
|
||||||
error: undefined,
|
error: undefined,
|
||||||
setPollInterval: this.setPollInterval,
|
requestPollInterval: this.requestPollInterval,
|
||||||
trail: this.trail,
|
trail: this.trail,
|
||||||
trailConvo: this.trailConvo,
|
trailConvo: this.trailConvo,
|
||||||
}
|
}
|
||||||
|
@ -149,12 +149,12 @@ export class MessagesEventBus {
|
||||||
switch (action.event) {
|
switch (action.event) {
|
||||||
case MessagesEventBusDispatchEvent.Ready: {
|
case MessagesEventBusDispatchEvent.Ready: {
|
||||||
this.status = MessagesEventBusStatus.Ready
|
this.status = MessagesEventBusStatus.Ready
|
||||||
this.setPollInterval(ACTIVE_POLL_INTERVAL)
|
this.resetPoll()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
case MessagesEventBusDispatchEvent.Background: {
|
case MessagesEventBusDispatchEvent.Background: {
|
||||||
this.status = MessagesEventBusStatus.Backgrounded
|
this.status = MessagesEventBusStatus.Backgrounded
|
||||||
this.setPollInterval(BACKGROUND_POLL_INTERVAL)
|
this.resetPoll()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
case MessagesEventBusDispatchEvent.Suspend: {
|
case MessagesEventBusDispatchEvent.Suspend: {
|
||||||
|
@ -173,7 +173,7 @@ export class MessagesEventBus {
|
||||||
switch (action.event) {
|
switch (action.event) {
|
||||||
case MessagesEventBusDispatchEvent.Background: {
|
case MessagesEventBusDispatchEvent.Background: {
|
||||||
this.status = MessagesEventBusStatus.Backgrounded
|
this.status = MessagesEventBusStatus.Backgrounded
|
||||||
this.setPollInterval(BACKGROUND_POLL_INTERVAL)
|
this.resetPoll()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
case MessagesEventBusDispatchEvent.Suspend: {
|
case MessagesEventBusDispatchEvent.Suspend: {
|
||||||
|
@ -194,7 +194,7 @@ export class MessagesEventBus {
|
||||||
switch (action.event) {
|
switch (action.event) {
|
||||||
case MessagesEventBusDispatchEvent.Resume: {
|
case MessagesEventBusDispatchEvent.Resume: {
|
||||||
this.status = MessagesEventBusStatus.Ready
|
this.status = MessagesEventBusStatus.Ready
|
||||||
this.setPollInterval(ACTIVE_POLL_INTERVAL)
|
this.resetPoll()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
case MessagesEventBusDispatchEvent.Suspend: {
|
case MessagesEventBusDispatchEvent.Suspend: {
|
||||||
|
@ -215,12 +215,12 @@ export class MessagesEventBus {
|
||||||
switch (action.event) {
|
switch (action.event) {
|
||||||
case MessagesEventBusDispatchEvent.Resume: {
|
case MessagesEventBusDispatchEvent.Resume: {
|
||||||
this.status = MessagesEventBusStatus.Ready
|
this.status = MessagesEventBusStatus.Ready
|
||||||
this.setPollInterval(ACTIVE_POLL_INTERVAL)
|
this.resetPoll()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
case MessagesEventBusDispatchEvent.Background: {
|
case MessagesEventBusDispatchEvent.Background: {
|
||||||
this.status = MessagesEventBusStatus.Backgrounded
|
this.status = MessagesEventBusStatus.Backgrounded
|
||||||
this.setPollInterval(BACKGROUND_POLL_INTERVAL)
|
this.resetPoll()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
case MessagesEventBusDispatchEvent.Error: {
|
case MessagesEventBusDispatchEvent.Error: {
|
||||||
|
@ -306,9 +306,14 @@ export class MessagesEventBus {
|
||||||
this.dispatch({event: MessagesEventBusDispatchEvent.Resume})
|
this.dispatch({event: MessagesEventBusDispatchEvent.Resume})
|
||||||
}
|
}
|
||||||
|
|
||||||
setPollInterval(interval: number) {
|
requestPollInterval(interval: number) {
|
||||||
this.pollInterval = interval
|
const id = nanoid()
|
||||||
|
this.requestedPollIntervals.set(id, interval)
|
||||||
this.resetPoll()
|
this.resetPoll()
|
||||||
|
return () => {
|
||||||
|
this.requestedPollIntervals.delete(id)
|
||||||
|
this.resetPoll()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
trail(handler: (events: ChatBskyConvoGetLog.OutputSchema['logs']) => void) {
|
trail(handler: (events: ChatBskyConvoGetLog.OutputSchema['logs']) => void) {
|
||||||
|
@ -375,7 +380,20 @@ export class MessagesEventBus {
|
||||||
private isPolling = false
|
private isPolling = false
|
||||||
private pollIntervalRef: NodeJS.Timeout | undefined
|
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() {
|
private resetPoll() {
|
||||||
|
this.pollInterval = this.getPollInterval()
|
||||||
this.stopPoll()
|
this.stopPoll()
|
||||||
this.startPoll()
|
this.startPoll()
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,12 +60,14 @@ export type TrailHandler = (
|
||||||
events: ChatBskyConvoGetLog.OutputSchema['logs'],
|
events: ChatBskyConvoGetLog.OutputSchema['logs'],
|
||||||
) => void
|
) => void
|
||||||
|
|
||||||
|
export type RequestPollIntervalHandler = (interval: number) => () => void
|
||||||
|
|
||||||
export type MessagesEventBusState =
|
export type MessagesEventBusState =
|
||||||
| {
|
| {
|
||||||
status: MessagesEventBusStatus.Uninitialized
|
status: MessagesEventBusStatus.Uninitialized
|
||||||
rev: undefined
|
rev: undefined
|
||||||
error: undefined
|
error: undefined
|
||||||
setPollInterval: (interval: number) => void
|
requestPollInterval: RequestPollIntervalHandler
|
||||||
trail: (handler: TrailHandler) => () => void
|
trail: (handler: TrailHandler) => () => void
|
||||||
trailConvo: (convoId: string, handler: TrailHandler) => () => void
|
trailConvo: (convoId: string, handler: TrailHandler) => () => void
|
||||||
}
|
}
|
||||||
|
@ -73,7 +75,7 @@ export type MessagesEventBusState =
|
||||||
status: MessagesEventBusStatus.Initializing
|
status: MessagesEventBusStatus.Initializing
|
||||||
rev: undefined
|
rev: undefined
|
||||||
error: undefined
|
error: undefined
|
||||||
setPollInterval: (interval: number) => void
|
requestPollInterval: RequestPollIntervalHandler
|
||||||
trail: (handler: TrailHandler) => () => void
|
trail: (handler: TrailHandler) => () => void
|
||||||
trailConvo: (convoId: string, handler: TrailHandler) => () => void
|
trailConvo: (convoId: string, handler: TrailHandler) => () => void
|
||||||
}
|
}
|
||||||
|
@ -81,7 +83,7 @@ export type MessagesEventBusState =
|
||||||
status: MessagesEventBusStatus.Ready
|
status: MessagesEventBusStatus.Ready
|
||||||
rev: string
|
rev: string
|
||||||
error: undefined
|
error: undefined
|
||||||
setPollInterval: (interval: number) => void
|
requestPollInterval: RequestPollIntervalHandler
|
||||||
trail: (handler: TrailHandler) => () => void
|
trail: (handler: TrailHandler) => () => void
|
||||||
trailConvo: (convoId: string, handler: TrailHandler) => () => void
|
trailConvo: (convoId: string, handler: TrailHandler) => () => void
|
||||||
}
|
}
|
||||||
|
@ -89,7 +91,7 @@ export type MessagesEventBusState =
|
||||||
status: MessagesEventBusStatus.Backgrounded
|
status: MessagesEventBusStatus.Backgrounded
|
||||||
rev: string | undefined
|
rev: string | undefined
|
||||||
error: undefined
|
error: undefined
|
||||||
setPollInterval: (interval: number) => void
|
requestPollInterval: RequestPollIntervalHandler
|
||||||
trail: (handler: TrailHandler) => () => void
|
trail: (handler: TrailHandler) => () => void
|
||||||
trailConvo: (convoId: string, handler: TrailHandler) => () => void
|
trailConvo: (convoId: string, handler: TrailHandler) => () => void
|
||||||
}
|
}
|
||||||
|
@ -97,7 +99,7 @@ export type MessagesEventBusState =
|
||||||
status: MessagesEventBusStatus.Suspended
|
status: MessagesEventBusStatus.Suspended
|
||||||
rev: string | undefined
|
rev: string | undefined
|
||||||
error: undefined
|
error: undefined
|
||||||
setPollInterval: (interval: number) => void
|
requestPollInterval: RequestPollIntervalHandler
|
||||||
trail: (handler: TrailHandler) => () => void
|
trail: (handler: TrailHandler) => () => void
|
||||||
trailConvo: (convoId: string, handler: TrailHandler) => () => void
|
trailConvo: (convoId: string, handler: TrailHandler) => () => void
|
||||||
}
|
}
|
||||||
|
@ -105,7 +107,7 @@ export type MessagesEventBusState =
|
||||||
status: MessagesEventBusStatus.Error
|
status: MessagesEventBusStatus.Error
|
||||||
rev: string | undefined
|
rev: string | undefined
|
||||||
error: MessagesEventBusError
|
error: MessagesEventBusError
|
||||||
setPollInterval: (interval: number) => void
|
requestPollInterval: RequestPollIntervalHandler
|
||||||
trail: (handler: TrailHandler) => () => void
|
trail: (handler: TrailHandler) => () => void
|
||||||
trailConvo: (convoId: string, handler: TrailHandler) => () => void
|
trailConvo: (convoId: string, handler: TrailHandler) => () => void
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue