Square complexity in Signal when using AdviseOnce pattern
- Dominant language
- C#
- Stars
- 417
- Forks
- 62
- Avg merge
- 9h 15m
- Merged PRs (30d)
- 7
Description
If we call `CurrentClientLifetime` N times and then terminate lifetime of `LocalCodeWithMeClientManager` which removes a client we get a N^2 calls of `ValueLifetime.ClearValueIfNotAlive`
```csharp
public static Lifetime CurrentClientLifetime(this ICodeWithMeClientManager manager)
{
var clientId = ClientId.Current;
var lifetimeDefinition = Lifetime.Define(Lifetime.Eternal, $"Lifetime of {clientId}");
Log.Assert(manager.Clients.Contains(clientId), $"Client manager doesn't know about {clientId}");
var lifetime = lifetimeDefinition.Lifetime;
manager.Clients.Advise(lifetime, (addRemove, triggeredId) =>
{
if (addRemove == AddRemove.Remove && triggeredId == clientId)
{
lifetimeDefinition.Terminate();
}
});
return lifetime;
}
....
public class LocalCodeWithMeClientManager : ICodeWithMeClientManager
{
public LocalCodeWithMeClientManager(Lifetime lifetime)
{
Clients.Add(ClientId.LocalId);
lifetime.OnTermination(() => Clients.Remove(ClientId.LocalId));
}
public IViewableSet Clients { get; } = new ViewableSet();
}
```


Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.