about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorEric Bailey <git@esb.lol>2024-05-08 11:34:04 -0500
committerGitHub <noreply@github.com>2024-05-08 11:34:04 -0500
commit7d06fb94322dd5141adc897081b0b9f3b26d4240 (patch)
tree1bd8b93ac9c0d3ec29b9fa541f9fdb52fabd9c06 /src
parentf3515e2673a36b031ed0286b0aa547ac8657cc96 (diff)
downloadvoidsky-7d06fb94322dd5141adc897081b0b9f3b26d4240.tar.zst
Move to requestPollInterval handling (#3914)
Diffstat (limited to 'src')
-rw-r--r--src/state/messages/events/agent.ts52
-rw-r--r--src/state/messages/events/types.ts14
2 files changed, 43 insertions, 23 deletions
diff --git a/src/state/messages/events/agent.ts b/src/state/messages/events/agent.ts
index c75f42ba0..422672853 100644
--- a/src/state/messages/events/agent.ts
+++ b/src/state/messages/events/agent.ts
@@ -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
@@ -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
 
@@ -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)
   }
@@ -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,
         }
@@ -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,
         }
@@ -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,
         }
@@ -113,7 +113,7 @@ export class MessagesEventBus {
               this.init()
             },
           },
-          setPollInterval: this.setPollInterval,
+          requestPollInterval: this.requestPollInterval,
           trail: this.trail,
           trailConvo: this.trailConvo,
         }
@@ -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,
         }
@@ -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: {
@@ -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: {
@@ -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: {
@@ -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: {
@@ -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) {
@@ -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()
   }
diff --git a/src/state/messages/events/types.ts b/src/state/messages/events/types.ts
index 52083b2c6..6959b4f06 100644
--- a/src/state/messages/events/types.ts
+++ b/src/state/messages/events/types.ts
@@ -60,12 +60,14 @@ 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
     }
@@ -73,7 +75,7 @@ export type MessagesEventBusState =
       status: MessagesEventBusStatus.Initializing
       rev: undefined
       error: undefined
-      setPollInterval: (interval: number) => void
+      requestPollInterval: RequestPollIntervalHandler
       trail: (handler: TrailHandler) => () => void
       trailConvo: (convoId: string, handler: TrailHandler) => () => void
     }
@@ -81,7 +83,7 @@ export type MessagesEventBusState =
       status: MessagesEventBusStatus.Ready
       rev: string
       error: undefined
-      setPollInterval: (interval: number) => void
+      requestPollInterval: RequestPollIntervalHandler
       trail: (handler: TrailHandler) => () => void
       trailConvo: (convoId: string, handler: TrailHandler) => () => void
     }
@@ -89,7 +91,7 @@ export type MessagesEventBusState =
       status: MessagesEventBusStatus.Backgrounded
       rev: string | undefined
       error: undefined
-      setPollInterval: (interval: number) => void
+      requestPollInterval: RequestPollIntervalHandler
       trail: (handler: TrailHandler) => () => void
       trailConvo: (convoId: string, handler: TrailHandler) => () => void
     }
@@ -97,7 +99,7 @@ export type MessagesEventBusState =
       status: MessagesEventBusStatus.Suspended
       rev: string | undefined
       error: undefined
-      setPollInterval: (interval: number) => void
+      requestPollInterval: RequestPollIntervalHandler
       trail: (handler: TrailHandler) => () => void
       trailConvo: (convoId: string, handler: TrailHandler) => () => void
     }
@@ -105,7 +107,7 @@ export type MessagesEventBusState =
       status: MessagesEventBusStatus.Error
       rev: string | undefined
       error: MessagesEventBusError
-      setPollInterval: (interval: number) => void
+      requestPollInterval: RequestPollIntervalHandler
       trail: (handler: TrailHandler) => () => void
       trailConvo: (convoId: string, handler: TrailHandler) => () => void
     }