CULambdaRunnable.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2018-current Getnamo. All Rights Reserved
  2. #include "CULambdaRunnable.h"
  3. #include "Async/Async.h"
  4. #include "Engine/Engine.h"
  5. void FCULambdaRunnable::RunLambdaOnBackGroundThread(TFunction< void()> InFunction)
  6. {
  7. Async(EAsyncExecution::Thread, InFunction);
  8. }
  9. void FCULambdaRunnable::RunLambdaOnBackGroundThreadPool(TFunction< void()> InFunction)
  10. {
  11. Async(EAsyncExecution::ThreadPool, InFunction);
  12. }
  13. FGraphEventRef FCULambdaRunnable::RunShortLambdaOnGameThread(TFunction< void()> InFunction)
  14. {
  15. return FFunctionGraphTask::CreateAndDispatchWhenReady(InFunction, TStatId(), nullptr, ENamedThreads::GameThread);
  16. }
  17. FGraphEventRef FCULambdaRunnable::RunShortLambdaOnBackGroundTask(TFunction< void()> InFunction)
  18. {
  19. return FFunctionGraphTask::CreateAndDispatchWhenReady(InFunction, TStatId(), nullptr, ENamedThreads::AnyThread);
  20. }
  21. void FCULambdaRunnable::SetTimeout(TFunction<void()>OnDone, float DurationInSec, bool bCallbackOnGameThread /*=true*/)
  22. {
  23. RunLambdaOnBackGroundThread([OnDone, DurationInSec, bCallbackOnGameThread]()
  24. {
  25. FPlatformProcess::Sleep(DurationInSec);
  26. if (bCallbackOnGameThread)
  27. {
  28. RunShortLambdaOnGameThread(OnDone);
  29. }
  30. else
  31. {
  32. OnDone();
  33. }
  34. });
  35. }
  36. FCULatentAction* FCULatentAction::CreateLatentAction(struct FLatentActionInfo& LatentInfo, UObject* WorldContext)
  37. {
  38. UWorld* World = GEngine->GetWorldFromContextObject(WorldContext, EGetWorldErrorMode::LogAndReturnNull);
  39. if (!World)
  40. {
  41. return nullptr;
  42. }
  43. FLatentActionManager& LatentActionManager = World->GetLatentActionManager();
  44. FCULatentAction *LatentAction = LatentActionManager.FindExistingAction<FCULatentAction>(LatentInfo.CallbackTarget, LatentInfo.UUID);
  45. LatentAction = new FCULatentAction(LatentInfo); //safe to use new since latentactionmanager will delete it
  46. int32 UUID = LatentInfo.UUID;
  47. LatentAction->OnCancelNotification = [UUID]()
  48. {
  49. UE_LOG(LogTemp, Log, TEXT("%d graph callback cancelled."), UUID);
  50. };
  51. LatentActionManager.AddNewAction(LatentInfo.CallbackTarget, LatentInfo.UUID, LatentAction);
  52. return LatentAction;
  53. }
  54. FCULatentAction::FCULatentAction(const FLatentActionInfo& LatentInfo) :
  55. ExecutionFunction(LatentInfo.ExecutionFunction),
  56. OutputLink(LatentInfo.Linkage),
  57. CallbackTarget(LatentInfo.CallbackTarget),
  58. Called(false)
  59. {
  60. }
  61. void FCULatentAction::UpdateOperation(FLatentResponse& Response)
  62. {
  63. Response.FinishAndTriggerIf(Called, ExecutionFunction, OutputLink, CallbackTarget);
  64. }
  65. void FCULatentAction::Call()
  66. {
  67. Called = true;
  68. }
  69. void FCULatentAction::Cancel()
  70. {
  71. if (OnCancelNotification)
  72. {
  73. OnCancelNotification();
  74. }
  75. }
  76. void FCULatentAction::NotifyObjectDestroyed()
  77. {
  78. Cancel();
  79. }
  80. void FCULatentAction::NotifyActionAborted()
  81. {
  82. Cancel();
  83. }
  84. #if WITH_EDITOR
  85. FString FCULatentAction::GetDescription() const
  86. {
  87. {
  88. if (Called)
  89. {
  90. return TEXT("Done.");
  91. }
  92. else
  93. {
  94. return TEXT("Pending.");
  95. }
  96. };
  97. }
  98. #endif