Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Changed ExtraData from an Array to an IEnumerable
  • Loading branch information
jamescrosswell committed Jun 12, 2025
commit a2643952dcd40e0d8c203eee280a63a288bcfa0b
29 changes: 25 additions & 4 deletions src/Sentry.Maui/BreadcrumbEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,53 @@ public sealed class BreadcrumbEvent
/// Any extra data to be included in the breadcrumb. This would typically be event specific information (for example
/// it could include the X, Y coordinates of a tap event).
/// </summary>
public (string Key, string Value)[] ExtraData { get; }
public IEnumerable<KeyValuePair<string, string>> ExtraData { get; }

/// <summary>
/// Creates a new BreadcrumbEvent
/// </summary>
public BreadcrumbEvent(object? sender, string eventName)
: this(sender, eventName, Array.Empty<KeyValuePair<string, string>>())
{
}

/// <summary>
/// Creates a new BreadcrumbEvent
/// </summary>
public BreadcrumbEvent(
object? sender,
string eventName,
params (string Key, string Value)[] extraData)
params IEnumerable<KeyValuePair<string, string>> extraData)
{
Sender = sender;
EventName = eventName;
ExtraData = extraData;
}

/// <summary>
/// Creates a new BreadcrumbEvent
/// </summary>
public BreadcrumbEvent(
object? sender,
string eventName,
params IEnumerable<(string key, string value)> extraData) : this(sender, eventName, extraData.Select(
Copy link
Member

@Flash0ver Flash0ver Jun 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

final comment: deferred execution

The Select assigned to ExtraData will have deferred execution.
Is this expected / intended?

On the other hand, since we accept IEnumerable in the .ctors, the user could pass IEnumerables with deferred execution anyway.

On second thought ... I believe this is great as it is.
Also considering we're enumerating through it only a single time, so materializing the IEnumerable beforehand would actually be slightly slower and allocate more memory.

e => new KeyValuePair<string, string>(e.key, e.value)))
{
}

