public static IObservable <T> AttachWriter <T>(this IObservable <T> data, string path, IDataSerializationFactory factory, Type targetDataType, int bufferSize) { return(Observable.Create <T>(o => { var streamWriter = new StreamWriter(path, true, new UTF8Encoding(false)); var dataWriter = factory.CreateWriterForType(streamWriter, targetDataType); var bufferedWriter = new BufferedDataWriter(dataWriter, bufferSize); return new CompositeDisposable ( data.Do(d => bufferedWriter.Write(d)) .Finally(bufferedWriter.Close) .Subscribe(o), dataWriter, streamWriter ); })); // removed this implementation due to exceptions being raised that the streamWriter was disposed sooner than the dataWriter. //return Observable.Using //( // () => new StreamWriter(path, true, new UTF8Encoding(false)), // streamWriter => Observable.Using // ( // () => factory.CreateWriterForType(streamWriter, targetDataType), // dataWriter => Observable.Using // ( // () => new BufferedDataWriter(dataWriter, bufferSize), // bufferedWriter => data.Do(d => bufferedWriter.Write(d)) // .Finally(bufferedWriter.Close) // ) // ) //); }
public IDisposable Connect(IObservableDevice device) { var writer = new InMemoryDataBufferWriter(device.DeviceType, _buffer, device.DataType); var bufferedWriter = new BufferedDataWriter(writer, TimeSpan.FromMilliseconds(_configuration.BufferSize)); return(new CompositeDisposable ( device.Data.Do(d => bufferedWriter.Write(d)) .Finally(bufferedWriter.Close) .Subscribe(), bufferedWriter, writer )); //return Observable.Using //( // () => new InMemoryDataBufferWriter(device.DeviceType, _buffer, device.RecordingDataType), // writer => Observable.Using // ( // () => new BufferedDataWriter(writer, TimeSpan.FromMilliseconds(_configuration.BufferSize)), // bufferedWriter => device.Data.Do(d => bufferedWriter.Write(d)) // .Finally(bufferedWriter.Close) // ) //).Subscribe(); }