< 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:28
Uncovered lines:2
Coverable lines:30
Total lines:68
Line coverage:93.3% (28 of 30)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Resolve(...)0%220100%
Reject(...)0%220100%
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
 11614        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        {
 28320            failed = false;
 28321            resolved = true;
 28322            value = result;
 28323            error = null;
 28324            onSuccess?.Invoke(result);
 2625        }
 26
 27        public void Reject(string errorMessage)
 28        {
 529            failed = true;
 530            resolved = true;
 531            error = errorMessage;
 532            onError?.Invoke(error);
 233        }
 34
 35        public Promise<T> Then(Action<T> successCallback)
 36        {
 57437            if (!resolved)
 38            {
 31739                onSuccess = successCallback;
 31740            }
 25741            else if (!failed)
 42            {
 25643                successCallback?.Invoke(value);
 44            }
 45
 57446            return this;
 47        }
 48
 49        public void Catch(Action<string> errorCallback)
 50        {
 16751            if (!resolved)
 52            {
 10453                onError = errorCallback;
 10454            }
 6355            else if (failed)
 56            {
 157                errorCallback?.Invoke(this.error);
 58            }
 6359        }
 60
 61        public void Dispose()
 62        {
 13963            onSuccess = null;
 13964            onError = null;
 13965            resolved = true;
 13966        }
 67    }
 68}