Skip to content

Commit

Permalink
Re-added deferred null checks and other cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
timcassell committed Oct 18, 2024
1 parent 8e2632f commit 946c1c6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 44 deletions.
56 changes: 21 additions & 35 deletions Package/Core/Promises/Deferred.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public bool IsValid
public bool IsValidAndPending
{
[MethodImpl(Internal.InlineOption)]
get => Internal.DeferredPromiseHelper.GetIsValidAndPending(_ref, _deferredId);
get => _ref?.DeferredId == _deferredId;
}

/// <summary>
Expand Down Expand Up @@ -119,10 +119,12 @@ public Promise<T>.Deferred AsDeferred<T>()
/// <exception cref="InvalidOperationException"/>
public void Reject<TReject>(TReject reason)
{
if (!TryReject(reason))
var _this = _ref;
if (_this?.TryIncrementDeferredId(_deferredId) != true)
{
throw new InvalidOperationException("DeferredBase.Reject: instance is not valid or already complete.", Internal.GetFormattedStacktrace(1));
}
_this.RejectDirect(Internal.CreateRejectContainer(reason, 1, null, _this));
}

/// <summary>
Expand All @@ -132,7 +134,7 @@ public void Reject<TReject>(TReject reason)
public bool TryReject<TReject>(TReject reason)
{
var _this = _ref;
if (Internal.DeferredPromiseHelper.TryIncrementDeferredId(_this, _deferredId))
if (_this?.TryIncrementDeferredId(_deferredId) == true)
{
_this.RejectDirect(Internal.CreateRejectContainer(reason, 1, null, _this));
return true;
Expand All @@ -148,11 +150,7 @@ public bool TryReject<TReject>(TReject reason)
public void Cancel()
{
var _this = _ref;
if (
#if PROMISE_DEBUG // We can skip the null check in RELEASE, the runtime will just throw NullReferenceException if it's null.
_this == null ||
#endif
!_this.TryIncrementDeferredId(_deferredId))
if (_this?.TryIncrementDeferredId(_deferredId) != true)
{
throw new InvalidOperationException("DeferredBase.Cancel: instance is not valid or already complete.", Internal.GetFormattedStacktrace(1));
}
Expand All @@ -167,7 +165,7 @@ public void Cancel()
public bool TryCancel()
{
var _this = _ref;
if (Internal.DeferredPromiseHelper.TryIncrementDeferredId(_this, _deferredId))
if (_this?.TryIncrementDeferredId(_deferredId) == true)
{
_this.CancelDirect();
return true;
Expand Down Expand Up @@ -277,11 +275,7 @@ public static Deferred New()
public void Resolve()
{
var _this = _ref;
if (
#if PROMISE_DEBUG // We can skip the null check in RELEASE, the runtime will just throw NullReferenceException if it's null.
_this == null ||
#endif
!_this.TryIncrementDeferredId(_deferredId))
if (_this?.TryIncrementDeferredId(_deferredId) != true)
{
throw new InvalidOperationException("Deferred.Resolve: instance is not valid or already complete.", Internal.GetFormattedStacktrace(1));
}
Expand All @@ -302,10 +296,12 @@ public bool TryResolve()
/// <exception cref="InvalidOperationException"/>
public void Reject<TReject>(TReject reason)
{
if (!TryReject(reason))
var _this = _ref;
if (_this?.TryIncrementDeferredId(_deferredId) != true)
{
throw new InvalidOperationException("Deferred.Reject: instance is not valid or already complete.", Internal.GetFormattedStacktrace(1));
}
_this.RejectDirect(Internal.CreateRejectContainer(reason, 1, null, _this));
}

/// <summary>
Expand All @@ -315,7 +311,7 @@ public void Reject<TReject>(TReject reason)
public bool TryReject<TReject>(TReject reason)
{
var _this = _ref;
if (_this != null && _this.TryIncrementDeferredId(_deferredId))
if (_this?.TryIncrementDeferredId(_deferredId) == true)
{
_this.RejectDirect(Internal.CreateRejectContainer(reason, 1, null, _this));
return true;
Expand All @@ -331,11 +327,7 @@ public bool TryReject<TReject>(TReject reason)
public void Cancel()
{
var _this = _ref;
if (
#if PROMISE_DEBUG // We can skip the null check in RELEASE, the runtime will just throw NullReferenceException if it's null.
_this == null ||
#endif
!_this.TryIncrementDeferredId(_deferredId))
if (_this?.TryIncrementDeferredId(_deferredId) != true)
{
throw new InvalidOperationException("Deferred.Cancel: instance is not valid or already complete.", Internal.GetFormattedStacktrace(1));
}
Expand All @@ -350,7 +342,7 @@ public void Cancel()
public bool TryCancel()
{
var _this = _ref;
if (_this != null && _this.TryIncrementDeferredId(_deferredId))
if (_this?.TryIncrementDeferredId(_deferredId) == true)
{
_this.CancelDirect();
return true;
Expand Down Expand Up @@ -477,11 +469,7 @@ public static Deferred New()
public void Resolve(T value)
{
var _this = _ref;
if (
#if PROMISE_DEBUG // We can skip the null check in RELEASE, the runtime will just throw NullReferenceException if it's null.
_this == null ||
#endif
!_this.TryIncrementDeferredId(_deferredId))
if (_this?.TryIncrementDeferredId(_deferredId) != true)
{
throw new InvalidOperationException("Deferred.Resolve: instance is not valid or already complete.", Internal.GetFormattedStacktrace(1));
}
Expand All @@ -502,10 +490,12 @@ public bool TryResolve(T value)
/// <exception cref="InvalidOperationException"/>
public void Reject<TReject>(TReject reason)
{
if (!TryReject(reason))
var _this = _ref;
if (_this?.TryIncrementDeferredId(_deferredId) != true)
{
throw new InvalidOperationException("Deferred.Reject: instance is not valid or already complete.", Internal.GetFormattedStacktrace(1));
}
_this.RejectDirect(Internal.CreateRejectContainer(reason, 1, null, _this));
}

/// <summary>
Expand All @@ -515,7 +505,7 @@ public void Reject<TReject>(TReject reason)
public bool TryReject<TReject>(TReject reason)
{
var _this = _ref;
if (_this != null && _this.TryIncrementDeferredId(_deferredId))
if (_this?.TryIncrementDeferredId(_deferredId) == true)
{
_this.RejectDirect(Internal.CreateRejectContainer(reason, 1, null, _this));
return true;
Expand All @@ -531,11 +521,7 @@ public bool TryReject<TReject>(TReject reason)
public void Cancel()
{
var _this = _ref;
if (
#if PROMISE_DEBUG // We can skip the null check in RELEASE, the runtime will just throw NullReferenceException if it's null.
_this == null ||
#endif
!_this.TryIncrementDeferredId(_deferredId))
if (_this?.TryIncrementDeferredId(_deferredId) != true)
{
throw new InvalidOperationException("Deferred.Cancel: instance is not valid or already complete.", Internal.GetFormattedStacktrace(1));
}
Expand All @@ -550,7 +536,7 @@ public void Cancel()
public bool TryCancel()
{
var _this = _ref;
if (_this != null && _this.TryIncrementDeferredId(_deferredId))
if (_this?.TryIncrementDeferredId(_deferredId) == true)
{
_this.CancelDirect();
return true;
Expand Down
9 changes: 0 additions & 9 deletions Package/Core/Promises/Internal/DeferredInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,6 @@ internal interface IDeferredPromise : ITraceable
void CancelDirect();
}

internal static class DeferredPromiseHelper
{
internal static bool GetIsValidAndPending(IDeferredPromise _this, int deferredId)
=> _this?.DeferredId == deferredId;

internal static bool TryIncrementDeferredId(IDeferredPromise _this, int deferredId)
=> _this?.TryIncrementDeferredId(deferredId) == true;
}

partial class PromiseRefBase
{
#if !PROTO_PROMISE_DEVELOPER_MODE
Expand Down

0 comments on commit 946c1c6

Please sign in to comment.