Future.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // <copyright file="Future.cs" company="Google Inc.">
  2. // Copyright (C) 2017 Google Inc. All Rights Reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. // </copyright>
  16. namespace Google {
  17. using System.Collections;
  18. using System.Threading.Tasks;
  19. using UnityEngine;
  20. /// <summary>
  21. /// Interface for implementations of the Future<T> API.
  22. /// </summary>
  23. internal interface FutureAPIImpl<T> {
  24. bool Pending { get; }
  25. GoogleSignInStatusCode Status { get; }
  26. T Result { get; }
  27. }
  28. /// <summary>
  29. /// Future return value.
  30. /// </summary>
  31. /// <remarks>This class provides a promise of a result from a method call.
  32. /// The typical usage is to check the Pending property until it is false.
  33. /// At this time either the Status or Result will be available for use.
  34. /// Result is only set if the operation was successful.
  35. /// As a convience, a coroutine to complete a Task is provided.
  36. /// </remarks>
  37. public class Future<T> {
  38. private FutureAPIImpl<T> apiImpl;
  39. internal Future(FutureAPIImpl<T> impl) {
  40. apiImpl = impl;
  41. }
  42. /// <summary>
  43. /// Gets a value indicating whether this
  44. /// <see cref="T:Google.Future`1"/> is pending.
  45. /// </summary>
  46. /// <value><c>true</c> if pending; otherwise, <c>false</c>.</value>
  47. public bool Pending { get { return apiImpl.Pending; } }
  48. /// <summary>
  49. /// Gets the status.
  50. /// </summary>
  51. /// <value>The status is set when Pending == false.</value>
  52. GoogleSignInStatusCode Status { get { return apiImpl.Status; } }
  53. /// <summary>
  54. /// Gets the result.
  55. /// </summary>
  56. /// <value>The result is set when Pending == false and there is no error.
  57. /// </value>
  58. T Result { get { return apiImpl.Result; } }
  59. /// <summary>
  60. /// Waits for result then completes the TaskCompleationSource.
  61. /// </summary>
  62. /// <returns>The for result.</returns>
  63. /// <param name="tcs">Tcs.</param>
  64. internal IEnumerator WaitForResult(TaskCompletionSource<T> tcs) {
  65. yield return new WaitUntil(() => !Pending);
  66. if (Status == GoogleSignInStatusCode.Canceled) {
  67. tcs.SetCanceled();
  68. } else if (Status == GoogleSignInStatusCode.Success ||
  69. Status == GoogleSignInStatusCode.SuccessCached) {
  70. tcs.SetResult(Result);
  71. } else {
  72. tcs.SetException(new GoogleSignIn.SignInException(Status));
  73. }
  74. }
  75. }
  76. }