File indexing completed on 2024-04-28 04:40:04
0001 // SPDX-FileCopyrightText: 2021 Carson Black <uhhadd@gmail.com> 0002 // 0003 // SPDX-License-Identifier: MIT 0004 0005 #include <QGuiApplication> 0006 #include <QTimer> 0007 0008 #include <QQmlApplicationEngine> 0009 0010 #include "coroutine_integration.h" 0011 #include "coroutine_integration_network.h" 0012 0013 #include "effects.h" 0014 0015 Croutons::FutureResult<> timer(int duration) { 0016 Croutons::FutureResult it; 0017 0018 QTimer::singleShot(duration, [it]() { 0019 it.succeed({}); 0020 }); 0021 0022 return it; 0023 } 0024 0025 Croutons::Future<int> returnFuture(int number) { 0026 co_await timer(200); 0027 0028 co_return number; 0029 } 0030 0031 Croutons::Future<int> flatMapTest() { 0032 using namespace Croutons; 0033 0034 auto transformed = co_await returnFuture(5) 0035 .flatMap([](int i) -> Future<qreal> { 0036 co_await timer(100); 0037 0038 co_return i + 1.0; 0039 }) 0040 .flatMap([](qreal i) -> Future<int> { 0041 return returnFuture(i + 1.0); 0042 }) 0043 .map([](int i) -> qreal { 0044 return i + 1.0; 0045 }); 0046 0047 Q_ASSERT(transformed == 8.0); 0048 0049 co_return 1.0; 0050 } 0051 0052 Croutons::Future<int> futureMain() { 0053 auto then = QTime::currentTime(); 0054 0055 co_await timer(1500); 0056 co_await flatMapTest(); 0057 0058 auto now = QTime::currentTime(); 0059 0060 Q_ASSERT(then.msecsTo(now) > 1000); 0061 0062 co_return 0; 0063 } 0064 0065 Croutons::Future<int> something() { 0066 co_return 0; 0067 } 0068 0069 void anotherThing() { 0070 something().then([](int i) { 0071 0072 }); 0073 } 0074 0075 Croutons::Future<void> voidSomething() { 0076 co_return; 0077 } 0078 0079 Croutons::FutureResult<int> yetAnotherThing() { 0080 co_return 0; 0081 } 0082 0083 void wawajete() { 0084 yetAnotherThing().then([](Croutons::Result<int, Croutons::Error> r) { 0085 0086 }); 0087 } 0088 0089 class Singleton : public QObject 0090 { 0091 0092 Q_OBJECT 0093 0094 public: 0095 Q_INVOKABLE Croutons::FutureBase wait() { 0096 co_await voidSomething(); 0097 0098 co_await timer(1000); 0099 0100 co_return "hi"; 0101 } 0102 0103 }; 0104 0105 using Maths = EffectFun<std::function<int(int, int)>>; 0106 using Log = EffectVoidFun<std::function<void(QString)>>; 0107 0108 Effect<int> contextDependentArithmetic() { 0109 co_return perform Maths(1, 2); 0110 } 0111 0112 Effect<void> contextDependentLogging() { 0113 perform Log("hi"); 0114 0115 co_return; 0116 } 0117 0118 void mu() { 0119 { 0120 auto handler = Maths::handler([](int i, int ii) -> int { 0121 return i + ii; 0122 }); 0123 0124 qDebug() << contextDependentArithmetic(); 0125 } 0126 { 0127 auto handler = Maths::handler([](int i, int ii) -> int { 0128 return i * ii; 0129 }); 0130 0131 qDebug() << contextDependentArithmetic(); 0132 } 0133 } 0134 0135 int main(int argc, char* argv[]) { 0136 mu(); 0137 0138 QGuiApplication app(argc, argv); 0139 0140 futureMain(); 0141 0142 qRegisterMetaType<Croutons::FutureBase>(); 0143 qmlRegisterSingletonType<Singleton>("org.kde.croutons", 1, 0, "Singleton", [](QQmlEngine*, QJSEngine*) -> QObject* { return new Singleton; }); 0144 0145 QQmlApplicationEngine eng; 0146 eng.load("main.qml"); 0147 0148 return app.exec(); 0149 } 0150 0151 #include "main.moc"