static IEnumerator FetchAssetBundle(WWW www, UtyRx.IObserver <AssetBundle> observer, UtyRx.IProgress <float> reportProgress, CancellationToken cancel) { using (www) { while (!www.isDone && !cancel.IsCancellationRequested) { if (reportProgress != null) { try { reportProgress.Report(www.progress); } catch (Exception ex) { observer.OnError(ex); yield break; } } yield return(null); } if (cancel.IsCancellationRequested) { yield break; } if (reportProgress != null) { try { reportProgress.Report(www.progress); } catch (Exception ex) { observer.OnError(ex); yield break; } } if (!string.IsNullOrEmpty(www.error)) { observer.OnError(new WWWErrorException(www)); } else { observer.OnNext(www.assetBundle); observer.OnCompleted(); } } }
static IEnumerator WrapEnumerator(IEnumerator enumerator, UtyRx.IObserver <Unit> observer, CancellationToken cancellationToken, bool publishEveryYield) { var hasNext = default(bool); var raisedError = false; do { try { hasNext = enumerator.MoveNext(); } catch (Exception ex) { try { raisedError = true; observer.OnError(ex); } finally { var d = enumerator as IDisposable; if (d != null) { d.Dispose(); } } yield break; } if (hasNext && publishEveryYield) { try { observer.OnNext(Unit.Default); } catch { var d = enumerator as IDisposable; if (d != null) { d.Dispose(); } throw; } } if (hasNext) { yield return(enumerator.Current); // yield inner YieldInstruction } } while (hasNext && !cancellationToken.IsCancellationRequested); try { if (!raisedError && !cancellationToken.IsCancellationRequested) { observer.OnNext(Unit.Default); // last one observer.OnCompleted(); } } finally { var d = enumerator as IDisposable; if (d != null) { d.Dispose(); } } }
static IEnumerator WrapEnumeratorYieldValue <T>(IEnumerator enumerator, UtyRx.IObserver <T> observer, CancellationToken cancellationToken, bool nullAsNextUpdate) { var hasNext = default(bool); var current = default(object); var raisedError = false; do { try { hasNext = enumerator.MoveNext(); if (hasNext) { current = enumerator.Current; } } catch (Exception ex) { try { raisedError = true; observer.OnError(ex); } finally { var d = enumerator as IDisposable; if (d != null) { d.Dispose(); } } yield break; } if (hasNext) { if (current != null && YieldInstructionTypes.Contains(current.GetType())) { yield return(current); } else if (current == null && nullAsNextUpdate) { yield return(null); } else { try { observer.OnNext((T)current); } catch { var d = enumerator as IDisposable; if (d != null) { d.Dispose(); } throw; } } } } while (hasNext && !cancellationToken.IsCancellationRequested); try { if (!raisedError && !cancellationToken.IsCancellationRequested) { observer.OnCompleted(); } } finally { var d = enumerator as IDisposable; if (d != null) { d.Dispose(); } } }