| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL; |
| | 3 | | using System; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Threading; |
| | 6 | | using UnityEngine; |
| | 7 | |
|
| | 8 | | namespace 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 | |
|
| 425 | 21 | | private readonly AsyncReactiveProperty<IReadOnlyList<IHotScenesController.HotSceneInfo>> scenes = |
| | 22 | | new (Array.Empty<IHotScenesController.HotSceneInfo>()); |
| | 23 | |
|
| 425 | 24 | | private readonly AsyncReactiveProperty<IReadOnlyList<IHotScenesController.HotWorldInfo.WorldInfo>> worlds = |
| | 25 | | new (Array.Empty<IHotScenesController.HotWorldInfo.WorldInfo>()); |
| | 26 | |
|
| 425 | 27 | | public HotScenesFetcher(float foregroundUpdateInterval, float backgroundUpdateInterval) |
| | 28 | | { |
| 425 | 29 | | this.foregroundUpdateInterval = foregroundUpdateInterval; |
| 425 | 30 | | this.backgroundUpdateInterval = backgroundUpdateInterval; |
| 425 | 31 | | } |
| | 32 | |
|
| 0 | 33 | | public IReadOnlyAsyncReactiveProperty<IReadOnlyList<IHotScenesController.HotSceneInfo>> ScenesInfo => scenes; |
| 0 | 34 | | public IReadOnlyAsyncReactiveProperty<IReadOnlyList<IHotScenesController.HotWorldInfo.WorldInfo>> WorldsInfo => |
| | 35 | |
|
| | 36 | | public void Dispose() |
| | 37 | | { |
| 425 | 38 | | cts?.Cancel(); |
| 425 | 39 | | cts?.Dispose(); |
| 0 | 40 | | } |
| | 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 |
| 425 | 45 | | if (hotScenesController.Ref == null) |
| 425 | 46 | | return; |
| | 47 | |
|
| 0 | 48 | | cts = new CancellationTokenSource(); |
| 0 | 49 | | scenes.AddTo(cts.Token); |
| 0 | 50 | | worlds.AddTo(cts.Token); |
| | 51 | |
|
| 0 | 52 | | updateInterval = backgroundUpdateInterval; |
| | 53 | |
|
| 0 | 54 | | UpdateLoop(cts.Token).Forget(); |
| 0 | 55 | | } |
| | 56 | |
|
| | 57 | | public void SetUpdateMode(IHotScenesFetcher.UpdateMode mode) |
| | 58 | | { |
| 88 | 59 | | if (mode is IHotScenesFetcher.UpdateMode.IMMEDIATELY_ONCE) |
| 0 | 60 | | updateInterval = 0; |
| | 61 | | else |
| 88 | 62 | | updateInterval = mode == IHotScenesFetcher.UpdateMode.BACKGROUND ? backgroundUpdateInterval : foreground |
| 88 | 63 | | } |
| | 64 | |
|
| | 65 | | private async UniTaskVoid UpdateLoop(CancellationToken ct) |
| | 66 | | { |
| | 67 | | try |
| | 68 | | { |
| 0 | 69 | | while (true) |
| | 70 | | { |
| 0 | 71 | | float time = Time.realtimeSinceStartup; |
| | 72 | |
|
| | 73 | | // We can't use UniTask.Delay as `updateInterval` can be changed in the process of waiting |
| 0 | 74 | | while (Time.realtimeSinceStartup - time < updateInterval) |
| 0 | 75 | | await UniTask.NextFrame(ct); |
| | 76 | |
|
| 0 | 77 | | scenes.Value = await hotScenesController.Ref.GetHotScenesListAsync(ct); |
| 0 | 78 | | worlds.Value = await hotScenesController.Ref.GetHotWorldsListAsync(ct); |
| | 79 | |
|
| | 80 | | // We set back `updateInterval` to BACKGROUND after IMMEDIATELY_ONCE |
| 0 | 81 | | if (updateInterval == 0) |
| 0 | 82 | | updateInterval = backgroundUpdateInterval; |
| | 83 | | } |
| | 84 | | } |
| 0 | 85 | | catch (OperationCanceledException) { } |
| 0 | 86 | | } |
| | 87 | | } |
| | 88 | | } |