Beispiel #1
0
 /// <summary>
 /// Takes a function that yields an enumerable of promises.
 /// Converts to a non-value promise.
 /// Returns a promise that resolves when the first of the promises has resolved.
 /// Yields the value from the first promise that has resolved.
 /// </summary>
 public IPromise ThenRace(Func <PromisedT, IEnumerable <IPromise> > chain)
 {
     return(Then(value => Promise.Race(chain(value))));
 }
Beispiel #2
0
        public IPromise <ConvertedT> Transform <ConvertedT>(Func <PromisedT, ConvertedT> transform)
        {
//            Argument.NotNull(() => transform);
            return(Then(value => Promise <ConvertedT> .Resolved(transform(value))));
        }
Beispiel #3
0
 /// <summary>
 /// Takes a function that yields an enumerable of promises.
 /// Returns a promise that resolves when the first of the promises has resolved.
 /// Yields the value from the first promise that has resolved.
 /// </summary>
 public IPromise <ConvertedT> ThenRace <ConvertedT>(Func <PromisedT, IEnumerable <IPromise <ConvertedT> > > chain)
 {
     return(Then(value => Promise <ConvertedT> .Race(chain(value))));
 }
Beispiel #4
0
 /// <summary>
 /// Chain an enumerable of promises, all of which must resolve.
 /// Converts to a non-value promise.
 /// 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.
 /// </summary>
 public IPromise ThenAll(Func <PromisedT, IEnumerable <IPromise> > chain)
 {
     return(this.Then(value => Promise.All(chain(value))));
 }
Beispiel #5
0
 /// <summary>
 /// Complete the promise. Adds a default error handler.
 /// </summary>
 public void Done()
 {
     Catch(ex =>
           Promise.PropagateUnhandledException(this, ex)
           );
 }
Beispiel #6
0
 /// <summary>
 /// Takes a function that yields an enumerable of promises.
 /// Converts to a value promise.
 /// Returns a promise that resolves when the first of the promises has resolved.
 /// </summary>
 public IPromise <ConvertedT> ThenRace <ConvertedT>(Func <IEnumerable <IPromise <ConvertedT> > > chain)
 {
     return(Then(() => Promise <ConvertedT> .Race(chain())));
 }
Beispiel #7
0
 /// <summary>
 /// Chain an enumerable of promises, all of which must resolve.
 /// Returns a promise for a collection of the resolved results.
 /// 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.
 /// </summary>
 public IPromise <IEnumerable <ConvertedT> > ThenAll <ConvertedT>(
     Func <PromisedT, IEnumerable <IPromise <ConvertedT> > > chain)
 {
     return(this.Then(value => Promise <ConvertedT> .All(chain(value))));
 }
Beispiel #8
0
 /// <summary>
 /// 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.
 /// </summary>
 public IPromise ThenAll(Func <IEnumerable <IPromise> > chain)
 {
     return(Then(() => Promise.All(chain())));
 }
Beispiel #9
0
 /// <summary>
 /// Takes a function that yields an enumerable of promises.
 /// Returns a promise that resolves when the first of the promises has resolved.
 /// </summary>
 public IPromise ThenRace(Func <IEnumerable <IPromise> > chain)
 {
     return(Then(() => Promise.Race(chain())));
 }
Beispiel #10
0
        /// <summary>
        /// Add a resolved callback, a rejected callback and a progress callback.
        /// The resolved callback chains a value promise (optionally converting to a different value type).
        /// </summary>
        public IPromise <ConvertedT> Then <ConvertedT>(
            Func <PromisedT, IPromise <ConvertedT> > onResolved,
            Func <Exception, IPromise <ConvertedT> > onRejected,
            Action <float> onProgress
            )
        {
            // This version of the function must supply an onResolved.
            // Otherwise there is now way to get the converted value to pass to the resulting promise.
//            Argument.NotNull(() => onResolved);

            var resultPromise = new Promise <ConvertedT>();

            resultPromise.WithName(Name);

            Action <PromisedT> resolveHandler = null;

            if (IsActionHandlersResolveValid())
            {
                resolveHandler = v =>
                {
                    onResolved(v)
                    .Progress(progress => resultPromise.ReportProgress(progress))
                    .Then(
                        // Should not be necessary to specify the arg type on the next line, but Unity (mono) has an internal compiler error otherwise.
                        chainedValue => resultPromise.Resolve(chainedValue),
                        ex => resultPromise.Reject(ex)
                        );
                };
            }

            Action <Exception> rejectHandler = null;

            if (IsActionHandlersRejectValid())
            {
                rejectHandler = ex =>
                {
                    if (onRejected == null)
                    {
                        resultPromise.Reject(ex);
                        return;
                    }

                    try
                    {
                        onRejected(ex)
                        .Then(
                            chainedValue => resultPromise.Resolve(chainedValue),
                            callbackEx => resultPromise.Reject(callbackEx)
                            );
                    }
                    catch (Exception callbackEx)
                    {
                        resultPromise.Reject(callbackEx);
                    }
                };
            }

            ActionHandlers(resultPromise, resolveHandler, rejectHandler);
            if (onProgress != null)
            {
                ProgressHandlers(this, onProgress);
            }

            return(resultPromise);
        }
Beispiel #11
0
 public IPromise Untyped()
 {
     return(Then(x => Promise.Resolved()));
 }
Beispiel #12
0
 private Promise(PromiseState initialState)
 {
     CurState = initialState;
     Id       = Promise.NextId();
 }
Beispiel #13
0
 protected ASignal()
 {
     OnResult = new RSG.Promise();
 }
Beispiel #14
0
 public static IPromise <T> v <T>(T value)
 {
     return(Promise <T> .Resolved(value));
 }