浏览代码

fix promise to be async only for "resolve".

/main
kg 6 年前
当前提交
99444935
共有 2 个文件被更改,包括 97 次插入78 次删除
  1. 82
      Runtime/promise/Promise.cs
  2. 93
      Runtime/promise/Promise_NonGeneric.cs

82
Runtime/promise/Promise.cs


/// Tracks the current state of the promise.
/// </summary>
public PromiseState CurState { get; private set; }
public bool IsSync { get; }
public Promise()
{
public Promise(bool isSync = false) {
this.IsSync = isSync;
this.CurState = PromiseState.Pending;
this.id = Promise.NextId();

}
}
public Promise(Action<Action<PromisedT>, Action<Exception>> resolver)
{
public Promise(Action<Action<PromisedT>, Action<Exception>> resolver, bool isSync = false) {
this.IsSync = isSync;
this.CurState = PromiseState.Pending;
this.id = Promise.NextId();

if (rejectHandlers != null)
{
rejectHandlers.Each(handler => InvokeHandler(handler.callback, handler.rejectable, ex));
} else {
Promise.PropagateUnhandledException(this, ex);
}
ClearHandlers();

/// <summary>
/// Reject the promise with an exception.
/// </summary>
public void Reject(Exception ex)
{
public void Reject(Exception ex) {
if (IsSync) {
RejectSync(ex);
} else {
Window.instance.run(() => RejectSync(ex));
}
}
public void RejectSync(Exception ex) {
if (CurState != PromiseState.Pending)
{
if (CurState != PromiseState.Pending) {
"Attempt to reject a promise that is already in state: " + CurState
+ ", a promise can only be rejected when it is still in state: "
"Attempt to reject a promise that is already in state: "
+ CurState
+ ", a promise can only be rejected when it is still in state: "
+ PromiseState.Pending
);
}

if (Promise.EnablePromiseTracking)
{
if (Promise.EnablePromiseTracking) {
Window.instance.scheduleMicrotask(() => {
InvokeRejectHandlers(ex);
});
InvokeRejectHandlers(ex);
public void Resolve(PromisedT value)
{
if (CurState != PromiseState.Pending)
{
public void Resolve(PromisedT value) {
if (IsSync) {
ResolveSync(value);
} else {
Window.instance.run(() => ResolveSync(value));
}
}
public void ResolveSync(PromisedT value) {
if (CurState != PromiseState.Pending) {
"Attempt to resolve a promise that is already in state: " + CurState
+ ", a promise can only be resolved when it is still in state: "
"Attempt to resolve a promise that is already in state: "
+ CurState
+ ", a promise can only be resolved when it is still in state: "
+ PromiseState.Pending
);
}

if (Promise.EnablePromiseTracking)
{
if (Promise.EnablePromiseTracking) {
Window.instance.scheduleMicrotask(() => {
InvokeResolveHandlers(value);
});
InvokeResolveHandlers(value);
}
/// <summary>

/// </summary>
public IPromise Catch(Action<Exception> onRejected)
{
var resultPromise = new Promise();
var resultPromise = new Promise(isSync: true);
resultPromise.WithName(Name);
Action<PromisedT> resolveHandler = _ => resultPromise.Resolve();

/// </summary>
public IPromise Then(Func<PromisedT, IPromise> onResolved, Action<Exception> onRejected, Action<float> onProgress)
{
var resultPromise = new Promise();
var resultPromise = new Promise(isSync: true);
resultPromise.WithName(Name);
Action<PromisedT> resolveHandler = v =>

/// </summary>
public IPromise Then(Action<PromisedT> onResolved, Action<Exception> onRejected, Action<float> onProgress)
{
var resultPromise = new Promise();
var resultPromise = new Promise(isSync: true);
resultPromise.WithName(Name);
Action<PromisedT> resolveHandler = v =>

private void ActionHandlers(IRejectable resultPromise, Action<PromisedT> resolveHandler, Action<Exception> rejectHandler)
{
if (CurState == PromiseState.Resolved) {
Window.instance.scheduleMicrotask(() => {
InvokeHandler(resolveHandler, resultPromise, resolveValue);
});
InvokeHandler(resolveHandler, resultPromise, resolveValue);
Window.instance.scheduleMicrotask(() => {
InvokeHandler(rejectHandler, resultPromise, rejectionException);
});
InvokeHandler(rejectHandler, resultPromise, rejectionException);
}
else {
AddResolveHandler(resolveHandler, resultPromise);

public IPromise ContinueWith(Func<IPromise> onComplete)
{
var promise = new Promise();
var promise = new Promise(isSync: true);
promise.WithName(Name);
this.Then(x => promise.Resolve());

public IPromise<ConvertedT> ContinueWith<ConvertedT>(Func<IPromise<ConvertedT>> onComplete)
{
var promise = new Promise();
var promise = new Promise(isSync: true);
promise.WithName(Name);
this.Then(x => promise.Resolve());

93
Runtime/promise/Promise_NonGeneric.cs


using System.Linq;
using RSG.Exceptions;
using Unity.UIWidgets.ui;
using UnityEngine;
namespace RSG
{

/// Tracks the current state of the promise.
/// </summary>
public PromiseState CurState { get; private set; }
public bool IsSync { get; }
public Promise()
{
public Promise(bool isSync = false) {
this.IsSync = isSync;
this.CurState = PromiseState.Pending;
this.id = NextId();
if (EnablePromiseTracking)

}
public Promise(Action<Action, Action<Exception>> resolver)
{
public Promise(Action<Action, Action<Exception>> resolver, bool isSync = false) {
this.IsSync = isSync;
this.CurState = PromiseState.Pending;
this.id = NextId();
if (EnablePromiseTracking)

if (rejectHandlers != null)
{
rejectHandlers.Each(handler => InvokeRejectHandler(handler.callback, handler.rejectable, ex));
} else {
PropagateUnhandledException(this, ex);
}
ClearHandlers();

/// <summary>
/// Reject the promise with an exception.
/// </summary>
public void Reject(Exception ex)
{
public void Reject(Exception ex) {
if (IsSync) {
RejectSync(ex);
} else {
Window.instance.run(() => RejectSync(ex));
}
}
public void RejectSync(Exception ex) {
if (CurState != PromiseState.Pending)
{
if (CurState != PromiseState.Pending) {
"Attempt to reject a promise that is already in state: " + CurState
+ ", a promise can only be rejected when it is still in state: "
"Attempt to reject a promise that is already in state: "
+ CurState
+ ", a promise can only be rejected when it is still in state: "
+ PromiseState.Pending
);
}

if (EnablePromiseTracking)
{
if (EnablePromiseTracking) {
Window.instance.scheduleMicrotask(() => {
InvokeRejectHandlers(ex);
});
InvokeRejectHandlers(ex);
}

public void Resolve()
{
if (CurState != PromiseState.Pending)
{
public void Resolve() {
if (IsSync) {
ResolveSync();
} else {
Window.instance.run(() => ResolveSync());
}
}
public void ResolveSync() {
if (CurState != PromiseState.Pending) {
"Attempt to resolve a promise that is already in state: " + CurState
+ ", a promise can only be resolved when it is still in state: "
"Attempt to resolve a promise that is already in state: "
+ CurState
+ ", a promise can only be resolved when it is still in state: "
+ PromiseState.Pending
);
}

if (EnablePromiseTracking)
{
if (EnablePromiseTracking) {
Window.instance.scheduleMicrotask(() => {
InvokeResolveHandlers();
});
InvokeResolveHandlers();
}

{
// Argument.NotNull(() => onRejected);
var resultPromise = new Promise();
var resultPromise = new Promise(isSync: true);
resultPromise.WithName(Name);
Action resolveHandler = () => resultPromise.Resolve();

/// </summary>
public IPromise Then(Func<IPromise> onResolved, Action<Exception> onRejected, Action<float> onProgress)
{
var resultPromise = new Promise();
var resultPromise = new Promise(isSync: true);
resultPromise.WithName(Name);
Action resolveHandler = () =>

/// </summary>
public IPromise Then(Action onResolved, Action<Exception> onRejected, Action<float> onProgress)
{
var resultPromise = new Promise();
var resultPromise = new Promise(isSync: true);
resultPromise.WithName(Name);
Action resolveHandler = () =>

private void ActionHandlers(IRejectable resultPromise, Action resolveHandler, Action<Exception> rejectHandler)
{
if (CurState == PromiseState.Resolved) {
Window.instance.scheduleMicrotask(() => {
InvokeResolveHandler(resolveHandler, resultPromise);
});
InvokeResolveHandler(resolveHandler, resultPromise);
Window.instance.scheduleMicrotask(() => {
InvokeRejectHandler(rejectHandler, resultPromise, rejectionException);
});
InvokeRejectHandler(rejectHandler, resultPromise, rejectionException);
}
else
{

}
var remainingCount = promisesArray.Length;
var resultPromise = new Promise();
var resultPromise = new Promise(isSync: true);
resultPromise.WithName("All");
var progress = new float[remainingCount];

/// </summary>
public static IPromise Sequence(IEnumerable<Func<IPromise>> fns)
{
var promise = new Promise();
var promise = new Promise(isSync: true);
int count = 0;

throw new InvalidOperationException("At least 1 input promise must be provided for Race");
}
var resultPromise = new Promise();
var resultPromise = new Promise(isSync: true);
resultPromise.WithName("Race");
var progress = new float[promisesArray.Length];

public IPromise Finally(Action onComplete)
{
var promise = new Promise();
var promise = new Promise(isSync: true);
promise.WithName(Name);
this.Then(() => promise.Resolve());

public IPromise ContinueWith(Func<IPromise> onComplete)
{
var promise = new Promise();
var promise = new Promise(isSync: true);
promise.WithName(Name);
this.Then(() => promise.Resolve());

public IPromise<ConvertedT> ContinueWith<ConvertedT>(Func<IPromise<ConvertedT>> onComplete)
{
var promise = new Promise();
var promise = new Promise(isSync: true);
promise.WithName(Name);
this.Then(() => promise.Resolve());

if (unhandlerException != null)
{
unhandlerException(sender, new ExceptionEventArgs(ex));
} else {
Debug.LogWarning("Unhandled Exception from " + sender + ": " + ex);
}
}
}
正在加载...
取消
保存