예제 #1
0
 public Widget_MapPreview(IPromise <Texture2D> promise, string seed)
 {
     PrepareComponents();
     this.promise = promise;
     promise.Done(OnPromiseResolved);
     this.seed = seed;
 }
예제 #2
0
        private void ExecuteSignalAsync(IGridSignal signal)
        {
            IPromise p = null;

            if (signal is AddMovieToGridSignal addMovieToGridSignal)
            {
                p = AddMovieToGrid(addMovieToGridSignal.Model);
            }
            else if (signal is RemoveMovieFromGridSignal removeMovieFromGridSignal)
            {
                p = RemoveMovieFromGrid(removeMovieFromGridSignal);
            }
            else
            {
                Debug.LogException(new NotImplementedException("Add Clear Queue case."));
            }

            var tmpSignal = signal;

            p.Done(() =>
            {
                lock (_gridSignalsQueue)
                {
                    _gridSignalsQueue.Remove(tmpSignal);
                }
                CheckAndExecuteQueue();
            });
        }
        public void Execute(UnloadAllScenesExceptSignal loadParams)
        {
            SceneManager.SetActiveScene(SceneManager.GetSceneByName(loadParams.Scene));

            IPromise lastPromise = null;

            int count = SceneManager.sceneCount;

            for (int i = 0; i < count; i++)
            {
                Scene scene = SceneManager.GetSceneAt(i);

                if (scene.isLoaded && !scene.name.Equals(loadParams.Scene))
                {
                    if (lastPromise != null)
                    {
                        lastPromise = lastPromise.Then(() => _sceneLoader.UnloadScene(scene.name));
                    }
                    else
                    {
                        lastPromise = _sceneLoader.UnloadScene(scene.name);
                    }
                }
            }

            //Add promise to resolve OnComplete
            if (lastPromise != null)
            {
                lastPromise.Done(
                    () =>
                {
                    Debug.Log(string.Format("{0} , scene loading/unloading completed!", this));

                    if (loadParams.OnComplete != null)
                    {
                        loadParams.OnComplete.Resolve();
                    }
                },
                    exception =>
                {
                    Debug.LogError("UnloadAllScenesExceptCommand.Execute: " + exception.ToString());

                    if (loadParams.OnComplete != null)
                    {
                        loadParams.OnComplete.Reject(exception);
                    }
                }
                    );
            }
            else
            {
                Debug.Log(string.Format("{0} , no scenes loaded/unloaded!", this));

                if (loadParams.OnComplete != null)
                {
                    loadParams.OnComplete.Resolve();
                }
            }
        }
예제 #4
0
 public Widget_MapPreview(Widget_MapPreview copyFrom)
 {
     PrepareComponents();
     promise = copyFrom.promise;
     promise.Done(OnPromiseResolved);
     seed              = copyFrom.seed;
     IsFavorite        = copyFrom.IsFavorite;
     OnFavoriteToggled = copyFrom.OnFavoriteToggled;
 }
예제 #5
0
        public static IPromise <D> Convert <S, D>(this IPromise <S> ext, Func <S, D> transform)
        {
            Deferred <D> def = new Deferred <D>();
            IPromise <D> res = new PromiseImpl <D>();

            ext.Done((value) => def.Resolve(transform(value)));
            ext.Fail((e) => def.Reject(e));

            return(res);
        }
예제 #6
0
        public static IEnumerator WaitForPromise(IPromise promise)
        {
            bool isComplete = false;

            promise.Done(() => isComplete = true);
            while (!isComplete)
            {
                yield return(null);
            }
        }
예제 #7
0
        public static T Join <T>(this IPromise <T> promise)
        {
            T   result = default(T);
            var sync   = new ManualResetEvent(false);

            promise.Done(res =>
            {
                result = res;
                sync.Set();
            });
            return(result);
        }
