Skip to content

Commit cb94df9

Browse files
Copilotstephentoub
andauthored
[C#] Deduplicate OnDisposeCall and improve implementation (#626)
* Initial plan * Extract ActionDisposable into its own file and use it from both Client.cs and Session.cs Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
1 parent 47cb899 commit cb94df9

File tree

3 files changed

+25
-25
lines changed

3 files changed

+25
-25
lines changed

dotnet/src/ActionDisposable.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
*--------------------------------------------------------------------------------------------*/
4+
5+
namespace GitHub.Copilot.SDK;
6+
7+
/// <summary>
8+
/// A disposable that invokes an action when disposed.
9+
/// </summary>
10+
internal sealed class ActionDisposable : IDisposable
11+
{
12+
private Action? _action;
13+
14+
public ActionDisposable(Action action)
15+
{
16+
_action = action;
17+
}
18+
19+
public void Dispose()
20+
{
21+
var action = Interlocked.Exchange(ref _action, null);
22+
action?.Invoke();
23+
}
24+
}

dotnet/src/Client.cs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,22 +1561,3 @@ public class ToolResultAIContent(ToolResultObject toolResult) : AIContent
15611561
{
15621562
public ToolResultObject Result => toolResult;
15631563
}
1564-
1565-
/// <summary>
1566-
/// A disposable that invokes an action when disposed.
1567-
/// </summary>
1568-
internal sealed class ActionDisposable : IDisposable
1569-
{
1570-
private Action? _action;
1571-
1572-
public ActionDisposable(Action action)
1573-
{
1574-
_action = action;
1575-
}
1576-
1577-
public void Dispose()
1578-
{
1579-
var action = Interlocked.Exchange(ref _action, null);
1580-
action?.Invoke();
1581-
}
1582-
}

dotnet/src/Session.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ void Handler(SessionEvent evt)
246246
public IDisposable On(SessionEventHandler handler)
247247
{
248248
_eventHandlers.Add(handler);
249-
return new OnDisposeCall(() => _eventHandlers.Remove(handler));
249+
return new ActionDisposable(() => _eventHandlers.Remove(handler));
250250
}
251251

252252
/// <summary>
@@ -600,11 +600,6 @@ await InvokeRpcAsync<object>(
600600
}
601601
}
602602

603-
private class OnDisposeCall(Action callback) : IDisposable
604-
{
605-
public void Dispose() => callback();
606-
}
607-
608603
internal record SendMessageRequest
609604
{
610605
public string SessionId { get; init; } = string.Empty;

0 commit comments

Comments
 (0)