Skip to content

Commit a2f3ac0

Browse files
authored
Merge pull request step-up-labs#212 from cabauman/colt/pull-async-fix
PullAsync fix
2 parents 37d4d67 + e871237 commit a2f3ac0

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

src/Firebase/Extensions/ObservableExtensions.cs

+16-5
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,39 @@ public static class ObservableExtensions
1111
/// <param name="source">The source observable.</param>
1212
/// <param name="dueTime">How long to wait between attempts.</param>
1313
/// <param name="retryOnError">A predicate determining for which exceptions to retry. Defaults to all</param>
14+
/// <param name="retryCount">The number of attempts of running the source observable before failing.</param>
1415
/// <returns>
1516
/// A cold observable which retries (re-subscribes to) the source observable on error up to the
1617
/// specified number of times or until it successfully terminates.
1718
/// </returns>
1819
public static IObservable<T> RetryAfterDelay<T, TException>(
1920
this IObservable<T> source,
2021
TimeSpan dueTime,
21-
Func<TException, bool> retryOnError)
22+
Func<TException, bool> retryOnError,
23+
int? retryCount = null)
2224
where TException: Exception
2325
{
2426
int attempt = 0;
2527

26-
return Observable.Defer(() =>
28+
var pipeline = Observable.Defer(() =>
2729
{
2830
return ((++attempt == 1) ? source : source.DelaySubscription(dueTime))
2931
.Select(item => new Tuple<bool, T, Exception>(true, item, null))
3032
.Catch<Tuple<bool, T, Exception>, TException>(e => retryOnError(e)
3133
? Observable.Throw<Tuple<bool, T, Exception>>(e)
3234
: Observable.Return(new Tuple<bool, T, Exception>(false, default(T), e)));
33-
})
34-
.Retry()
35-
.SelectMany(t => t.Item1
35+
});
36+
37+
if (retryCount.HasValue)
38+
{
39+
pipeline = pipeline.Retry(retryCount.Value);
40+
}
41+
else
42+
{
43+
pipeline = pipeline.Retry();
44+
}
45+
46+
return pipeline.SelectMany(t => t.Item1
3647
? Observable.Return(t.Item2)
3748
: Observable.Throw<T>(t.Item3));
3849
}

src/Firebase/Offline/RealtimeDatabase.cs

+8-5
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,17 @@ public void Pull(string key, int priority = 1)
149149
/// <summary>
150150
/// Fetches everything from the remote database.
151151
/// </summary>
152-
public async Task PullAsync()
152+
/// <param name="retryCount">
153+
/// The number of attempts of running the source observable before failing.
154+
/// null means infinite retries, 0 means it won't execute at all, and 1 means it will only try once.
155+
/// </param>
156+
public async Task PullAsync(int? retryCount = null)
153157
{
154-
var existingEntries = await this.childQuery
155-
.OnceAsync<T>()
156-
.ToObservable()
158+
var existingEntries = await Observable.Defer(() => this.childQuery.OnceAsync<T>().ToObservable())
157159
.RetryAfterDelay<IReadOnlyCollection<FirebaseObject<T>>, FirebaseException>(
158160
this.childQuery.Client.Options.SyncPeriod,
159-
ex => ex.StatusCode == System.Net.HttpStatusCode.OK) // OK implies the request couldn't complete due to network error.
161+
ex => ex.StatusCode == System.Net.HttpStatusCode.OK, // OK implies the request couldn't complete due to network error.
162+
retryCount)
160163
.Select(e => this.ResetDatabaseFromInitial(e, false))
161164
.SelectMany(e => e)
162165
.Do(e =>

0 commit comments

Comments
 (0)