Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 023e534

Browse files
committed
Better way to handle notifications
1 parent 3765f64 commit 023e534

File tree

5 files changed

+45
-5
lines changed

5 files changed

+45
-5
lines changed

src/GitHub.Exports.Reactive/Services/NotificationDispatcher.cs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.ComponentModel.Composition;
34
using System.Reactive.Linq;
45
using System.Reactive.Subjects;
@@ -12,39 +13,66 @@ namespace GitHub.Services
1213
public sealed class NotificationDispatcher : INotificationDispatcher, IDisposable
1314
{
1415
Subject<Notification> notifications;
16+
Stack<INotificationService> notificationHandlers;
1517

1618
public NotificationDispatcher()
1719
{
1820
notifications = new Subject<Notification>();
21+
notificationHandlers = new Stack<INotificationService>();
1922
}
2023

2124
public IObservable<Notification> Listen()
2225
{
2326
return notifications;
2427
}
2528

29+
public void AddListener(INotificationService handler)
30+
{
31+
notificationHandlers.Push(handler);
32+
}
33+
34+
public void RemoveListener()
35+
{
36+
notificationHandlers.Pop();
37+
}
38+
39+
public void RemoveListener(INotificationService handler)
40+
{
41+
Stack<INotificationService> handlers = new Stack<INotificationService>();
42+
while(notificationHandlers.TryPeek() != handler)
43+
handlers.Push(notificationHandlers.Pop());
44+
if (notificationHandlers.Count > 0)
45+
notificationHandlers.Pop();
46+
while (handlers.Count > 0)
47+
notificationHandlers.Push(handlers.Pop());
48+
}
49+
2650
public void ShowMessage(string message)
2751
{
2852
notifications.OnNext(new Notification(message, Notification.NotificationType.Message));
53+
var handler = notificationHandlers.TryPeek();
54+
handler?.ShowMessage(message);
2955
}
3056

3157
public void ShowMessage(string message, ICommand command)
3258
{
3359
notifications.OnNext(new Notification(message, Notification.NotificationType.Message, command));
60+
var handler = notificationHandlers.TryPeek();
61+
handler?.ShowMessage(message, command);
3462
}
3563

3664
public void ShowWarning(string message)
3765
{
3866
notifications.OnNext(new Notification(message, Notification.NotificationType.Warning));
67+
var handler = notificationHandlers.TryPeek();
68+
handler.ShowWarning(message);
3969
}
4070

4171
public void ShowError(string message)
4272
{
4373
notifications.OnNext(new Notification(message, Notification.NotificationType.Error));
44-
}
45-
46-
public void ClearNotifications()
47-
{
74+
var handler = notificationHandlers.TryPeek();
75+
handler?.ShowError(message);
4876
}
4977

5078
bool disposed; // To detect redundant calls

src/GitHub.Exports/Services/INotificationDispatcher.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,8 @@ public Notification(string message, NotificationType type, ICommand command = nu
2727
public interface INotificationDispatcher : INotificationService
2828
{
2929
IObservable<Notification> Listen();
30+
void AddListener(INotificationService notificationHandler);
31+
void RemoveListener();
32+
void RemoveListener(INotificationService notificationHandler);
3033
}
3134
}

src/GitHub.Exports/Services/INotificationService.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,5 @@ public interface INotificationService
88
void ShowMessage(string message, ICommand command);
99
void ShowWarning(string message);
1010
void ShowError(string message);
11-
void ClearNotifications();
1211
}
1312
}

src/GitHub.Exports/Services/ITeamExplorerServices.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ namespace GitHub.Services
44
{
55
public interface ITeamExplorerServices : INotificationService
66
{
7+
void ClearNotifications();
78
}
89
}

src/GitHub.Extensions/EnumerableExtensions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,14 @@ public static IEnumerable<TSource> Except<TSource>(
2121

2222
}
2323

24+
public static class StackExtensions
25+
{
26+
public static T TryPeek<T>(this Stack<T> stack) where T : class
27+
{
28+
if (stack.Count > 0)
29+
return stack.Peek();
30+
return default(T);
31+
}
32+
}
2433
}
2534

0 commit comments

Comments
 (0)