Skip to content

Commit 5880cf4

Browse files
committed
Optimize operations on resolved promises
1 parent 93dab19 commit 5880cf4

File tree

2 files changed

+123
-2
lines changed

2 files changed

+123
-2
lines changed

src/Promise.cs

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,9 @@ public void Done(Action<PromisedT> onResolved)
512512
/// </summary>
513513
public void Done()
514514
{
515+
if (CurState == PromiseState.Resolved)
516+
return;
517+
515518
Catch(ex =>
516519
Promise.PropagateUnhandledException(this, ex)
517520
);
@@ -531,6 +534,11 @@ public IPromise<PromisedT> WithName(string name)
531534
/// </summary>
532535
public IPromise Catch(Action<Exception> onRejected)
533536
{
537+
if (CurState == PromiseState.Resolved)
538+
{
539+
return Promise.Resolved();
540+
}
541+
534542
var resultPromise = new Promise();
535543
resultPromise.WithName(Name);
536544

@@ -560,6 +568,11 @@ public IPromise Catch(Action<Exception> onRejected)
560568
/// </summary>
561569
public IPromise<PromisedT> Catch(Func<Exception, PromisedT> onRejected)
562570
{
571+
if (CurState == PromiseState.Resolved)
572+
{
573+
return this;
574+
}
575+
563576
var resultPromise = new Promise<PromisedT>();
564577
resultPromise.WithName(Name);
565578

@@ -647,9 +660,21 @@ public IPromise<ConvertedT> Then<ConvertedT>(
647660
Action<float> onProgress
648661
)
649662
{
663+
if (CurState == PromiseState.Resolved)
664+
{
665+
try
666+
{
667+
return onResolved(resolveValue);
668+
}
669+
catch (Exception ex)
670+
{
671+
return Promise<ConvertedT>.Rejected(ex);
672+
}
673+
}
674+
650675
// This version of the function must supply an onResolved.
651676
// Otherwise there is now way to get the converted value to pass to the resulting promise.
652-
// Argument.NotNull(() => onResolved);
677+
// Argument.NotNull(() => onResolved);
653678

654679
var resultPromise = new Promise<ConvertedT>();
655680
resultPromise.WithName(Name);
@@ -702,6 +727,18 @@ Action<float> onProgress
702727
/// </summary>
703728
public IPromise Then(Func<PromisedT, IPromise> onResolved, Action<Exception> onRejected, Action<float> onProgress)
704729
{
730+
if (CurState == PromiseState.Resolved)
731+
{
732+
try
733+
{
734+
return onResolved(resolveValue);
735+
}
736+
catch (Exception ex)
737+
{
738+
return Promise.Rejected(ex);
739+
}
740+
}
741+
705742
var resultPromise = new Promise();
706743
resultPromise.WithName(Name);
707744

@@ -750,6 +787,19 @@ public IPromise Then(Func<PromisedT, IPromise> onResolved, Action<Exception> onR
750787
/// </summary>
751788
public IPromise Then(Action<PromisedT> onResolved, Action<Exception> onRejected, Action<float> onProgress)
752789
{
790+
if (CurState == PromiseState.Resolved)
791+
{
792+
try
793+
{
794+
onResolved(resolveValue);
795+
return Promise.Resolved();
796+
}
797+
catch (Exception ex)
798+
{
799+
return Promise.Rejected(ex);
800+
}
801+
}
802+
753803
var resultPromise = new Promise();
754804
resultPromise.WithName(Name);
755805

@@ -1076,6 +1126,19 @@ public static IPromise<PromisedT> Rejected(Exception ex)
10761126

10771127
public IPromise<PromisedT> Finally(Action onComplete)
10781128
{
1129+
if (CurState == PromiseState.Resolved)
1130+
{
1131+
try
1132+
{
1133+
onComplete();
1134+
return this;
1135+
}
1136+
catch (Exception ex)
1137+
{
1138+
return Rejected(ex);
1139+
}
1140+
}
1141+
10791142
var promise = new Promise<PromisedT>();
10801143
promise.WithName(Name);
10811144

src/Promise_NonGeneric.cs

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,9 @@ public void Done(Action onResolved)
632632
/// </summary>
633633
public void Done()
634634
{
635+
if (CurState == PromiseState.Resolved)
636+
return;
637+
635638
Catch(ex => PropagateUnhandledException(this, ex));
636639
}
637640

@@ -651,6 +654,11 @@ public IPromise Catch(Action<Exception> onRejected)
651654
{
652655
// Argument.NotNull(() => onRejected);
653656

657+
if (CurState == PromiseState.Resolved)
658+
{
659+
return this;
660+
}
661+
654662
var resultPromise = new Promise();
655663
resultPromise.WithName(Name);
656664

@@ -734,9 +742,21 @@ public IPromise<ConvertedT> Then<ConvertedT>(
734742
Func<Exception, IPromise<ConvertedT>> onRejected,
735743
Action<float> onProgress)
736744
{
745+
if (CurState == PromiseState.Resolved)
746+
{
747+
try
748+
{
749+
return onResolved();
750+
}
751+
catch (Exception ex)
752+
{
753+
return Promise<ConvertedT>.Rejected(ex);
754+
}
755+
}
756+
737757
// This version of the function must supply an onResolved.
738758
// Otherwise there is now way to get the converted value to pass to the resulting promise.
739-
// Argument.NotNull(() => onResolved);
759+
// Argument.NotNull(() => onResolved);
740760

741761
var resultPromise = new Promise<ConvertedT>();
742762
resultPromise.WithName(Name);
@@ -789,6 +809,18 @@ public IPromise<ConvertedT> Then<ConvertedT>(
789809
/// </summary>
790810
public IPromise Then(Func<IPromise> onResolved, Action<Exception> onRejected, Action<float> onProgress)
791811
{
812+
if (CurState == PromiseState.Resolved)
813+
{
814+
try
815+
{
816+
return onResolved();
817+
}
818+
catch (Exception ex)
819+
{
820+
return Rejected(ex);
821+
}
822+
}
823+
792824
var resultPromise = new Promise();
793825
resultPromise.WithName(Name);
794826

@@ -838,6 +870,19 @@ public IPromise Then(Func<IPromise> onResolved, Action<Exception> onRejected, Ac
838870
/// </summary>
839871
public IPromise Then(Action onResolved, Action<Exception> onRejected, Action<float> onProgress)
840872
{
873+
if (CurState == PromiseState.Resolved)
874+
{
875+
try
876+
{
877+
onResolved();
878+
return this;
879+
}
880+
catch (Exception ex)
881+
{
882+
return Rejected(ex);
883+
}
884+
}
885+
841886
var resultPromise = new Promise();
842887
resultPromise.WithName(Name);
843888

@@ -1159,6 +1204,19 @@ public static IPromise Rejected(Exception ex)
11591204

11601205
public IPromise Finally(Action onComplete)
11611206
{
1207+
if (CurState == PromiseState.Resolved)
1208+
{
1209+
try
1210+
{
1211+
onComplete();
1212+
return this;
1213+
}
1214+
catch (Exception ex)
1215+
{
1216+
return Rejected(ex);
1217+
}
1218+
}
1219+
11621220
var promise = new Promise();
11631221
promise.WithName(Name);
11641222

0 commit comments

Comments
 (0)