Skip to content

Commit 420830d

Browse files
authored
feat(advanced): add the SwitchMap fused sink (#163)
- Add SwitchMapSignal, which projects each source value to an inner observable and mirrors the latest one in a single sink, and the SwitchMap operator over it. - Every source value switches, null included, so a caller that projects null onto its own inner observable detaches the previous one. - Reimplement SwitchSelect on the same sink, which skips nulls instead of switching on them. Its switching bookkeeping now lives in one place.
1 parent 8dbb7e6 commit 420830d

39 files changed

Lines changed: 670 additions & 217 deletions

File tree

Lines changed: 264 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
1+
// Copyright (c) 2019-2026 ReactiveUI Association Incorporated. All rights reserved.
2+
// ReactiveUI Association Incorporated licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for full license information.
4+
5+
#if REACTIVE_SHIM
6+
namespace ReactiveUI.Primitives.Reactive.Advanced;
7+
#else
8+
namespace ReactiveUI.Primitives.Advanced;
9+
#endif
10+
11+
/// <summary>Projects each source value to an inner observable and mirrors only the latest one.</summary>
12+
/// <typeparam name="TSource">The source element type.</typeparam>
13+
/// <typeparam name="TResult">The element type of the projected inner observables.</typeparam>
14+
/// <remarks>
15+
/// Fuses the projection into the switch: one object and one observer hop, where a projection followed by a
16+
/// separate switch costs two of each and an intermediate sequence of observables.
17+
/// </remarks>
18+
public sealed class SwitchMapSignal<TSource, TResult> : IObservable<TResult>
19+
{
20+
/// <summary>The source whose values are projected to inner observables.</summary>
21+
private readonly IObservable<TSource> _source;
22+
23+
/// <summary>Projects a source value to the inner observable to switch to.</summary>
24+
private readonly Func<TSource, IObservable<TResult>> _selector;
25+
26+
/// <summary>Whether a null source value is skipped rather than projected.</summary>
27+
private readonly bool _skipNullSources;
28+
29+
/// <summary>Initializes a new instance of the <see cref="SwitchMapSignal{TSource, TResult}"/> class.</summary>
30+
/// <param name="source">The source whose values are projected to inner observables.</param>
31+
/// <param name="selector">Projects a source value to the inner observable to switch to.</param>
32+
public SwitchMapSignal(IObservable<TSource> source, Func<TSource, IObservable<TResult>> selector)
33+
: this(source, selector, skipNullSources: false)
34+
{
35+
}
36+
37+
/// <summary>Initializes a new instance of the <see cref="SwitchMapSignal{TSource, TResult}"/> class.</summary>
38+
/// <param name="source">The source whose values are projected to inner observables.</param>
39+
/// <param name="selector">Projects a source value to the inner observable to switch to.</param>
40+
/// <param name="skipNullSources">
41+
/// Whether a null source value leaves the active inner subscription in place rather than switching. A caller
42+
/// that projects null onto its own inner observable switches on it instead, which detaches the previous one.
43+
/// </param>
44+
internal SwitchMapSignal(
45+
IObservable<TSource> source,
46+
Func<TSource, IObservable<TResult>> selector,
47+
bool skipNullSources)
48+
{
49+
ArgumentExceptionHelper.ThrowIfNull(source);
50+
ArgumentExceptionHelper.ThrowIfNull(selector);
51+
52+
_source = source;
53+
_selector = selector;
54+
_skipNullSources = skipNullSources;
55+
}
56+
57+
/// <inheritdoc/>
58+
public IDisposable Subscribe(IObserver<TResult> observer)
59+
{
60+
ArgumentExceptionHelper.ThrowIfNull(observer);
61+
62+
Sink sink = new(_selector, observer, _skipNullSources);
63+
sink.Run(_source);
64+
return sink;
65+
}
66+
67+
/// <summary>Subscribes to the source, switching the active inner subscription as values arrive.</summary>
68+
/// <param name="selector">Projects a source value to the inner observable to switch to.</param>
69+
/// <param name="downstream">The downstream observer that receives values from the latest inner observable.</param>
70+
/// <param name="skipNullSources">Whether a null source value is skipped rather than projected.</param>
71+
private sealed class Sink(
72+
Func<TSource, IObservable<TResult>> selector,
73+
IObserver<TResult> downstream,
74+
bool skipNullSources) : IObserver<TSource>, IDisposable
75+
{
76+
/// <summary>Guards the switching state so outer and inner notifications stay consistent.</summary>
77+
private readonly Lock _gate = new();
78+
79+
/// <summary>The outer (source) subscription.</summary>
80+
private readonly OnceDisposable _outer = new();
81+
82+
/// <summary>The active inner subscription; assigning a new value disposes the previous one.</summary>
83+
private readonly SwapDisposable _inner = new();
84+
85+
/// <summary>Generation id of the most recent inner observable; stale inner notifications are ignored.</summary>
86+
private ulong _latest;
87+
88+
/// <summary>Whether an inner subscription is currently active.</summary>
89+
private bool _hasInner;
90+
91+
/// <summary>Whether the outer source has completed.</summary>
92+
private bool _outerCompleted;
93+
94+
/// <summary>Whether this sink has been disposed.</summary>
95+
private bool _disposed;
96+
97+
/// <summary>Begins observing the source.</summary>
98+
/// <param name="source">The source observable.</param>
99+
public void Run(IObservable<TSource> source) => _outer.Disposable = source.Subscribe(this);
100+
101+
/// <inheritdoc/>
102+
public void OnNext(TSource value)
103+
{
104+
if (skipNullSources && value is null)
105+
{
106+
return;
107+
}
108+
109+
IObservable<TResult> inner;
110+
try
111+
{
112+
inner = selector(value);
113+
}
114+
catch (Exception ex)
115+
{
116+
OnError(ex);
117+
return;
118+
}
119+
120+
ulong id;
121+
lock (_gate)
122+
{
123+
if (_disposed)
124+
{
125+
return;
126+
}
127+
128+
id = ++_latest;
129+
_hasInner = true;
130+
}
131+
132+
_inner.Disposable = inner.Subscribe(new InnerWitness(this, id));
133+
}
134+
135+
/// <inheritdoc/>
136+
public void OnError(Exception error)
137+
{
138+
lock (_gate)
139+
{
140+
if (_disposed)
141+
{
142+
return;
143+
}
144+
}
145+
146+
downstream.OnError(error);
147+
Dispose();
148+
}
149+
150+
/// <inheritdoc/>
151+
public void OnCompleted()
152+
{
153+
bool complete;
154+
lock (_gate)
155+
{
156+
if (_disposed)
157+
{
158+
return;
159+
}
160+
161+
_outerCompleted = true;
162+
complete = !_hasInner;
163+
}
164+
165+
if (!complete)
166+
{
167+
return;
168+
}
169+
170+
downstream.OnCompleted();
171+
Dispose();
172+
}
173+
174+
/// <inheritdoc/>
175+
public void Dispose()
176+
{
177+
lock (_gate)
178+
{
179+
if (_disposed)
180+
{
181+
return;
182+
}
183+
184+
_disposed = true;
185+
}
186+
187+
_outer.Dispose();
188+
_inner.Dispose();
189+
}
190+
191+
/// <summary>Forwards an inner value to the downstream observer if it belongs to the active inner subscription.</summary>
192+
/// <param name="id">The generation id of the inner subscription that produced the value.</param>
193+
/// <param name="value">The value to forward.</param>
194+
private void InnerOnNext(ulong id, TResult value)
195+
{
196+
lock (_gate)
197+
{
198+
if (_disposed || id != _latest)
199+
{
200+
return;
201+
}
202+
}
203+
204+
downstream.OnNext(value);
205+
}
206+
207+
/// <summary>Forwards an inner error to the downstream observer if it belongs to the active inner subscription.</summary>
208+
/// <param name="id">The generation id of the inner subscription that errored.</param>
209+
/// <param name="error">The error to forward.</param>
210+
private void InnerOnError(ulong id, Exception error)
211+
{
212+
lock (_gate)
213+
{
214+
if (_disposed || id != _latest)
215+
{
216+
return;
217+
}
218+
}
219+
220+
downstream.OnError(error);
221+
Dispose();
222+
}
223+
224+
/// <summary>Clears the active inner subscription; completes downstream only if the outer has also completed.</summary>
225+
/// <param name="id">The generation id of the inner subscription that completed.</param>
226+
private void InnerOnCompleted(ulong id)
227+
{
228+
bool complete;
229+
lock (_gate)
230+
{
231+
if (_disposed || id != _latest)
232+
{
233+
return;
234+
}
235+
236+
_hasInner = false;
237+
complete = _outerCompleted;
238+
}
239+
240+
if (!complete)
241+
{
242+
return;
243+
}
244+
245+
downstream.OnCompleted();
246+
Dispose();
247+
}
248+
249+
/// <summary>Observes a single inner observable and routes its notifications through the parent sink.</summary>
250+
/// <param name="parent">The owning sink.</param>
251+
/// <param name="id">The generation id of this inner subscription.</param>
252+
private sealed class InnerWitness(Sink parent, ulong id) : IObserver<TResult>
253+
{
254+
/// <inheritdoc/>
255+
public void OnNext(TResult value) => parent.InnerOnNext(id, value);
256+
257+
/// <inheritdoc/>
258+
public void OnError(Exception error) => parent.InnerOnError(id, error);
259+
260+
/// <inheritdoc/>
261+
public void OnCompleted() => parent.InnerOnCompleted(id);
262+
}
263+
}
264+
}

0 commit comments

Comments
 (0)
⌂ 换网址