async: add Promise and use it for AsyncDialogBox

This commit is contained in:
Vaxry 2025-04-27 00:03:32 +01:00
parent 4f868a1f3c
commit 0302bfdc22
No known key found for this signature in database
GPG key ID: 665806380871D640
7 changed files with 208 additions and 36 deletions

View file

@ -59,8 +59,7 @@ void CAsyncDialogBox::onWrite(int fd, uint32_t mask) {
if (mask & (WL_EVENT_HANGUP | WL_EVENT_ERROR)) {
Debug::log(LOG, "CAsyncDialogBox: dialog {:x} hung up, closed.");
if (m_onResolution)
m_onResolution(m_stdout);
m_promiseResolver->resolve(m_stdout);
wl_event_source_remove(m_readEventSource);
m_selfReference.reset();
@ -68,9 +67,7 @@ void CAsyncDialogBox::onWrite(int fd, uint32_t mask) {
}
}
void CAsyncDialogBox::open(std::function<void(std::string)> onResolution) {
m_onResolution = onResolution;
SP<CPromise<std::string>> CAsyncDialogBox::open() {
std::string buttonsString = "";
for (auto& b : m_buttons) {
buttonsString += b + ";";
@ -83,7 +80,7 @@ void CAsyncDialogBox::open(std::function<void(std::string)> onResolution) {
int outPipe[2];
if (pipe(outPipe)) {
Debug::log(ERR, "CAsyncDialogBox::open: failed to pipe()");
return;
return nullptr;
}
m_pipeReadFd = CFileDescriptor(outPipe[0]);
@ -94,7 +91,7 @@ void CAsyncDialogBox::open(std::function<void(std::string)> onResolution) {
if (!m_readEventSource) {
Debug::log(ERR, "CAsyncDialogBox::open: failed to add read fd to loop");
return;
return nullptr;
}
m_selfReference = m_selfWeakReference.lock();
@ -102,13 +99,17 @@ void CAsyncDialogBox::open(std::function<void(std::string)> onResolution) {
if (!proc.runAsync()) {
Debug::log(ERR, "CAsyncDialogBox::open: failed to run async");
wl_event_source_remove(m_readEventSource);
return;
return nullptr;
}
m_dialogPid = proc.pid();
// close the write fd, only the dialog owns it now
close(outPipe[1]);
auto promise = CPromise<std::string>::make([this](SP<CPromiseResolver<std::string>> r) { m_promiseResolver = r; });
return promise;
}
void CAsyncDialogBox::kill() {