Skip to content

Commit d2c791d

Browse files
kblokclaude
andcommitted
Make FromEventBuffered's bufferSize nullable, defaulting to unbounded
The 0.1.3 fallout: FromEventBuffered's own default of 1 was an arbitrary opinion this wrapper had no business holding, and it was wrong for the wrapper's actual dominant use case (filtering for a specific match among payloads that could arrive in the pre-subscribe gap this method exists to cover) - even I fell into it myself, in puppeteer-sharp's own WaitForTargetAsync, before a code review caught it. bufferSize is now int? = null, where null defers entirely to ReplaySubject's own default (unbounded, matching rxjs's own ReplaySubject(bufferSize = Infinity)) instead of inventing a second, smaller opinionated default. The gap this method targets is normally microseconds; an unbounded buffer only becomes a real memory concern if a BufferedEventSource stays attached a long time without ever being subscribed to, which callers can still avoid by passing an explicit bound for that specific usage. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01K92eapPm8e7mX7puT4cF4s
1 parent 0aafc93 commit d2c791d

3 files changed

Lines changed: 36 additions & 20 deletions

File tree

‎src/RxSharp/Extras/FromEventBuffered.cs‎

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,24 @@ public static partial class RxExtensions
6969
/// <param name="removeHandler">Called on <see cref="BufferedEventSource{TEventArgs}.Dispose"/>, with the same handler, to remove it from the event.</param>
7070
/// <param name="conversion">Converts an <see cref="Action{TEventArgs}"/> callback into the event's actual delegate shape.</param>
7171
/// <param name="bufferSize">
72-
/// The maximum number of most-recent payloads kept for replay. Defaults to 1 - fine if the caller only
73-
/// cares about the single most recent payload, but a real risk if the caller filters for a specific match
74-
/// among possibly-several payloads that could arrive in the pre-subscribe gap this method exists to cover:
75-
/// with the default of 1, a matching payload can be silently evicted from the buffer by a later
76-
/// non-matching one before anyone subscribes to see it. Pass a larger value whenever more than one payload
77-
/// could plausibly arrive before subscription and only some of them are what the caller is looking for.
72+
/// The maximum number of most-recent payloads kept for replay. Defaults to <see langword="null"/>, which
73+
/// defers entirely to <see cref="ReplaySubject{T}"/>'s own default (unbounded) - this wrapper has no
74+
/// buffering opinion of its own beyond what the primitive it's built on already does. A caller filtering
75+
/// for a specific match among payloads that could arrive in the pre-subscribe gap this method exists to
76+
/// cover needs more than 1: with too small a buffer, a matching payload can be silently evicted by a later
77+
/// non-matching one before anyone subscribes to see it. Pass an explicit bound only if this source stays
78+
/// attached for a long time without being subscribed to and unbounded growth is a real concern for that
79+
/// specific usage - the gap this method targets is normally microseconds, not something an unbounded
80+
/// buffer meaningfully grows during.
7881
/// </param>
7982
/// <returns>A handle exposing the buffered payloads as an observable, and detaching the handler on disposal.</returns>
8083
public static BufferedEventSource<TEventArgs> FromEventBuffered<TDelegate, TEventArgs>(
8184
Action<TDelegate> addHandler,
8285
Action<TDelegate> removeHandler,
8386
Func<Action<TEventArgs>, TDelegate> conversion,
84-
int bufferSize = 1)
87+
int? bufferSize = null)
8588
{
86-
var subject = new ReplaySubject<TEventArgs>(bufferSize);
89+
var subject = bufferSize is { } size ? new ReplaySubject<TEventArgs>(size) : new ReplaySubject<TEventArgs>();
8790
var handler = conversion(subject.OnNext);
8891
addHandler(handler);
8992
return new BufferedEventSource<TEventArgs>(subject, () => removeHandler(handler));
@@ -93,18 +96,11 @@ public static BufferedEventSource<TEventArgs> FromEventBuffered<TDelegate, TEven
9396
/// <typeparam name="TEventArgs">The type of the event's payload.</typeparam>
9497
/// <param name="addHandler">Called immediately, with the handler to add to the event.</param>
9598
/// <param name="removeHandler">Called on <see cref="BufferedEventSource{TEventArgs}.Dispose"/>, with the same handler, to remove it from the event.</param>
96-
/// <param name="bufferSize">
97-
/// The maximum number of most-recent payloads kept for replay. Defaults to 1 - fine if the caller only
98-
/// cares about the single most recent payload, but a real risk if the caller filters for a specific match
99-
/// among possibly-several payloads that could arrive in the pre-subscribe gap this method exists to cover:
100-
/// with the default of 1, a matching payload can be silently evicted from the buffer by a later
101-
/// non-matching one before anyone subscribes to see it. Pass a larger value whenever more than one payload
102-
/// could plausibly arrive before subscription and only some of them are what the caller is looking for.
103-
/// </param>
99+
/// <param name="bufferSize">See <see cref="FromEventBuffered{TDelegate, TEventArgs}"/>'s parameter of the same name.</param>
104100
/// <returns>A handle exposing the buffered payloads as an observable, and detaching the handler on disposal.</returns>
105101
public static BufferedEventSource<TEventArgs> FromEventBuffered<TEventArgs>(
106102
Action<EventHandler<TEventArgs>> addHandler,
107103
Action<EventHandler<TEventArgs>> removeHandler,
108-
int bufferSize = 1)
104+
int? bufferSize = null)
109105
=> FromEventBuffered<EventHandler<TEventArgs>, TEventArgs>(addHandler, removeHandler, onNext => (_, args) => onNext(args), bufferSize);
110106
}

