Skip to content

Commit

Permalink
add more coroutine helper functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
pigpigyyy committed Nov 23, 2023
1 parent 9a7502e commit fcce822
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
10 changes: 10 additions & 0 deletions Source/Common/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
#include "Common/Utils.h"

#include "Basic/Application.h"
#include "Basic/Director.h"
#include "Basic/Scheduler.h"
#include "Event/Event.h"
#include "Lua/ToLua/tolua++.h"

Expand Down Expand Up @@ -166,4 +168,12 @@ std::function<bool(double)> loop(const std::function<Job()>& work) {
});
}

void thread(const std::function<Job()>& work) {
SharedDirector.getScheduler()->schedule(once(work));
}

void threadLoop(const std::function<Job()>& work) {
SharedDirector.getScheduler()->schedule(loop(work));
}

NS_DOROTHY_END
3 changes: 2 additions & 1 deletion Source/Common/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,8 @@ using Job = Coroutine<bool>;
}

std::function<bool(double)> once(const std::function<Job()>& work);

std::function<bool(double)> loop(const std::function<Job()>& work);
void thread(const std::function<Job()>& work);
void threadLoop(const std::function<Job()>& work);

NS_DOROTHY_END
16 changes: 8 additions & 8 deletions Source/Test/HelloWorldCpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ using namespace Dorothy;

DORA_TEST_ENTRY(HelloWorldCpp) {
auto node = Node::create();
node->slot("Enter"_slice, [](Event*) {
println("on enter event");
node->slot("Enter"sv, [](Event*) {
println("on enter event"sv);
});
node->slot("Exit"_slice, [](Event*) {
println("on exit event");
node->slot("Exit"sv, [](Event*) {
println("on exit event"sv);
});
node->slot("Cleanup"_slice, [](Event*) {
println("on node destoyed event");
node->slot("Cleanup"sv, [](Event*) {
println("on node destoyed event"sv);
});
node->schedule(once([]() -> Job {
for (int i = 3; i > 0; i--) {
for (int i = 5; i > 0; i--) {
println("{}", i);
co_sleep(1);
}
println("Hello World!");
println("Hello World!"sv);
}));
SharedDirector.getEntry()->addChild(node);

Expand Down

0 comments on commit fcce822

Please sign in to comment.