File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1- import { TaskRun , TaskRunAttempt } from "@trigger.dev/database" ;
1+ import { TaskRun } from "@trigger.dev/database" ;
22import { eventStream } from "remix-utils/sse/server" ;
33import { PrismaClient , prisma } from "~/db.server" ;
44import { logger } from "~/services/logger.server" ;
5+ import { throttle } from "~/utils/throttle" ;
56import { eventRepository } from "~/v3/eventRepository.server" ;
67
7- type RunWithAttempts = {
8- updatedAt : Date ;
9- attempts : {
10- status : TaskRunAttempt [ "status" ] ;
11- updatedAt : Date ;
12- } [ ] ;
13- } ;
14-
158const pingInterval = 1000 ;
169
1710export class RunStreamPresenter {
@@ -77,8 +70,10 @@ export class RunStreamPresenter {
7770 }
7871 } ;
7972
73+ const throttledSend = throttle ( safeSend , 1000 ) ;
74+
8075 eventEmitter . addListener ( "message" , ( event ) => {
81- safeSend ( { data : event } ) ;
76+ throttledSend ( { data : event } ) ;
8277 } ) ;
8378
8479 pinger = setInterval ( ( ) => {
Original file line number Diff line number Diff line change 1+ //From: https://kettanaito.com/blog/debounce-vs-throttle
2+
3+ /** A very simple throttle. Will execute the function every Xms and discard any other calls during that period. */
4+ export function throttle (
5+ func : ( ...args : any [ ] ) => void ,
6+ duration : number
7+ ) : ( ...args : any [ ] ) => void {
8+ let shouldWait = false ;
9+
10+ return ( ...args : any [ ] ) => {
11+ if ( ! shouldWait ) {
12+ func ( ...args ) ;
13+ shouldWait = true ;
14+
15+ setTimeout ( ( ) => {
16+ shouldWait = false ;
17+ } , duration ) ;
18+ }
19+ } ;
20+ }
You can’t perform that action at this time.
0 commit comments