Skip to content

Commit

Permalink
Make Promise.Sequence report progress.
Browse files Browse the repository at this point in the history
  • Loading branch information
alonsohki committed Feb 18, 2018
1 parent 0c9908a commit 9f49b4b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
33 changes: 30 additions & 3 deletions Promise_NonGeneric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1001,10 +1001,37 @@ public static IPromise Sequence(params Func<IPromise>[] fns)
/// </summary>
public static IPromise Sequence(IEnumerable<Func<IPromise>> fns)
{
return fns.Aggregate(
var promise = new Promise();

int count = 0;

fns.Aggregate(
Resolved(),
(prevPromise, fn) => prevPromise.Then(fn)
);
(prevPromise, fn) =>
{
int itemSequence = count;
++count;

return prevPromise
.Then(() =>
{
var sliceLength = 1f / count;
promise.ReportProgress(sliceLength * itemSequence);
return fn();
})
.Progress(v =>
{
var sliceLength = 1f / count;
promise.ReportProgress(sliceLength * (v + itemSequence));
})
;
}
)
.Then(() => promise.Resolve())
.Catch(promise.Reject)
.Done();

return promise;
}

/// <summary>
Expand Down
36 changes: 36 additions & 0 deletions Tests/Promise_NonGeneric_ProgressTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,5 +213,41 @@ public void all_progress_with_resolved()

Assert.Equal(1, reportedCount);
}

[Fact]
public void sequence_reports_progress()
{
var promiseA = new Promise();
var promiseB = new Promise();
var promiseC = Promise.Resolved();
var promiseD = new Promise();
int currentReport = 0;
var expectedProgress = new[] { 0.125f, 0.25f, 0.25f, 0.3125f, 0.375f, 0.4375f, 0.5f, 0.75f, 0.875f, 1f };

Promise
.Sequence(() => promiseA, () => promiseB, () => promiseC, () => promiseD)
.Progress(v =>
{
Assert.Equal(expectedProgress[currentReport], v);
++currentReport;
})
.Done()
;

promiseA.ReportProgress(0.5f);
promiseA.ReportProgress(1f);
promiseA.Resolve();

promiseB.ReportProgress(0.25f);
promiseB.ReportProgress(0.5f);
promiseB.ReportProgress(0.75f);
promiseB.Resolve();

promiseD.ReportProgress(0.5f);
promiseD.ReportProgress(1f);
promiseD.Resolve();

Assert.Equal(expectedProgress.Length, currentReport);
}
}
}

0 comments on commit 9f49b4b

Please sign in to comment.