예제 #8
0
        public IPromise <List <MovieDataModel> > GetMovies(string name, int count = 10)
        {
            Promise <List <MovieDataModel> > promise = new Promise <List <MovieDataModel> >();

            if (!_moviesSearchResult.ContainsKey(name))
            {
                List <MovieDataModel> models = _moviesSearchResult[name] = new List <MovieDataModel>();

                IPromise <List <MovieDataModel> > p = null;

                List <int> page = new List <int>();
                for (int i = models.Count, pageNumber = models.Count / 10; i <= count; i += 10)
                {
                    if (p == null)
                    {
                        p = GetMoviesInternal(name, ++pageNumber);
                    }
                    else
                    {
                        p = p.Then(lst =>
                        {
                            lst.ForEach(models.Add);
                            return(GetMoviesInternal(name, ++pageNumber));
                        });
//                        p = GetMoviesInternal(name, ++pageNumber).Then(lst =>
//                        {
//                            lst.ForEach(models.Add);
//                        });
                    }
                }

                if (p == null)
                {
                    promise.Resolve(models);
                }
                else
                {
                    p.Done(lst =>
                    {
                        lst.ForEach(models.Add);
                        promise.Resolve(models);
                    });
                }
            }
            else
            {
                Debug.Log("Result found in Cache.");
                promise.Resolve(_moviesSearchResult[name]);
            }

            return(promise);
        }
예제 #9
0
    public void Command(Func <IPromise> command)
    {
        var ready = new Promise();

        UpdateAfter(ready);
        commands = commands.Then(command).Then(() => {
            if (ready.CurState == PromiseState.Pending)
            {
                ready.Resolve();
            }
        });
        commands.Done();
    }
예제 #10
0
        public static IPromise <T> Then <T>(this IPromise <T> ext, Func <T, IPromise <T> > success, Action <Exception> fail)
        {
            Deferred <T> def = new Deferred <T>();
            IPromise <T> res = def.Promise();

            if (fail != null)
            {
                res.Fail(fail);
            }

            ext.Done((value) => success(value).Done(p => def.Resolve(p)).Fail(e => def.Reject(e)));
            ext.Fail((e) => def.Reject(e));

            return(res);
        }
        public static IPromise WaitAFrame(this IPromise p, int n = 1)
        {
            IEnumerator OneFrame(int x)
            {
                for (var i = 0; i < x; i++)
                {
                    yield return(null);
                }
            }

            var next = new Promise();

            p.Done(() => BPF_CoroutineInvoker.Invoke(OneFrame(n), next.Resolve));
            return(next);
        }
예제 #12
0
        /// <summary>
        /// adds new promise constructor to current promise and returns wrapper that resolves with added promise
        /// A.Then(B) - returns promise that resolves when promise from B is resolved
        /// </summary>
        /// <param name="next">constructor for next promise</param>
        /// <returns>Promise</returns>
        public IPromise Then(Func <IPromise> next)
        {
            Deferred deferred = Deferred.GetFromPool();

            Done(() =>
            {
                IPromise promise = next();

                promise.Done(() => deferred.Resolve());
                promise.Fail(ex => deferred.Reject(ex));
            });

            Fail(ex => deferred.Reject(ex));

            return(deferred);
        }
        //First loads an optional array of scenes, then
        //unloads a separate optional array of scenes.
        //Each call to load/unload is added into a
        //promise chain. When complete, the last promise
        //fires an optional OnComplete delegate.
        public void Execute(LoadUnloadScenesSignal loadUnloadParams)
        {
            IPromise lastPromise = null;

            //Load scenes
            if (loadUnloadParams.LoadScenes != null)
            {
                foreach (var sceneName in loadUnloadParams.LoadScenes)
                {
                    var name = sceneName;
                    lastPromise = lastPromise != null?lastPromise.Then(() => _sceneLoader.LoadScene(name)) : _sceneLoader.LoadScene(sceneName);
                }
            }

            //Unload scenes
            if (loadUnloadParams.UnloadScenes != null)
            {
                foreach (var sceneName in loadUnloadParams.UnloadScenes)
                {
                    var name = sceneName;
                    lastPromise = lastPromise != null?lastPromise.Then(() => _sceneLoader.UnloadScene(name)) : _sceneLoader.UnloadScene(sceneName);
                }
            }

            //Add promise to resolve OnComplete
            if (lastPromise != null)
            {
                lastPromise.Done(
                    () =>
                {
                    Debug.Log($"{this} , scene loading/unloading completed!");

                    loadUnloadParams.OnComplete?.Resolve();
                },
                    exception =>
                {
                    loadUnloadParams.OnComplete?.Reject(exception);
                }
                    );
            }
            else
            {
                Debug.Log($"{this} , no scenes loaded/unloaded!");

                loadUnloadParams.OnComplete?.Resolve();
            }
        }
