< Summary

Class:DCL.Components.BaseDisposable
Assembly:MainScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/BaseDisposable.cs
Covered lines:41
Uncovered lines:7
Coverable lines:48
Total lines:121
Line coverage:85.4% (41 of 48)
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%110100%
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/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    {
 14712        public virtual string componentName => GetType().Name;
 6413        public string id { get; private set; }
 6414        public IParcelScene scene { get; private set; }
 15
 16        public abstract int GetClassId();
 17
 18        public virtual void Initialize(IParcelScene scene, string id)
 19        {
 54320            this.scene = scene;
 54321            this.id = id;
 54322        }
 23
 24        ComponentUpdateHandler updateHandler;
 025        public CustomYieldInstruction yieldInstruction => updateHandler.yieldInstruction;
 6726        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 Action<BaseDisposable> OnAppliedChanges;
 32
 54333        public HashSet<IDCLEntity> attachedEntities = new HashSet<IDCLEntity>();
 34
 35        protected BaseModel model;
 36
 137        public HashSet<IDCLEntity> GetAttachedEntities() { return attachedEntities; }
 38
 113639        public virtual void UpdateFromJSON(string json) { UpdateFromModel(model.GetDataFromJSON(json)); }
 40
 41        public virtual void UpdateFromModel(BaseModel newModel)
 42        {
 61943            model = newModel;
 61944            updateHandler.ApplyChangesIfModified(model);
 61945        }
 46
 162947        public BaseDisposable() { updateHandler = CreateUpdateHandler(); }
 48
 62949        public virtual void RaiseOnAppliedChanges() { OnAppliedChanges?.Invoke(this); }
 50
 51        public virtual void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null)
 52        {
 34653            if (attachedEntities.Contains(entity))
 54            {
 055                return;
 56            }
 57
 34658            System.Type thisType = overridenAttachedType != null ? overridenAttachedType : GetType();
 34659            entity.AddSharedComponent(thisType, this);
 60
 34661            attachedEntities.Add(entity);
 62
 34663            entity.OnRemoved += OnEntityRemoved;
 64
 34665            OnAttach?.Invoke(entity);
 27266        }
 67
 6868        private void OnEntityRemoved(IDCLEntity entity) { DetachFrom(entity); }
 69
 70        public virtual void DetachFrom(IDCLEntity entity, System.Type overridenAttachedType = null)
 71        {
 33972            if (!attachedEntities.Contains(entity))
 073                return;
 74
 33975            entity.OnRemoved -= OnEntityRemoved;
 76
 33977            System.Type thisType = overridenAttachedType != null ? overridenAttachedType : GetType();
 33978            entity.RemoveSharedComponent(thisType, false);
 79
 33980            attachedEntities.Remove(entity);
 81
 33982            OnDetach?.Invoke(entity);
 26583        }
 84
 85        public void DetachFromEveryEntity()
 86        {
 50187            IDCLEntity[] attachedEntitiesArray = new IDCLEntity[attachedEntities.Count];
 88
 50189            attachedEntities.CopyTo(attachedEntitiesArray);
 90
 155491            for (int i = 0; i < attachedEntitiesArray.Length; i++)
 92            {
 27693                DetachFrom(attachedEntitiesArray[i]);
 94            }
 50195        }
 96
 100297        public virtual void Dispose() { DetachFromEveryEntity(); }
 98
 24399        public virtual BaseModel GetModel() => model;
 100
 101        public abstract IEnumerator ApplyChanges(BaseModel model);
 102
 465103        public virtual ComponentUpdateHandler CreateUpdateHandler() { return new ComponentUpdateHandler(this); }
 104
 619105        public bool IsValid() { return true; }
 106
 107        public void Cleanup()
 108        {
 0109            if (isRoutineRunning)
 110            {
 0111                CoroutineStarter.Stop(routine);
 112            }
 0113        }
 114
 115        public virtual void CallWhenReady(Action<ISharedComponent> callback)
 116        {
 117            //By default there's no initialization process and we call back as soon as we get the suscription
 6118            callback.Invoke(this);
 6119        }
 120    }
 121}