Skip to content

Commit bf77562

Browse files
Fixed storeMessages() issue.
1 parent 675c23a commit bf77562

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/lib/reddit.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ export const redditClient = new Snoowrap({
2121
* @param items Array of Reddit messages or comments
2222
* @returns Array of newly created database records
2323
*/
24+
// Helper type guard to determine if an item is a PrivateMessage
25+
function isPrivateMessage(
26+
item: Snoowrap.PrivateMessage | Snoowrap.Comment
27+
): item is Snoowrap.PrivateMessage {
28+
return 'new' in item
29+
}
30+
2431
export async function storeMessages(items: Array<Snoowrap.PrivateMessage | Snoowrap.Comment>) {
2532
const newMessages = []
2633
console.debug(`Processing ${items.length} message(s)`)
@@ -30,7 +37,8 @@ export async function storeMessages(items: Array<Snoowrap.PrivateMessage | Snoow
3037

3138
for (const item of items) {
3239
try {
33-
const isMessage = item instanceof Snoowrap.PrivateMessage
40+
// Use the type guard instead of instanceof
41+
const isMessage = isPrivateMessage(item)
3442
const type = isMessage ? 'private_message' : 'comment'
3543

3644
// Check for existing record
@@ -80,17 +88,15 @@ export async function storeMessages(items: Array<Snoowrap.PrivateMessage | Snoow
8088
badge: 'https://new.codebuilder.org/images/logo2.png',
8189
}
8290

83-
// Loop and send notifications concurrently
91+
// Send notifications concurrently
8492
const notificationPromises = subscriptions.map((sub) =>
8593
sendNotification(sub, notificationPayload)
8694
)
87-
88-
// Wait for all notifications to complete
8995
await Promise.all(notificationPromises)
9096

9197
console.debug(`Stored new ${type} [${createdMsg.redditId}] from /u/${createdMsg.author}`)
9298
newMessages.push(createdMsg)
93-
} catch (error) {
99+
} catch (error: any) {
94100
console.error(`Error processing message ${item.name}:`, error.message)
95101
}
96102
}

0 commit comments

Comments
 (0)