/// <summary>
        /// Reject the promise with an exception.
        /// </summary>
        public void Reject(Exception ex)
        {
            //            Argument.NotNull(() => ex);

            lock (this)
            {
                if (CurState != PromiseState.Pending)
                {
                    throw new PromiseStateException(
                              "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
                              );
                }

                rejectionException = ex;
                CurState           = PromiseState.Rejected;

                if (EnablePromiseTracking)
                {
                    PendingPromises.Remove(this);
                }

                InvokeRejectHandlers(ex);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Reject the promise with an exception.
        /// </summary>
        public void Reject(Exception ex)
        {
            //            Argument.NotNull(() => ex);

            lock (this)
            {
                if (CurState != PromiseState.Pending)
                {
                    Log.Error("Promise rejected multiple times (exception {ex})", ex);
                    return;

                    /*
                     * throw new PromiseStateException(
                     *  "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
                     + );
                     */
                }

                rejectionException = ex;
                CurState           = PromiseState.Rejected;

                if (Promise.EnablePromiseTracking)
                {
                    Promise.PendingPromises.Remove(this);
                }

                InvokeRejectHandlers(ex);
            }
        }
Beispiel #3
0
        public Promise(Action <Action, Action <Exception> > resolver)
        {
            this.CurState = PromiseState.Pending;
            this.Id       = ++Promise.nextPromiseId;

            if (EnablePromiseTracking)
            {
                pendingPromises.Add(this);
            }

            try
            {
                resolver(
                    // Resolve
                    () => Resolve(),

                    // Reject
                    ex => Reject(ex)
                    );
            }
            catch (Exception ex)
            {
                if (Promise.DoNotHandleExceptions && !(ex is RejectableException))
                {
                    throw;
                }
                Reject(ex);
            }
        }
 public T Fulfill(T value)
 {
     Requires.That(nameof(state), state == PromiseState.InProgress, "must be in progress is " + state);
     this.value = value;
     state      = PromiseState.Fulfilled;
     return(value);
 }
Beispiel #5
0
 public Deferred(Action <TInput> method, TInput obj)
 {
     this.method = method;
     state       = PromiseState.Pending;
     promise     = new Promise <TResult, TInput>(this);
     asyncResult = method.BeginInvoke(this, obj, MasterCallback, null);
 }
Beispiel #6
0
        private IEnumerator Execute(IEnumerator enumerator)
        {
            try {
                yield return(enumerator);

                this.state = PromiseState.Fulfilled;
                if (this.onFulfilled != null)
                {
                    this.onFulfilled();
                }
            } finally {
                if (this.state != PromiseState.Fulfilled)
                {
                    this.state = PromiseState.Rejected;
                    if (this.onRejected != null)
                    {
                        this.onRejected("Enumator did not fulfill");
                    }
                }
                if (this.onFinally != null)
                {
                    this.onFinally();
                }
            }
        }
Beispiel #7
0
 internal void DequeueCallbacks(PromiseState promiseState)
 {
     while (callbacks.Count > 0)
     {
         var callback = callbacks.Dequeue();
         if (callback.State == CallbackState.Always)
         {
             callback.Call(deferred.Result);
         }
         else if (promiseState == PromiseState.Resolved && callback.State == CallbackState.Resolved)
         {
             callback.Call(deferred.Result);
         }                 //else if (promiseState == PromiseState.Rejected && callback.State == CallbackState.Rejected) {
                           //	callback.Call(deferred.Result); // TODO: return error
                           //}
     }
     while (exceptions.Count > 0)
     {
         var callback = exceptions.Dequeue();
         if (callback.State == CallbackState.Always)
         {
             callback.Call(deferred.Exception);
         }
         else if (promiseState == PromiseState.Rejected && callback.State == CallbackState.Rejected)
         {
             callback.Call(deferred.Exception);
         }
     }
 }
Beispiel #8
0
        public void Reject(Exception e)
        {
            if (CurrentState != PromiseState.Pending)
            {
                throw new Exception("Can't reject a promise that is not in pending state");
            }

            rejectException = e;
            CurrentState    = PromiseState.Rejected;

            for (int i = 0, count = rejectActions.Count; i < count; ++i)
            {
                try
                {
                    rejectActions[i].action(e);
                }
                catch (Exception ex)
                {
                    rejectActions[i].rejectable.Reject(ex);
                }
            }

            resolveActions.Clear();
            rejectActions.Clear();
        }
Beispiel #9
0
        protected virtual void OnAbortRequested(object sender, EventArgs e)
        {
            if (this.promiseState == SailorsPromises.PromiseState.Pending)
            {
                this.promiseState = PromiseState.Aborted;

                Action <Action> action = (SynchronizationContext != null) ? (Action <Action>) this.InvokeCall : this.Call;

                foreach (Action onAbortCallback in this.onAbortCallbacks)
                {
                    try
                    {
                        action(onAbortCallback);
                    }
                    catch
                    {
                    }
                }

                if (AbortRequested != null)
                {
                    AbortRequested(sender, e);
                }
            }
        }
Beispiel #10
0
        public Promise(Action <Action <PromisedT>, Action <Exception> > resolver)
        {
            this.CurState = PromiseState.Pending;
            this.Id       = ++Promise.nextPromiseId;

            if (Promise.EnablePromiseTracking)
            {
                Promise.pendingPromises.Add(this);
            }

            try
            {
                resolver(
                    // Resolve
                    value => Resolve(value),

                    // Reject
                    ex => Reject(ex)
                    );
            }
            catch (Exception ex)
            {
                Reject(ex);
            }
        }
 public Reaction(PromiseInstance chain)
 {
     Callback = null;
     State    = PromiseState.Pending;
     Chain    = chain;
     Action   = null;
 }
 public Reaction(Action <object> action, PromiseState state)
 {
     Callback = null;
     State    = state;
     Chain    = null;
     Action   = action;
 }
Beispiel #13
0
 /// <inheritdoc cref="IPromise.Fail"/>
 public void Fail(Exception ex)
 {
     _exception = ex;
     State      = PromiseState.Failed;
     _fail?.Invoke(ex);
     _finally?.Invoke();
 }
 public Reaction(object callback, PromiseState state)
 {
     Callback = callback as FunctionInstance;
     State    = state;
     Chain    = null;
     Action   = null;
 }
        internal virtual void Reject(Exception reason)
        {
            if (this.promiseState != PromiseState.Pending)
            {
                throw new InvalidOperationException("Cannot reject a not pending promise");
            }

            ActionExceptionDelegate action =
                (SynchronizationContext != null) ?
                new ActionExceptionDelegate(this.InvokeCall) :
                new ActionExceptionDelegate(this.Call);

            foreach (Action <Exception> onRejectedCallback in this.onRejectedCallbacks)
            {
                try
                {
                    action(onRejectedCallback, reason);
                }
                catch
                {
                }
            }

            this.reason       = reason;
            this.promiseState = PromiseState.Rejected;

            if (this.followingPromise != null)
            {
                this.followingPromise.Reject(reason);
            }
        }
Beispiel #16
0
        public void Reset()
        {
            if (CurState == PromiseState.Resolved || CurState == PromiseState.Rejected)
            {
                rejectionException = null;

                resolveValue = default(PromisedT);

                rejectHandlers.Clear();

                resolveCallbacks.Clear();
                resolveRejectables.Clear();

                Name = null;

                CurState = PromiseState.Pending;
            }
            else if (CurState == PromiseState.Pending)
            {
                throw new Exception($"Reset CurState == Pending");
            }
            else
            {
                throw new Exception($"Invalid CurState: {CurState.ToString()}");
            }
        }
Beispiel #17
0
        public void Reject(Exception exception, bool waitForMainThread = false)
        {
            if (waitForMainThread && Dispatcher.IsOnMainThread == false)
            {
                Dispatcher.RunOnMainThread(() => Reject(exception));
                return;
            }

            if (state != PromiseState.Pending)
            {
                Debug.LogError($"Trying to reject promise with state: {state}");
                return;
            }

            Debug.LogException(exception);

            this.exception = exception;

            state = PromiseState.Rejected;

            for (int i = 0; i < failActions.Count; i++)
            {
                failActions[i]?.Invoke(exception);
            }
        }
        private IEnumerator Execute(Action <Action <T>, Action <string> > executor)
        {
            yield return(null);

            executor(
                value => {
                if (this.state != PromiseState.Pending)
                {
                    return;
                }
                this.state = PromiseState.Fulfilled;
                if (this.onFulfilled != null)
                {
                    this.onFulfilled(value);
                }
                if (this.onFinally != null)
                {
                    this.onFinally();
                }
            }, reason => {
                if (this.state != PromiseState.Pending)
                {
                    return;
                }
                this.state = PromiseState.Rejected;
                if (this.onRejected != null)
                {
                    this.onRejected(reason);
                }
                if (this.onFinally != null)
                {
                    this.onFinally();
                }
            });
        }
Beispiel #19
0
        /// <summary>
        /// Creates a new Promise instance.
        /// </summary>
        /// <param name="prototype"></param>
        /// <param name="executor"></param>
        internal PromiseInstance(ObjectInstance prototype, FunctionInstance executor) : base(prototype)
        {
            FunctionInstance resolveFunc = new ClrStubFunction(Engine.FunctionInstancePrototype, (engine, thisObj, param) =>
            {
                this.state = PromiseState.Fulfilled;
                if (param.Length > 0)
                {
                    result = param[0];
                }
                return(Undefined.Value);
            });
            FunctionInstance rejectFunc = new ClrStubFunction(Engine.FunctionInstancePrototype, (engine, thisObj, param) =>
            {
                this.state = PromiseState.Rejected;
                if (param.Length > 0)
                {
                    result = param[0];
                }
                return(Undefined.Value);
            });

            try
            {
                executor.Call(Undefined.Value, resolveFunc, rejectFunc);
            }
            catch (JavaScriptException ex)
            {
                rejectFunc.Call(Undefined.Value, ex.ErrorObject);
            }
        }
 public Promise()
 {
     this.CurState = PromiseState.Pending;
     if (EnablePromiseTracking)
     {
         pendingPromises.Add(this);
     }
 }
Beispiel #21
0
 State(PromiseState state, T result, Exception error, float progress, bool allDelegatesCalled)
 {
     _state              = state;
     _result             = result;
     _error              = error;
     _progress           = progress;
     _allDelegatesCalled = allDelegatesCalled;
 }
Beispiel #22
0
        /// <summary>
        /// Resolves the promise with the given value.  If the value is an object with a "then"
        /// function, the returned promise will "follow" that thenable, adopting its eventual
        /// state; otherwise the returned promise will be fulfilled with the value.
        /// </summary>
        /// <param name="value"> The resolved value of the promise, or a promise or thenable to
        /// follow. </param>
        internal void Resolve(object value)
        {
            // Do nothing if the promise has already been resolved.
            if (state != PromiseState.Pending)
            {
                return;
            }

            if (value == this)
            {
                // Reject promise.
                Reject(Engine.TypeError.Construct("A promise cannot be resolved with itself."));
                return;
            }
            else if (value is ObjectInstance thenObject)
            {
                // Try to call a method on the object called "then".
                try
                {
                    if (thenObject.GetPropertyValue("then") is FunctionInstance thenFunction)
                    {
                        Engine.AddPendingCallback(() =>
                        {
                            try
                            {
                                thenFunction.Call(thenObject, resolveFunction, rejectFunction);
                            }
                            catch (JavaScriptException ex)
                            {
                                Reject(ex.ErrorObject);
                            }
                        });
                        return;
                    }
                    // If 'then' doesn't exist or is not a function, then fulfill normally.
                }
                catch (JavaScriptException ex)
                {
                    // GetPropertyValue threw an exception.
                    Reject(ex.ErrorObject);
                    return;
                }
            }

            // Fulfill promise.
            this.state  = PromiseState.Fulfilled;
            this.result = value;
            var reactions = this.fulfillReactions;

            if (reactions != null)
            {
                this.fulfillReactions = null;
                foreach (var reaction in reactions)
                {
                    EnqueueJob(reaction);
                }
            }
        }
Beispiel #23
0
 public Promise()
 {
     this.CurState = PromiseState.Pending;
     this.id       = NextId();
     if (EnablePromiseTracking)
     {
         PendingPromises.Add(this);
     }
 }
Beispiel #24
0
 public Promise()
 {
     CurrentState = PromiseState.Pending;
     Id           = Promise.NextId();
     if (Promise.enablePromiseTracking)
     {
         Promise.pendingPromises.Add(this);
     }
 }
Beispiel #25
0
 public void ReportFail(Exception ex)
 {
     State = PromiseState.Failed;
     if (OnFail != null)
     {
         OnFail(ex);
     }
     Finally();
 }
Beispiel #26
0
 public Promise()
 {
     CurState = PromiseState.Pending;
     Id       = NextId();
     if (EnablePromiseTracking)
     {
         pendingPromises.Add(this);
     }
 }
Beispiel #27
0
        public Promise()
        {
            this.CurState = PromiseState.Pending;
            this.Id       = Promise.NextId();

            if (Promise.EnablePromiseTracking)
            {
                Promise.pendingPromises.Add(this);
            }
        }
Beispiel #28
0
        /// <summary>
        /// Returns false if the Promise has yet to be resolved. If resolved,
        /// sets the state to Fulfilled and returns true.
        /// </summary>
        protected bool Fulfill()
        {
            if (Resolved)
            {
                return(false);
            }

            State = PromiseState.Fulfilled;
            return(true);
        }
Beispiel #29
0
        private void _Reject(Exception ex)
        {
            _state = PromiseState.Rejected;
            _rejectedReason = ex;

            if (_canSettle)
            {
                _Settle();
            }
        }
Beispiel #30
0
        public Promise()
        {
            this.CurState = PromiseState.Pending;
            this.Id       = ++Promise.nextPromiseId;

            if (Promise.EnablePromiseTracking)
            {
                Promise.pendingPromises.Add(this);
            }
        }