< Summary

Class:DCL.Helpers.Promise[T]
Assembly:Promise
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/Promise/Promise.cs
Covered lines:27
Uncovered lines:3
Coverable lines:30
Total lines:68
Line coverage:90% (27 of 30)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Resolve(...)0%220100%
Reject(...)0%2.032080%
Then(...)0%440100%
Catch(...)0%440100%
Dispose()0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/Promise/Promise.cs

#LineLine coverage
 1using System;
 2using UnityEngine;
 3
 4namespace DCL.Helpers
 5{
 6    public class Promise<T> : CustomYieldInstruction, IDisposable
 7    {
 8        private bool resolved = false;
 9        private bool failed = false;
 10
 11        private Action<T> onSuccess;
 12        private Action<string> onError;
 13
 12363014        public override bool keepWaiting => !resolved;
 015        public T value { private set; get; }
 016        public string error { private set; get; }
 17
 18        public void Resolve(T result)
 19        {
 90320            failed = false;
 90321            resolved = true;
 90322            value = result;
 90323            error = null;
 90324            onSuccess?.Invoke(result);
 2025        }
 26
 27        public void Reject(string errorMessage)
 28        {
 329            failed = true;
 330            resolved = true;
 331            error = errorMessage;
 332            onError?.Invoke(error);
 033        }
 34
 35        public Promise<T> Then(Action<T> successCallback)
 36        {
 73037            if (!resolved)
 38            {
 41739                onSuccess = successCallback;
 41740            }
 31341            else if (!failed)
 42            {
 31243                successCallback?.Invoke(value);
 44            }
 45
 73046            return this;
 47        }
 48
 49        public void Catch(Action<string> errorCallback)
 50        {
 6051            if (!resolved)
 52            {
 2853                onError = errorCallback;
 2854            }
 3255            else if (failed)
 56            {
 157                errorCallback?.Invoke(this.error);
 58            }
 3259        }
 60
 61        public void Dispose()
 62        {
 1663            onSuccess = null;
 1664            onError = null;
 1665            resolved = true;
 1666        }
 67    }
 68}