Mercurial > pub > ImplabNet
diff Implab/Promise.cs @ 104:5f10d54b45df v2
renamed Promise.Last -> Promise.On
Promise.On doesn't changes Promise.IsExclusive property
author | cin |
---|---|
date | Sun, 09 Nov 2014 23:03:45 +0300 |
parents | 279e226dffdd |
children | 4d308952fd5e |
line wrap: on
line diff
--- a/Implab/Promise.cs Sat Nov 08 10:02:47 2014 +0300 +++ b/Implab/Promise.cs Sun Nov 09 23:03:45 2014 +0300 @@ -117,7 +117,8 @@ if (parent.IsExclusive) parent.Cancel(); }, - null + null, + false ); } @@ -220,7 +221,7 @@ var medium = new Promise<T>(this); - AddHandler(success, error, cancel, medium); + AddHandler(success, error, cancel, medium, true); return medium; } @@ -238,7 +239,7 @@ var medium = new Promise<T>(this); - AddHandler(success, error, null, medium); + AddHandler(success, error, null, medium, true); return medium; } @@ -252,7 +253,7 @@ var medium = new Promise<T>(this); - AddHandler(success, null, null, medium); + AddHandler(success, null, null, medium, true); return medium; } @@ -274,7 +275,7 @@ /// всей цепи обещаний снизу (с самого последнего обещания). /// </para> /// </remarks> - public void Last(Action<T> success, Action<Exception> error, Action cancel) { + public void On(Action<T> success, Action<Exception> error, Action cancel) { if (success == null && error == null && cancel == null) return; @@ -284,15 +285,28 @@ error(err); return default(T); }; - AddHandler(success, errorHandler, cancel, null); + AddHandler(success, errorHandler, cancel, null, false); + } + + public void On(Action<T> success, Action<Exception> error) { + On(success, error, null); + } + + public void On(Action<T> success) { + On(success, null, null); } - public void Last(Action<T> success, Action<Exception> error) { - Last(success, error, null); - } + public void On(Action handler, PromiseEventType events) { + Safe.ArgumentNotNull(handler, "handler"); - public void Last(Action<T> success) { - Last(success, null, null); + Action<T> success = events.HasFlag(PromiseEventType.Success) ? new Action<T>(x => handler()) : null; + Func<Exception,T> error = events.HasFlag(PromiseEventType.Error) ? new Func<Exception,T>(e => { + handler(); + return default(T); + }) : null; + Action cancel = events.HasFlag(PromiseEventType.Cancelled) ? handler : null; + + AddHandler(success, error, cancel, null, false); } public IPromise Error(Action<Exception> error) { @@ -308,7 +322,8 @@ return default(T); }, null, - medium + medium, + true ); return medium; @@ -328,7 +343,7 @@ var medium = new Promise<T>(this); - AddHandler(null, handler, null, medium); + AddHandler(null, handler, null, medium, true); return medium; } @@ -380,7 +395,8 @@ resultHandler, errorHandler, cancelHandler, - null + null, + true ); return medium; @@ -421,7 +437,7 @@ var promise = chained(result); - promise.Last( + promise.On( medium.Resolve, medium.Reject, () => medium.Reject(new OperationCanceledException()) // внешняя отмена связанной операции рассматривается как ошибка @@ -430,7 +446,7 @@ // notify chained operation that it's not needed anymore // порядок вызова Then, Cancelled важен, поскольку от этого // зависит IsExclusive - medium.Last( + medium.On( null, null, () => { @@ -447,7 +463,7 @@ try { var promise = error(e); - promise.Last( + promise.On( medium.Resolve, medium.Reject, () => medium.Reject(new OperationCanceledException()) // внешняя отмена связанной операции рассматривается как ошибка @@ -486,7 +502,8 @@ resultHandler, errorHandler, cancelHandler, - null + null, + true ); return medium; @@ -502,7 +519,7 @@ public IPromise<T> Cancelled(Action handler) { var medium = new Promise<T>(this); - AddHandler(null, null, handler, medium); + AddHandler(null, null, handler, medium, false); return medium; } @@ -513,7 +530,9 @@ /// <returns>self</returns> public IPromise<T> Anyway(Action handler) { Safe.ArgumentNotNull(handler, "handler"); - + + var medium = new Promise<T>(this); + AddHandler( x => handler(), e => { @@ -521,9 +540,11 @@ throw new TransientPromiseException(e); }, handler, - null + medium, + true ); - return this; + + return medium; } /// <summary> @@ -579,8 +600,8 @@ return Join(Timeout.Infinite); } - void AddHandler(Action<T> success, Func<Exception,T> error, Action cancel, Promise<T> medium) { - if (success != null || error != null) + void AddHandler(Action<T> success, Func<Exception,T> error, Action cancel, Promise<T> medium, bool inc) { + if (inc) Interlocked.Increment(ref m_childrenCount); var handler = new HandlerDescriptor { @@ -784,39 +805,39 @@ } IPromise ChainNoResult(Func<IPromise> chained, Func<Exception,IPromise> error, Action cancel) { - Safe.ArgumentNotNull(chained, "chained"); + Safe.ArgumentNotNull(chained, "chained"); var medium = new Promise<object>(this); Action<T> resultHandler = delegate { - if (medium.IsCancelled) - return; + if (medium.IsCancelled) + return; - var promise = chained(); + var promise = chained(); - promise.Last( - medium.Resolve, - medium.Reject, - () => medium.Reject(new OperationCanceledException()) // внешняя отмена связанной операции рассматривается как ошибка - ); + promise.On( + medium.Resolve, + medium.Reject, + () => medium.Reject(new OperationCanceledException()) // внешняя отмена связанной операции рассматривается как ошибка + ); - // notify chained operation that it's not needed anymore - // порядок вызова Then, Cancelled важен, поскольку от этого - // зависит IsExclusive - medium.Cancelled(() => { - if (promise.IsExclusive) - promise.Cancel(); - }); - }; + // notify chained operation that it's not needed anymore + // порядок вызова Then, Cancelled важен, поскольку от этого + // зависит IsExclusive + medium.Cancelled(() => { + if (promise.IsExclusive) + promise.Cancel(); + }); + }; Func<Exception,T> errorHandler; - if (error != null) - errorHandler = delegate(Exception e) { + if (error != null) + errorHandler = delegate(Exception e) { try { var promise = error(e); - promise.Last( + promise.On( medium.Resolve, medium.Reject, () => medium.Reject(new OperationCanceledException()) // внешняя отмена связанной операции рассматривается как ошибка @@ -834,50 +855,53 @@ } return default(T); }; - else - errorHandler = err => { + else + errorHandler = err => { medium.Reject(err); return default(T); }; - Action cancelHandler; - if (cancel != null) - cancelHandler = () => { + Action cancelHandler; + if (cancel != null) + cancelHandler = () => { if (cancel != null) cancel(); medium.Cancel(); }; - else - cancelHandler = medium.Cancel; + else + cancelHandler = medium.Cancel; - AddHandler( - resultHandler, - errorHandler, - cancelHandler, - null - ); + AddHandler( + resultHandler, + errorHandler, + cancelHandler, + null, + true + ); - return medium; + return medium; } + IPromise IPromise.Chain(Func<IPromise> chained, Func<Exception,IPromise> error) { return ChainNoResult(chained, error, null); } + IPromise IPromise.Chain(Func<IPromise> chained) { return ChainNoResult(chained, null, null); - } + } - void IPromise.Last(Action success, Action<Exception> error, Action cancel) { - Last(x => success(), error, cancel); + void IPromise.On(Action success, Action<Exception> error, Action cancel) { + On(x => success(), error, cancel); } - void IPromise.Last(Action success, Action<Exception> error) { - Last(x => success(), error, null); + void IPromise.On(Action success, Action<Exception> error) { + On(x => success(), error, null); } - void IPromise.Last(Action success) { - Last(x => success(), null, null); + void IPromise.On(Action success) { + On(x => success(), null, null); } IPromise IPromise.Error(Action<Exception> error) {