/// <summary>
/// This constructor remains for backward compatibility.
/// </summary>
/// <param name="sender"></param>
/// <param name="eventName"></param>
/// <param name="extraData"></param>
[Obsolete("Use the simpler constructor with params (string Key, string Value)[] extraData instead.")]
[Obsolete("Use one of the other simpler constructors instead.")]
public BreadcrumbEvent(
object? sender,
string eventName,
IEnumerable<(string Key, string Value)>[] extraData) : this(sender, eventName, extraData.SelectMany(e => e).ToArray())
IEnumerable<(string Key, string Value)>[] extraData) : this(sender, eventName, extraData.SelectMany(
x => x.Select(pair => new KeyValuePair<string, string>(pair.Key, pair.Value)))
)
{
}
}
10 changes: 5 additions & 5 deletions src/Sentry.Maui/Internal/MauiButtonEventsBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ namespace Sentry.Maui.Internal;
/// <inheritdoc />
public class MauiButtonEventsBinder : IMauiElementEventBinder
{
private Action<BreadcrumbEvent>? addBreadcrumbCallback;
private Action<BreadcrumbEvent>? _addBreadcrumbCallback;

/// <inheritdoc />
public void Bind(VisualElement element, Action<BreadcrumbEvent> addBreadcrumb)
{
addBreadcrumbCallback = addBreadcrumb;
_addBreadcrumbCallback = addBreadcrumb;

if (element is Button button)
{
Expand All @@ -30,11 +30,11 @@ public void UnBind(VisualElement element)
}

private void OnButtonOnClicked(object? sender, EventArgs _)
=> addBreadcrumbCallback?.Invoke(new(sender, nameof(Button.Clicked)));
=> _addBreadcrumbCallback?.Invoke(new(sender, nameof(Button.Clicked)));

private void OnButtonOnPressed(object? sender, EventArgs _)
=> addBreadcrumbCallback?.Invoke(new(sender, nameof(Button.Pressed)));
=> _addBreadcrumbCallback?.Invoke(new(sender, nameof(Button.Pressed)));

private void OnButtonOnReleased(object? sender, EventArgs _)
=> addBreadcrumbCallback?.Invoke(new(sender, nameof(Button.Released)));
=> _addBreadcrumbCallback?.Invoke(new(sender, nameof(Button.Released)));
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,17 @@ namespace Sentry.Maui
{
public sealed class BreadcrumbEvent
{
[System.Obsolete("Use the simpler constructor with params (string Key, string Value)[] extraData in" +
"stead.")]
public BreadcrumbEvent(object? sender, string eventName) { }
public BreadcrumbEvent(object? sender, string eventName, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>> extraData) { }
public BreadcrumbEvent(object? sender, string eventName, [System.Runtime.CompilerServices.TupleElementNames(new string[] {
"Key",
"Value"})] System.Collections.Generic.IEnumerable<System.ValueTuple<string, string>>[] extraData) { }
"key",
"value"})] System.Collections.Generic.IEnumerable<System.ValueTuple<string, string>> extraData) { }
[System.Obsolete("Use one of the other simpler constructors instead.")]
public BreadcrumbEvent(object? sender, string eventName, [System.Runtime.CompilerServices.TupleElementNames(new string[] {
"Key",
"Value"})] params System.ValueTuple<string, string>[] extraData) { }
"Value"})] System.Collections.Generic.IEnumerable<System.ValueTuple<string, string>>[] extraData) { }
public string EventName { get; }
[System.Runtime.CompilerServices.TupleElementNames(new string[] {
"Key",
"Value"})]
public System.ValueTuple<string, string>[] ExtraData { get; }
public System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>> ExtraData { get; }
public object? Sender { get; }
}
public interface IMauiElementEventBinder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,17 @@ namespace Sentry.Maui
{
public sealed class BreadcrumbEvent
{
[System.Obsolete("Use the simpler constructor with params (string Key, string Value)[] extraData in" +
"stead.")]
public BreadcrumbEvent(object? sender, string eventName) { }
public BreadcrumbEvent(object? sender, string eventName, [System.Runtime.CompilerServices.ParamCollection] System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>> extraData) { }
public BreadcrumbEvent(object? sender, string eventName, [System.Runtime.CompilerServices.ParamCollection] [System.Runtime.CompilerServices.TupleElementNames(new string[] {
"key",
"value"})] System.Collections.Generic.IEnumerable<System.ValueTuple<string, string>> extraData) { }
[System.Obsolete("Use one of the other simpler constructors instead.")]
public BreadcrumbEvent(object? sender, string eventName, [System.Runtime.CompilerServices.TupleElementNames(new string[] {
"Key",
"Value"})] System.Collections.Generic.IEnumerable<System.ValueTuple<string, string>>[] extraData) { }
public BreadcrumbEvent(object? sender, string eventName, [System.Runtime.CompilerServices.TupleElementNames(new string[] {
"Key",
"Value"})] params System.ValueTuple<string, string>[] extraData) { }
public string EventName { get; }
[System.Runtime.CompilerServices.TupleElementNames(new string[] {
"Key",
"Value"})]
public System.ValueTuple<string, string>[] ExtraData { get; }
public System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>> ExtraData { get; }
public object? Sender { get; }
}
public interface IMauiElementEventBinder
Expand Down
2 changes: 1 addition & 1 deletion test/Sentry.Maui.Tests/MauiEventsBinderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void OnBreadcrumbCreateCallback_CreatesBreadcrumb()
Assert.Equal(MauiEventsBinder.UserType, crumb.Type);
Assert.Equal(MauiEventsBinder.UserActionCategory, crumb.Category);
Assert.NotNull(crumb.Data);
Assert.Equal(breadcrumbEvent.ExtraData.Length, crumb.Data.Count);
Assert.Equal(breadcrumbEvent.ExtraData.Count(), crumb.Data.Count);
foreach (var (key, value) in breadcrumbEvent.ExtraData)
{
crumb.Data.Should().Contain(kvp => kvp.Key == key && kvp.Value == value);
Expand Down