ReactiveObject mirrors ReactiveListModel for a single entity. Loads via GET <baseUrl><source>, stays in sync via Mercure SSE on `topic`, and exposes the entity's JSON keys on a `data` QQmlPropertyMap so QML reads them as `obj.data.title` with bindings that re-evaluate on change. Properties: - source / topic / baseUrl / token (configuration) - data (QQmlPropertyMap*) — entity fields - ready — initial fetch finished - exists — entity present (false on 404 / delete) - pending — at least one optimistic mutation in flight - error invoke(method, path, body, optimistic) is identical in shape to ReactiveListModel.invoke(): apply optimistic to `data`, send the request with an Idempotency-Key, clear `pending` on the matching Mercure echo, roll back on 4xx/5xx or 10s timeout. The rollback restores backed-up values and removes keys we added optimistically. Wired into the QML module; the skeleton builds clean. Used by Phase 3 sub-commit 3's todo edit form. Includes the merged CI trigger change (workflow now runs on `main` branch only, not `dev` — keeps Gitea-runner pressure low while we're iterating on dev). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
137 lines
4.4 KiB
C++
137 lines
4.4 KiB
C++
#pragma once
|
|
|
|
#include <QHash>
|
|
#include <QJsonObject>
|
|
#include <QObject>
|
|
#include <QQmlPropertyMap>
|
|
#include <QString>
|
|
#include <QtQmlIntegration>
|
|
|
|
class QNetworkAccessManager;
|
|
class QNetworkReply;
|
|
class QTimer;
|
|
|
|
namespace PhpQml::Bridge {
|
|
|
|
class MercureClient;
|
|
|
|
/// Single-entity twin of ReactiveListModel.
|
|
///
|
|
/// Loads via HTTP GET on `source`, then keeps the entity in sync via
|
|
/// Mercure SSE on `topic`. The entity's JSON keys are exposed as
|
|
/// QML-bindable properties on `data` (a QQmlPropertyMap), so QML
|
|
/// reads them as `obj.data.title` etc. and re-evaluates on change.
|
|
///
|
|
/// `invoke()` provides optimistic mutations identical in shape to
|
|
/// ReactiveListModel.invoke(): apply locally + Idempotency-Key + roll
|
|
/// back on `4xx`/`5xx`/timeout, clear `pending` on the matching
|
|
/// Mercure echo (PLAN.md §5).
|
|
class ReactiveObject : public QObject
|
|
{
|
|
Q_OBJECT
|
|
QML_ELEMENT
|
|
|
|
Q_PROPERTY(QString baseUrl READ baseUrl WRITE setBaseUrl NOTIFY baseUrlChanged)
|
|
Q_PROPERTY(QString token READ token WRITE setToken NOTIFY tokenChanged)
|
|
Q_PROPERTY(QString source READ source WRITE setSource NOTIFY sourceChanged)
|
|
Q_PROPERTY(QString topic READ topic WRITE setTopic NOTIFY topicChanged)
|
|
Q_PROPERTY(QQmlPropertyMap* data READ data CONSTANT)
|
|
Q_PROPERTY(bool ready READ ready NOTIFY readyChanged)
|
|
Q_PROPERTY(bool pending READ pending NOTIFY pendingChanged)
|
|
Q_PROPERTY(bool exists READ exists NOTIFY existsChanged)
|
|
Q_PROPERTY(QString error READ error NOTIFY errorChanged)
|
|
|
|
public:
|
|
explicit ReactiveObject(QObject* parent = nullptr);
|
|
~ReactiveObject() override;
|
|
|
|
QString baseUrl() const { return m_baseUrl; }
|
|
void setBaseUrl(const QString& v);
|
|
|
|
QString token() const { return m_token; }
|
|
void setToken(const QString& v);
|
|
|
|
QString source() const { return m_source; }
|
|
void setSource(const QString& v);
|
|
|
|
QString topic() const { return m_topic; }
|
|
void setTopic(const QString& v);
|
|
|
|
QQmlPropertyMap* data() const { return m_data; }
|
|
bool ready() const noexcept { return m_ready; }
|
|
bool pending() const noexcept { return m_pending; }
|
|
bool exists() const noexcept { return m_exists; }
|
|
QString error() const { return m_error; }
|
|
|
|
Q_INVOKABLE void refresh();
|
|
|
|
/// Same shape as ReactiveListModel::invoke(). The optimistic diff
|
|
/// is applied to `data`'s keys; rollback restores the prior values
|
|
/// on failure.
|
|
Q_INVOKABLE void invoke(const QString& method,
|
|
const QString& path,
|
|
const QVariant& body,
|
|
const QVariantMap& optimistic);
|
|
|
|
signals:
|
|
void baseUrlChanged();
|
|
void tokenChanged();
|
|
void sourceChanged();
|
|
void topicChanged();
|
|
void readyChanged();
|
|
void pendingChanged();
|
|
void existsChanged();
|
|
void errorChanged();
|
|
|
|
void commandSucceeded(const QString& correlationKey, const QVariant& response);
|
|
void commandFailed(const QString& correlationKey, int status, const QVariant& problem);
|
|
void commandTimedOut(const QString& correlationKey);
|
|
|
|
private slots:
|
|
void onFetchFinished();
|
|
void onMercureUpdate(const QString& data, const QString& id);
|
|
void onCommandReplyFinished();
|
|
|
|
private:
|
|
void rewireMercure();
|
|
void fetchInitial();
|
|
void applyPayload(const QJsonObject& payload);
|
|
void clearData();
|
|
|
|
void setReady(bool r);
|
|
void setPending(bool p);
|
|
void setExists(bool e);
|
|
void setError(const QString& e);
|
|
|
|
QByteArray buildIdempotencyKey() const;
|
|
void rollbackOptimistic(const QByteArray& key);
|
|
|
|
QString m_baseUrl;
|
|
QString m_token;
|
|
QString m_source;
|
|
QString m_topic;
|
|
bool m_ready = false;
|
|
bool m_pending = false;
|
|
bool m_exists = false;
|
|
QString m_error;
|
|
|
|
QQmlPropertyMap* m_data = nullptr;
|
|
QNetworkAccessManager* m_nam = nullptr;
|
|
QNetworkReply* m_pendingFetch = nullptr;
|
|
MercureClient* m_mercure = nullptr;
|
|
|
|
int m_lastVersion = 0;
|
|
|
|
struct InFlight {
|
|
QHash<QString, QVariant> backup; // pre-mutation values for rollback
|
|
QStringList addedKeys; // keys we added (need removal on rollback)
|
|
};
|
|
QHash<QByteArray, InFlight> m_inFlight;
|
|
QHash<QNetworkReply*, QByteArray> m_replyToKey;
|
|
QHash<QByteArray, QTimer*> m_echoTimers;
|
|
|
|
static constexpr int kEchoTimeoutMs = 10000;
|
|
};
|
|
|
|
} // namespace PhpQml::Bridge
|