Skip to content

Commit ee5daac

Browse files
committed
A few minor optimizations
1 parent 5880cf4 commit ee5daac

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

src/Promise.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,12 @@ public Promise(Action<Action<PromisedT>, Action<Exception>> resolver)
295295
}
296296
}
297297

298+
private Promise(PromiseState initialState)
299+
{
300+
CurState = initialState;
301+
id = Promise.NextId();
302+
}
303+
298304
/// <summary>
299305
/// Add a rejection handler for this promise.
300306
/// </summary>
@@ -1107,8 +1113,8 @@ public static IPromise<PromisedT> Race(IEnumerable<IPromise<PromisedT>> promises
11071113
/// </summary>
11081114
public static IPromise<PromisedT> Resolved(PromisedT promisedValue)
11091115
{
1110-
var promise = new Promise<PromisedT>();
1111-
promise.Resolve(promisedValue);
1116+
var promise = new Promise<PromisedT>(PromiseState.Resolved);
1117+
promise.resolveValue = promisedValue;
11121118
return promise;
11131119
}
11141120

@@ -1119,8 +1125,8 @@ public static IPromise<PromisedT> Rejected(Exception ex)
11191125
{
11201126
// Argument.NotNull(() => ex);
11211127

1122-
var promise = new Promise<PromisedT>();
1123-
promise.Reject(ex);
1128+
var promise = new Promise<PromisedT>(PromiseState.Rejected);
1129+
promise.rejectionException = ex;
11241130
return promise;
11251131
}
11261132

@@ -1183,7 +1189,7 @@ public IPromise<ConvertedT> ContinueWith<ConvertedT>(Func<IPromise<ConvertedT>>
11831189

11841190
public IPromise<PromisedT> Progress(Action<float> onProgress)
11851191
{
1186-
if (onProgress != null)
1192+
if (CurState == PromiseState.Pending && onProgress != null)
11871193
{
11881194
ProgressHandlers(this, onProgress);
11891195
}

src/Promise_NonGeneric.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,12 @@ public Promise(Action<Action, Action<Exception>> resolver)
369369
}
370370
}
371371

372+
private Promise(PromiseState initialState)
373+
{
374+
CurState = initialState;
375+
id = NextId();
376+
}
377+
372378
/// <summary>
373379
/// Increments the ID counter and gives us the ID for the next promise.
374380
/// </summary>
@@ -1178,16 +1184,10 @@ public static IPromise Race(IEnumerable<IPromise> promises)
11781184
/// <summary>
11791185
/// Convert a simple value directly into a resolved promise.
11801186
/// </summary>
1181-
private static IPromise resolvedPromised;
1187+
private static IPromise resolvedPromise = new Promise(PromiseState.Resolved);
11821188
public static IPromise Resolved()
11831189
{
1184-
if (resolvedPromised == null)
1185-
{
1186-
var promise = new Promise();
1187-
promise.Resolve();
1188-
resolvedPromised = promise;
1189-
}
1190-
return resolvedPromised;
1190+
return resolvedPromise;
11911191
}
11921192

11931193
/// <summary>
@@ -1197,8 +1197,8 @@ public static IPromise Rejected(Exception ex)
11971197
{
11981198
// Argument.NotNull(() => ex);
11991199

1200-
var promise = new Promise();
1201-
promise.Reject(ex);
1200+
var promise = new Promise(PromiseState.Rejected);
1201+
promise.rejectionException = ex;
12021202
return promise;
12031203
}
12041204

@@ -1257,7 +1257,7 @@ public IPromise<ConvertedT> ContinueWith<ConvertedT>(Func<IPromise<ConvertedT>>
12571257

12581258
public IPromise Progress(Action<float> onProgress)
12591259
{
1260-
if (onProgress != null)
1260+
if (CurState == PromiseState.Pending && onProgress != null)
12611261
{
12621262
ProgressHandlers(this, onProgress);
12631263
}

0 commit comments

Comments
 (0)