< Summary

Class:DCL.Components.BaseDisposable
Assembly:DCL.Runtime.Interfaces
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/Interfaces/BaseDisposable.cs
Covered lines:44
Uncovered lines:8
Coverable lines:52
Total lines:129
Line coverage:84.6% (44 of 52)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Initialize(...)0%110100%
BaseDisposable()0%110100%
GetAttachedEntities()0%110100%
UpdateFromJSON(...)0%110100%
UpdateFromModel(...)0%110100%
RaiseOnAppliedChanges()0%220100%
AttachTo(...)0%4.034087.5%
OnEntityRemoved(...)0%110100%
DetachFrom(...)0%4.034087.5%
DetachFromEveryEntity()0%3.013088.89%
Dispose()0%220100%
GetModel()0%110100%
CreateUpdateHandler()0%110100%
IsValid()0%110100%
Cleanup()0%6200%
CallWhenReady(...)0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/Interfaces/BaseDisposable.cs

#LineLine coverage
 1using DCL.Models;
 2using System;
 3using System.Collections;
 4using System.Collections.Generic;
 5using DCL.Controllers;
 6using UnityEngine;
 7
 8namespace DCL.Components
 9{
 10    public abstract class BaseDisposable : IDelayedComponent, ISharedComponent
 11    {
 19212        public virtual string componentName => GetType().Name;
 13        public event Action<IDCLEntity> OnAttach;
 14        public event Action<IDCLEntity> OnDetach;
 137115        public string id { get; private set; }
 295416        public IParcelScene scene { get; private set; }
 17
 18        public abstract int GetClassId();
 19
 20        public virtual void Initialize(IParcelScene scene, string id)
 21        {
 55022            this.scene = scene;
 55023            this.id = id;
 55024        }
 25
 26        ComponentUpdateHandler updateHandler;
 027        public CustomYieldInstruction yieldInstruction => updateHandler.yieldInstruction;
 64228        public Coroutine routine => updateHandler.routine;
 029        public bool isRoutineRunning => updateHandler.isRoutineRunning;
 30
 31        public event Action<BaseDisposable> OnDispose;
 32        public event Action<BaseDisposable> OnAppliedChanges;
 33
 55034        public HashSet<IDCLEntity> attachedEntities = new HashSet<IDCLEntity>();
 35
 36        protected BaseModel model;
 37
 15938        public HashSet<IDCLEntity> GetAttachedEntities() { return attachedEntities; }
 39
 138040        public virtual void UpdateFromJSON(string json) { UpdateFromModel(model.GetDataFromJSON(json)); }
 41
 42        public virtual void UpdateFromModel(BaseModel newModel)
 43        {
 72744            model = newModel;
 72745            updateHandler.ApplyChangesIfModified(model);
 72746        }
 47
 165048        public BaseDisposable() { updateHandler = CreateUpdateHandler(); }
 49
 73750        public virtual void RaiseOnAppliedChanges() { OnAppliedChanges?.Invoke(this); }
 51
 52        public virtual void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null)
 53        {
 36754            if (attachedEntities.Contains(entity))
 55            {
 056                return;
 57            }
 58
 36759            System.Type thisType = overridenAttachedType != null ? overridenAttachedType : GetType();
 36760            scene.componentsManagerLegacy.AddSharedComponent(entity, thisType, this);
 61
 36762            attachedEntities.Add(entity);
 63
 36764            entity.OnRemoved += OnEntityRemoved;
 65
 36766            OnAttach?.Invoke(entity);
 32967        }
 68
 37869        private void OnEntityRemoved(IDCLEntity entity) { DetachFrom(entity); }
 70
 71        public virtual void DetachFrom(IDCLEntity entity, System.Type overridenAttachedType = null)
 72        {
 30073            if (!attachedEntities.Contains(entity))
 074                return;
 75
 30076            entity.OnRemoved -= OnEntityRemoved;
 77
 30078            System.Type thisType = overridenAttachedType != null ? overridenAttachedType : GetType();
 30079            scene.componentsManagerLegacy.RemoveSharedComponent(entity, thisType, false);
 80
 30081            attachedEntities.Remove(entity);
 82
 30083            OnDetach?.Invoke(entity);
 26284        }
 85
 86        public void DetachFromEveryEntity()
 87        {
 15488            if (attachedEntities == null)
 089                return;
 90
 15491            IDCLEntity[] attachedEntitiesArray = new IDCLEntity[attachedEntities.Count];
 92
 15493            attachedEntities.CopyTo(attachedEntitiesArray);
 94
 38495            for (int i = 0; i < attachedEntitiesArray.Length; i++)
 96            {
 3897                DetachFrom(attachedEntitiesArray[i]);
 98            }
 15499        }
 100
 101        public virtual void Dispose()
 102        {
 154103            OnDispose?.Invoke(this);
 154104            DetachFromEveryEntity();
 154105        }
 106
 237107        public virtual BaseModel GetModel() => model;
 108
 109        public abstract IEnumerator ApplyChanges(BaseModel model);
 110
 472111        public virtual ComponentUpdateHandler CreateUpdateHandler() { return new ComponentUpdateHandler(this); }
 112
 727113        public bool IsValid() { return true; }
 114
 115        public void Cleanup()
 116        {
 0117            if (isRoutineRunning)
 118            {
 0119                CoroutineStarter.Stop(routine);
 120            }
 0121        }
 122
 123        public virtual void CallWhenReady(Action<ISharedComponent> callback)
 124        {
 125            //By default there's no initialization process and we call back as soon as we get the suscription
 6126            callback.Invoke(this);
 6127        }
 128    }
 129}