< 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:43
Uncovered lines:7
Coverable lines:50
Total lines:126
Line coverage:86% (43 of 50)
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%220100%
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;
 11015        public string id { get; private set; }
 8816        public IParcelScene scene { get; private set; }
 17
 18        public abstract int GetClassId();
 19
 20        public virtual void Initialize(IParcelScene scene, string id)
 21        {
 64722            this.scene = scene;
 64723            this.id = id;
 64724        }
 25
 26        ComponentUpdateHandler updateHandler;
 027        public CustomYieldInstruction yieldInstruction => updateHandler.yieldInstruction;
 9128        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
 64734        public HashSet<IDCLEntity> attachedEntities = new HashSet<IDCLEntity>();
 35
 36        protected BaseModel model;
 37
 15638        public HashSet<IDCLEntity> GetAttachedEntities() { return attachedEntities; }
 39
 140440        public virtual void UpdateFromJSON(string json) { UpdateFromModel(model.GetDataFromJSON(json)); }
 41
 42        public virtual void UpdateFromModel(BaseModel newModel)
 43        {
 80944            model = newModel;
 80945            updateHandler.ApplyChangesIfModified(model);
 80946        }
 47
 194148        public BaseDisposable() { updateHandler = CreateUpdateHandler(); }
 49
 86650        public virtual void RaiseOnAppliedChanges() { OnAppliedChanges?.Invoke(this); }
 51
 52        public virtual void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null)
 53        {
 46554            if (attachedEntities.Contains(entity))
 55            {
 056                return;
 57            }
 58
 46559            System.Type thisType = overridenAttachedType != null ? overridenAttachedType : GetType();
 46560            scene.componentsManagerLegacy.AddSharedComponent(entity, thisType, this);
 61
 46562            attachedEntities.Add(entity);
 63
 46564            entity.OnRemoved += OnEntityRemoved;
 65
 46566            OnAttach?.Invoke(entity);
 35867        }
 68
 57069        private void OnEntityRemoved(IDCLEntity entity) { DetachFrom(entity); }
 70
 71        public virtual void DetachFrom(IDCLEntity entity, System.Type overridenAttachedType = null)
 72        {
 39673            if (!attachedEntities.Contains(entity))
 074                return;
 75
 39676            entity.OnRemoved -= OnEntityRemoved;
 77
 39678            System.Type thisType = overridenAttachedType != null ? overridenAttachedType : GetType();
 39679            scene.componentsManagerLegacy.RemoveSharedComponent(entity, thisType, false);
 80
 39681            attachedEntities.Remove(entity);
 82
 39683            OnDetach?.Invoke(entity);
 28984        }
 85
 86        public void DetachFromEveryEntity()
 87        {
 15488            IDCLEntity[] attachedEntitiesArray = new IDCLEntity[attachedEntities.Count];
 89
 15490            attachedEntities.CopyTo(attachedEntitiesArray);
 91
 38492            for (int i = 0; i < attachedEntitiesArray.Length; i++)
 93            {
 3894                DetachFrom(attachedEntitiesArray[i]);
 95            }
 15496        }
 97
 98        public virtual void Dispose()
 99        {
 154100            OnDispose?.Invoke(this);
 154101            DetachFromEveryEntity();
 154102        }
 103
 203104        public virtual BaseModel GetModel() => model;
 105
 106        public abstract IEnumerator ApplyChanges(BaseModel model);
 107
 569108        public virtual ComponentUpdateHandler CreateUpdateHandler() { return new ComponentUpdateHandler(this); }
 109
 809110        public bool IsValid() { return true; }
 111
 112        public void Cleanup()
 113        {
 0114            if (isRoutineRunning)
 115            {
 0116                CoroutineStarter.Stop(routine);
 117            }
 0118        }
 119
 120        public virtual void CallWhenReady(Action<ISharedComponent> callback)
 121        {
 122            //By default there's no initialization process and we call back as soon as we get the suscription
 6123            callback.Invoke(this);
 6124        }
 125    }
 126}