Beispiel #1
0
        public void CreateAsync <T>(ViewLink link, Action <T> onLoad, Action <Exception> error = null) where T : class, IView
        {
            if (pools.TryGetValue(link.GetHashCode(), out Pool pool))
            {
                onLoad(pool.Take <T>());
                return;
            }

            link.Load(prefab =>
            {
                if (pools.TryGetValue(link.GetHashCode(), out Pool p))
                {
                    onLoad(p.Take <T>());
                    return;
                }

                if (prefab == null)
                {
                    Exception e = new NullReferenceException($"Cannot load view of type {typeof(T).Name}: {link}");
                    Debug.LogException(e);
                    error?.Invoke(e);
                    return;
                }

                pool = Pool.Create(() => Create <T>(prefab.GetRoot()));
                pools.Add(link.GetHashCode(), pool);
                onLoad(pool.Take <T>());
            }, error);
        }
 public void UnloadStage(ViewLink stage)
 {
     if (loadedStages.TryGetValue(stage, out var loadedStage))
     {
         loadedStage.UnloadStage(this);
         loadedStages.Remove(stage);
     }
 }
Beispiel #3
0
        public IPromise <T> CreateAsync <T>(ViewLink link) where T : class, IView
        {
            Promise <T> promise = Promise <T> .Create();

            CreateAsync <T>(link, r => promise.Resolve(r), e => promise.Reject(e));

            return(promise);
        }
        public IPromise <T> Load <T>(ViewLink stageLink) where T : class, IStage
        {
            if (IsLoading.Value)
            {
                return(Promise <T> .Rejected(new InvalidOperationException("Already loading stage")));
            }

            isLoading.SetValue(true);
            Promise <T> promise = Promise <T> .Create();

            Promise loadPromise = Promise.Create();

            App.Core.MakeTransition(loadPromise, () =>
            {
                if (ActiveStageLink != null && ActiveStageLink.GetPath() != stageLink.GetPath())
                {
                    history.Push(ActiveStageLink);
                    ActiveStage.CloseStage(this);
                }

                void SetActive(ViewLink link, T stage)
                {
                    ActiveStage     = stage;
                    ActiveStageLink = link;
                    stage.OpenStage(this).Done(() =>
                    {
                        isLoading.SetValue(false);
                        loadPromise.Resolve();
                        promise.Resolve(stage);

                        if (loadedStages.ContainsKey(link) == false)
                        {
                            loadedStages.Add(link, stage);
                        }
                        OnStageLoaded(link);
                    });
                }

                if (loadedStages.ContainsKey(stageLink))
                {
                    T loaded = loadedStages[stageLink] as T;
                    SetActive(stageLink, loaded);
                }
                else
                {
                    App.Core.Views.CreateAsync <T>(stageLink).Done(stage =>
                    {
                        SetActive(stageLink, stage);
                    });
                }
            });

            return(promise);
        }
Beispiel #5
0
 public T GetOrCreateView <T>(ViewLink link) where T : class, IView
 {
     if (pools.TryGetValue(link.GetHashCode(), out Pool pool))
     {
         foreach (var item in pool.GetActiveItems <T>())
         {
             return(item);
         }
     }
     return(Create <T>(link));
 }
Beispiel #6
0
 public T Create <T>(ViewLink link) where T : class, IView
 {
     if (IsLoaded(link))
     {
         return(pools[link.GetHashCode()].Take <T>());
     }
     if (AssetCache.Loaded(link.GetPath()))
     {
         Pool pool = Pool.Create(() => Create <T>(link.Value.GetRoot()));
         pools.Add(link.GetHashCode(), pool);
         return(pool.Take <T>());
     }
     Debug.LogError($"Cannot create view {link} synchronously. Not loaded yet. Use CreateAsync or WarmUp");
     return(null);
 }
Beispiel #7
0
 private T TakeFromPool <T>(ViewLink link) where T : class, IView
 {
     return(pools[link.GetHashCode()].Take <T>());
 }