|
15 | 15 | #include <chrono>
|
16 | 16 | #include <future>
|
17 | 17 | #include <iostream>
|
| 18 | +#include <mutex> |
18 | 19 |
|
19 | 20 | namespace graphql::today {
|
20 | 21 |
|
| 22 | +const response::IdType& getFakeAppointmentId() noexcept |
| 23 | +{ |
| 24 | + static const auto s_fakeId = []() noexcept { |
| 25 | + std::string fakeIdString("fakeAppointmentId"); |
| 26 | + response::IdType result(fakeIdString.size()); |
| 27 | + |
| 28 | + std::copy(fakeIdString.cbegin(), fakeIdString.cend(), result.begin()); |
| 29 | + |
| 30 | + return result; |
| 31 | + }(); |
| 32 | + |
| 33 | + return s_fakeId; |
| 34 | +} |
| 35 | + |
| 36 | +const response::IdType& getFakeTaskId() noexcept |
| 37 | +{ |
| 38 | + static const auto s_fakeId = []() noexcept { |
| 39 | + std::string fakeIdString("fakeTaskId"); |
| 40 | + response::IdType result(fakeIdString.size()); |
| 41 | + |
| 42 | + std::copy(fakeIdString.cbegin(), fakeIdString.cend(), result.begin()); |
| 43 | + |
| 44 | + return result; |
| 45 | + }(); |
| 46 | + |
| 47 | + return s_fakeId; |
| 48 | +} |
| 49 | + |
| 50 | +const response::IdType& getFakeFolderId() noexcept |
| 51 | +{ |
| 52 | + static const auto s_fakeId = []() noexcept { |
| 53 | + std::string fakeIdString("fakeFolderId"); |
| 54 | + response::IdType result(fakeIdString.size()); |
| 55 | + |
| 56 | + std::copy(fakeIdString.cbegin(), fakeIdString.cend(), result.begin()); |
| 57 | + |
| 58 | + return result; |
| 59 | + }(); |
| 60 | + |
| 61 | + return s_fakeId; |
| 62 | +} |
| 63 | + |
| 64 | +std::unique_ptr<TodayMockService> mock_service() noexcept |
| 65 | +{ |
| 66 | + auto result = std::make_unique<TodayMockService>(); |
| 67 | + |
| 68 | + auto query = std::make_shared<Query>( |
| 69 | + [mockService = result.get()]() -> std::vector<std::shared_ptr<Appointment>> { |
| 70 | + ++mockService->getAppointmentsCount; |
| 71 | + return { std::make_shared<Appointment>(response::IdType(getFakeAppointmentId()), |
| 72 | + "tomorrow", |
| 73 | + "Lunch?", |
| 74 | + false) }; |
| 75 | + }, |
| 76 | + [mockService = result.get()]() -> std::vector<std::shared_ptr<Task>> { |
| 77 | + ++mockService->getTasksCount; |
| 78 | + return { |
| 79 | + std::make_shared<Task>(response::IdType(getFakeTaskId()), "Don't forget", true) |
| 80 | + }; |
| 81 | + }, |
| 82 | + [mockService = result.get()]() -> std::vector<std::shared_ptr<Folder>> { |
| 83 | + ++mockService->getUnreadCountsCount; |
| 84 | + return { |
| 85 | + std::make_shared<Folder>(response::IdType(getFakeFolderId()), "\"Fake\" Inbox", 3) |
| 86 | + }; |
| 87 | + }); |
| 88 | + auto mutation = std::make_shared<Mutation>( |
| 89 | + [](CompleteTaskInput&& input) -> std::shared_ptr<CompleteTaskPayload> { |
| 90 | + return std::make_shared<CompleteTaskPayload>( |
| 91 | + std::make_shared<Task>(std::move(input.id), "Mutated Task!", *(input.isComplete)), |
| 92 | + std::move(input.clientMutationId)); |
| 93 | + }); |
| 94 | + auto subscription = std::make_shared<NextAppointmentChange>( |
| 95 | + [](const std::shared_ptr<service::RequestState>&) -> std::shared_ptr<Appointment> { |
| 96 | + return { std::make_shared<Appointment>(response::IdType(getFakeAppointmentId()), |
| 97 | + "tomorrow", |
| 98 | + "Lunch?", |
| 99 | + true) }; |
| 100 | + }); |
| 101 | + |
| 102 | + result->service = std::make_shared<Operations>(std::move(query), |
| 103 | + std::move(mutation), |
| 104 | + std::move(subscription)); |
| 105 | + |
| 106 | + return result; |
| 107 | +} |
| 108 | + |
21 | 109 | Appointment::Appointment(
|
22 | 110 | response::IdType&& id, std::string&& when, std::string&& subject, bool isNow)
|
23 | 111 | : _id(std::move(id))
|
@@ -51,6 +139,9 @@ Query::Query(appointmentsLoader&& getAppointments, tasksLoader&& getTasks,
|
51 | 139 |
|
52 | 140 | void Query::loadAppointments(const std::shared_ptr<service::RequestState>& state)
|
53 | 141 | {
|
| 142 | + static std::mutex s_loaderMutex {}; |
| 143 | + std::lock_guard lock { s_loaderMutex }; |
| 144 | + |
54 | 145 | if (_getAppointments)
|
55 | 146 | {
|
56 | 147 | if (state)
|
@@ -84,6 +175,9 @@ std::shared_ptr<Appointment> Query::findAppointment(
|
84 | 175 |
|
85 | 176 | void Query::loadTasks(const std::shared_ptr<service::RequestState>& state)
|
86 | 177 | {
|
| 178 | + static std::mutex s_loaderMutex {}; |
| 179 | + std::lock_guard lock { s_loaderMutex }; |
| 180 | + |
87 | 181 | if (_getTasks)
|
88 | 182 | {
|
89 | 183 | if (state)
|
@@ -117,6 +211,9 @@ std::shared_ptr<Task> Query::findTask(
|
117 | 211 |
|
118 | 212 | void Query::loadUnreadCounts(const std::shared_ptr<service::RequestState>& state)
|
119 | 213 | {
|
| 214 | + static std::mutex s_loaderMutex {}; |
| 215 | + std::lock_guard lock { s_loaderMutex }; |
| 216 | + |
120 | 217 | if (_getUnreadCounts)
|
121 | 218 | {
|
122 | 219 | if (state)
|
@@ -496,6 +593,10 @@ double Mutation::applySetFloat(double valueArg) noexcept
|
496 | 593 | return valueArg;
|
497 | 594 | }
|
498 | 595 |
|
| 596 | +size_t NextAppointmentChange::_notifySubscribeCount = 0; |
| 597 | +size_t NextAppointmentChange::_subscriptionCount = 0; |
| 598 | +size_t NextAppointmentChange::_notifyUnsubscribeCount = 0; |
| 599 | + |
499 | 600 | std::stack<CapturedParams> NestedType::_capturedParams;
|
500 | 601 |
|
501 | 602 | NestedType::NestedType(service::FieldParams&& params, int depth)
|
|
0 commit comments