diff --git a/Nadeko.Tests/PubSubTests.cs b/Nadeko.Tests/PubSubTests.cs index f2c34c5f6..79b3a2ab8 100644 --- a/Nadeko.Tests/PubSubTests.cs +++ b/Nadeko.Tests/PubSubTests.cs @@ -17,7 +17,7 @@ namespace Nadeko.Tests { Assert.AreEqual(expected, data); Assert.Pass(); - return Task.CompletedTask; + return default; }); await pubsub.Pub(key, expected); Assert.Fail("Event not registered"); @@ -33,9 +33,9 @@ namespace Nadeko.Tests { Assert.AreEqual(expected, data); Assert.Pass(); - return Task.CompletedTask; + return default; }); - await pubsub.Unsub(key, _ => Task.CompletedTask); + await pubsub.Unsub(key, _ => default); await pubsub.Pub(key, expected); Assert.Fail("Event not registered"); } @@ -50,13 +50,13 @@ namespace Nadeko.Tests { Assert.AreEqual(expected, data); Assert.Pass(); - return Task.CompletedTask; + return default; }); await pubsub.Unsub(key, data => { Assert.AreEqual(expected, data); Assert.Pass(); - return Task.CompletedTask; + return default; }); await pubsub.Pub(key, expected); Assert.Fail("Event not registered"); @@ -68,10 +68,10 @@ namespace Nadeko.Tests TypedKey key = "test_key"; var pubsub = new EventPubSub(); - Task Action(int data) + ValueTask Action(int data) { Assert.Fail("Event is raised when it shouldn't be"); - return Task.CompletedTask; + return default; } await pubsub.Sub(key, Action); @@ -88,11 +88,11 @@ namespace Nadeko.Tests var localData = new byte[1]; - Task Action(byte[] data) + ValueTask Action(byte[] data) { Assert.AreEqual(localData, data); Assert.Pass(); - return Task.CompletedTask; + return default; } await pubsub.Sub(key, Action); @@ -110,18 +110,18 @@ namespace Nadeko.Tests var localData = new object(); int successCounter = 0; - Task Action1(object data) + ValueTask Action1(object data) { Assert.AreEqual(localData, data); successCounter+=10; - return Task.CompletedTask; + return default; } - Task Action2(object data) + ValueTask Action2(object data) { Assert.AreEqual(localData, data); successCounter++; - return Task.CompletedTask; + return default; } await pubsub.Sub(key, Action1); // + 10 \ diff --git a/NadekoBot.Core/Common/PubSub/EventPubSub.cs b/NadekoBot.Core/Common/PubSub/EventPubSub.cs index e0205a77b..5bf034a68 100644 --- a/NadekoBot.Core/Common/PubSub/EventPubSub.cs +++ b/NadekoBot.Core/Common/PubSub/EventPubSub.cs @@ -7,26 +7,26 @@ namespace NadekoBot.Core.Common { public class EventPubSub : IPubSub { - private readonly Dictionary>>> _actions - = new Dictionary>>>(); + private readonly Dictionary>>> _actions + = new Dictionary>>>(); private readonly object locker = new object(); - public Task Sub(in TypedKey key, Func action) + public Task Sub(in TypedKey key, Func action) { - Func localAction = obj => action((TData) obj); + Func localAction = obj => action((TData) obj); lock(locker) { - Dictionary>> keyActions; + Dictionary>> keyActions; if (!_actions.TryGetValue(key.Key, out keyActions)) { - keyActions = new Dictionary>>(); + keyActions = new Dictionary>>(); _actions[key.Key] = keyActions; } - List> sameActions; + List> sameActions; if (!keyActions.TryGetValue(action, out sameActions)) { - sameActions = new List>(); + sameActions = new List>(); keyActions[action] = sameActions; } @@ -42,16 +42,19 @@ namespace NadekoBot.Core.Common { if(_actions.TryGetValue(key.Key, out var actions)) { + // if this class ever gets used, this needs to be properly implemented + // 1. ignore all valuetasks which are completed + // 2. return task.whenall all other tasks return Task.WhenAll(actions .SelectMany(kvp => kvp.Value) - .Select(action => action(data))); + .Select(action => action(data).AsTask())); } return Task.CompletedTask; } } - public Task Unsub(in TypedKey key, Func action) + public Task Unsub(in TypedKey key, Func action) { lock (locker) { @@ -88,4 +91,5 @@ namespace NadekoBot.Core.Common } } } + } \ No newline at end of file diff --git a/NadekoBot.Core/Common/PubSub/IPubSub.cs b/NadekoBot.Core/Common/PubSub/IPubSub.cs index 1d28b762e..911429bce 100644 --- a/NadekoBot.Core/Common/PubSub/IPubSub.cs +++ b/NadekoBot.Core/Common/PubSub/IPubSub.cs @@ -6,6 +6,6 @@ namespace NadekoBot.Core.Common public interface IPubSub { public Task Pub(in TypedKey key, TData data); - public Task Sub(in TypedKey key, Func action); + public Task Sub(in TypedKey key, Func action); } } \ No newline at end of file diff --git a/NadekoBot.Core/Common/PubSub/RedisPubSub.cs b/NadekoBot.Core/Common/PubSub/RedisPubSub.cs index 0413c05f0..0d18de222 100644 --- a/NadekoBot.Core/Common/PubSub/RedisPubSub.cs +++ b/NadekoBot.Core/Common/PubSub/RedisPubSub.cs @@ -28,7 +28,7 @@ namespace NadekoBot.Core.Common return _multi.GetSubscriber().PublishAsync($"{_creds.RedisKey()}:{key.Key}", serialized, CommandFlags.FireAndForget); } - public Task Sub(in TypedKey key, Func action) + public Task Sub(in TypedKey key, Func action) { var eventName = key.Key; return _multi.GetSubscriber().SubscribeAsync($"{_creds.RedisKey()}:{eventName}", async (ch, data) => diff --git a/NadekoBot.Core/Modules/Permissions/Services/BlacklistService.cs b/NadekoBot.Core/Modules/Permissions/Services/BlacklistService.cs index 01c967ee9..f3c9f93d7 100644 --- a/NadekoBot.Core/Modules/Permissions/Services/BlacklistService.cs +++ b/NadekoBot.Core/Modules/Permissions/Services/BlacklistService.cs @@ -30,10 +30,10 @@ namespace NadekoBot.Modules.Permissions.Services _pubSub.Sub(blPubKey, OnReload); } - private Task OnReload(BlacklistEntry[] blacklist) + private ValueTask OnReload(BlacklistEntry[] blacklist) { _blacklist = blacklist; - return Task.CompletedTask; + return default; } public async Task RunBehavior(DiscordSocketClient _, IGuild guild, IUserMessage usrMsg) diff --git a/NadekoBot.Core/Services/Settings/ConfigServiceBase.cs b/NadekoBot.Core/Services/Settings/ConfigServiceBase.cs index 64f1854d1..73cad0e01 100644 --- a/NadekoBot.Core/Services/Settings/ConfigServiceBase.cs +++ b/NadekoBot.Core/Services/Settings/ConfigServiceBase.cs @@ -52,11 +52,11 @@ namespace NadekoBot.Core.Services _pubSub.Pub(_changeKey, _data); } - private Task OnChangePublished(TSettings newData) + private ValueTask OnChangePublished(TSettings newData) { _data = newData; OnStateUpdate(); - return Task.CompletedTask; + return default; } private TSettings CreateCopy()