< Summary

Class:DCL.Components.BaseDisposable
Assembly:WorldRuntimeInterfaces
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    {
 18912        public virtual string componentName => GetType().Name;
 7613        public string id { get; private set; }
 7614        public IParcelScene scene { get; private set; }
 15
 16        public abstract int GetClassId();
 17
 18        public virtual void Initialize(IParcelScene scene, string id)
 19        {
 63020            this.scene = scene;
 63021            this.id = id;
 63022        }
 23
 24        ComponentUpdateHandler updateHandler;
 025        public CustomYieldInstruction yieldInstruction => updateHandler.yieldInstruction;
 7926        public Coroutine routine => updateHandler.routine;
 027        public bool isRoutineRunning => updateHandler.isRoutineRunning;
 28
 29        public event System.Action<IDCLEntity> OnAttach;
 30        public event System.Action<IDCLEntity> OnDetach;
 31        public event System.Action<BaseDisposable> OnDispose;
 32        public event Action<BaseDisposable> OnAppliedChanges;
 33
 63034        public HashSet<IDCLEntity> attachedEntities = new HashSet<IDCLEntity>();
 35
 36        protected BaseModel model;
 37
 7538        public HashSet<IDCLEntity> GetAttachedEntities() { return attachedEntities; }
 39
 134640        public virtual void UpdateFromJSON(string json) { UpdateFromModel(model.GetDataFromJSON(json)); }
 41
 42        public virtual void UpdateFromModel(BaseModel newModel)
 43        {
 77444            model = newModel;
 77445            updateHandler.ApplyChangesIfModified(model);
 77446        }
 47
 189048        public BaseDisposable() { updateHandler = CreateUpdateHandler(); }
 49
 82850        public virtual void RaiseOnAppliedChanges() { OnAppliedChanges?.Invoke(this); }
 51
 52        public virtual void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null)
 53        {
 45054            if (attachedEntities.Contains(entity))
 55            {
 056                return;
 57            }
 58
 45059            System.Type thisType = overridenAttachedType != null ? overridenAttachedType : GetType();
 45060            entity.AddSharedComponent(thisType, this);
 61
 45062            attachedEntities.Add(entity);
 63
 45064            entity.OnRemoved += OnEntityRemoved;
 65
 45066            OnAttach?.Invoke(entity);
 34367        }
 68
 10669        private void OnEntityRemoved(IDCLEntity entity) { DetachFrom(entity); }
 70
 71        public virtual void DetachFrom(IDCLEntity entity, System.Type overridenAttachedType = null)
 72        {
 15973            if (!attachedEntities.Contains(entity))
 074                return;
 75
 15976            entity.OnRemoved -= OnEntityRemoved;
 77
 15978            System.Type thisType = overridenAttachedType != null ? overridenAttachedType : GetType();
 15979            entity.RemoveSharedComponent(thisType, false);
 80
 15981            attachedEntities.Remove(entity);
 82
 15983            OnDetach?.Invoke(entity);
 11584        }
 85
 86        public void DetachFromEveryEntity()
 87        {
 14688            IDCLEntity[] attachedEntitiesArray = new IDCLEntity[attachedEntities.Count];
 89
 14690            attachedEntities.CopyTo(attachedEntitiesArray);
 91
 37692            for (int i = 0; i < attachedEntitiesArray.Length; i++)
 93            {
 4294                DetachFrom(attachedEntitiesArray[i]);
 95            }
 14696        }
 97
 98        public virtual void Dispose()
 99        {
 146100            OnDispose?.Invoke(this);
 146101            DetachFromEveryEntity();
 146102        }
 103
 138104        public virtual BaseModel GetModel() => model;
 105
 106        public abstract IEnumerator ApplyChanges(BaseModel model);
 107
 552108        public virtual ComponentUpdateHandler CreateUpdateHandler() { return new ComponentUpdateHandler(this); }
 109
 774110        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}