예제 #14
0
        public void RSGPromise()
        {
            var deferred = new Promise <object>();
            IPromise <object> promise = deferred;

            for (int i = 0; i < N; ++i)
            {
                promise = promise
                          // Native methods.
                          .ContinueWith(() => RSGPromiseHelper.rsgVoid)
                          .ContinueWith(() => RSGPromiseHelper.rsgVector)
                          .ContinueWith(() => RSGPromiseHelper.rsgObject);
            }

            promise.Done();
            deferred.Resolve(Instances.obj);
        }
예제 #15
0
        public void RSGPromise()
        {
            var deferred = new Promise <object>();
            IPromise <object> promise = deferred;

            for (int i = 0; i < N; ++i)
            {
                promise = promise
                          // Extension methods created here.
                          .ContinueWith(() => { })
                          .ContinueWith(() => Instances.vector)
                          .ContinueWith(() => Instances.obj);
            }

            promise.Done();
            deferred.Resolve(Instances.obj);
        }
예제 #16
0
        public IPromise MakeAITurn()
        {
            EntityFaction opposingFaction = Faction == EntityFaction.Player ? EntityFaction.Enemy : EntityFaction.Player;
            bool          attackSuccess   = TryAttackFractionInRange(opposingFaction);

            if (attackSuccess == false)
            {
                Entity closestPlayerCharacter = levelService.GetClosestCharacter(GridPosition, opposingFaction);
                if (closestPlayerCharacter != null)
                {
                    List <Vector2Int> path        = gridNavigator.GetPath(this, closestPlayerCharacter.GridPosition, MaxWalkDistance, closestPlayerCharacter);
                    Vector2Int        moveTarget  = path.Last() == closestPlayerCharacter.GridPosition ? path[path.Count - 2] : path[path.Count - 1];
                    IPromise          movePromise = Move(moveTarget);
                    movePromise.Done(() => TryAttackFractionInRange(opposingFaction));
                    return(movePromise);
                }
            }
            return(Deferred.GetFromPool().Resolve());
        }
예제 #17
0
        public void RSGPromise()
        {
            var deferred = new Promise <object>();
            IPromise <object> promise = deferred;

            for (int i = 0; i < N; ++i)
            {
                int index = i;
                promise = promise
                          // Native methods.
                          .ContinueWith(() => (IPromise)RSGPromiseHelper.rsgVoids[index])
                          .ContinueWith(() => (IPromise <Vector4>)RSGPromiseHelper.rsgVectors[index])
                          .ContinueWith(() => (IPromise <object>)RSGPromiseHelper.rsgObjects[index]);
            }

            promise.Done();
            deferred.Resolve(Instances.obj);
            RSGPromiseHelper.ResolveDeferreds();
        }
        public void RSGPromise()
        {
            var deferred = new Promise <object>();
            IPromise <object> promise = deferred;

            for (int i = 0; i < N; ++i)
            {
                promise = promise
                          // Extension methods created here.
                          .ContinueWith(() => { })
                          .ContinueWith(() => Program.vector)
                          .ContinueWith(() => Program.obj)
                          // Native methods.
                          .ContinueWith(() => RSGPromiseHelper.rsgVoid)
                          .ContinueWith(() => RSGPromiseHelper.rsgVector)
                          .ContinueWith(() => RSGPromiseHelper.rsgObject);
            }

            promise.Done();
            deferred.Resolve(Program.obj);
        }
