From 7c73ebd67ca921713c468bab15d099d85536930d Mon Sep 17 00:00:00 2001 From: Eric Newton Date: Thu, 18 Oct 2018 02:59:14 -0400 Subject: [PATCH] clean up build --- Promise_Base.cs | 290 ------------------------------------------- Tests/A+ Spec/2.2.cs | 2 +- src/Promise.cs | 10 +- 3 files changed, 6 insertions(+), 296 deletions(-) delete mode 100644 Promise_Base.cs diff --git a/Promise_Base.cs b/Promise_Base.cs deleted file mode 100644 index 7e2cc2b..0000000 --- a/Promise_Base.cs +++ /dev/null @@ -1,290 +0,0 @@ -using System; -using System.Collections.Generic; -using RSG.Promises; - -namespace RSG -{ - public interface IPromiseBase - { - /// - /// ID of the promise, useful for debugging. - /// - int Id { get; } - - /// - /// Set the name of the promise, useful for debugging. - /// - IPromiseBase WithName(string name); - - /// - /// Completes the promise. - /// onResolved is called on successful completion. - /// onRejected is called on error. - /// - void Done(Action onResolved, Action onRejected); - - /// - /// Completes the promise. - /// onResolved is called on successful completion. - /// Adds a default error handler. - /// - void Done(Action onResolved); - - /// - /// Complete the promise. Adds a default error handler. - /// - void Done(); - - /// - /// Handle errors for the promise. - /// - IPromiseBase Catch(Action onRejected); - - /// - /// Add a resolved callback that chains a non-value promise. - /// - IPromiseBase Then(Func onResolved); - - /// - /// Add a resolved callback. - /// - IPromiseBase Then(Action onResolved); - - /// - /// Add a resolved callback and a rejected callback. - /// The resolved callback chains a non-value promise. - /// - IPromiseBase Then(Func onResolved, Action onRejected); - - /// - /// Add a resolved callback and a rejected callback. - /// - IPromiseBase Then(Action onResolved, Action onRejected); - - /// - /// Chain an enumerable of promises, all of which must resolve. - /// The resulting promise is resolved when all of the promises have resolved. - /// It is rejected as soon as any of the promises have been rejected. - /// - IPromiseBase ThenAll(Func> chain); - } - - /// - /// Interface for a promise that can be rejected. - /// - public interface IRejectable - { - /// - /// Reject the promise with an exception. - /// - void Reject(Exception ex); - } - - /// - /// Arguments to the UnhandledError event. - /// - public class ExceptionEventArgs : EventArgs - { - internal ExceptionEventArgs(Exception exception) - { - // Argument.NotNull(() => exception); - - this.Exception = exception; - } - - public Exception Exception - { - get; - private set; - } - } - - /// - /// Represents a handler invoked when the promise is rejected. - /// - public struct RejectHandler - { - /// - /// Callback fn. - /// - public Action callback; - - /// - /// The promise that is rejected when there is an error while invoking the handler. - /// - public IRejectable rejectable; - } - - /// - /// Option type for generic access to promised data - /// - public struct PromiseResult - { - public static readonly PromiseResult None = new PromiseResult(); - - public readonly bool hasValue; - public readonly object result; - - public PromiseResult(object value) - { - hasValue = true; - result = value; - } - } - - /// - /// Specifies the state of a promise. - /// - public enum PromiseState - { - Pending, // The promise is in-flight. - Rejected, // The promise has been rejected. - Resolved // The promise has been resolved. - }; - - /// - /// Used to list information of pending promises. - /// - public interface IPromiseInfo - { - /// - /// Id of the promise. - /// - int Id { get; } - - /// - /// Human-readable name for the promise. - /// - string Name { get; } - } - - public abstract class Promise_Base : IPromiseInfo - { - /// - /// Id for the next promise that is created. - /// - private static int nextPromiseId = 0; - - /// - /// The exception when the promise is rejected. - /// - protected Exception rejectionException; - - /// - /// Error handlers. - /// - protected List rejectHandlers; - - /// - /// ID of the promise, useful for debugging. - /// - public int Id { get; } - - /// - /// Name of the promise, when set, useful for debugging. - /// - public string Name { get; protected set; } - - /// - /// Tracks the current state of the promise. - /// - public PromiseState CurState { get; protected set; } - - public Promise_Base() - { - this.CurState = PromiseState.Pending; - this.Id = NextId(); - - if (Promise.EnablePromiseTracking) - { - Promise.pendingPromises.Add(this); - } - } - - /// - /// Increments the ID counter and gives us the ID for the next promise. - /// - internal static int NextId() - { - return ++nextPromiseId; - } - - /// - /// Add a rejection handler for this promise. - /// - protected void AddRejectHandler(Action onRejected, IRejectable rejectable) - { - if (rejectHandlers == null) - { - rejectHandlers = new List(); - } - - rejectHandlers.Add(new RejectHandler() - { - callback = onRejected, - rejectable = rejectable - }); - } - - /// - /// Invoke a single handler. - /// - protected void InvokeHandler(Action callback, IRejectable rejectable, T value) - { - // Argument.NotNull(() => callback); - // Argument.NotNull(() => rejectable); - - try - { - callback(value); - } - catch (Exception ex) - { - rejectable.Reject(ex); - } - } - - protected virtual void ClearHandlers() - { - rejectHandlers = null; - } - - /// - /// Invoke all reject handlers. - /// - protected void InvokeRejectHandlers(Exception ex) - { - // Argument.NotNull(() => ex); - - if (rejectHandlers != null) - { - rejectHandlers.Each(handler => InvokeHandler(handler.callback, handler.rejectable, ex)); - } - - ClearHandlers(); - } - - /// - /// Reject the promise with an exception. - /// - public void Reject(Exception ex) - { - // Argument.NotNull(() => ex); - - if (CurState != PromiseState.Pending) - { - throw new ApplicationException("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); - } - } -} diff --git a/Tests/A+ Spec/2.2.cs b/Tests/A+ Spec/2.2.cs index cf95514..c57e506 100644 --- a/Tests/A+ Spec/2.2.cs +++ b/Tests/A+ Spec/2.2.cs @@ -348,7 +348,7 @@ public void _when_promise1_is_rejected_with_no_value_in_catch() new Promise((res, rej) => rej(new Exception())) .Catch(_ => {}) - .Then(() => callbackInvoked = true); + .Then((x) => callbackInvoked = true); Assert.True(callbackInvoked); } diff --git a/src/Promise.cs b/src/Promise.cs index 2f0844b..bb1b0c0 100644 --- a/src/Promise.cs +++ b/src/Promise.cs @@ -1,4 +1,4 @@ -using RSG.Promises; +using RSG.Promises; using System; using System.Collections.Generic; using System.Linq; @@ -427,19 +427,19 @@ IPromiseBase IPromiseBase.WithName(string name) /// /// Handle errors for the promise. /// - public IPromise Catch(Action onRejected) + public IPromise Catch(Action onRejected) { - var resultPromise = new Promise(); + var resultPromise = new Promise(); resultPromise.WithName(Name); - Action resolveHandler = _ => resultPromise.Resolve(); + Action resolveHandler = _ => resultPromise.Resolve(default(PromisedT)); Action rejectHandler = ex => { try { onRejected(ex); - resultPromise.Resolve(); + resultPromise.Resolve(default(PromisedT)); } catch(Exception cbEx) {