From 7b7a17ca497d8d8c50b72c63060c9c28a172be22 Mon Sep 17 00:00:00 2001 From: ig Date: Mon, 2 Oct 2023 15:36:50 +0200 Subject: [PATCH] minor --- csharp/Lib/Utils/AsyncEnumerableEx.cs | 8 ++++- csharp/Lib/Utils/GraphTraversal.cs | 9 +++--- csharp/Lib/Utils/None.cs | 2 +- csharp/Lib/Utils/Utils.cs | 10 ++++++- .../Lib/Victron/VeDBus/VeProperties.Dbus.cs | 29 +++++++++---------- 5 files changed, 36 insertions(+), 22 deletions(-) diff --git a/csharp/Lib/Utils/AsyncEnumerableEx.cs b/csharp/Lib/Utils/AsyncEnumerableEx.cs index 5bfc21015..78f4d252e 100644 --- a/csharp/Lib/Utils/AsyncEnumerableEx.cs +++ b/csharp/Lib/Utils/AsyncEnumerableEx.cs @@ -73,7 +73,13 @@ public static class AsyncEnumerableEx } } -#pragma warning disable 1998 + public static async void ForEach(this IAsyncEnumerable ts, Action action) + { + await foreach (var t in ts) + action(t); + } + + #pragma warning disable 1998 public static async IAsyncEnumerable Repeat(this T t, [EnumeratorCancellation] CancellationToken ct = default) { while(!ct.IsCancellationRequested) diff --git a/csharp/Lib/Utils/GraphTraversal.cs b/csharp/Lib/Utils/GraphTraversal.cs index e84690dbc..52fce9fb0 100644 --- a/csharp/Lib/Utils/GraphTraversal.cs +++ b/csharp/Lib/Utils/GraphTraversal.cs @@ -59,17 +59,18 @@ public static class GraphTraversal } [SuppressMessage("ReSharper", "PossibleMultipleEnumeration")] - private static IEnumerable Traverse(IEnumerable sources, + private static IEnumerable Traverse( + IEnumerable sources, Func>,IEnumerable> traversor, Func> getChildren, IEqualityComparer? comparer = null) { var set = new HashSet(sources, comparer ?? EqualityComparer.Default); - IEnumerable GetUniqueChildren(T n) => getChildren(n).Where(set.Add); + IEnumerable GetUniqueChildren(T n) => getChildren(n).Where(set!.Add); return from s in sources - from e in traversor(s, GetUniqueChildren) - select e; + from e in traversor(s, GetUniqueChildren) + select e; } // TODO: IEqualityComparer diff --git a/csharp/Lib/Utils/None.cs b/csharp/Lib/Utils/None.cs index 6053717c2..16103ef84 100644 --- a/csharp/Lib/Utils/None.cs +++ b/csharp/Lib/Utils/None.cs @@ -33,4 +33,4 @@ public readonly struct None : IEquatable, IComparable [Pure] public static implicit operator ValueTuple(None _) => default; [Pure] public static implicit operator None(ValueTuple _) => default; -} \ No newline at end of file +} diff --git a/csharp/Lib/Utils/Utils.cs b/csharp/Lib/Utils/Utils.cs index 7f7d441c5..e382172cd 100644 --- a/csharp/Lib/Utils/Utils.cs +++ b/csharp/Lib/Utils/Utils.cs @@ -1,5 +1,5 @@ +using System.Collections.Concurrent; using System.Diagnostics; -using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Runtime.CompilerServices; using static System.Runtime.CompilerServices.MethodImplOptions; @@ -171,4 +171,12 @@ public static class Utils } public static String ExecutingProcessName => Process.GetCurrentProcess().ProcessName; + + + public static Func Memoize(Func func) where A : notnull + { + var cache = new ConcurrentDictionary(); + + return arg => cache.GetOrAdd(arg, func); + } } \ No newline at end of file diff --git a/csharp/Lib/Victron/VeDBus/VeProperties.Dbus.cs b/csharp/Lib/Victron/VeDBus/VeProperties.Dbus.cs index bfa9cfce3..19ffe3e13 100644 --- a/csharp/Lib/Victron/VeDBus/VeProperties.Dbus.cs +++ b/csharp/Lib/Victron/VeDBus/VeProperties.Dbus.cs @@ -43,22 +43,22 @@ public static class VePropertiesDbus public static async Task PublishOnDBus(this VeProperties veProperties, DBusConnection con) { var calls = con.IncomingMessages - .Where(m => m.Type == MessageType.MethodCall) - .Select(msg => AnswerMethodCall(veProperties, msg)) - .Do(con.OutgoingMessages.OnNext); + .Where(m => m.Type == MessageType.MethodCall) + .Select(msg => AnswerMethodCall(veProperties, msg)) + .Do(con.OutgoingMessages.OnNext); var changes = veProperties - .PropertyChanged - .Do(con.BroadcastPropertiesChanged); + .PropertyChanged + .Do(con.BroadcastPropertiesChanged); var initialPropertiesChanged = veProperties - .ToObservable(TaskPoolScheduler.Default) - .Do(con.BroadcastPropertiesChanged); + .ToObservable(TaskPoolScheduler.Default) + .Do(con.BroadcastPropertiesChanged); return await calls - .MergeErrors(changes) - .MergeErrors(initialPropertiesChanged) - .Finally(con.Dispose); + .MergeErrors(changes) + .MergeErrors(initialPropertiesChanged) + .Finally(con.Dispose); } private static async Task GetName(this DBusDaemonConnection dbusConnection, String busName) @@ -107,11 +107,11 @@ public static class VePropertiesDbus return msg.Member switch { VeDBusApi.GetValue => msg.Ok(p.Value), - VeDBusApi.GetText => msg.Ok(p.Text), + VeDBusApi.GetText => msg.Ok(p.Text), VeDBusApi.GetItems => msg.Ok(p.GetItem()), - VeDBusApi.SetValue => (p.Writeable && msg.ObjectPath != null) - ? msg.Ok((p.Value==msg.Payload! || props.Set(path: msg.ObjectPath.Value, value: msg.Payload!, writable: true)) ? 0 : -1) - : msg.ObjectNotWritable(msg.Member), + VeDBusApi.SetValue => p.Writeable && msg.ObjectPath != null + ? msg.Ok(p.Value==msg.Payload! || props.Set(path: msg.ObjectPath.Value, value: msg.Payload!, writable: true) ? 0 : -1) + : msg.ObjectNotWritable(msg.Member), _ => msg.UnknownMember(msg.Member) }; } @@ -150,7 +150,6 @@ public static class VePropertiesDbus return CreateErrorReply(msg, $"Not Writable Property: {objectPath}"); } - private static Message CreateErrorReply(Message msg, String error) { Debug.WriteLine("ERROR: " + error);