< 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:45
Uncovered lines:8
Coverable lines:53
Total lines:135
Line coverage:84.9% (45 of 53)
Covered branches:0
Total branches:0
Covered methods:22
Total methods:25
Method coverage:88% (22 of 25)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Initialize(...)0%110100%
BaseDisposable()0%110100%
GetAttachedEntities()0%110100%
UpdateFromJSON(...)0%110100%
UpdateFromPb(...)0%2100%
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;
 7using Decentraland.Sdk.Ecs6;
 8
 9namespace DCL.Components
 10{
 11    public abstract class BaseDisposable : IDelayedComponent, ISharedComponent
 12    {
 19113        public virtual string componentName => GetType().Name;
 14        public event Action<IDCLEntity> OnAttach;
 15        public event Action<IDCLEntity> OnDetach;
 149016        public string id { get; private set; }
 319817        public IParcelScene scene { get; private set; }
 18
 19        public abstract int GetClassId();
 20
 21        public virtual void Initialize(IParcelScene scene, string id)
 22        {
 57923            this.scene = scene;
 57924            this.id = id;
 57925        }
 26
 27        ComponentUpdateHandler updateHandler;
 028        public CustomYieldInstruction yieldInstruction => updateHandler.yieldInstruction;
 65229        public Coroutine routine => updateHandler.routine;
 1030        public bool isRoutineRunning => updateHandler.isRoutineRunning;
 31
 32        public event Action<BaseDisposable> OnDispose;
 33        public event Action<BaseDisposable> OnAppliedChanges;
 34
 57935        public HashSet<IDCLEntity> attachedEntities = new HashSet<IDCLEntity>();
 36
 37        protected BaseModel model;
 38
 39        public HashSet<IDCLEntity> GetAttachedEntities() =>
 19940            attachedEntities;
 41
 42        public virtual void UpdateFromJSON(string json) =>
 72743            UpdateFromModel(model.GetDataFromJSON(json));
 44
 45        public virtual void UpdateFromPb(ComponentBodyPayload payload) =>
 046            UpdateFromModel(model.GetDataFromPb(payload));
 47
 48        public virtual void UpdateFromModel(BaseModel newModel)
 49        {
 75950            model = newModel;
 75951            updateHandler.ApplyChangesIfModified(model);
 75952        }
 53
 173754        public BaseDisposable() { updateHandler = CreateUpdateHandler(); }
 55
 76756        public virtual void RaiseOnAppliedChanges() { OnAppliedChanges?.Invoke(this); }
 57
 58        public virtual void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null)
 59        {
 37660            if (attachedEntities.Contains(entity))
 61            {
 062                return;
 63            }
 64
 37665            System.Type thisType = overridenAttachedType != null ? overridenAttachedType : GetType();
 37666            scene.componentsManagerLegacy.AddSharedComponent(entity, thisType, this);
 67
 37668            attachedEntities.Add(entity);
 69
 37670            entity.OnRemoved += OnEntityRemoved;
 71
 37672            OnAttach?.Invoke(entity);
 32673        }
 74
 38075        private void OnEntityRemoved(IDCLEntity entity) { DetachFrom(entity); }
 76
 77        public virtual void DetachFrom(IDCLEntity entity, System.Type overridenAttachedType = null)
 78        {
 30979            if (!attachedEntities.Contains(entity))
 080                return;
 81
 30982            entity.OnRemoved -= OnEntityRemoved;
 83
 30984            System.Type thisType = overridenAttachedType != null ? overridenAttachedType : GetType();
 30985            scene.componentsManagerLegacy.RemoveSharedComponent(entity, thisType, false);
 86
 30987            attachedEntities.Remove(entity);
 88
 30989            OnDetach?.Invoke(entity);
 25990        }
 91
 92        public void DetachFromEveryEntity()
 93        {
 15794            if (attachedEntities == null)
 095                return;
 96
 15797            IDCLEntity[] attachedEntitiesArray = new IDCLEntity[attachedEntities.Count];
 98
 15799            attachedEntities.CopyTo(attachedEntitiesArray);
 100
 390101            for (int i = 0; i < attachedEntitiesArray.Length; i++)
 102            {
 38103                DetachFrom(attachedEntitiesArray[i]);
 104            }
 157105        }
 106
 107        public virtual void Dispose()
 108        {
 157109            OnDispose?.Invoke(this);
 157110            DetachFromEveryEntity();
 157111        }
 112
 114113        public virtual BaseModel GetModel() => model;
 114
 115        public abstract IEnumerator ApplyChanges(BaseModel model);
 116
 499117        public virtual ComponentUpdateHandler CreateUpdateHandler() { return new ComponentUpdateHandler(this); }
 118
 759119        public bool IsValid() { return true; }
 120
 121        public void Cleanup()
 122        {
 0123            if (isRoutineRunning)
 124            {
 0125                CoroutineStarter.Stop(routine);
 126            }
 0127        }
 128
 129        public virtual void CallWhenReady(Action<ISharedComponent> callback)
 130        {
 131            //By default there's no initialization process and we call back as soon as we get the suscription
 6132            callback.Invoke(this);
 6133        }
 134    }
 135}