コード例 #1
0
        /// <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);
            }
        }
コード例 #2
0
ファイル: Promise.cs プロジェクト: slavig/pepperspray
        /// <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);
            }
        }
コード例 #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);
            }
        }
コード例 #4
0
 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);
 }
コード例 #5
0
ファイル: Deferred.cs プロジェクト: Thanood/Promises
 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);
 }
コード例 #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();
                }
            }
        }
コード例 #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);
         }
     }
 }
コード例 #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();
        }
コード例 #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);
                }
            }
        }
コード例 #10
0
ファイル: Promise.cs プロジェクト: craus/lunohodes
        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);
            }
        }
コード例 #11
0
 public Reaction(PromiseInstance chain)
 {
     Callback = null;
     State    = PromiseState.Pending;
     Chain    = chain;
     Action   = null;
 }
コード例 #12
0
 public Reaction(Action <object> action, PromiseState state)
 {
     Callback = null;
     State    = state;
     Chain    = null;
     Action   = action;
 }
コード例 #13
0
ファイル: Promise.cs プロジェクト: SheepBros/Promise
 /// <inheritdoc cref="IPromise.Fail"/>
 public void Fail(Exception ex)
 {
     _exception = ex;
     State      = PromiseState.Failed;
     _fail?.Invoke(ex);
     _finally?.Invoke();
 }
コード例 #14
0
 public Reaction(object callback, PromiseState state)
 {
     Callback = callback as FunctionInstance;
     State    = state;
     Chain    = null;
     Action   = null;
 }
コード例 #15
0
        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);
            }
        }
コード例 #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()}");
            }
        }
コード例 #17
0
ファイル: Promise.cs プロジェクト: studentutu/swift-framework
        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);
            }
        }
コード例 #18
0
        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();
                }
            });
        }
コード例 #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);
            }
        }
コード例 #20
0
 public Promise()
 {
     this.CurState = PromiseState.Pending;
     if (EnablePromiseTracking)
     {
         pendingPromises.Add(this);
     }
 }
コード例 #21
0
 State(PromiseState state, T result, Exception error, float progress, bool allDelegatesCalled)
 {
     _state              = state;
     _result             = result;
     _error              = error;
     _progress           = progress;
     _allDelegatesCalled = allDelegatesCalled;
 }
コード例 #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);
                }
            }
        }
コード例 #23
0
ファイル: Promise_NonGeneric.cs プロジェクト: jorik041/UniMap
 public Promise()
 {
     this.CurState = PromiseState.Pending;
     this.id       = NextId();
     if (EnablePromiseTracking)
     {
         PendingPromises.Add(this);
     }
 }
コード例 #24
0
ファイル: PromiseGeneric.cs プロジェクト: pacatum/Helmbet-RPS
 public Promise()
 {
     CurrentState = PromiseState.Pending;
     Id           = Promise.NextId();
     if (Promise.enablePromiseTracking)
     {
         Promise.pendingPromises.Add(this);
     }
 }
コード例 #25
0
 public void ReportFail(Exception ex)
 {
     State = PromiseState.Failed;
     if (OnFail != null)
     {
         OnFail(ex);
     }
     Finally();
 }
コード例 #26
0
 public Promise()
 {
     CurState = PromiseState.Pending;
     Id       = NextId();
     if (EnablePromiseTracking)
     {
         pendingPromises.Add(this);
     }
 }
コード例 #27
0
ファイル: Promise.cs プロジェクト: wang080134/C-Sharp-Promise
        public Promise()
        {
            this.CurState = PromiseState.Pending;
            this.Id       = Promise.NextId();

            if (Promise.EnablePromiseTracking)
            {
                Promise.pendingPromises.Add(this);
            }
        }
コード例 #28
0
ファイル: BasePromise.cs プロジェクト: orangutanmatter/Snake
        /// <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);
        }
コード例 #29
0
ファイル: Promise.cs プロジェクト: twabbott/CSharpPromise
        private void _Reject(Exception ex)
        {
            _state = PromiseState.Rejected;
            _rejectedReason = ex;

            if (_canSettle)
            {
                _Settle();
            }
        }
コード例 #30
0
ファイル: Promise.cs プロジェクト: craus/lunohodes
        public Promise()
        {
            this.CurState = PromiseState.Pending;
            this.Id       = ++Promise.nextPromiseId;

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