Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore/Audacity CLA applies
4 : *
5 : * Copyright (C) 2026 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 : #include "commanddispatcher.h"
20 :
21 : #include "async/promise.h"
22 :
23 : #include "../commandable.h"
24 : #include "../commandtypes.h"
25 :
26 : #include "log.h"
27 :
28 : using namespace muse;
29 : using namespace muse::rcommand;
30 :
31 32 : CommandDispatcher::~CommandDispatcher()
32 : {
33 544 : for (auto it = m_clients.begin(); it != m_clients.end(); ++it) {
34 512 : Commandable* client = it->second.client;
35 512 : if (client) {
36 512 : client->setDispatcher(nullptr);
37 : }
38 : }
39 :
40 32 : m_clients.clear();
41 32 : }
42 :
43 0 : async::Promise<Response> CommandDispatcher::dispatch(const Request& request, bool maybeRedirect)
44 : {
45 0 : if (maybeRedirect) {
46 0 : auto redirect = commandsRegister()->commandInfo(request.command).redirect;
47 0 : if (redirect) {
48 0 : Request redirectRequest = request;
49 0 : redirectRequest.command = redirect->first;
50 0 : redirectRequest.params = redirect->second;
51 0 : return dispatch(redirectRequest, false);
52 0 : }
53 0 : }
54 :
55 0 : if (request.command == NONE_COMMAND) {
56 0 : return async::make_promise<Response>([request](auto resolve) {
57 0 : return resolve(make_response(request, make_ret(Ret::Code::Ok)));
58 0 : });
59 : }
60 :
61 0 : auto it = m_clients.find(request.command);
62 0 : if (it == m_clients.end()) {
63 0 : LOGW() << "command not registered: " << request.command;
64 0 : m_notRegistered.send(request.command);
65 0 : return async::make_promise<Response>([request](auto resolve) {
66 0 : return resolve(make_response(request, make_ret(Ret::Code::UnknownError)));
67 0 : });
68 : }
69 :
70 0 : LOGI() << "try call command: " << request.command << ", params: " << request.params;
71 :
72 0 : Client client = it->second;
73 :
74 0 : return async::make_promise<Response>([this, request, client](auto resolve) {
75 0 : if (client.asyncCallback) {
76 0 : bool allowDispatch = true;
77 0 : m_preDispatch.send(request.command, &allowDispatch);
78 0 : if (!allowDispatch) {
79 0 : return resolve(make_response(request, make_ret(Ret::Code::Cancel)));
80 : }
81 :
82 0 : client.asyncCallback(request, [this, request, resolve](const Response& response) {
83 0 : (void)resolve(response);
84 0 : m_postDispatch.send(request.command);
85 : });
86 :
87 0 : return async::Promise<Response>::dummy_result();
88 : }
89 :
90 0 : if (client.callback) {
91 0 : bool allowDispatch = true;
92 0 : m_preDispatch.send(request.command, &allowDispatch);
93 0 : if (!allowDispatch) {
94 0 : return resolve(make_response(request, make_ret(Ret::Code::Cancel)));
95 : }
96 :
97 0 : auto res = resolve(client.callback(request));
98 0 : m_postDispatch.send(request.command);
99 0 : return res;
100 : }
101 :
102 0 : UNREACHABLE;
103 : return resolve(make_response(request, make_ret(Ret::Code::UnknownError)));
104 0 : });
105 0 : }
106 :
107 36 : Response CommandDispatcher::dispatch(const Command& command, const Params& params)
108 : {
109 36 : Request request = make_request(command, params);
110 :
111 36 : auto it = m_clients.find(command);
112 36 : if (it == m_clients.end()) {
113 0 : LOGW() << "command not registered: " << command;
114 0 : m_notRegistered.send(command);
115 0 : return make_response(request, make_ret(Ret::Code::UnknownError));
116 : }
117 :
118 36 : LOGI() << "try call command: " << command << " with params: " << params;
119 :
120 36 : CallBack callback = it->second.callback;
121 :
122 36 : IF_ASSERT_FAILED(callback) {
123 0 : return make_response(request, make_ret(Ret::Code::NotSupported));
124 : }
125 :
126 36 : return callback(request);
127 36 : }
128 :
129 0 : Response CommandDispatcher::dispatch(const CommandQuery& query)
130 : {
131 0 : return dispatch(query.uri(), query.params());
132 : }
133 :
134 512 : void CommandDispatcher::onRequest(Commandable* client, const Command& command, const CallBack& callback)
135 : {
136 512 : reg(client, command, { client, callback, nullptr });
137 1024 : }
138 :
139 0 : void CommandDispatcher::onRequest(Commandable* client, const Command& command, const AsyncCallBack& callback)
140 : {
141 0 : reg(client, command, { client, nullptr, callback });
142 0 : }
143 :
144 512 : void CommandDispatcher::reg(Commandable* client, const Command& command, const Client& c)
145 : {
146 512 : if (m_clients.find(command) != m_clients.end()) {
147 0 : LOGW() << "command already registered: " << command;
148 0 : return;
149 : }
150 :
151 512 : m_clients[command] = c;
152 512 : client->setDispatcher(this);
153 : }
154 :
155 0 : void CommandDispatcher::unreg(Commandable* client)
156 : {
157 0 : if (!client || !client->isDispatcher(this)) {
158 0 : return;
159 : }
160 :
161 0 : for (auto it = m_clients.begin(); it != m_clients.end();) {
162 0 : if (it->second.client == client) {
163 0 : it = m_clients.erase(it);
164 0 : continue;
165 : }
166 0 : ++it;
167 : }
168 :
169 0 : client->setDispatcher(nullptr);
170 : }
171 :
172 0 : async::Channel<Command, bool*> CommandDispatcher::preDispatch() const
173 : {
174 0 : return m_preDispatch;
175 : }
176 :
177 0 : async::Channel<Command> CommandDispatcher::postDispatch() const
178 : {
179 0 : return m_postDispatch;
180 : }
181 :
182 0 : async::Channel<Command> CommandDispatcher::notRegistered() const
183 : {
184 0 : return m_notRegistered;
185 : }
|