< Summary

Class:DCL.AssetPromise[AssetType]
Assembly:AssetPromiseKeeper
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/AssetManager/Common/AssetPromise.cs
Covered lines:62
Uncovered lines:12
Coverable lines:74
Total lines:191
Line coverage:83.7% (62 of 74)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ClearEvents()0%2100%
ForceFail(...)0%110100%
SetWaitingState()0%2100%
CallAndClearEvents(...)0%660100%
Load()0%5.045088.24%
GetLibraryAssetCheckId()0%110100%
GetAsset(...)0%110100%
OnReuse(...)0%220100%
OnReuseFinished()0%110100%
OnLoadSuccess()0%2.092071.43%
OnLoadFailure(...)0%110100%
AddToLibrary()0%110100%
Unload()0%220100%
Cleanup()0%440100%
OnForget()0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/AssetManager/Common/AssetPromise.cs

#LineLine coverage
 1using System;
 2using UnityEngine;
 3
 4namespace DCL
 5{
 6    public enum AssetPromiseState
 7    {
 8        IDLE_AND_EMPTY,
 9        WAITING,
 10        LOADING,
 11        FINISHED
 12    }
 13
 14    /// <summary>
 15    /// AssetPromise is in charge of handling all the logic related to loading the specified AssetType.
 16    /// It also should return the cached asset if applicable.
 17    ///
 18    /// If we need settings related to how the asset should be loaded/handled, we add them here.
 19    ///
 20    /// If we need many ways of loading the same Asset type, we can create different AssetPromise
 21    /// subclasses to do so. This can be useful to attach the GLTFSceneImporter loading logic for
 22    /// materials/textures to system.
 23    /// </summary>
 24    /// <typeparam name="AssetType">The Asset type to be loaded.</typeparam>
 25    public abstract class AssetPromise<AssetType> : CustomYieldInstruction
 26        where AssetType : Asset, new()
 27    {
 28        internal bool isDirty = false;
 29        internal AssetLibrary<AssetType> library;
 030        public AssetType asset { get; protected set; }
 31
 032        public AssetPromiseState state { get; protected set; }
 033        public bool isForgotten { get; protected set; }
 34
 35        internal event Action<AssetPromise<AssetType>> OnPreFinishEvent;
 36        public event Action<AssetType> OnSuccessEvent;
 37        public event Action<AssetType, Exception> OnFailEvent;
 38
 771639        public override bool keepWaiting { get { return state == AssetPromiseState.LOADING || state == AssetPromiseState
 40
 41        public void ClearEvents()
 42        {
 043            OnSuccessEvent = null;
 044            OnFailEvent = null;
 045        }
 46
 47        internal void ForceFail(Exception reason)
 48        {
 849            OnPreFinishEvent = null;
 850            CallAndClearEvents(false, reason);
 851            state = AssetPromiseState.IDLE_AND_EMPTY;
 852        }
 53
 54        internal void SetWaitingState()
 55        {
 56            //TODO(Brian): This is made to make the promises yielding not return automatically when the promise is block
 57            //
 58            //             Managing the blocked promises handling entirely in the AssetPromiseKeeper is coupling the cod
 59            //             It's better to have a "WaitForPromise" method here and lighten the APK logic a bit. For now t
 060            state = AssetPromiseState.WAITING;
 061        }
 62
 63        protected void CallAndClearEvents(bool isSuccess, Exception exception)
 64        {
 39765            if (asset == null)
 66            {
 1867                isSuccess = false;
 68            }
 69
 39770            OnPreFinishEvent?.Invoke(this);
 39771            OnPreFinishEvent = null;
 72
 39773            if (isSuccess)
 35374                OnSuccessEvent?.Invoke(asset);
 75            else
 4476                OnFailEvent?.Invoke(asset, exception);
 77
 39778            ClearEvents();
 39779        }
 80
 81        internal virtual void Load()
 82        {
 42683            if (state == AssetPromiseState.LOADING || state == AssetPromiseState.FINISHED)
 284                return;
 85
 42486            state = AssetPromiseState.LOADING;
 87
 88            // NOTE(Brian): Get existent library element
 42489            object libraryAssetCheckId = GetLibraryAssetCheckId();
 42490            if (library.Contains(libraryAssetCheckId))
 91            {
 15792                asset = GetAsset(libraryAssetCheckId);
 93
 15794                if (asset != null)
 95                {
 15796                    OnBeforeLoadOrReuse();
 15797                    OnReuse(OnReuseFinished);
 15798                }
 99                else
 100                {
 0101                    CallAndClearEvents(false, new Exception("Asset is null"));
 102                }
 103
 0104                return;
 105            }
 106
 107            // NOTE(Brian): Get new library element
 267108            asset = new AssetType();
 267109            OnBeforeLoadOrReuse();
 267110            asset.id = GetId();
 111
 267112            OnLoad(OnLoadSuccess, OnLoadFailure);
 267113        }
 114
 346115        protected virtual object GetLibraryAssetCheckId() { return GetId(); }
 116
 139117        protected virtual AssetType GetAsset(object id) { return library.Get(id); }
 118
 206119        protected virtual void OnReuse(Action OnFinish) { OnFinish?.Invoke(); }
 120
 121        protected void OnReuseFinished()
 122        {
 155123            OnAfterLoadOrReuse();
 155124            state = AssetPromiseState.FINISHED;
 155125            CallAndClearEvents(true, null);
 155126        }
 127
 128        protected void OnLoadSuccess()
 129        {
 198130            if (AddToLibrary())
 131            {
 198132                OnAfterLoadOrReuse();
 198133                state = AssetPromiseState.FINISHED;
 198134                CallAndClearEvents(true, null);
 198135            }
 136            else
 137            {
 0138                OnLoadFailure(new Exception("Could not add asset to library"));
 139            }
 0140        }
 141
 142        protected void OnLoadFailure(Exception exception)
 143        {
 36144            CallAndClearEvents(false, exception);
 36145            Cleanup();
 36146        }
 147
 10148        protected virtual bool AddToLibrary() { return library.Add(asset); }
 149
 150        internal virtual void Unload()
 151        {
 250152            if (state == AssetPromiseState.IDLE_AND_EMPTY)
 8153                return;
 154
 242155            Cleanup();
 242156        }
 157
 158        public void Cleanup()
 159        {
 293160            if (state == AssetPromiseState.LOADING)
 161            {
 70162                OnCancelLoading();
 70163                ClearEvents();
 164            }
 165
 293166            state = AssetPromiseState.IDLE_AND_EMPTY;
 167
 293168            if (asset != null)
 169            {
 273170                if (library.Contains(asset))
 205171                    library.Release(asset);
 172                else
 68173                    asset.Cleanup();
 174
 273175                asset = null;
 176            }
 293177        }
 178
 179        internal virtual void OnForget()
 180        {
 562181            isForgotten = true;
 562182            ClearEvents();
 562183        }
 184
 185        protected abstract void OnCancelLoading();
 186        protected abstract void OnLoad(Action OnSuccess, Action<Exception> OnFail);
 187        protected abstract void OnBeforeLoadOrReuse();
 188        protected abstract void OnAfterLoadOrReuse();
 189        public abstract object GetId();
 190    }
 191}