コード例 #1
0
ファイル: Promise.cs プロジェクト: studentutu/swift-framework
        public static IPromise <T> Race(IEnumerable <IPromise <T> > promises)
        {
            Promise <T> promise = Create();

            foreach (IPromise <T> p in promises)
            {
                p.Then(r =>
                {
                    if (promise.CurrentState == PromiseState.Pending)
                    {
                        promise.Resolve(r);
                    }
                })
                .Catch(e =>
                {
                    if (promise.CurrentState == PromiseState.Pending)
                    {
                        promise.Reject(e);
                    }
                });
            }

            return(promise);
        }
コード例 #2
0
        public virtual IPromise <T> Load()
        {
            if (loadPromise != null)
            {
                return(loadPromise);
            }

            loadPromise = Promise <T> .Create();

            if (IsGenerated())
            {
                loadPromise.Resolve(Value);
                return(loadPromise);
            }

            if (HasValue == false)
            {
                loadPromise.Reject(new EntryPointNotFoundException($"Link doesn't have any value: {GetPath()}"));
                return(loadPromise);
            }

#if USE_ADDRESSABLES
            if (loaded || AssetCache.Loaded(Path))
            {
                loadPromise.Resolve(Value);
                return(loadPromise);
            }

            loadHandle = Addressables.LoadAssetAsync <ScriptableObject>(Path);

            loadHandle.Value.Completed += a =>
            {
                if (Loaded == false)
                {
                    if (a.Status == AsyncOperationStatus.Succeeded)
                    {
                        cachedAsset = a.Result as T;
                        Initialize(cachedAsset);
                        loaded = cachedAsset != null;
                        loadPromise.Resolve(cachedAsset);
                    }
                    else
                    {
                        loadPromise.Reject(new EntryPointNotFoundException($"Cannot load value from link: {Path}"));
                    }
                }
            };
#else
            if (loaded)
            {
                loadPromise.Resolve(Value);
                return(loadPromise);
            }

            loadHandle = Resources.LoadAsync <ScriptableObject>(Path);

            loadHandle.completed += a =>
            {
                if (Loaded == false)
                {
                    if (a.isDone)
                    {
                        cachedAsset = loadHandle.asset as T;
                        Initialize(cachedAsset);
                        loaded = cachedAsset != null;
                        loadPromise.Resolve(cachedAsset);
                    }
                    else
                    {
                        loadPromise.Reject(new EntryPointNotFoundException($"Cannot load value from link: {Path}"));
                    }
                }
            };
#endif

            return(loadPromise);
        }
コード例 #3
0
ファイル: LinkTo.cs プロジェクト: dimaswift/swift-framework
        public virtual IPromise <T> Load()
        {
            if (loadPromise != null)
            {
                return(loadPromise);
            }

            loadPromise = Promise <T> .Create();

            if (HasValue == false)
            {
                loadPromise.Reject(new EntryPointNotFoundException("Link doesn't have any value"));
                return(loadPromise);
            }

#if USE_ADDRESSABLES
            if (loaded || AssetCache.Loaded(Path))
            {
                loadPromise.Resolve(Value);
                return(loadPromise);
            }

            if (IsGenerated())
            {
                cachedAsset = App.Core.Storage.Load <T>(this);
                Initialize(cachedAsset);
                loaded = cachedAsset != null;
                loadPromise.Resolve(cachedAsset);
                return(loadPromise);
            }

            loadHandle = Addressables.LoadAssetAsync <T>(Path);

            loadHandle.Value.Completed += a =>
            {
                if (Loaded == false)
                {
                    if (a.Status == AsyncOperationStatus.Succeeded)
                    {
                        cachedAsset = a.Result;
                        Initialize(cachedAsset);
                        loaded = cachedAsset != null;
                        loadPromise.Resolve(a.Result);
                    }
                    else
                    {
                        var e = new EntryPointNotFoundException($"Cannot load value from link: {Path}");
                        UnityEngine.Debug.LogException(e);
                        loadPromise.Reject(e);
                    }
                }
            };
#else
            if (loaded)
            {
                loadPromise.Resolve(Value);
                return(loadPromise);
            }

            if (IsGenerated())
            {
                cachedAsset = App.Core.Storage.Load <T>(this);
                Initialize(cachedAsset);
                loaded = cachedAsset != null;
                loadPromise.Resolve(cachedAsset);
                return(loadPromise);
            }

            loadHandle = Resources.LoadAsync <T>(Path);

            loadHandle.completed += r =>
            {
                if (Loaded == false && loadHandle.isDone && loadHandle.asset)
                {
                    cachedAsset = loadHandle.asset as T;
                    Initialize(cachedAsset);
                    loaded = cachedAsset != null;
                    loadPromise.Resolve(cachedAsset);
                }
                else
                {
                    loadPromise.Reject(new EntryPointNotFoundException($"Cannot load resource of type {typeof(T)} at path {Path}"));
                }
            };
#endif

            return(loadPromise);
        }
コード例 #4
0
        private IEnumerator WaitForCoroutine(float seconds, Promise promise)
        {
            yield return(new WaitForSeconds(seconds));

            promise.Resolve();
        }
コード例 #5
0
        private IEnumerator WaitForUnscaledCoroutine(float seconds, Promise promise)
        {
            yield return(new WaitForSecondsRealtime(seconds));

            promise.Resolve();
        }
コード例 #6
0
        private IEnumerator WaitForNextFrameCoroutine(Promise promise)
        {
            yield return(new WaitForEndOfFrame());

            promise.Resolve();
        }
コード例 #7
0
        private IEnumerator WaitForFixedUpdateCoroutine(Promise promise)
        {
            yield return(new WaitForFixedUpdate());

            promise.Resolve();
        }