1 | // Copyright (C) 2021 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | #include "qjsontypedrpc_p.h" |
4 | #include <QtCore/QtGlobal> |
5 | |
6 | QT_BEGIN_NAMESPACE |
7 | |
8 | namespace QJsonRpc { |
9 | void TypedResponse::addOnCloseAction(const OnCloseAction &act) |
10 | { |
11 | switch (m_status) { |
12 | case Status::Started: |
13 | m_onCloseActions.append(t: act); |
14 | break; |
15 | case Status::Invalid: |
16 | qCWarning(QTypedJson::jsonRpcLog) |
17 | << "addOnCloseAction called on moved QJsonTypedResponse" << idToString(v: m_id); |
18 | Q_ASSERT(false); |
19 | Q_FALLTHROUGH(); |
20 | case Status::SentSuccess: |
21 | case Status::SentError: |
22 | act(m_status, m_id, *m_typedRpc); |
23 | break; |
24 | } |
25 | } |
26 | |
27 | void TypedResponse::doOnCloseActions() |
28 | { |
29 | m_typedRpc->doOnCloseAction(m_status, m_id); |
30 | for (const auto &a : m_onCloseActions) { |
31 | a(m_status, m_id, *m_typedRpc); |
32 | } |
33 | m_onCloseActions.clear(); |
34 | } |
35 | |
36 | void TypedResponse::sendErrorResponse(int code, const QByteArray &message) |
37 | { |
38 | sendErrorResponse<std::optional<int>>(code, message, data: std::optional<int>()); |
39 | } |
40 | |
41 | void TypedRpc::installOnCloseAction(const TypedResponse::OnCloseAction &closeAction) |
42 | { |
43 | m_onCloseAction = closeAction; |
44 | } |
45 | |
46 | TypedResponse::OnCloseAction TypedRpc::onCloseAction() |
47 | { |
48 | return m_onCloseAction; |
49 | } |
50 | |
51 | void TypedRpc::doOnCloseAction(TypedResponse::Status status, const IdType &id) |
52 | { |
53 | if (m_onCloseAction) |
54 | m_onCloseAction(status, id, *this); |
55 | } |
56 | |
57 | } // namespace QJsonRpc |
58 | |
59 | QT_END_NAMESPACE |
60 | |