예제 #19
0
파일: Defer.cs 프로젝트: linq2js/inieditor
        public virtual IPromise Then(IPromise nextPromise)
        {
            var nextDeferred = new Deferred();

            this.Done(() =>
            {
                nextPromise
                .Done(() =>
                {
                    nextDeferred.Resolve();
                })
                .Fail((error) =>
                {
                    nextDeferred.Reject(error);
                });
            })
            .Fail((error) =>
            {
                nextDeferred.Reject(error);
            });
            return(nextDeferred.Promise());
        }
예제 #20
0
        /// <summary>
        /// Convert a promise to a .Net 4.0 Task for use with the <c>async</c>/<c>await</c> keywords
        /// </summary>
        public static Task <T> ToTask <T>(this IPromise <T> promise, CancellationToken ct = default(CancellationToken))
        {
            var tcs = new TaskCompletionSource <T>();

            promise.Done(v =>
            {
                try
                {
                    tcs.TrySetResult(v);
                }
                catch (Exception ex)
                {
                    tcs.TrySetException(ex);
                }
            }).Fail(ex =>
            {
                var cancel = ex as OperationCanceledException;
                if (cancel == null)
                {
                    tcs.TrySetException(ex);
                }
                else
                {
                    tcs.TrySetCanceled();
                }
            });

            if (ct != default(CancellationToken))
            {
                ct.Register(() =>
                {
                    promise.Cancel();
                });
            }

            return(tcs.Task);
        }
예제 #21
0
        public static IPromise <T> AddTimeout <T>(IPromise <T> promise, ITimers timers, float timeout)
        {
            var result = new Deferred <T>();
            var status = EStatus.Pending;

            var timeoutPromise = timers.Wait(timeout);

            promise.Done(r =>
            {
                if (status == EStatus.Pending)
                {
                    status = EStatus.Resolved;
                    result.Resolve(r);
                }
            });

            promise.Fail(() =>
            {
                if (status == EStatus.Pending)
                {
                    status = EStatus.Rejected;
                    result.Reject();
                }
            });

            timeoutPromise.Done(() =>
            {
                if (status == EStatus.Pending)
                {
                    status = EStatus.Rejected;
                    result.Reject();
                }
            });

            return(result);
        }
예제 #22
0
 public IPromise <D, F, P> Done(Action <D> doneCallback)
 {
     return(promise.Done(doneCallback));
 }
예제 #23
0
        //First loads an optional array of scenes, then
        //unloads a separate optional array of scenes.
        //Each call to load/unload is added into a
        //promise chain. When complete, the last promise
        //fires an optional OnComplete delegate.
        public void Execute(LoadUnloadScenesSignal loadUnloadParams)
        {
            IPromise lastPromise = null;

            //Load scenes
            if (loadUnloadParams.LoadScenes != null)
            {
                for (int i = 0; i < loadUnloadParams.LoadScenes.Length; i++)
                {
                    string sceneName = loadUnloadParams.LoadScenes[i];
                    if (lastPromise != null)
                    {
                        lastPromise = lastPromise.Then(() => _sceneLoader.LoadScene(sceneName));
                    }
                    else
                    {
                        lastPromise = _sceneLoader.LoadScene(sceneName);
                    }
                }
            }

            //Unload scenes
            if (loadUnloadParams.UnloadScenes != null)
            {
                for (int i = 0; i < loadUnloadParams.UnloadScenes.Length; i++)
                {
                    string sceneName = loadUnloadParams.UnloadScenes[i];
                    if (lastPromise != null)
                    {
                        lastPromise = lastPromise.Then(() => _sceneLoader.UnloadScene(sceneName));
                    }
                    else
                    {
                        lastPromise = _sceneLoader.UnloadScene(sceneName);
                    }
                }
            }

            //Add promise to resolve OnComplete
            if (lastPromise != null)
            {
                lastPromise.Done(
                    () =>
                {
                    Debug.Log(string.Format("{0} , scene loading/unloading completed!", this));

                    loadUnloadParams.OnComplete?.Resolve();
                },
                    exception =>
                {
                    loadUnloadParams.OnComplete?.Reject(exception);
                }
                    );
            }
            else
            {
                Debug.Log(string.Format("{0} , no scenes loaded/unloaded!", this));

                loadUnloadParams.OnComplete?.Resolve();
            }
        }