~aleteoryx/muditaos

ref: 48d31b876cf6fed438676e4fa73b6e16b1f6b3a7 muditaos/module-apps/AsyncTask.cpp -rw-r--r-- 2.1 KiB
48d31b87 — tomaszkrosnowski [EGD-6311] Audio settings windows 4 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "AsyncTask.hpp"
#include "Application.hpp"

namespace app
{
    AsyncCallbackReceiver::AsyncCallbackReceiver(AsyncCallbacksDeleter *deleter) noexcept : deleter{deleter}
    {}

    AsyncCallbackReceiver::~AsyncCallbackReceiver()
    {
        if (deleter != nullptr) {
            deleter->cancelCallbacks(this);
        }
    }

    void AsyncTask::execute(Application *application, AsyncCallbackReceiver::Ptr receiverObject)
    {
        const auto requestId = onExecute(application);
        application->callbackStorage->registerCallback(requestId, receiverObject);
    }

    std::unique_ptr<AsyncQuery> AsyncQuery::createFromQuery(std::unique_ptr<db::Query> &&query,
                                                            db::Interface::Name target)
    {
        return std::make_unique<AsyncQuery>(std::move(query), target);
    }

    AsyncQuery::AsyncQuery(std::unique_ptr<db::Query> &&query, db::Interface::Name target) noexcept
        : query{std::move(query)}, target{target}
    {}

    void AsyncQuery::setCallback(std::unique_ptr<db::QueryListener> &&listener) noexcept
    {
        query->setQueryListener(std::move(listener));
    }

    void AsyncQuery::setCallback(db::QueryCallbackFunction &&callback) noexcept
    {
        query->setQueryListener(db::QueryCallback::fromFunction(std::move(callback)));
    }

    RequestId AsyncQuery::onExecute(Application *application)
    {
        const auto [_, id] = DBServiceAPI::GetQuery(application, target, std::move(query));
        return id;
    }

    auto NullCallback::execute() -> bool
    {
        // Nothing to do.
        return false;
    }

    QueryCallback::QueryCallback(db::QueryResponse *response) : response{response}
    {}

    auto QueryCallback::execute() -> bool
    {
        const auto result = response->getResult();
        if (result != nullptr && result->hasListener()) {
            return result->handle();
        }
        return false;
    }
} // namespace app