Bluesky app fork with some witchin' additions 💫
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Move to requestPollInterval handling (#3914)

authored by

Eric Bailey and committed by
GitHub
7d06fb94 f3515e26

+43 -23
+35 -17
src/state/messages/events/agent.ts
··· 15 15 16 16 const LOGGER_CONTEXT = 'MessagesEventBus' 17 17 18 - const ACTIVE_POLL_INTERVAL = 60e3 19 - const BACKGROUND_POLL_INTERVAL = 60e3 18 + const DEFAULT_POLL_INTERVAL = 60e3 20 19 21 20 export class MessagesEventBus { 22 21 private id: string ··· 26 25 private emitter = new EventEmitter() 27 26 28 27 private status: MessagesEventBusStatus = MessagesEventBusStatus.Uninitialized 29 - private pollInterval = ACTIVE_POLL_INTERVAL 30 28 private error: MessagesEventBusError | undefined 31 29 private latestRev: string | undefined = undefined 30 + private pollInterval = DEFAULT_POLL_INTERVAL 31 + private requestedPollIntervals: Map<string, number> = new Map() 32 32 33 33 snapshot: MessagesEventBusState | undefined 34 34 ··· 42 42 this.init = this.init.bind(this) 43 43 this.suspend = this.suspend.bind(this) 44 44 this.resume = this.resume.bind(this) 45 - this.setPollInterval = this.setPollInterval.bind(this) 45 + this.requestPollInterval = this.requestPollInterval.bind(this) 46 46 this.trail = this.trail.bind(this) 47 47 this.trailConvo = this.trailConvo.bind(this) 48 48 } ··· 78 78 status: MessagesEventBusStatus.Initializing, 79 79 rev: undefined, 80 80 error: undefined, 81 - setPollInterval: this.setPollInterval, 81 + requestPollInterval: this.requestPollInterval, 82 82 trail: this.trail, 83 83 trailConvo: this.trailConvo, 84 84 } ··· 88 88 status: this.status, 89 89 rev: this.latestRev!, 90 90 error: undefined, 91 - setPollInterval: this.setPollInterval, 91 + requestPollInterval: this.requestPollInterval, 92 92 trail: this.trail, 93 93 trailConvo: this.trailConvo, 94 94 } ··· 98 98 status: this.status, 99 99 rev: this.latestRev, 100 100 error: undefined, 101 - setPollInterval: this.setPollInterval, 101 + requestPollInterval: this.requestPollInterval, 102 102 trail: this.trail, 103 103 trailConvo: this.trailConvo, 104 104 } ··· 113 113 this.init() 114 114 }, 115 115 }, 116 - setPollInterval: this.setPollInterval, 116 + requestPollInterval: this.requestPollInterval, 117 117 trail: this.trail, 118 118 trailConvo: this.trailConvo, 119 119 } ··· 123 123 status: MessagesEventBusStatus.Uninitialized, 124 124 rev: undefined, 125 125 error: undefined, 126 - setPollInterval: this.setPollInterval, 126 + requestPollInterval: this.requestPollInterval, 127 127 trail: this.trail, 128 128 trailConvo: this.trailConvo, 129 129 } ··· 149 149 switch (action.event) { 150 150 case MessagesEventBusDispatchEvent.Ready: { 151 151 this.status = MessagesEventBusStatus.Ready 152 - this.setPollInterval(ACTIVE_POLL_INTERVAL) 152 + this.resetPoll() 153 153 break 154 154 } 155 155 case MessagesEventBusDispatchEvent.Background: { 156 156 this.status = MessagesEventBusStatus.Backgrounded 157 - this.setPollInterval(BACKGROUND_POLL_INTERVAL) 157 + this.resetPoll() 158 158 break 159 159 } 160 160 case MessagesEventBusDispatchEvent.Suspend: { ··· 173 173 switch (action.event) { 174 174 case MessagesEventBusDispatchEvent.Background: { 175 175 this.status = MessagesEventBusStatus.Backgrounded 176 - this.setPollInterval(BACKGROUND_POLL_INTERVAL) 176 + this.resetPoll() 177 177 break 178 178 } 179 179 case MessagesEventBusDispatchEvent.Suspend: { ··· 194 194 switch (action.event) { 195 195 case MessagesEventBusDispatchEvent.Resume: { 196 196 this.status = MessagesEventBusStatus.Ready 197 - this.setPollInterval(ACTIVE_POLL_INTERVAL) 197 + this.resetPoll() 198 198 break 199 199 } 200 200 case MessagesEventBusDispatchEvent.Suspend: { ··· 215 215 switch (action.event) { 216 216 case MessagesEventBusDispatchEvent.Resume: { 217 217 this.status = MessagesEventBusStatus.Ready 218 - this.setPollInterval(ACTIVE_POLL_INTERVAL) 218 + this.resetPoll() 219 219 break 220 220 } 221 221 case MessagesEventBusDispatchEvent.Background: { 222 222 this.status = MessagesEventBusStatus.Backgrounded 223 - this.setPollInterval(BACKGROUND_POLL_INTERVAL) 223 + this.resetPoll() 224 224 break 225 225 } 226 226 case MessagesEventBusDispatchEvent.Error: { ··· 306 306 this.dispatch({event: MessagesEventBusDispatchEvent.Resume}) 307 307 } 308 308 309 - setPollInterval(interval: number) { 310 - this.pollInterval = interval 309 + requestPollInterval(interval: number) { 310 + const id = nanoid() 311 + this.requestedPollIntervals.set(id, interval) 311 312 this.resetPoll() 313 + return () => { 314 + this.requestedPollIntervals.delete(id) 315 + this.resetPoll() 316 + } 312 317 } 313 318 314 319 trail(handler: (events: ChatBskyConvoGetLog.OutputSchema['logs']) => void) { ··· 375 380 private isPolling = false 376 381 private pollIntervalRef: NodeJS.Timeout | undefined 377 382 383 + private getPollInterval() { 384 + switch (this.status) { 385 + case MessagesEventBusStatus.Ready: { 386 + const requested = Array.from(this.requestedPollIntervals.values()) 387 + const lowest = Math.min(DEFAULT_POLL_INTERVAL, ...requested) 388 + return lowest 389 + } 390 + default: 391 + return DEFAULT_POLL_INTERVAL 392 + } 393 + } 394 + 378 395 private resetPoll() { 396 + this.pollInterval = this.getPollInterval() 379 397 this.stopPoll() 380 398 this.startPoll() 381 399 }
+8 -6
src/state/messages/events/types.ts
··· 60 60 events: ChatBskyConvoGetLog.OutputSchema['logs'], 61 61 ) => void 62 62 63 + export type RequestPollIntervalHandler = (interval: number) => () => void 64 + 63 65 export type MessagesEventBusState = 64 66 | { 65 67 status: MessagesEventBusStatus.Uninitialized 66 68 rev: undefined 67 69 error: undefined 68 - setPollInterval: (interval: number) => void 70 + requestPollInterval: RequestPollIntervalHandler 69 71 trail: (handler: TrailHandler) => () => void 70 72 trailConvo: (convoId: string, handler: TrailHandler) => () => void 71 73 } ··· 73 75 status: MessagesEventBusStatus.Initializing 74 76 rev: undefined 75 77 error: undefined 76 - setPollInterval: (interval: number) => void 78 + requestPollInterval: RequestPollIntervalHandler 77 79 trail: (handler: TrailHandler) => () => void 78 80 trailConvo: (convoId: string, handler: TrailHandler) => () => void 79 81 } ··· 81 83 status: MessagesEventBusStatus.Ready 82 84 rev: string 83 85 error: undefined 84 - setPollInterval: (interval: number) => void 86 + requestPollInterval: RequestPollIntervalHandler 85 87 trail: (handler: TrailHandler) => () => void 86 88 trailConvo: (convoId: string, handler: TrailHandler) => () => void 87 89 } ··· 89 91 status: MessagesEventBusStatus.Backgrounded 90 92 rev: string | undefined 91 93 error: undefined 92 - setPollInterval: (interval: number) => void 94 + requestPollInterval: RequestPollIntervalHandler 93 95 trail: (handler: TrailHandler) => () => void 94 96 trailConvo: (convoId: string, handler: TrailHandler) => () => void 95 97 } ··· 97 99 status: MessagesEventBusStatus.Suspended 98 100 rev: string | undefined 99 101 error: undefined 100 - setPollInterval: (interval: number) => void 102 + requestPollInterval: RequestPollIntervalHandler 101 103 trail: (handler: TrailHandler) => () => void 102 104 trailConvo: (convoId: string, handler: TrailHandler) => () => void 103 105 } ··· 105 107 status: MessagesEventBusStatus.Error 106 108 rev: string | undefined 107 109 error: MessagesEventBusError 108 - setPollInterval: (interval: number) => void 110 + requestPollInterval: RequestPollIntervalHandler 109 111 trail: (handler: TrailHandler) => () => void 110 112 trailConvo: (convoId: string, handler: TrailHandler) => () => void 111 113 }