# HG changeset patch # User cin # Date 2016-10-25 14:40:33 # Node ID 8200ab154c8a8ecec0a9c6279d61e47b9252ab01 # Parent 4d9830a9bbb837a3301c2b2b4072bfaef1ae62f5 Added ResetState to RunnableComponent to reset in case of failure Added StateChanged event to IRunnable Renamed Promise.SUCCESS -> Promise.Success Added Promise.FromException Renamed Bundle -> PromiseAll in PromiseExtensions diff --git a/Implab.Test/AsyncTests.cs b/Implab.Test/AsyncTests.cs --- a/Implab.Test/AsyncTests.cs +++ b/Implab.Test/AsyncTests.cs @@ -336,7 +336,7 @@ namespace Implab.Test { Console.WriteLine("done reader #2: {0} ms", Environment.TickCount - t1); } ) - .Bundle() + .PromiseAll() .Join(); Assert.AreEqual(count * 3, res1 + res2); @@ -414,7 +414,7 @@ namespace Implab.Test { Console.WriteLine("done reader #2: {0} ms", Environment.TickCount - t1); } ) - .Bundle() + .PromiseAll() .Join(); Assert.AreEqual(summ , r1 + r2); @@ -490,7 +490,7 @@ namespace Implab.Test { Console.WriteLine("done reader #2: {0} ms, avg chunk size: {1}", Environment.TickCount - t1, avgchunk); } ) - .Bundle() + .PromiseAll() .Join(); Assert.AreEqual(summ , r1 + r2); @@ -593,7 +593,7 @@ namespace Implab.Test { Console.WriteLine("done reader #2: {0} ms, {1} items", Environment.TickCount - t1, count); } ) - .Bundle() + .PromiseAll() .Join(); Assert.AreEqual(summ , r1 + r2); @@ -835,7 +835,7 @@ namespace Implab.Test { log.Enqueue("Writer #1 lock released"); } } - ).Bundle().Join(1000); + ).PromiseAll().Join(1000); log.Enqueue("Done"); } catch(Exception error) { log.Enqueue(error.Message); diff --git a/Implab.Test/Implab.Format.Test/Implab.Format.Test.csproj b/Implab.Test/Implab.Format.Test/Implab.Format.Test.csproj --- a/Implab.Test/Implab.Format.Test/Implab.Format.Test.csproj +++ b/Implab.Test/Implab.Format.Test/Implab.Format.Test.csproj @@ -1,52 +1,55 @@ - - - - Debug - AnyCPU - 8.0.30703 - 2.0 - {4D364996-7ECD-4193-8F90-F223FFEA49DA} - Library - Implab.Format.Test - Implab.Format.Test - v4.5 - 0.2 - - - true - full - false - bin\Debug - DEBUG; - prompt - 4 - false - - - full - true - bin\Release - prompt - 4 - false - - - - - ..\..\packages\NUnit.2.6.4\lib\nunit.framework.dll - - - - - - - - - {F550F1F8-8746-4AD0-9614-855F4C4B7F05} - Implab - - - - - + + + + Debug + AnyCPU + 8.0.30703 + 2.0 + {4D364996-7ECD-4193-8F90-F223FFEA49DA} + Library + Implab.Format.Test + Implab.Format.Test + v4.5 + 0.2 + + + true + full + false + bin\Debug + DEBUG; + prompt + 4 + false + + + full + true + bin\Release + prompt + 4 + false + + + + + ..\..\packages\NUnit.2.6.4\lib\nunit.framework.dll + + + + + + + + + {F550F1F8-8746-4AD0-9614-855F4C4B7F05} + Implab + + + + + + + + \ No newline at end of file diff --git a/Implab.Test/Implab.Test.mono.csproj b/Implab.Test/Implab.Test.mono.csproj --- a/Implab.Test/Implab.Test.mono.csproj +++ b/Implab.Test/Implab.Test.mono.csproj @@ -1,75 +1,81 @@ - - - - Debug - AnyCPU - 8.0.30703 - 2.0 - {2BD05F84-E067-4B87-9477-FDC2676A21C6} - Library - Implab.Test - Implab.Test - v4.5 - 0.2 - - - true - full - false - bin\Debug - DEBUG;MONO - prompt - 4 - false - - - true - bin\Release - prompt - 4 - false - MONO - - - true - full - false - bin\Debug - DEBUG;TRACE;NET_4_5;MONO - prompt - 4 - false - - - true - bin\Release - NET_4_5;MONO - prompt - 4 - false - - - - - - - - - - - - - - - - - - - {F550F1F8-8746-4AD0-9614-855F4C4B7F05} - Implab - - - - - + + + + Debug + AnyCPU + 8.0.30703 + 2.0 + {2BD05F84-E067-4B87-9477-FDC2676A21C6} + Library + Implab.Test + Implab.Test + v4.5 + 0.2 + + + true + full + false + bin\Debug + DEBUG;MONO + prompt + 4 + false + + + true + bin\Release + prompt + 4 + false + MONO + + + true + full + false + bin\Debug + DEBUG;TRACE;NET_4_5;MONO + prompt + 4 + false + + + true + bin\Release + NET_4_5;MONO + prompt + 4 + false + + + + ..\packages\NUnit.2.6.4\lib\nunit.framework.dll + True + + + + + + + + + + + + + + + + + {F550F1F8-8746-4AD0-9614-855F4C4B7F05} + Implab + + + + + + + + \ No newline at end of file diff --git a/Implab.Test/Mock/MockPollingComponent.cs b/Implab.Test/Mock/MockPollingComponent.cs --- a/Implab.Test/Mock/MockPollingComponent.cs +++ b/Implab.Test/Mock/MockPollingComponent.cs @@ -50,7 +50,7 @@ namespace Implab.Test.Mock { } protected override IPromise OnTick(ICancellationToken cancellationToken) { - return MockTick != null ? Safe.Run(() => MockTick(cancellationToken)) : Promise.SUCCESS; + return MockTick != null ? Safe.Run(() => MockTick(cancellationToken)) : Promise.Success; } protected override void OnTickCancel(Exception error) { diff --git a/Implab.Test/Mock/MockRunnableComponent.cs b/Implab.Test/Mock/MockRunnableComponent.cs --- a/Implab.Test/Mock/MockRunnableComponent.cs +++ b/Implab.Test/Mock/MockRunnableComponent.cs @@ -6,6 +6,9 @@ namespace Implab.Test.Mock { public MockRunnableComponent(bool initialized) : base(initialized) { } + public MockRunnableComponent(bool initialized, bool reusable) : base(initialized, reusable) { + } + public Action MockInit { get; set; @@ -21,6 +24,11 @@ namespace Implab.Test.Mock { set; } + public Action MockDispose { + get; + set; + } + protected override IPromise OnStart() { return MockStart != null ? Safe.Run(MockStart).Chain(base.OnStart) : Safe.Run(base.OnStart); } @@ -33,6 +41,12 @@ namespace Implab.Test.Mock { if (MockInit != null) MockInit(); } + + protected override void Dispose(bool disposing, Exception lastError) { + if (MockDispose != null) + MockDispose(disposing, lastError); + base.Dispose(disposing, lastError); + } } } diff --git a/Implab.Test/PollingComponentTests.cs b/Implab.Test/PollingComponentTests.cs --- a/Implab.Test/PollingComponentTests.cs +++ b/Implab.Test/PollingComponentTests.cs @@ -36,7 +36,7 @@ namespace Implab.Test { Assert.AreEqual(ExecutionState.Created, comp.State); - comp.Init(); + comp.Initialize(); Assert.AreEqual(ExecutionState.Ready, comp.State); @@ -57,7 +57,7 @@ namespace Implab.Test { var comp = new MockPollingComponent(TimeSpan.FromMilliseconds(1), null, true); comp.MockTick = ct => { signal.Set(); - return Promise.SUCCESS; + return Promise.Success; }; comp.Start().Join(1000); diff --git a/Implab.Test/RunnableComponentTests.cs b/Implab.Test/RunnableComponentTests.cs --- a/Implab.Test/RunnableComponentTests.cs +++ b/Implab.Test/RunnableComponentTests.cs @@ -39,7 +39,7 @@ namespace Implab.Test { Assert.AreEqual(ExecutionState.Created, comp.State); - comp.Init(); + comp.Initialize(); Assert.AreEqual(ExecutionState.Ready, comp.State); @@ -65,7 +65,7 @@ namespace Implab.Test { ShouldThrow(() => comp.Stop()); Assert.AreEqual(ExecutionState.Created, comp.State); - ShouldThrow(comp.Init); + ShouldThrow(comp.Initialize); Assert.AreEqual(ExecutionState.Failed, comp.State); @@ -85,12 +85,77 @@ namespace Implab.Test { ShouldThrow(() => comp.Start()); ShouldThrow(() => comp.Stop()); - ShouldThrow(comp.Init); + ShouldThrow(comp.Initialize); Assert.AreEqual(ExecutionState.Disposed, comp.State); } [TestMethod] + public void ShouldCallDisposeOnStop() { + var comp = new MockRunnableComponent(true); + + bool disposed = false; + comp.MockDispose = (disposing, error) => { + disposed = true; + }; + + comp.Start().Join(1000); + comp.Stop().Join(1000); + + ShouldThrow(() => comp.Start()); + ShouldThrow(() => comp.Stop()); + ShouldThrow(comp.Initialize); + + Assert.AreEqual(ExecutionState.Disposed, comp.State); + Assert.IsTrue(disposed); + } + + [TestMethod] + public void ShouldNotCallDisposeOnStop() { + var comp = new MockRunnableComponent(true, true); + + bool disposed = false; + comp.MockDispose = (disposing, error) => { + disposed = true; + }; + + comp.Start().Join(1000); + comp.Stop().Join(1000); + + Assert.AreEqual(ExecutionState.Ready, comp.State); + Assert.IsFalse(disposed); + } + + [TestMethod] + public void SelfDisposeOnStop() { + var comp = new MockRunnableComponent(true, true); + + bool disposed = false; + Exception lastError = null; + comp.MockDispose = (disposing, error) => { + disposed = true; + lastError = error; + }; + + comp.Start().Join(1000); + comp.Stop().Join(1000); + + Assert.AreEqual(ExecutionState.Ready, comp.State); + Assert.IsFalse(disposed); + + comp.MockStop = () => { + comp.Dispose(); + return Promise.Success; + }; + + comp.Start().Join(1000); + comp.Stop().Join(1000); + + Assert.AreEqual(ExecutionState.Disposed, comp.State); + Assert.IsTrue(disposed); + } + + [TestMethod] public void StartCancelTest() { var comp = new MockRunnableComponent(true) { MockStart = () => PromiseHelper.Sleep(100000, 0) diff --git a/Implab.Test/packages.config b/Implab.Test/packages.config new file mode 100644 --- /dev/null +++ b/Implab.Test/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Implab/Automaton/AutomatonTransition.cs b/Implab/Automaton/AutomatonTransition.cs --- a/Implab/Automaton/AutomatonTransition.cs +++ b/Implab/Automaton/AutomatonTransition.cs @@ -28,6 +28,14 @@ namespace Implab.Automaton { public override int GetHashCode() { return s1 + s2 + edge; } + + public static bool operator == (AutomatonTransition rv, AutomatonTransition lv) { + return rv.Equals(lv); + } + + public static bool operator !=(AutomatonTransition rv, AutomatonTransition lv) { + return rv.Equals(lv); + } } } diff --git a/Implab/Components/IInitializable.cs b/Implab/Components/IInitializable.cs --- a/Implab/Components/IInitializable.cs +++ b/Implab/Components/IInitializable.cs @@ -3,8 +3,8 @@ namespace Implab.Components { /// /// Initializable components are created and initialized in two steps, first we have create the component, - /// then we have to complete it's creation by calling an method. All parameters needed - /// to complete the initialization must be passed before the calling + /// then we have to complete it's creation by calling an method. All parameters needed + /// to complete the initialization must be passed before the calling /// public interface IInitializable { /// @@ -12,10 +12,10 @@ namespace Implab.Components { /// /// /// Normally virtual methods shouldn't be called from the constructor, due to the incomplete object state, but - /// they can be called from this method. This method is also usefull when we constructing a complex grpah + /// they can be called from this method. This method is aьуерщlso usefull when we constructing a complex grpah /// of components where cyclic references may take place. /// - void Init(); + void Initialize(); } } diff --git a/Implab/Components/IRunnable.cs b/Implab/Components/IRunnable.cs --- a/Implab/Components/IRunnable.cs +++ b/Implab/Components/IRunnable.cs @@ -14,6 +14,8 @@ namespace Implab.Components { ExecutionState State { get; } + event EventHandler StateChanged; + Exception LastError { get; } } } diff --git a/Implab/Components/PollingComponent.cs b/Implab/Components/PollingComponent.cs --- a/Implab/Components/PollingComponent.cs +++ b/Implab/Components/PollingComponent.cs @@ -124,7 +124,7 @@ namespace Implab.Components { /// Invoked when the timer ticks, use this method to implement your logic /// protected virtual IPromise OnTick(ICancellationToken cancellationToken) { - return Promise.SUCCESS; + return Promise.Success; } protected override IPromise OnStop() { diff --git a/Implab/Components/RunnableComponent.cs b/Implab/Components/RunnableComponent.cs --- a/Implab/Components/RunnableComponent.cs +++ b/Implab/Components/RunnableComponent.cs @@ -9,7 +9,8 @@ namespace Implab.Components { Start, Stop, Dispose, - Last = Dispose + Reset, + Last = Reset } class StateMachine { @@ -37,9 +38,11 @@ namespace Implab.Components { Edge(ExecutionState.Running, ExecutionState.Disposed, Commands.Dispose); Edge(ExecutionState.Stopping, ExecutionState.Failed, Commands.Fail); - Edge(ExecutionState.Stopping, ExecutionState.Disposed, Commands.Ok); + Edge(ExecutionState.Stopping, ExecutionState.Ready, Commands.Ok); + Edge(ExecutionState.Stopping, ExecutionState.Disposed, Commands.Dispose); Edge(ExecutionState.Failed, ExecutionState.Disposed, Commands.Dispose); + Edge(ExecutionState.Failed, ExecutionState.Initializing, Commands.Reset); } static void Edge(ExecutionState s1, ExecutionState s2, Commands cmd) { @@ -67,11 +70,26 @@ namespace Implab.Components { IPromise m_pending; Exception m_lastError; - readonly StateMachine m_stateMachine; - - protected RunnableComponent(bool initialized) { + readonly StateMachine m_stateMachine; + readonly bool m_reusable; + public event EventHandler StateChanged; + + /// + /// Initializes component state. + /// + /// If set, the component initial state is and the component is ready to start, otherwise initialization is required. + /// If set, the component may start after it has been stopped, otherwise the component is disposed after being stopped. + protected RunnableComponent(bool initialized, bool reusable) { m_stateMachine = new StateMachine(initialized ? ExecutionState.Ready : ExecutionState.Created); - DisposeTimeout = 10000; + m_reusable = reusable; + DisposeTimeout = 10000; + } + + /// + /// Initializes component state. The component created with this constructor is not reusable, i.e. it will be disposed after stop. + /// + /// If set, the component initial state is and the component is ready to start, otherwise initialization is required. + protected RunnableComponent(bool initialized) : this(initialized, false) { } /// @@ -84,49 +102,119 @@ namespace Implab.Components { void ThrowInvalidCommand(Commands cmd) { if (m_stateMachine.State == ExecutionState.Disposed) - throw new ObjectDisposedException(ToString()); - - throw new InvalidOperationException(String.Format("Commnd {0} is not allowed in the state {1}", cmd, m_stateMachine.State)); + throw new ObjectDisposedException(ToString()); + + throw new InvalidOperationException(String.Format("Command {0} is not allowed in the state {1}", cmd, m_stateMachine.State)); + } + + bool MoveIfInState(Commands cmd, IPromise pending, Exception error, ExecutionState state) { + ExecutionState prev, current; + lock (m_stateMachine) { + if (m_stateMachine.State != state) + return false; + + prev = m_stateMachine.State; + if (!m_stateMachine.Move(cmd)) + ThrowInvalidCommand(cmd); + current = m_stateMachine.State; + + m_pending = pending; + m_lastError = error; + } + if (prev != current) + OnStateChanged(prev, current, error); + return true; } - void Move(Commands cmd) { - if (!m_stateMachine.Move(cmd)) - ThrowInvalidCommand(cmd); + bool MoveIfPending(Commands cmd, IPromise pending, Exception error, IPromise expected) { + ExecutionState prev, current; + lock (m_stateMachine) { + if (m_pending != expected) + return false; + prev = m_stateMachine.State; + if (!m_stateMachine.Move(cmd)) + ThrowInvalidCommand(cmd); + current = m_stateMachine.State; + m_pending = pending; + m_lastError = error; + } + if (prev != current) + OnStateChanged(prev, current, error); + return true; + } + + IPromise Move(Commands cmd, IPromise pending, Exception error) { + ExecutionState prev, current; + IPromise ret; + lock (m_stateMachine) { + prev = m_stateMachine.State; + if (!m_stateMachine.Move(cmd)) + ThrowInvalidCommand(cmd); + current = m_stateMachine.State; + + ret = m_pending; + m_pending = pending; + m_lastError = error; + + } + if(prev != current) + OnStateChanged(prev, current, error); + return ret; + } + + protected virtual void OnStateChanged(ExecutionState previous, ExecutionState current, Exception error) { + var h = StateChanged; + if (h != null) + h(this, new StateChangeEventArgs { + State = current, + LastError = error + }); } /// /// Moves the component from running to failed state. /// /// The exception which is describing the error. - /// Returns true if the component is set to the failed state, false - otherwise. - /// This method works only for the running state, in any other state it will return false. - protected bool Fail(Exception error) { - lock (m_stateMachine) { - if(m_stateMachine.State == ExecutionState.Running) { - m_stateMachine.Move(Commands.Fail); - m_lastError = error; - return true; - } - } - return false; + protected bool Fail(Exception error) { + return MoveIfInState(Commands.Fail, null, error, ExecutionState.Running); } - void Invoke(Commands cmd, Action action) { - lock (m_stateMachine) - Move(cmd); - + /// + /// Tries to reset state to . + /// + /// True if component is reset to , false if the componet wasn't + /// in state. + /// + /// This method checks the current state of the component and if it's in + /// moves component to . + /// The is called and if this method completes succesfully the component moved + /// to state, otherwise the component is moved to + /// state. If throws an exception it will be propagated by this method to the caller. + /// + protected bool ResetState() { + if (!MoveIfInState(Commands.Reset, null, null, ExecutionState.Failed)) + return false; + try { - action(); - lock(m_stateMachine) - Move(Commands.Ok); - + OnResetState(); + Move(Commands.Ok, null, null); + return true; } catch (Exception err) { - lock (m_stateMachine) { - Move(Commands.Fail); - m_lastError = err; - } + Move(Commands.Fail, null, err); throw; - } + } + } + + /// + /// This method is called by to reinitialize component in the failed state. + /// + /// + /// Default implementation throws which will cause the component + /// fail to reset it's state and it left in state. + /// If this method doesn't throw exceptions the component is moved to state. + /// + protected virtual void OnResetState() { + throw new NotImplementedException(); } IPromise InvokeAsync(Commands cmd, Func action, Action chain) { @@ -135,40 +223,20 @@ namespace Implab.Components { var task = new ActionChainTask(action, null, null, true); - lock (m_stateMachine) { - Move(cmd); - - prev = m_pending; + Action errorOrCancel = e => { + if (e == null) + e = new OperationCanceledException(); + MoveIfPending(Commands.Fail, null, e, promise); + throw new PromiseTransientException(e); + }; - Action errorOrCancel = e => { - if (e == null) - e = new OperationCanceledException(); - - lock (m_stateMachine) { - if (m_pending == promise) { - Move(Commands.Fail); - m_pending = null; - m_lastError = e; - } - } - throw new PromiseTransientException(e); - }; + promise = task.Then( + () => MoveIfPending(Commands.Ok, null, null, promise), + errorOrCancel, + errorOrCancel + ); - promise = task.Then( - () => { - lock(m_stateMachine) { - if (m_pending == promise) { - Move(Commands.Ok); - m_pending = null; - } - } - }, - errorOrCancel, - errorOrCancel - ); - - m_pending = promise; - } + prev = Move(cmd, promise, null); if (prev == null) task.Resolve(); @@ -181,8 +249,16 @@ namespace Implab.Components { #region IInitializable implementation - public void Init() { - Invoke(Commands.Init, OnInitialize); + public void Initialize() { + Move(Commands.Init, null, null); + + try { + OnInitialize(); + Move(Commands.Ok, null, null); + } catch (Exception err) { + Move(Commands.Fail, null, err); + throw; + } } protected virtual void OnInitialize() { @@ -197,15 +273,16 @@ namespace Implab.Components { } protected virtual IPromise OnStart() { - return Promise.SUCCESS; + return Promise.Success; } public IPromise Stop() { - return InvokeAsync(Commands.Stop, OnStop, StopPending).Then(Dispose); + var pending = InvokeAsync(Commands.Stop, OnStop, StopPending); + return m_reusable ? pending : pending.Then(Dispose); } protected virtual IPromise OnStop() { - return Promise.SUCCESS; + return Promise.Success; } /// @@ -258,17 +335,14 @@ namespace Implab.Components { /// public void Dispose() { IPromise pending; + lock (m_stateMachine) { if (m_stateMachine.State == ExecutionState.Disposed) return; - - Move(Commands.Dispose); + pending = Move(Commands.Dispose, null, null); + } - GC.SuppressFinalize(this); - - pending = m_pending; - m_pending = null; - } + GC.SuppressFinalize(this); if (pending != null) { pending.Cancel(); pending.Timeout(DisposeTimeout).On( @@ -277,7 +351,7 @@ namespace Implab.Components { reason => Dispose(true, new OperationCanceledException("The operation is cancelled", reason)) ); } else { - Dispose(true, m_lastError); + Dispose(true, null); } } @@ -287,6 +361,11 @@ namespace Implab.Components { #endregion + /// + /// Releases all resources used by the component, called automatically, override this method to implement your cleanup. + /// + /// true if this method is called during normal dispose process. + /// The last error which occured during the component stop. protected virtual void Dispose(bool disposing, Exception lastError) { } diff --git a/Implab/Components/StateChangeEventArgs.cs b/Implab/Components/StateChangeEventArgs.cs new file mode 100644 --- /dev/null +++ b/Implab/Components/StateChangeEventArgs.cs @@ -0,0 +1,16 @@ +using System; + +namespace Implab.Components +{ + public class StateChangeEventArgs { + /// + /// The error information if any + /// + public Exception LastError { get; set; } + + /// + /// The state of the service corresponding to this event + /// + public ExecutionState State { get; set; } + } +} diff --git a/Implab/Diagnostics/TextFileListener.cs b/Implab/Diagnostics/TextFileListener.cs --- a/Implab/Diagnostics/TextFileListener.cs +++ b/Implab/Diagnostics/TextFileListener.cs @@ -5,6 +5,7 @@ using System.Text; namespace Implab.Diagnostics { public class TextFileListener: ListenerBase { readonly TextWriter m_textWriter; + readonly object m_lock = new object(); public TextFileListener(string fileName) { m_textWriter = File.CreateText(fileName); @@ -20,7 +21,7 @@ namespace Implab.Diagnostics { msg.Append(" "); msg.AppendFormat("[{0}]:{1}: {2}", args.ThreadId, args.Channel, entry); - lock (m_textWriter) { + lock (m_lock) { if (!IsDisposed) { // тут гарантировано еще не освобожден m_textWriter m_textWriter.WriteLine(msg); @@ -35,7 +36,7 @@ namespace Implab.Diagnostics { base.Dispose(disposing); if (disposing) { // IsDisposed = true - lock (m_textWriter) { + lock (m_lock) { Safe.Dispose(m_textWriter); } } diff --git a/Implab/Implab.csproj b/Implab/Implab.csproj --- a/Implab/Implab.csproj +++ b/Implab/Implab.csproj @@ -75,6 +75,7 @@ + @@ -269,10 +270,5 @@ - - - - - - + \ No newline at end of file diff --git a/Implab/Promise.cs b/Implab/Promise.cs --- a/Implab/Promise.cs +++ b/Implab/Promise.cs @@ -3,11 +3,10 @@ using Implab.Parallels; namespace Implab { public class Promise : AbstractPromise, IDeferred { - public static readonly Promise SUCCESS; + public static readonly IPromise Success; static Promise() { - SUCCESS = new Promise(); - SUCCESS.Resolve(); + Success = new SuccessPromise(); } public void Resolve() { @@ -16,7 +15,11 @@ namespace Implab { public void Reject(Exception error) { SetError(error); - } + } + + public static IPromise FromException(Exception exception) { + return new FailedPromise(exception); + } } } diff --git a/Implab/PromiseExtensions.cs b/Implab/PromiseExtensions.cs --- a/Implab/PromiseExtensions.cs +++ b/Implab/PromiseExtensions.cs @@ -2,7 +2,8 @@ using System; using Implab.Diagnostics; using System.Collections.Generic; - +using System.Linq; + namespace Implab { public static class PromiseExtensions { public static IPromise DispatchToCurrentContext(this IPromise that) { @@ -75,7 +76,7 @@ namespace Implab { ((ICancellable)cookie).Cancel(new TimeoutException()); } - /// +/// /// Cancells promise after the specified timeout is elapsed. /// /// The promise to cancel on timeout. @@ -88,7 +89,17 @@ namespace Implab { return that; } - public static IPromise Bundle(this ICollection that) { + public static IPromise PromiseAll(this IEnumerable that) { + Safe.ArgumentNotNull(that, "that"); + return PromiseAll(that.ToList()); + } + + public static IPromise PromiseAll(this IEnumerable> that) { + Safe.ArgumentNotNull(that, "that"); + return PromiseAll(that.ToList()); + } + + public static IPromise PromiseAll(this ICollection that) { Safe.ArgumentNotNull(that, "that"); int count = that.Count; @@ -128,7 +139,7 @@ namespace Implab { return medium; } - public static IPromise Bundle(this ICollection> that) { + public static IPromise PromiseAll(this ICollection> that) { Safe.ArgumentNotNull(that, "that"); int count = that.Count; diff --git a/Implab/Safe.cs b/Implab/Safe.cs --- a/Implab/Safe.cs +++ b/Implab/Safe.cs @@ -83,7 +83,7 @@ namespace Implab try { action(); - return Promise.SUCCESS; + return Promise.Success; } catch (Exception err) { return new FailedPromise(err); }