| | 1 | | using DCL.Components; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.Assertions; |
| | 6 | |
|
| | 7 | | namespace DCL |
| | 8 | | { |
| | 9 | | public class ComponentUpdateHandler |
| | 10 | | { |
| 1414 | 11 | | protected Queue<BaseModel> queue = new Queue<BaseModel>(); |
| 0 | 12 | | public Coroutine routine { get; protected set; } |
| | 13 | |
|
| | 14 | | public WaitForComponentUpdate yieldInstruction; |
| | 15 | |
|
| | 16 | | public IComponent owner; |
| | 17 | |
|
| 0 | 18 | | public bool isRoutineRunning { get { return routine != null; } } |
| | 19 | |
|
| | 20 | | #if UNITY_EDITOR |
| | 21 | | bool applyChangesRunning = false; |
| | 22 | | #endif |
| 1414 | 23 | | public ComponentUpdateHandler(IDelayedComponent owner) |
| | 24 | | { |
| 1414 | 25 | | this.owner = owner; |
| 1414 | 26 | | this.routine = null; |
| | 27 | |
|
| 1414 | 28 | | yieldInstruction = new WaitForComponentUpdate(owner); |
| 1414 | 29 | | } |
| | 30 | |
|
| 1842 | 31 | | public void ApplyChangesIfModified(BaseModel model) { HandleUpdate(model); } |
| | 32 | |
|
| | 33 | | protected void HandleUpdate(BaseModel newSerialization) |
| | 34 | | { |
| 921 | 35 | | queue.Enqueue(newSerialization); |
| | 36 | |
|
| 921 | 37 | | if (!isRoutineRunning) |
| | 38 | | { |
| 914 | 39 | | var enumerator = HandleUpdateCoroutines(); |
| | 40 | |
|
| 914 | 41 | | if (enumerator != null) |
| | 42 | | { |
| 914 | 43 | | routine = CoroutineStarter.Start(enumerator); |
| | 44 | | } |
| | 45 | | } |
| 921 | 46 | | } |
| | 47 | |
|
| | 48 | | public void Stop() |
| | 49 | | { |
| 702 | 50 | | if (routine != null) |
| 3 | 51 | | CoroutineStarter.Stop(routine); |
| | 52 | |
|
| 702 | 53 | | routine = null; |
| | 54 | | #if UNITY_EDITOR |
| 702 | 55 | | applyChangesRunning = false; |
| | 56 | | #endif |
| 702 | 57 | | } |
| | 58 | |
|
| | 59 | | public void Cleanup() |
| | 60 | | { |
| 702 | 61 | | Stop(); |
| | 62 | |
|
| 702 | 63 | | queue.Clear(); |
| 702 | 64 | | } |
| | 65 | |
|
| | 66 | | protected IEnumerator HandleUpdateCoroutines() |
| | 67 | | { |
| 1826 | 68 | | while (queue.Count > 0) |
| | 69 | | { |
| 921 | 70 | | BaseModel model = queue.Dequeue(); |
| | 71 | |
|
| 921 | 72 | | IEnumerator updateRoutine = ApplyChangesWrapper(model); |
| | 73 | |
|
| 921 | 74 | | if (updateRoutine != null) |
| | 75 | | { |
| | 76 | | // we don't want to start coroutines if we have early finalization in IEnumerators |
| | 77 | | // ergo, we return null without yielding any result |
| 921 | 78 | | yield return updateRoutine; |
| | 79 | | } |
| | 80 | | } |
| | 81 | |
|
| 905 | 82 | | routine = null; |
| 905 | 83 | | } |
| | 84 | |
|
| | 85 | | public virtual IEnumerator ApplyChangesWrapper(BaseModel model) |
| | 86 | | { |
| | 87 | | #if UNITY_EDITOR |
| 921 | 88 | | Assert.IsFalse(applyChangesRunning, "ApplyChanges routine was interrupted when it shouldn't!"); |
| 921 | 89 | | applyChangesRunning = true; |
| | 90 | | #endif |
| 921 | 91 | | if (owner.IsValid()) |
| | 92 | | { |
| 920 | 93 | | var enumerator = owner.ApplyChanges(model); |
| | 94 | |
|
| 920 | 95 | | if (enumerator != null) |
| | 96 | | { |
| 315 | 97 | | yield return enumerator; |
| | 98 | | } |
| | 99 | | } |
| | 100 | | #if UNITY_EDITOR |
| 912 | 101 | | applyChangesRunning = false; |
| | 102 | | #endif |
| 912 | 103 | | owner.RaiseOnAppliedChanges(); |
| 912 | 104 | | } |
| | 105 | | } |
| | 106 | | } |