Skip to content

Commit a890bb1

Browse files
committed
changed balance update interval
1 parent a9c8071 commit a890bb1

File tree

1 file changed

+75
-44
lines changed

1 file changed

+75
-44
lines changed

apps/banking-app/src/mocks/handlers/subscriptionHandlers.ts

Lines changed: 75 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,9 @@ function accountBalanceStream(accountId: string, id: string): ReadableStream {
145145
return new ReadableStream({
146146
start(controller) {
147147
// 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+
});
149151
if (!account) {
150152
sendSSE(controller, "error", id, {
151153
errors: [{ message: "Account not found" }],
@@ -164,53 +166,59 @@ function accountBalanceStream(accountId: string, id: string): ReadableStream {
164166
},
165167
});
166168

167-
// Bursty balance updates
169+
// Random interval updates (1-5 seconds, skewed toward 5)
168170
let count = 0;
169171
let active = true;
170-
function burstUpdate() {
172+
function sendUpdate() {
171173
if (!active || count >= 300) {
172174
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();
173184
return;
174185
}
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;
181186

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+
});
191196

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(),
201205
},
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)
206211
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);
209214
} else {
210215
sendSSE(controller, "complete", id);
216+
controller.close();
211217
}
212218
}
213-
burstUpdate();
219+
// Start the first update after initial delay
220+
const initialDelay = 1000 + 4000 * (1 - Math.sqrt(Math.random()));
221+
setTimeout(sendUpdate, initialDelay);
214222
return () => {
215223
active = false;
216224
};
@@ -222,7 +230,9 @@ function transactionsStream(accountId: string, id: string): ReadableStream {
222230
return new ReadableStream({
223231
start(controller) {
224232
// 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+
});
226236
if (!account) {
227237
sendSSE(controller, "error", id, {
228238
errors: [{ message: "Account not found" }],
@@ -232,14 +242,25 @@ function transactionsStream(accountId: string, id: string): ReadableStream {
232242
}
233243

234244
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+
}
236252
// 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+
}
239260

240261
const amount = Math.floor(Math.random() * 10000 - 5000);
241262
const newBalance = currentAccount.balance + amount;
242-
263+
243264
// Update account balance in database
244265
db.account.update({
245266
where: { id: { equals: accountId } },
@@ -248,7 +269,9 @@ function transactionsStream(accountId: string, id: string): ReadableStream {
248269

249270
// Create new transaction in database
250271
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+
)}`,
252275
date: new Date().toISOString(),
253276
description: "Live transaction update",
254277
amount,
@@ -262,13 +285,21 @@ function transactionsStream(accountId: string, id: string): ReadableStream {
262285
},
263286
});
264287
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 {
267294
sendSSE(controller, "complete", id);
295+
controller.close();
268296
}
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);
270301
return () => {
271-
clearInterval(interval);
302+
active = false;
272303
};
273304
},
274305
});

0 commit comments

Comments
 (0)