< Summary

Class:DCL.WebRequestAsyncOperation
Assembly:WebRequestControllerInterfaces
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/WebRequest/Interfaces/WebRequestAsyncOperation.cs
Covered lines:25
Uncovered lines:5
Coverable lines:30
Total lines:155
Line coverage:83.3% (25 of 30)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
WebRequestAsyncOperation(...)0%110100%
SetAsCompleted(...)0%330100%
Abort()0%330100%
Dispose()0%220100%
GetResultData()0%2100%
SetNewWebRequest(...)0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/WebRequest/Interfaces/WebRequestAsyncOperation.cs

#LineLine coverage
 1using System;
 2using UnityEngine;
 3using UnityEngine.Networking;
 4
 5namespace DCL
 6{
 7    public interface IWebRequestAsyncOperation
 8    {
 9        /// <summary>
 10        /// Event that will be invoked when the request has been completed.
 11        /// </summary>
 12        event Action<WebRequestAsyncOperation> completed;
 13
 14        /// <summary>
 15        /// WebRequest that is being managed.
 16        /// </summary>
 17        UnityWebRequest webRequest { get; }
 18
 19        /// <summary>
 20        /// Returns true after the request has finished communicating with the remote server.
 21        /// </summary>
 22        bool isDone { get; }
 23
 24        /// <summary>
 25        /// Returns true if the request was successfully finished.
 26        /// </summary>
 27        bool isSucceded { get; }
 28
 29        /// <summary>
 30        /// Returns true if webRequest has been disposed (webRequest = null).
 31        /// </summary>
 32        bool isDisposed { get; }
 33
 34        /// <summary>
 35        /// Set to true for disposing the request just after it has been completed.
 36        /// </summary>
 37        bool disposeOnCompleted { get; }
 38
 39        /// <summary>
 40        /// Returns the data that has been downloaded as a byte array, if not done or success it will return an empty ar
 41        /// </summary>
 42        byte[] GetResultData();
 43
 44        /// <summary>
 45        /// If in progress, halts the request as soon as possible.
 46        /// </summary>
 47        void Abort();
 48
 49        /// <summary>
 50        /// Signals that this request is no longer being used, and should clean up any resources it is using (it aborts 
 51        /// </summary>
 52        void Dispose();
 53
 54        /// <summary>
 55        /// Mark the request as completed and throw the corresponding event.
 56        /// </summary>
 57        /// <param name="success">True if the request was successfully ended.</param>
 58        void SetAsCompleted(bool success);
 59    }
 60
 61    /// <summary>
 62    /// Our custom request async operation to be used with the WebRequestController.
 63    /// </summary>
 64    public class WebRequestAsyncOperation : CustomYieldInstruction, IWebRequestAsyncOperation
 65    {
 66        /// <summary>
 67        /// Event that will be invoked when the request has been completed.
 68        /// </summary>
 69        public event Action<WebRequestAsyncOperation> completed;
 70
 71        /// <summary>
 72        /// WebRequest that is being managed.
 73        /// </summary>
 7974        public UnityWebRequest webRequest { get; private set; }
 75
 76        /// <summary>
 77        /// Returns true after the request has finished communicating with the remote server.
 78        /// </summary>
 079        public bool isDone { get; private set; }
 80
 81        /// <summary>
 82        /// Returns true if the request was successfully finished.
 83        /// </summary>
 984        public bool isSucceded { get; private set; }
 85
 86        /// <summary>
 87        /// Returns true if webRequest has been disposed (webRequest = null).
 88        /// </summary>
 089        public bool isDisposed { get { return webRequest == null; } }
 90
 91        /// <summary>
 92        /// Set to true for disposing the request just after it has been completed.
 93        /// </summary>
 094        public bool disposeOnCompleted { get; set; }
 95
 63596        public override bool keepWaiting { get { return !isDone; } }
 97
 38798        public WebRequestAsyncOperation(UnityWebRequest webRequest)
 99        {
 387100            this.webRequest = webRequest;
 387101            isDone = false;
 387102            isSucceded = false;
 387103            disposeOnCompleted = true;
 387104        }
 105
 106        /// <summary>
 107        /// Mark the request as completed and throw the corresponding event.
 108        /// </summary>
 109        /// <param name="success">True if the request was successfully ended.</param>
 110        public void SetAsCompleted(bool success)
 111        {
 309112            completed?.Invoke(this);
 309113            isDone = true;
 309114            isSucceded = success;
 115
 309116            if (disposeOnCompleted)
 106117                Dispose();
 309118        }
 119
 120        /// <summary>
 121        /// If in progress, halts the request as soon as possible.
 122        /// </summary>
 123        public void Abort()
 124        {
 491125            if (webRequest == null || isDone)
 389126                return;
 127
 102128            webRequest.Abort();
 102129        }
 130
 131        /// <summary>
 132        /// Signals that this request is no longer being used, and should clean up any resources it is using (it aborts 
 133        /// </summary>
 134        public void Dispose()
 135        {
 491136            Abort();
 137
 491138            if (webRequest == null)
 108139                return;
 140
 383141            webRequest.Dispose();
 383142            webRequest = null;
 383143        }
 144
 145        /// <summary>
 146        /// Returns the data that has been downloaded as a byte array, if not done or success it will return an empty ar
 147        /// </summary>
 148        public byte[] GetResultData()
 149        {
 0150            return webRequest.downloadHandler.data;
 151        }
 152
 0153        internal void SetNewWebRequest(UnityWebRequest newRequest) { webRequest = newRequest; }
 154    }
 155}