public void Resolve(T outcome) { if (_state != DeferredState.PENDING) { return; } _outcome = outcome; _state = DeferredState.RESOLVED; _completeListeners.ForEach(then => { try { then(outcome); } catch (Exception e) { Debug.LogError("An error occurred:" + e); } }); ClearListeners(); InvokeFinalCallback(); }
/** * Aborts the deferred operation; none of the handlers bound to the Promise will be invoked; typically this * is used when the Deferred's host needs to cancel the operation. */ public void Abort() { _state = DeferredState.ABORTED; _outcome = _Null; _finalCallback = null; ClearListeners(); }
/** * Notifies all 'fails' handlers that this deferred operation has been unsuccesful. The supplied Error object * will be supplied to all of the handlers. * * @param error Error object which explains how or why the operation was unsuccesful. */ public void Reject(Exception error) { if (_state != DeferredState.PENDING) { return; } // By contact, we will always supply an Error object to the fail handlers. _error = error != null ? error : new Exception("Promise Rejected"); _state = DeferredState.REJECTED; ClearListeners(); InvokeFinalCallback(); }
/// <summary> /// 処理をキャンセルします。 /// </summary> /// <returns>キャンセル出来た場合にはtrue、既に実行が完了されたなどの理由でキャンセル出来なかった場合にはfalse</returns> public Boolean Cancel() { IsCanceled = true; if (AsyncResult.IsCompleted) { return(false); } DeferredState <TResult> state = AsyncResult.AsyncState as DeferredState <TResult>; state.WaitHandle.Set(); Target.EndInvoke(AsyncResult); return(IsRunning); }
/// <summary> /// /// </summary> /// <typeparam name="TResult"></typeparam> /// <param name="func"></param> /// <param name="millisecond"></param> /// <param name="asyncCallback"></param> /// <returns></returns> private static DeferredState <TResult> DeferredInvokeInternal <TResult>(Func <TResult> func, Int32 millisecond, AsyncCallback asyncCallback) { DeferredState <TResult> state = new DeferredState <TResult>(); state.WaitHandle = new ManualResetEvent(false); // 暫定で0から10秒 if (millisecond > 10 * 1000) { millisecond = 10 * 1000; } else if (millisecond < 0) { millisecond = 0; } Func <TResult> d = () => { state.WaitHandle.WaitOne(millisecond); lock (state) { if (state.IsCanceled) { state.IsRunning = true; return(default(TResult)); } else { return(func()); } } }; state.Target = d; state.AsyncResult = d.BeginInvoke(asyncCallback, state); return(state); }