Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore/Audacity CLA applies
4 : *
5 : * Copyright (C) MuseScore/Audacity and others
6 : *
7 : * This program is free software: you can redistribute it and/or modify
8 : * it under the terms of the GNU General Public License version 3 as
9 : * published by the Free Software Foundation.
10 : *
11 : * This program is distributed in the hope that it will be useful,
12 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : * GNU General Public License for more details.
15 : *
16 : * You should have received a copy of the GNU General Public License
17 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 : */
19 :
20 : #pragma once
21 :
22 : #include "modularity/imoduleinterface.h"
23 :
24 : #include "global/async/promise.h"
25 : #include "global/async/channel.h"
26 : #include "global/types/ret.h"
27 : #include "global/types/retval.h"
28 : #include "commandtypes.h"
29 : #include "typedcommand.h"
30 :
31 : namespace muse::rcommand {
32 : class Commandable;
33 : class ICommandDispatcher : MODULE_CONTEXT_INTERFACE
34 : {
35 78 : INTERFACE_ID(ICommandDispatcher)
36 : public:
37 236 : virtual ~ICommandDispatcher() = default;
38 :
39 : using OnResponse = std::function<void (const Response& response)>;
40 :
41 : using CallBack = std::function<Response (const Request& request)>;
42 : using AsyncCallBack = std::function<void (const Request& request, OnResponse onResponse)>;
43 :
44 : using CallBackRet = std::function<Ret ()>;
45 : using CallBackParamsRet = std::function<Ret (const Params& params)>;
46 : using CallBackParamsPromiseRet = std::function<async::Promise<Ret>(const Params& params)>;
47 :
48 : virtual async::Promise<Response> dispatch(const Request& request, bool maybeRedirect = true) = 0;
49 : virtual void onRequest(Commandable* client, const Command& command, const CallBack& callback) = 0;
50 : virtual void onRequest(Commandable* client, const Command& command, const AsyncCallBack& callback) = 0;
51 : virtual void unreg(Commandable* client) = 0;
52 :
53 : virtual async::Channel<Command, bool* /*allow dispatch*/> preDispatch() const = 0;
54 : virtual async::Channel<Command> postDispatch() const = 0;
55 : virtual async::Channel<Command> notRegistered() const = 0;
56 :
57 : // Helpers for convenience
58 2 : async::Promise<Response> dispatch(const Command& command)
59 : {
60 2 : return dispatch(make_request(command, {}));
61 : }
62 :
63 7 : async::Promise<Response> dispatch(const Command& command, const Params& params)
64 : {
65 7 : return dispatch(make_request(command, params));
66 : }
67 :
68 0 : async::Promise<Response> dispatch(const CommandQuery& query)
69 : {
70 0 : return dispatch(make_request(query.uri(), query.params()));
71 : }
72 :
73 550 : void onRequest(Commandable* client, const Command& command, const CallBackRet& callback)
74 : {
75 550 : onRequest(client, command, [callback](const Request& request) {
76 36 : return make_response(request, callback());
77 : });
78 550 : }
79 :
80 0 : void onRequest(Commandable* client, const Command& command, const CallBackParamsRet& callback)
81 : {
82 0 : onRequest(client, command, [callback](const Request& request) {
83 0 : return make_response(request, callback(request.params));
84 : });
85 0 : }
86 :
87 0 : void onRequest(Commandable* client, const Command& command, const CallBackParamsPromiseRet& callback)
88 : {
89 0 : onRequest(client, command, AsyncCallBack([callback](const Request& request, const OnResponse& onResponse) {
90 0 : answerWhenResolved(request, callback(request.params), onResponse);
91 0 : }));
92 0 : }
93 :
94 : // Typed commands
95 : template<TypedCommand C>
96 0 : async::Promise<Response> dispatch(const C& command)
97 : {
98 : if constexpr (HasRedirect<C>) {
99 0 : return dispatch(C::redirect);
100 : }
101 :
102 0 : Params params;
103 : if constexpr (HasInput<C>) {
104 0 : params = toInputParams(command);
105 : }
106 0 : return dispatch(make_request(command.id, params), false);
107 0 : }
108 :
109 : template<TypedCommand C>
110 0 : void onRequest(Commandable* client, std::function<Ret(const C&)> callback)
111 : {
112 0 : onRequest(client, C::id, CallBack([callback](const Request& request) {
113 0 : C command;
114 : if constexpr (HasInput<C>) {
115 0 : ConvertParamsResult res = fromInputParams(request.params, command);
116 0 : if (!res) {
117 0 : return make_response(request, res.ret);
118 : }
119 0 : }
120 0 : return make_response(request, callback(command));
121 0 : }));
122 0 : }
123 :
124 : template<HasOutput C>
125 0 : void onRequest(Commandable* client, std::function<RetVal<typename C::Output>(const C&)> callback)
126 : {
127 0 : onRequest(client, C::id, CallBack([callback](const Request& request) {
128 : C command;
129 : if constexpr (HasInput<C>) {
130 : ConvertParamsResult res = fromInputParams(request.params, command);
131 : if (!res) {
132 : return make_response(request, res.ret);
133 : }
134 : }
135 0 : RetVal<typename C::Output> rv = callback(command);
136 0 : return make_response(request, rv.ret, toOutputParams<C>(rv.val));
137 0 : }));
138 0 : }
139 :
140 : private:
141 :
142 0 : static void answerWhenResolved(const Request& request, async::Promise<Ret> retPromise, const OnResponse& onResponse)
143 : {
144 0 : retPromise.onResolve(nullptr, [request, onResponse](const Ret& ret) {
145 0 : onResponse(make_response(request, ret));
146 0 : });
147 0 : }
148 : };
149 : }
|