< Summary

Class:MainScripts.DCL.Controllers.HotScenes.HotScenesFetcher
Assembly:HotScenesController
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HotScenes/HotScenesFetcher.cs
Covered lines:13
Uncovered lines:20
Coverable lines:33
Total lines:88
Line coverage:39.3% (13 of 33)
Covered branches:0
Total branches:0
Covered methods:4
Total methods:7
Method coverage:57.1% (4 of 7)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
HotScenesFetcher(...)0%110100%
Dispose()0%3.333066.67%
Initialize()0%3.692025%
SetUpdateMode(...)0%4.254075%
UpdateLoop()0%90900%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HotScenes/HotScenesFetcher.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using DCL;
 3using System;
 4using System.Collections.Generic;
 5using System.Threading;
 6using UnityEngine;
 7
 8namespace MainScripts.DCL.Controllers.HotScenes
 9{
 10    public class HotScenesFetcher : IHotScenesFetcher
 11    {
 12        private readonly float foregroundUpdateInterval;
 13        private readonly float backgroundUpdateInterval;
 14
 15        private float updateInterval;
 16
 17        private CancellationTokenSource cts;
 18
 19        private Service<IHotScenesController> hotScenesController;
 20
 42521        private readonly AsyncReactiveProperty<IReadOnlyList<IHotScenesController.HotSceneInfo>> scenes =
 22            new (Array.Empty<IHotScenesController.HotSceneInfo>());
 23
 42524        private readonly AsyncReactiveProperty<IReadOnlyList<IHotScenesController.HotWorldInfo.WorldInfo>> worlds =
 25            new (Array.Empty<IHotScenesController.HotWorldInfo.WorldInfo>());
 26
 42527        public HotScenesFetcher(float foregroundUpdateInterval, float backgroundUpdateInterval)
 28        {
 42529            this.foregroundUpdateInterval = foregroundUpdateInterval;
 42530            this.backgroundUpdateInterval = backgroundUpdateInterval;
 42531        }
 32
 033        public IReadOnlyAsyncReactiveProperty<IReadOnlyList<IHotScenesController.HotSceneInfo>> ScenesInfo => scenes;
 034        public IReadOnlyAsyncReactiveProperty<IReadOnlyList<IHotScenesController.HotWorldInfo.WorldInfo>> WorldsInfo => 
 35
 36        public void Dispose()
 37        {
 42538            cts?.Cancel();
 42539            cts?.Dispose();
 040        }
 41
 42        public void Initialize()
 43        {
 44            // dirty hack to prevent any logic from execution as it can be invoked in tests that are using the realtime 
 42545            if (hotScenesController.Ref == null)
 42546                return;
 47
 048            cts = new CancellationTokenSource();
 049            scenes.AddTo(cts.Token);
 050            worlds.AddTo(cts.Token);
 51
 052            updateInterval = backgroundUpdateInterval;
 53
 054            UpdateLoop(cts.Token).Forget();
 055        }
 56
 57        public void SetUpdateMode(IHotScenesFetcher.UpdateMode mode)
 58        {
 8859            if (mode is IHotScenesFetcher.UpdateMode.IMMEDIATELY_ONCE)
 060                updateInterval = 0;
 61            else
 8862                updateInterval = mode == IHotScenesFetcher.UpdateMode.BACKGROUND ? backgroundUpdateInterval : foreground
 8863        }
 64
 65        private async UniTaskVoid UpdateLoop(CancellationToken ct)
 66        {
 67            try
 68            {
 069                while (true)
 70                {
 071                    float time = Time.realtimeSinceStartup;
 72
 73                    // We can't use UniTask.Delay as `updateInterval` can be changed in the process of waiting
 074                    while (Time.realtimeSinceStartup - time < updateInterval)
 075                        await UniTask.NextFrame(ct);
 76
 077                    scenes.Value = await hotScenesController.Ref.GetHotScenesListAsync(ct);
 078                    worlds.Value = await hotScenesController.Ref.GetHotWorldsListAsync(ct);
 79
 80                    // We set back `updateInterval` to BACKGROUND after IMMEDIATELY_ONCE
 081                    if (updateInterval == 0)
 082                        updateInterval = backgroundUpdateInterval;
 83                }
 84            }
 085            catch (OperationCanceledException) { }
 086        }
 87    }
 88}