Page MenuHomePhorge

No OneTemporary

Size
13 KB
Referenced Files
None
Subscribers
None
diff --git a/doc.org b/doc.org
index 788ca4f..7af3c8d 100644
--- a/doc.org
+++ b/doc.org
@@ -1,134 +1,149 @@
#+PROPERTY: header-args :exports code
* What is lager?
- library to help with *value-oriented design*
- store application state as a *value* type
- state transformations are described in pure functions (*reducers*), src_c++{auto update(Model, Action) -> Model;}
- actions are also value types
* What is a value type?
- copyable
- equality comparable
#+BEGIN_SRC c++
T a = ...;
T b = a;
REQUIRE(a == b);
#+END_SRC
- identity does not depend on a reference
#+BEGIN_SRC c++
// not a value type
struct Person
{
std::string name;
std::vector<std::weak_ptr<Person>> contacts;
};
// value type
struct Person
{
using IdType = std::string;
IdType id;
std::string name;
std::vector<IdType> contacts;
};
#+END_SRC
* Basic example of a value-oriented program
+=logic.hpp=
#+INCLUDE: "src/basic-example/logic.hpp" src c++
+=logic.cpp=
#+INCLUDE: "src/basic-example/logic.cpp" src c++
+=main.cpp=
#+INCLUDE: "src/basic-example/main.cpp" src c++
The basic example just covered doesn't even make use of lager (!!!)
* Benefits of using lager?
- Testability
- libkazv: [[https://lily-is.land/kazv/libkazv][76.9%]] test coverage
- libQuotient: [[https://sonarcloud.io/component_measures?metric=coverage&selected=quotient-im_libQuotient%3AQuotient&id=quotient-im_libQuotient][41.6%]] (raw), 46.9% (adjusted)
+=test.cpp=
#+INCLUDE: "src/basic-example/test.cpp" src c++
- Offload work into worker threads
#+BEGIN_SRC c++
QtConcurrent::run([model]() { // capture model by value
save(model);
});
#+END_SRC
- Performance of copying? Structural sharing, immer containers
- Undo/redo for free
- History is a list of models
- Undo/redo = changing the current index
- Normal actions = push a new model onto the list
* How to do IO?
** Reducers revisited
- src_c++{auto update(Model, Action) -> Model;}
- But also src_c++{auto update(Model, Action) -> std::pair<Model, Effect>;}
- What is an effect? A (maybe non-pure) function that takes a *context* as an argument.
- A *store* contains the model and the context. It also has an event loop. Reducers will be run in the event loop.
- The store can be constructed with *dependencies*, which can be accessed through the context.
- The context can also be used to dispatch actions.
+=model.hpp=
+#+INCLUDE: "src/effects-example/model.hpp" src c++
+=main.cpp=
#+INCLUDE: "src/effects-example/main.cpp" src c++
* How can lager help you integrate value-oriented design with a Qt application?
** Missing part: view
- In the example, src_c++{show()} is the view.
- We want to show the state as a src_c++{QWidget} or src_c++{QQuickItem}.
- Signal/slot connections to update display values.
- [[https://sinusoid.es/lager/cursors.html][Cursors]]
- src_c++{lager::reader} - readonly cursors
- readers can be watched
- src_c++{lager::store} is a reader
- map-transformations and some lenses
- src_c++{LAGER_QT} and src_c++{LAGER_QT_READER}
+=demo-app.hpp=
+#+INCLUDE: "src/qml-example/demo-app.hpp" src c++
+=demo-app.cpp=
+#+INCLUDE: "src/qml-example/demo-app.cpp" src c++
+=Main.qml=
+#+INCLUDE: "src/qml-example/Main.qml" src c++
+=main.cpp=
+#+INCLUDE: "src/qml-example/main.cpp" src c++
* Practical application problems
* List views
[[https://github.com/arximboldi/lager/issues/179]]
** Number as model in QML ListView
=main.cpp=
#+INCLUDE: "src/list-view/main.cpp" src c++
=demo-app.hpp=
#+INCLUDE: "src/list-view/demo-app.hpp" src c++
=demo-app.cpp=
#+INCLUDE: "src/list-view/demo-app.cpp" src c++
=demo-list.hpp=
#+INCLUDE: "src/list-view/demo-list.hpp" src c++
=demo-list.cpp=
#+INCLUDE: "src/list-view/demo-list.cpp" src c++
=demo-item.hpp=
#+INCLUDE: "src/list-view/demo-item.hpp" src c++
=demo-item.cpp=
#+INCLUDE: "src/list-view/demo-item.cpp" src c++
=Main.qml=
#+INCLUDE: "src/list-view/Main.qml" src qml
- Problem: scroll will reset every time we add/remove an item
- https://github.com/arximboldi/lager/issues/179
** Using a src_c++{QAbstractListModel}
=demo-list.hpp=
#+INCLUDE: "src/list-view-qt-model/demo-list.hpp" src c++
=demo-list.cpp=
#+INCLUDE: "src/list-view-qt-model/demo-list.cpp" src c++
=Main.qml=
#+INCLUDE: "src/list-view-qt-model/Main.qml" src qml
* Post-dispatch then-continuations
[[https://github.com/arximboldi/lager/issues/96]], [[https://github.com/arximboldi/lager/issues/106]]
- lager does not support this
- Resolved by creating another kind of store
- Effects return a Promise (for C++ programmers, it's more like src_c++{std::future}) instead of void
- src_c++{dispatch} can be src_c++{then}-ed, the callback will be run after the returned future becomes ready
#+INCLUDE: "src/libkazvstore-example/main.cpp" src c++
* Offloading work into worker threads
- src_c++{ctx.dispatch} is thread-safe, can use it to pass down the result of a heavy computation
- When heavy computation needs to be atomic, put the event loop into a worker thread
- Create a secondary store that has an event loop in the UI thread
- This is needed because cursor operations are not thread-safe [[https://github.com/arximboldi/lager/issues/118]]
* Using src_c++{lager::state} (leave to Dmitry)
* Appendix
** Calculating libQuotient coverage adjusted
libQuotient includes the generated csapi folder into coverage, while libkazv does not. Here is the code that calculates the coverage of libQuotient excluding the csapi folder.
#+NAME: libquotient-coverage
#+BEGIN_SRC perl
my $csapiCoverage = 0.169;
my $csapiUncovered = 1_668;
my $totalLines = 11_229;
my $totalUncovered = 6_561;
my $csapiLines = $csapiUncovered / (1-$csapiCoverage);
my $remainingLines = $totalLines - $csapiLines;
my $remainingUncovered = $totalUncovered - $csapiUncovered;
my $remainingCoverage = 1 - $remainingUncovered / $remainingLines;
$remainingCoverage;
#+END_SRC
#+RESULTS: libquotient-coverage
: 0.46940827964562
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 71cfd60..9bdd59d 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -1,7 +1,8 @@
add_subdirectory(basic-example)
add_subdirectory(effects-example)
+add_subdirectory(qml-example)
add_subdirectory(list-view)
add_subdirectory(list-view-qt-model)
if(libkazv_FOUND)
add_subdirectory(libkazvstore-example)
endif()
diff --git a/src/effects-example/main.cpp b/src/effects-example/main.cpp
index a55485b..835816c 100644
--- a/src/effects-example/main.cpp
+++ b/src/effects-example/main.cpp
@@ -1,96 +1,50 @@
#include <iostream>
-#include <variant>
#include <boost/asio.hpp>
#include <lager/event_loop/qt.hpp>
#include <lager/store.hpp>
#include <QObject>
#include <QCoreApplication>
#include <QTimer>
-
-struct Model
-{
- int value{0};
-};
-
-struct IncrementAction {};
-struct IncrementTwiceAction {};
-
-using Action = std::variant<IncrementAction, IncrementTwiceAction>;
-
-struct IOHandler
-{
- template<class T>
- void show(T x)
- {
- std::cout << "-->" << x << std::endl;
- }
-};
-
-using DepsT = lager::deps<IOHandler &>;
-
-std::pair<Model, lager::effect<Action, DepsT>> update(Model m, IncrementAction)
-{
- m.value += 1;
- return {m, [m](const auto &ctx) {
- auto &h = ctx.template get<IOHandler &>();
- h.show(m.value);
- h.show("increment action");
- }};
-}
-
-std::pair<Model, lager::effect<Action, DepsT>> update(Model m, IncrementTwiceAction)
-{
- return {m, [](const auto &ctx) {
- ctx.dispatch(IncrementAction{});
- ctx.dispatch(IncrementAction{});
- }};
-}
-
-std::pair<Model, lager::effect<Action, DepsT>> update(Model m, Action a)
-{
- return std::visit([&](const auto &action) {
- return update(m, action);
- }, a);
-}
+#include "model.hpp"
void repl(QObject *obj, lager::context<Action> ctx)
{
std::cout << "?" << std::endl;
std::string a;
std::cin >> a;
if (a == "1") {
ctx.dispatch(IncrementAction{});
} else if (a == "2") {
ctx.dispatch(IncrementTwiceAction{});
} else {
std::cout << "unrecognized action" << std::endl;
}
// XXX Why can't I just use QMetaObject::invokeMethod
// with QueuedConnection?
// Let's bear with it now. Will go back to this later.
QTimer::singleShot(100, obj, [=]() {
repl(obj, ctx);
});
}
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
QObject mainObject;
IOHandler handler;
auto store = lager::make_store<Action>(
Model{},
lager::with_qt_event_loop{mainObject},
lager::with_deps(std::ref(handler)));
auto context = static_cast<lager::context<Action>>(store);
QTimer::singleShot(0, &mainObject,
[obj=&mainObject, context]() {
repl(obj, context);
});
return app.exec();
}
diff --git a/src/effects-example/model.hpp b/src/effects-example/model.hpp
new file mode 100644
index 0000000..880cd0c
--- /dev/null
+++ b/src/effects-example/model.hpp
@@ -0,0 +1,52 @@
+
+#pragma once
+#include <variant>
+#include <iostream>
+#include <lager/context.hpp>
+#include <lager/effect.hpp>
+
+struct Model
+{
+ int value{0};
+};
+
+struct IncrementAction {};
+struct IncrementTwiceAction {};
+
+using Action = std::variant<IncrementAction, IncrementTwiceAction>;
+
+struct IOHandler
+{
+ template<class T>
+ void show(T x)
+ {
+ std::cout << "-->" << x << std::endl;
+ }
+};
+
+using DepsT = lager::deps<IOHandler &>;
+
+inline std::pair<Model, lager::effect<Action, DepsT>> update(Model m, IncrementAction)
+{
+ m.value += 1;
+ return {m, [m](const auto &ctx) {
+ auto &h = ctx.template get<IOHandler &>();
+ h.show(m.value);
+ h.show("increment action");
+ }};
+}
+
+inline std::pair<Model, lager::effect<Action, DepsT>> update(Model m, IncrementTwiceAction)
+{
+ return {m, [](const auto &ctx) {
+ ctx.dispatch(IncrementAction{});
+ ctx.dispatch(IncrementAction{});
+ }};
+}
+
+inline std::pair<Model, lager::effect<Action, DepsT>> update(Model m, Action a)
+{
+ return std::visit([&](const auto &action) {
+ return update(m, action);
+ }, a);
+}
diff --git a/src/qml-example/CMakeLists.txt b/src/qml-example/CMakeLists.txt
new file mode 100644
index 0000000..967e02a
--- /dev/null
+++ b/src/qml-example/CMakeLists.txt
@@ -0,0 +1,25 @@
+qt_add_qml_module(qml-example-qmlmodule
+ STATIC
+ URI land.lilyis.tusooa.lagerqtdemo.qmlexample
+ VERSION 0.0
+ SOURCES demo-app.cpp
+ QML_FILES Main.qml
+)
+
+target_link_libraries(qml-example-qmlmodule
+ PUBLIC
+ Qt6::Quick
+ Qt6::Concurrent
+)
+
+add_executable(qml-example
+ main.cpp
+)
+
+target_link_libraries(qml-example
+ PRIVATE
+ Qt6::Gui
+ Qt6::Widgets
+ qml-example-qmlmodule
+ qml-example-qmlmoduleplugin
+)
diff --git a/src/qml-example/Main.qml b/src/qml-example/Main.qml
new file mode 100644
index 0000000..f799a21
--- /dev/null
+++ b/src/qml-example/Main.qml
@@ -0,0 +1,26 @@
+
+import QtQuick
+import QtQuick.Controls
+import QtQuick.Layouts
+import org.kde.kirigami as Kirigami
+import land.lilyis.tusooa.lagerqtdemo.qmlexample
+
+Kirigami.ApplicationWindow {
+ id: root
+ property DemoApp app: DemoApp {}
+ pageStack.initialPage: Kirigami.ScrollablePage {
+ ColumnLayout {
+ Label {
+ text: `value: ${app.value}`
+ }
+ Button {
+ text: 'Increment'
+ onClicked: app.increment()
+ }
+ Button {
+ text: 'IncrementTwice'
+ onClicked: app.incrementTwice()
+ }
+ }
+ }
+}
diff --git a/src/qml-example/demo-app.cpp b/src/qml-example/demo-app.cpp
new file mode 100644
index 0000000..d7c4299
--- /dev/null
+++ b/src/qml-example/demo-app.cpp
@@ -0,0 +1,44 @@
+
+#include <immer/config.hpp>
+#include <lager/config.hpp>
+#include <lager/store.hpp>
+#include <lager/event_loop/qt.hpp>
+#include "demo-app.hpp"
+#include "model.hpp"
+
+struct DemoApp::Private
+{
+ IOHandler handler;
+ lager::store<Action, Model, DepsT> store;
+
+ Private(DemoApp *q)
+ : handler()
+ , store(lager::make_store<Action>(
+ Model{},
+ lager::with_qt_event_loop{*q},
+ lager::with_deps(std::ref(handler))
+ ))
+ {
+ }
+};
+
+DemoApp::DemoApp(QObject *parent)
+ : QObject(parent)
+ , m_d(std::make_unique<Private>(this))
+ , LAGER_QT(value)(m_d->store.map([](Model m) {
+ return m.value;
+ }))
+{
+}
+
+DemoApp::~DemoApp() = default;
+
+void DemoApp::increment()
+{
+ m_d->store.dispatch(IncrementAction{});
+}
+
+void DemoApp::incrementTwice()
+{
+ m_d->store.dispatch(IncrementTwiceAction{});
+}
diff --git a/src/qml-example/demo-app.hpp b/src/qml-example/demo-app.hpp
new file mode 100644
index 0000000..9cb4def
--- /dev/null
+++ b/src/qml-example/demo-app.hpp
@@ -0,0 +1,26 @@
+#pragma once
+#include <immer/config.hpp>
+#include <lager/config.hpp>
+#include <memory>
+#include <lager/extra/qt.hpp>
+#include <QObject>
+#include <QQmlEngine>
+
+class DemoApp : public QObject
+{
+ Q_OBJECT
+ QML_ELEMENT
+
+ struct Private;
+ std::unique_ptr<Private> m_d;
+
+public:
+ DemoApp(QObject *parent = nullptr);
+ ~DemoApp() override;
+
+ LAGER_QT_READER(int, value);
+
+ Q_INVOKABLE void increment();
+
+ Q_INVOKABLE void incrementTwice();
+};
diff --git a/src/qml-example/main.cpp b/src/qml-example/main.cpp
new file mode 100644
index 0000000..12709cd
--- /dev/null
+++ b/src/qml-example/main.cpp
@@ -0,0 +1,14 @@
+
+#include <QApplication>
+#include <QQmlApplicationEngine>
+
+using namespace Qt::Literals::StringLiterals;
+
+int main(int argc, char *argv[])
+{
+ QApplication app(argc, argv);
+ QQmlApplicationEngine engine;
+ engine.loadFromModule(u"land.lilyis.tusooa.lagerqtdemo.qmlexample"_s, u"Main"_s);
+
+ return app.exec();
+}
diff --git a/src/qml-example/model.hpp b/src/qml-example/model.hpp
new file mode 120000
index 0000000..5e1ca09
--- /dev/null
+++ b/src/qml-example/model.hpp
@@ -0,0 +1 @@
+../effects-example/model.hpp
\ No newline at end of file

File Metadata

Mime Type
text/x-diff
Expires
Sat, Nov 23, 4:18 PM (21 h, 59 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
39018
Default Alt Text
(13 KB)

Event Timeline