JetBrains / JetBrains/rd

Square complexity in Signal when using AdviseOnce pattern

Open
#224 0 comments 0 reactions 1 assignee Claimed by @Iliya-usov View on GitHub
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();
}

```
![image](https://user-images.githubusercontent.com/623624/118688553-d3f47b80-b80e-11eb-9455-e4db643dae75.png)
![image](https://user-images.githubusercontent.com/623624/118688685-f1c1e080-b80e-11eb-9614-e149ada222e8.png)

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.