Best way to wrap IAsyncEnumerable

I did some quick test to import the lib whisper.net into vl. Like often in these days the lib makes a havy use of Tasks. I know the common Threads about Tasks and await:

But i could not find anything about the IAsyncEnumerable Interface which is used in the lib for example here.

Basically i want to do something like this.

public static async Task Process(WhisperProcessor processor, String FilePath)
{
    using var fileStream = File.OpenRead(FilePath);
    await foreach (SegmentData result in processor.ProcessAsync(fileStream))
    {
      
    }
}

But then i just get information, when the Task finished, but i do not get how to access/ return the SegementData from the await foreach. Any ideas on this?

edit:
Creating an public Event an then just Invoke this Event seems to work, but is that the right way?

public static event EventHandler<SegmentData> OnReceivedSegementData;
public static async Task Process(WhisperProcessor processor, String FilePath)
{
    using var fileStream = File.OpenRead(FilePath);
    await foreach (SegmentData result in processor.ProcessAsync(fileStream))
    {

        OnReceivedSegementData?.Invoke(new object(), result);
    }
}

1 Like

can you do something like this?

using System.Reactive.Linq;

public static IObservable<SegmentData> Process(FileStream fileStream) 
{
  return processor.ProcessAsync(fileStream)..ToObservable();
}

as far as docs say, reactive extension should support ToObservable(this IAsyncEnumerable)

handling lifetime of the file is another matter though.

1 Like

@woei thanks for the hint. This seems also to work.

public static IObservable<SegmentData> Process(WhisperProcessor processor, Stream file)
{
    return processor.ProcessAsync(file).ToObservable();
}