Пример #1
0
        /// <summary>Runs each promise delegate. Note that if any method fails, this will catch it.</summary>
        private void run(bool success, object result)
        {
            List <PromiseFrame> pd = eventsToRun;

            eventsToRun = null;

            if (pd == null)
            {
                return;
            }

            try{
                for (int i = 0; i < pd.Count; i++)
                {
                    // Get the frame:
                    PromiseFrame frame = pd[i];

                    // Run it now:
                    Promise prom = frame.run(success, result);

                    if (prom != null)
                    {
                        // We're now targeting it instead:
                        if (prom.target != null)
                        {
                            prom = prom.target;
                        }

                        target = prom;

                        // Yep! All further methods are added to *that* promise and we quit here.
                        for (int x = i + 1; x < pd.Count; x++)
                        {
                            // Add directly:
                            if (prom.eventsToRun == null)
                            {
                                prom.eventsToRun = new List <PromiseFrame>();
                            }

                            // Add the frame:
                            prom.eventsToRun.Add(pd[x]);
                        }

                        // If prom is actually already done, instantly run it too:
                        if (prom.state != PROMISE_PENDING)
                        {
                            // Run it now!
                            prom.run(prom.state == PROMISE_FULFILLED, prom.latestMessage);
                        }

                        return;
                    }
                }
            }catch (Exception e) {
                Dom.Log.Add("Promise method failure: " + e.ToString());
            }
        }
Пример #2
0
        /// <summary>Provides methods to run when this promise completes.</summary>
        public Promise then(PromiseDelegate onFulfil, PromiseDelegate onReject)
        {
            if (target != null)
            {
                return(target.then(onFulfil, onReject));
            }

            // Create the frame now:
            PromiseFrame frame = new PromiseFrame();

            frame.success = onFulfil;
            frame.fail    = onReject;

            // Already complete?
            if (state != PROMISE_PENDING)
            {
                // Instantly run:
                Promise prom = frame.run((state == PROMISE_FULFILLED), latestMessage);

                if (prom != null)
                {
                    // We've got a new target!
                    target = prom;
                    return(prom);
                }

                return(this);
            }


            // Add a frame now:
            if (eventsToRun == null)
            {
                eventsToRun = new List <PromiseFrame>();
            }

            eventsToRun.Add(frame);

            return(this);
        }