-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(time): add missing methods to ticker/timer
- Loading branch information
Showing
10 changed files
with
189 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// https://gobyexample.com/tickers | ||
// | ||
// package main | ||
// | ||
// import ( | ||
// "fmt" | ||
// "time" | ||
// ) | ||
// | ||
// func main() { | ||
// ticker := time.NewTicker(500 * time.Millisecond) | ||
// done := make(chan bool) | ||
// | ||
// go func() { | ||
// for { | ||
// select { | ||
// case <-done: | ||
// return | ||
// case t := <-ticker.C: | ||
// fmt.Println("Tick at", t) | ||
// } | ||
// } | ||
// }() | ||
// | ||
// time.Sleep(1600 * time.Millisecond) | ||
// ticker.Stop() | ||
// done <- true | ||
// fmt.Println("Ticker stopped") | ||
// } | ||
|
||
#include <eo/fmt.h> | ||
#include <eo/time.h> | ||
|
||
using namespace eo; | ||
|
||
func<> eo_main() { | ||
auto ticker = time::new_ticker(std::chrono::milliseconds(500)); | ||
auto done = make_chan<bool>(); | ||
|
||
go([&]() -> func<> { | ||
auto select = Select{*done, *ticker->c}; | ||
for (;;) { | ||
switch (co_await select.index()) { | ||
case 0: | ||
co_await select.process<0>(); | ||
co_return; | ||
case 1: | ||
auto t = co_await select.process<1>(); | ||
fmt::println("Tick at {}", t); | ||
break; | ||
} | ||
} | ||
}); | ||
|
||
co_await time::sleep(std::chrono::milliseconds(1600)); | ||
ticker->stop(); | ||
co_await *(done << true); | ||
fmt::println("Ticker stopped"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// package main | ||
// | ||
// import ( | ||
// "fmt" | ||
// "time" | ||
// ) | ||
// | ||
// func main() { | ||
// timer1 := time.NewTimer(2 * time.Second) | ||
// | ||
// <-timer1.C | ||
// fmt.Println("Timer 1 fired") | ||
// | ||
// timer2 := time.NewTimer(time.Second) | ||
// go func() { | ||
// <-timer2.C | ||
// fmt.Println("Timer 2 fired") | ||
// }() | ||
// stop2 := timer2.Stop() | ||
// if stop2 { | ||
// fmt.Println("Timer 2 stopped") | ||
// } | ||
// | ||
// time.Sleep(2 * time.Second) | ||
// } | ||
|
||
#include <eo/fmt.h> | ||
#include <eo/time.h> | ||
|
||
using namespace eo; | ||
|
||
func<> eo_main() { | ||
auto timer1 = time::new_timer(std::chrono::seconds(2)); | ||
|
||
co_await **timer1->c; | ||
fmt::println("Timer 1 fired"); | ||
|
||
auto timer2 = time::new_timer(std::chrono::seconds(1)); | ||
go([=]() -> func<> { | ||
co_await **timer2->c; | ||
fmt::println("Timer 2 fired"); | ||
}); | ||
auto stop2 = timer2->stop(); | ||
if (stop2) { | ||
fmt::println("Timer 2 stopped"); | ||
} | ||
|
||
co_await time::sleep(std::chrono::seconds(2)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters