refactor: Ensure stream subscriptions only disposed once
In the debugger, I noticed a few streams were unhooking their internal onChange callbacks unnecessarily (when they were already unhooked).
This commit is contained in:
1 parent
849c6c28e5
commit
9de4b27003
4 files changed
+74
-35
No files matched your search
@@ -102,8 +102,11 @@ namespace BCG
|
||||
myRealSource.onChange -= currValue;
|
||||
else
|
||||
{
|
||||
myOnChangeDelegate =
|
||||
(ChangeDelegate) Delegate.Remove (myOnChangeDelegate,currValue);
|
||||
if (myOnChangeDelegate is null) {
|
||||
Log.DataFlow.warn("Attempted to remove DataStream callback called when change delegate already null");
|
||||
} else {
|
||||
myOnChangeDelegate = (ChangeDelegate)Delegate.Remove(myOnChangeDelegate, currValue);
|
||||
}
|
||||
|
||||
// this is need to avoid a callback in a reentrant add.
|
||||
if (myPendingAdds.Count > 0)
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
#nullable enable
|
||||
namespace Bts.DataStreamCore;
|
||||
|
||||
using BCG;
|
||||
|
||||
using mbts.utils;
|
||||
|
||||
using Spark.Core.Disposables;
|
||||
|
||||
using System;
|
||||
|
||||
public static class ViewStreamExtensions {
|
||||
public static IDisposable Subscribe(this ViewStream viewStream, Action<IDataPacket> onData) {
|
||||
viewStream.onChange += OnChange;
|
||||
return Disposable.Create(Unsubscribe);
|
||||
|
||||
void OnChange(object? obj) =>
|
||||
onData(ExceptionAssert.Argument.Is<IDataPacket>(obj));
|
||||
|
||||
void Unsubscribe() =>
|
||||
viewStream.onChange -= OnChange;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user