@@ -145,7 +145,9 @@ function accountBalanceStream(accountId: string, id: string): ReadableStream {
145
145
return new ReadableStream ( {
146
146
start ( controller ) {
147
147
// Get initial balance from database
148
- const account = db . account . findFirst ( { where : { id : { equals : accountId } } } ) ;
148
+ const account = db . account . findFirst ( {
149
+ where : { id : { equals : accountId } } ,
150
+ } ) ;
149
151
if ( ! account ) {
150
152
sendSSE ( controller , "error" , id , {
151
153
errors : [ { message : "Account not found" } ] ,
@@ -164,53 +166,59 @@ function accountBalanceStream(accountId: string, id: string): ReadableStream {
164
166
} ,
165
167
} ) ;
166
168
167
- // Bursty balance updates
169
+ // Random interval updates (1-5 seconds, skewed toward 5)
168
170
let count = 0 ;
169
171
let active = true ;
170
- function burstUpdate ( ) {
172
+ function sendUpdate ( ) {
171
173
if ( ! active || count >= 300 ) {
172
174
sendSSE ( controller , "complete" , id ) ;
175
+ controller . close ( ) ;
176
+ return ;
177
+ }
178
+ // Get current balance from database
179
+ const currentAccount = db . account . findFirst ( {
180
+ where : { id : { equals : accountId } } ,
181
+ } ) ;
182
+ if ( ! currentAccount ) {
183
+ controller . close ( ) ;
173
184
return ;
174
185
}
175
- // Simulate a burst: 2-8 rapid updates
176
- const burstSize = Math . floor ( Math . random ( ) * 7 ) + 2 ;
177
- for ( let i = 0 ; i < burstSize && count < 300 ; i ++ ) {
178
- // Get current balance from database
179
- const currentAccount = db . account . findFirst ( { where : { id : { equals : accountId } } } ) ;
180
- if ( ! currentAccount ) return ;
181
186
182
- // Random payment between $1.00 and $8.00, in cents
183
- const payment = Math . floor ( Math . random ( ) * ( 800 - 100 + 1 ) ) + 100 ;
184
- const newBalance = currentAccount . balance + payment ;
185
-
186
- // Update the database
187
- db . account . update ( {
188
- where : { id : { equals : accountId } } ,
189
- data : { balance : newBalance } ,
190
- } ) ;
187
+ // Random payment between $1.00 and $8.00, in cents
188
+ const payment = Math . floor ( Math . random ( ) * ( 120 - - 120 + 1 ) ) + - 120 ;
189
+ const newBalance = currentAccount . balance + payment ;
190
+
191
+ // Update the database
192
+ db . account . update ( {
193
+ where : { id : { equals : accountId } } ,
194
+ data : { balance : newBalance } ,
195
+ } ) ;
191
196
192
- sendSSE ( controller , "next" , id , {
193
- data : {
194
- accountBalanceUpdated : {
195
- id : accountId ,
196
- balance : newBalance ,
197
- payment,
198
- description : `Payment received: $${ ( payment / 100 ) . toFixed ( 2 ) } ` ,
199
- timestamp : new Date ( ) . toISOString ( ) ,
200
- } ,
197
+ sendSSE ( controller , "next" , id , {
198
+ data : {
199
+ accountBalanceUpdated : {
200
+ id : accountId ,
201
+ balance : newBalance ,
202
+ payment,
203
+ description : `Payment received: $${ ( payment / 100 ) . toFixed ( 2 ) } ` ,
204
+ timestamp : new Date ( ) . toISOString ( ) ,
201
205
} ,
202
- } ) ;
203
- count ++ ;
204
- }
205
- // Wait 200ms to 2s before next burst
206
+ } ,
207
+ } ) ;
208
+ count ++ ;
209
+
210
+ // Schedule next update with random delay (1-5s, skewed toward 5)
206
211
if ( count < 300 ) {
207
- const nextDelay = Math . floor ( Math . random ( ) * 1800 ) + 200 ;
208
- setTimeout ( burstUpdate , nextDelay ) ;
212
+ const delay = 1000 + 10000 * ( 1 - Math . sqrt ( Math . random ( ) ) ) ;
213
+ setTimeout ( sendUpdate , delay ) ;
209
214
} else {
210
215
sendSSE ( controller , "complete" , id ) ;
216
+ controller . close ( ) ;
211
217
}
212
218
}
213
- burstUpdate ( ) ;
219
+ // Start the first update after initial delay
220
+ const initialDelay = 1000 + 4000 * ( 1 - Math . sqrt ( Math . random ( ) ) ) ;
221
+ setTimeout ( sendUpdate , initialDelay ) ;
214
222
return ( ) => {
215
223
active = false ;
216
224
} ;
@@ -222,7 +230,9 @@ function transactionsStream(accountId: string, id: string): ReadableStream {
222
230
return new ReadableStream ( {
223
231
start ( controller ) {
224
232
// Verify account exists
225
- const account = db . account . findFirst ( { where : { id : { equals : accountId } } } ) ;
233
+ const account = db . account . findFirst ( {
234
+ where : { id : { equals : accountId } } ,
235
+ } ) ;
226
236
if ( ! account ) {
227
237
sendSSE ( controller , "error" , id , {
228
238
errors : [ { message : "Account not found" } ] ,
@@ -232,14 +242,25 @@ function transactionsStream(accountId: string, id: string): ReadableStream {
232
242
}
233
243
234
244
let count = 0 ;
235
- const interval = setInterval ( ( ) => {
245
+ let active = true ;
246
+ function sendTransaction ( ) {
247
+ if ( ! active || count >= 3 ) {
248
+ sendSSE ( controller , "complete" , id ) ;
249
+ controller . close ( ) ;
250
+ return ;
251
+ }
236
252
// Get current balance from database
237
- const currentAccount = db . account . findFirst ( { where : { id : { equals : accountId } } } ) ;
238
- if ( ! currentAccount ) return ;
253
+ const currentAccount = db . account . findFirst ( {
254
+ where : { id : { equals : accountId } } ,
255
+ } ) ;
256
+ if ( ! currentAccount ) {
257
+ controller . close ( ) ;
258
+ return ;
259
+ }
239
260
240
261
const amount = Math . floor ( Math . random ( ) * 10000 - 5000 ) ;
241
262
const newBalance = currentAccount . balance + amount ;
242
-
263
+
243
264
// Update account balance in database
244
265
db . account . update ( {
245
266
where : { id : { equals : accountId } } ,
@@ -248,7 +269,9 @@ function transactionsStream(accountId: string, id: string): ReadableStream {
248
269
249
270
// Create new transaction in database
250
271
const transaction = db . transaction . create ( {
251
- id : `txn-live-${ accountId } -${ Date . now ( ) } -${ Math . floor ( Math . random ( ) * 1000000 ) } ` ,
272
+ id : `txn-live-${ accountId } -${ Date . now ( ) } -${ Math . floor (
273
+ Math . random ( ) * 1000000
274
+ ) } `,
252
275
date : new Date ( ) . toISOString ( ) ,
253
276
description : "Live transaction update" ,
254
277
amount,
@@ -262,13 +285,21 @@ function transactionsStream(accountId: string, id: string): ReadableStream {
262
285
} ,
263
286
} ) ;
264
287
count ++ ;
265
- if ( count >= 3 ) {
266
- clearInterval ( interval ) ;
288
+
289
+ // Schedule next transaction with random delay (1-5s, skewed toward 5)
290
+ if ( count < 3 ) {
291
+ const delay = 1000 + 4000 * ( 1 - Math . sqrt ( Math . random ( ) ) ) ;
292
+ setTimeout ( sendTransaction , delay ) ;
293
+ } else {
267
294
sendSSE ( controller , "complete" , id ) ;
295
+ controller . close ( ) ;
268
296
}
269
- } , 1000 ) ;
297
+ }
298
+ // Start the first transaction after initial delay
299
+ const initialDelay = 1000 + 4000 * ( 1 - Math . sqrt ( Math . random ( ) ) ) ;
300
+ setTimeout ( sendTransaction , initialDelay ) ;
270
301
return ( ) => {
271
- clearInterval ( interval ) ;
302
+ active = false ;
272
303
} ;
273
304
} ,
274
305
} ) ;
0 commit comments