Skip to content

Commit 6d2a478

Browse files
Fixes
1 parent a84d543 commit 6d2a478

File tree

9 files changed

+69
-15
lines changed

9 files changed

+69
-15
lines changed

.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ dotnet_diagnostic.IDE0010.severity = none
1414

1515
# IDE0063: Use simple 'using' statement
1616
csharp_prefer_simple_using_statement = false:suggestion
17+
18+
# IDE0090: Use 'new(...)'
19+
dotnet_diagnostic.IDE0090.severity = none

New Text Document.txt

Whitespace-only changes.

sdk/Notifo.SDK/CommandQueue/DefaultCommandQueue.cs

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.Linq;
1313
using System.Threading;
1414
using System.Threading.Tasks;
15+
using Notifo.SDK.Resources;
1516

1617
namespace Notifo.SDK.CommandQueue
1718
{
@@ -25,6 +26,8 @@ internal sealed class DefaultCommandQueue : ICommandQueue
2526
private readonly BlockingCollection<QueuedCommand> queue = new BlockingCollection<QueuedCommand>();
2627
private readonly Queue<QueuedCommand> retryQueue = new Queue<QueuedCommand>();
2728

29+
public event EventHandler<NotificationErrorEventArgs> OnError;
30+
2831
public DefaultCommandQueue(
2932
ICommandStore commandStore,
3033
ICommandTrigger[] commandTriggers,
@@ -56,7 +59,7 @@ public void Dispose()
5659
task.Wait();
5760
}
5861

59-
public Task ExecuteAsync(ICommand command)
62+
public void Run(ICommand command)
6063
{
6164
var queudCommand = new QueuedCommand
6265
{
@@ -70,7 +73,7 @@ public Task ExecuteAsync(ICommand command)
7073
{
7174
if (existing.Command.Merge(command))
7275
{
73-
return Task.CompletedTask;
76+
return;
7477
}
7578
}
7679

@@ -79,7 +82,19 @@ public Task ExecuteAsync(ICommand command)
7982

8083
Trigger();
8184

82-
return commandStore.StoreAsync(queudCommand).AsTask();
85+
_ = StoreAsync(queudCommand);
86+
}
87+
88+
private async Task StoreAsync(QueuedCommand queudCommand)
89+
{
90+
try
91+
{
92+
await commandStore.StoreAsync(queudCommand).AsTask();
93+
}
94+
catch (Exception ex)
95+
{
96+
OnError?.Invoke(this, new NotificationErrorEventArgs(Strings.CommandError, ex, this));
97+
}
8398
}
8499

85100
public void Trigger()
@@ -95,15 +110,22 @@ public void Trigger()
95110

96111
private async Task RunAsync()
97112
{
98-
var pendingCommands = await commandStore.GetCommandsAsync();
113+
try
114+
{
115+
var pendingCommands = await commandStore.GetCommandsAsync();
99116

100-
foreach (var command in pendingCommands)
117+
foreach (var command in pendingCommands)
118+
{
119+
retryQueue.Enqueue(command);
120+
}
121+
122+
Trigger();
123+
}
124+
catch (Exception ex)
101125
{
102-
retryQueue.Enqueue(command);
126+
OnError?.Invoke(this, new NotificationErrorEventArgs(Strings.CommandError, ex, this));
103127
}
104128

105-
Trigger();
106-
107129
try
108130
{
109131
foreach (var enqueued in queue.GetConsumingEnumerable())
@@ -123,7 +145,14 @@ private async Task RunAsync()
123145
}
124146

125147
// We have completed the command successfully, so we can remove it here.
126-
await commandStore.RemoveAsync(enqueued.CommandId);
148+
try
149+
{
150+
await commandStore.RemoveAsync(enqueued.CommandId);
151+
}
152+
catch (Exception ex)
153+
{
154+
OnError?.Invoke(this, new NotificationErrorEventArgs(Strings.CommandError, ex, this));
155+
}
127156

128157
// We have just completed a command, so it is very likely that the next one will be successful as well.
129158
Trigger();

sdk/Notifo.SDK/CommandQueue/ICommand.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// All rights reserved. Licensed under the MIT license.
66
// ==========================================================================
77

8+
using System;
89
using System.Threading;
910
using System.Threading.Tasks;
1011

sdk/Notifo.SDK/CommandQueue/ICommandQueue.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
// All rights reserved. Licensed under the MIT license.
66
// ==========================================================================
77

8-
using System.Threading.Tasks;
8+
using System;
99

1010
namespace Notifo.SDK.CommandQueue
1111
{
1212
internal interface ICommandQueue
1313
{
14-
Task ExecuteAsync(ICommand command);
14+
event EventHandler<NotificationErrorEventArgs> OnError;
15+
16+
void Run(ICommand command);
1517

1618
void Trigger();
1719
}

sdk/Notifo.SDK/NotifoMobilePush/NotifoMobilePushImplementation.shared.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ internal partial class NotifoMobilePushImplementation : INotifoMobilePush
6262
public NotifoMobilePushImplementation(Func<HttpClient> httpClientFactory, ISeenNotificationsStore seenNotificationsStore, ICommandQueue commandQueue)
6363
{
6464
this.seenNotificationsStore = seenNotificationsStore;
65-
this.clientProvider = new NotifoClientProvider(httpClientFactory);
6665
this.commandQueue = commandQueue;
66+
this.commandQueue.OnError += CommandQueue_OnError;
67+
this.clientProvider = new NotifoClientProvider(httpClientFactory);
6768

6869
SetupPlatform();
6970
}
@@ -119,7 +120,7 @@ public void Register()
119120
return;
120121
}
121122

122-
_ = commandQueue.ExecuteAsync(new TokenRegisterCommand { Token = token });
123+
commandQueue.Run(new TokenRegisterCommand { Token = token });
123124
}
124125

125126
public void Unregister()
@@ -129,7 +130,7 @@ public void Unregister()
129130
return;
130131
}
131132

132-
_ = commandQueue.ExecuteAsync(new TokenUnregisterCommand { Token = token });
133+
commandQueue.Run(new TokenUnregisterCommand { Token = token });
133134
}
134135

