Skip to content

Commit

Permalink
add message handler
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkNerdi committed May 17, 2024
1 parent e5a9b42 commit c9be51f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,34 @@ export class NotificationsManager {
}

this.notifyClient.on(
NotifyEvent.Subscription,
(event: NotifyClientTypes.EventArguments[NotifyEvent.Subscription]) => {
console.warn('Unimplemented: NotifyEvent.Subscription', event)
NotifyEvent.SubscriptionsChanged,
(event: NotifyClientTypes.EventArguments[NotifyEvent.SubscriptionsChanged]) => {
this.updateSubscriptionsPerAddress(event.params.subscriptions)
void this.updateNotificationsForSubscriptions(event.params.subscriptions)
}
)

this.notifyClient.on(NotifyEvent.Message, (event: NotifyClientTypes.EventArguments[NotifyEvent.Message]) => {
console.warn('Unimplemented: NotifyEvent.Message', event)
})
// There is both a notification and a message event that returns at the same time,
// We have only registered a listener for notification and will ask WC what the difference is
this.notifyClient.on(
NotifyEvent.Notification,
(event: NotifyClientTypes.EventArguments[NotifyEvent.Notification]) => {
this.handleNewNotification(event.topic, event.params.notification)
}
)

this.notifyClient.on(NotifyEvent.Update, (event: NotifyClientTypes.EventArguments[NotifyEvent.Update]) => {
console.warn('Unimplemented: NotifyEvent.Update', event)
})

this.notifyClient.on(NotifyEvent.Delete, (event: NotifyClientTypes.EventArguments[NotifyEvent.Delete]) => {
console.warn('Unimplemented: NotifyEvent.Delete', event)
})

this.notifyClient.on(
NotifyEvent.SubscriptionsChanged,
(event: NotifyClientTypes.EventArguments[NotifyEvent.SubscriptionsChanged]) => {
this.updateSubscriptionsPerAddress(event.params.subscriptions)
void this.updateNotificationsForSubscriptions(event.params.subscriptions)
NotifyEvent.Subscription,
(event: NotifyClientTypes.EventArguments[NotifyEvent.Subscription]) => {
console.warn('Unimplemented: NotifyEvent.Subscription', event)
}
)
}
Expand Down Expand Up @@ -198,6 +207,18 @@ export class NotificationsManager {
})
}

private handleNewNotification(topic: string, notification: NotifyClientTypes.NotifyNotification): void {
this.notificationsPerSubscription.update((state) => {
if (!state[topic]) {
return state
}

const notificationsForTopic = state[topic]
state[topic] = [...notificationsForTopic, notification]
return state
})
}

async subscribeToDapp(
appDomain: string,
account: IAccountState | undefined,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export enum NotifyEvent {
Subscription = 'notify_subscription',
Message = 'notify_message',
Notification = 'notify_notification',
Delete = 'notify_delete',
Update = 'notify_update',
SubscriptionsChanged = 'notify_subscriptions_changed',
}

0 comments on commit c9be51f

Please sign in to comment.