Method ListenOnce
- Namespace
- SodaFlow
- Assembly
- SodaFlow.dll
ListenOnce<T>(Stream<T>, Action<T>)
Handle the first event on this stream and then automatically unregister, without keeping the stream alive.
public static IWeakListener ListenOnce<T>(this Stream<T> s, Action<T> handler)
Parameters
sStream<T>The stream.
handlerAction<T>The handler to run with the first value this stream fires.
Returns
- IWeakListener
An IWeakListener. A call to its Unlisten() stops the listener before that first event arrives.
Type Parameters
TThe type of the stream.
Remarks
This does not root the stream, and the listener from this call is the only thing that keeps the handler in memory. Keep it until that first event arrives. Code that discards it compiles, and the handler then runs or does not run, which is dependent on the moment the GC collects. Use ListenOnceStrong<T>(Stream<T>, Action<T>) where the caller does not keep the listener.