135136
private void PushEventsProvider_OnNotificationReceived(object sender, NotificationEventArgs e)
@@ -150,6 +151,12 @@ private void PushEventsProvider_OnError(object sender, NotificationErrorEventArg
150151
OnError?.Invoke(sender, e);
151152
}
152153

154+
private void CommandQueue_OnError(object sender, NotificationErrorEventArgs e)
155+
{
156+
// Forward the event to the application.
157+
OnError?.Invoke(sender, e);
158+
}
159+
153160
private void PushEventsProvider_OnTokenRefresh(object sender, TokenRefreshEventArgs e)
154161
{
155162
UpdateToken(e.Token);

sdk/Notifo.SDK/NotifoMobilePush/NotifoMobilePushImplementation.tracking.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public async Task TrackNotificationsAsync(params Guid[] ids)
7070
Add(id);
7171
}
7272

73-
await commandQueue.ExecuteAsync(new TrackSeenCommand { Ids = ids.ToHashSet(), Token = token });
73+
await commandQueue.Run(new TrackSeenCommand { Ids = ids.ToHashSet(), Token = token });
7474
}
7575
catch (Exception ex)
7676
{

sdk/Notifo.SDK/Resources/Strings.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/Notifo.SDK/Resources/Strings.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@
117117
<resheader name="writer">
118118
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
119119
</resheader>
120+
<data name="CommandError" xml:space="preserve">
121+
<value>Command queue has thrown an error.</value>
122+
</data>
120123
<data name="DownloadImageError" xml:space="preserve">
121124
<value>Could not download image.</value>
122125
</data>

0 commit comments

Comments
 (0)