‎src/RxSharp/RxSharp.csproj‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
<PackageProjectUrl>https://github.com/hardkoded/ReactiveExtensions-Sharp</PackageProjectUrl>
1717
<RepositoryUrl>https://github.com/hardkoded/ReactiveExtensions-Sharp</RepositoryUrl>
1818
<PackageReadmeFile>README.md</PackageReadmeFile>
19-
<PackageVersion>0.1.3</PackageVersion>
20-
<AssemblyVersion>0.1.3.0</AssemblyVersion>
21-
<FileVersion>0.1.3.0</FileVersion>
19+
<PackageVersion>0.1.4</PackageVersion>
20+
<AssemblyVersion>0.1.4.0</AssemblyVersion>
21+
<FileVersion>0.1.4.0</FileVersion>
2222
</PropertyGroup>
2323

2424
<PropertyGroup>

‎test/RxSharp.Tests/Extras/FromEventBufferedExtrasTests.cs‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,26 @@ public void ShouldDeliverToEveryIndependentSubscriber()
6767
Assert.That(second, Is.EqualTo(new[] { "shared-target" }));
6868
}
6969

70+
[Test]
71+
public void ShouldBufferEveryPreSubscribeValueByDefault()
72+
{
73+
// Locks in the default: null defers to ReplaySubject's own default (unbounded), not a small opinionated
74+
// number - a matching payload must never be evicted by a later non-matching one before subscription.
75+
var manager = new FakeTargetManager();
76+
using var source = RxExtensions.FromEventBuffered<string>(
77+
h => manager.TargetCreated += h,
78+
h => manager.TargetCreated -= h);
79+
80+
manager.RaiseTargetCreated("one");
81+
manager.RaiseTargetCreated("two");
82+
manager.RaiseTargetCreated("three");
83+
84+
var received = new List<string>();
85+
source.AsObservable().Subscribe(received.Add);
86+
87+
Assert.That(received, Is.EqualTo(new[] { "one", "two", "three" }));
88+
}
89+
7090
[Test]
7191
public void ShouldOnlyReplayUpToTheRequestedBufferSize()
7292
{

0 commit comments

Comments
 (0)