Bridge.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using System.Threading.Tasks;
  3. namespace TapSDK.Core
  4. {
  5. public class EngineBridge
  6. {
  7. private static volatile EngineBridge _sInstance;
  8. private readonly IBridge _bridge;
  9. private static readonly object Locker = new object();
  10. public static EngineBridge GetInstance()
  11. {
  12. lock (Locker)
  13. {
  14. if (_sInstance == null)
  15. {
  16. _sInstance = new EngineBridge();
  17. }
  18. }
  19. return _sInstance;
  20. }
  21. private EngineBridge()
  22. {
  23. if (Platform.IsAndroid())
  24. {
  25. _bridge = BridgeAndroid.GetInstance();
  26. }
  27. else if (Platform.IsIOS())
  28. {
  29. _bridge = BridgeIOS.GetInstance();
  30. }
  31. }
  32. public void Register(string serviceClzName, string serviceImplName)
  33. {
  34. _bridge?.Register(serviceClzName, serviceImplName);
  35. }
  36. public void CallHandler(Command command)
  37. {
  38. _bridge?.Call(command);
  39. }
  40. public string CallWithReturnValue(Command command, Action<Result> action = null)
  41. {
  42. return _bridge?.CallWithReturnValue(command, action);
  43. }
  44. public void CallHandler(Command command, Action<Result> action)
  45. {
  46. _bridge?.Call(command, action);
  47. }
  48. public Task<Result> Emit(Command command)
  49. {
  50. var tcs = new TaskCompletionSource<Result>();
  51. CallHandler(command, result =>
  52. {
  53. tcs.TrySetResult(result);
  54. });
  55. return tcs.Task;
  56. }
  57. public static bool CheckResult(Result result)
  58. {
  59. if (result == null)
  60. {
  61. return false;
  62. }
  63. if (result.code != Result.RESULT_SUCCESS)
  64. {
  65. return false;
  66. }
  67. return !string.IsNullOrEmpty(result.content);
  68. }
  69. }
  70. }