@@ -899,6 +899,13 @@ void Object::endSelectionSet(const std::shared_ptr<RequestState>&) const
899
899
{
900
900
}
901
901
902
+ OperationData::OperationData (std::shared_ptr<RequestState>&& state, response::Value&& variables, FragmentMap&& fragments)
903
+ : state(std::move(state))
904
+ , variables(std::move(variables))
905
+ , fragments(std::move(fragments))
906
+ {
907
+ }
908
+
902
909
// FragmentDefinitionVisitor visits the AST and collects all of the fragment
903
910
// definitions in the document.
904
911
class FragmentDefinitionVisitor
@@ -929,22 +936,6 @@ void FragmentDefinitionVisitor::visit(const peg::ast_node& fragmentDefinition)
929
936
_fragments.insert ({ fragmentDefinition.children .front ()->content (), Fragment (fragmentDefinition) });
930
937
}
931
938
932
- struct OperationParams : public std ::enable_shared_from_this<OperationParams>
933
- {
934
- explicit OperationParams (std::shared_ptr<RequestState>&& state, response::Value&& variables, FragmentMap&& fragments);
935
-
936
- std::shared_ptr<RequestState> state;
937
- response::Value variables;
938
- FragmentMap fragments;
939
- };
940
-
941
- OperationParams::OperationParams (std::shared_ptr<RequestState>&& state, response::Value&& variables, FragmentMap&& fragments)
942
- : state(std::move(state))
943
- , variables(std::move(variables))
944
- , fragments(std::move(fragments))
945
- {
946
- }
947
-
948
939
// OperationDefinitionVisitor visits the AST and executes the one with the specified
949
940
// operation name.
950
941
class OperationDefinitionVisitor
@@ -957,14 +948,17 @@ class OperationDefinitionVisitor
957
948
void visit (const peg::ast_node& operationDefinition);
958
949
959
950
private:
960
- std::shared_ptr<OperationParams > _params;
951
+ std::shared_ptr<OperationData > _params;
961
952
const TypeMap& _operations;
962
953
const std::string& _operationName;
963
954
std::future<response::Value> _result;
964
955
};
965
956
966
957
OperationDefinitionVisitor::OperationDefinitionVisitor (std::shared_ptr<RequestState> state, const TypeMap& operations, const std::string& operationName, response::Value&& variables, FragmentMap&& fragments)
967
- : _params(std::make_shared<OperationParams>(std::move(state), std::move(variables), std::move(fragments)))
958
+ : _params(std::make_shared<OperationData>(
959
+ std::move (state),
960
+ std::move(variables),
961
+ std::move(fragments)))
968
962
, _operations(operations)
969
963
, _operationName(operationName)
970
964
{
@@ -1151,6 +1145,18 @@ void OperationDefinitionVisitor::visit(const peg::ast_node& operationDefinition)
1151
1145
}
1152
1146
}
1153
1147
1148
+ SubscriptionData::SubscriptionData (std::shared_ptr<OperationData>&& data, std::unordered_set<SubscriptionName>&& fieldNames,
1149
+ std::unique_ptr<peg::ast<std::string>>&& query, std::string&& operationName, SubscriptionCallback&& callback,
1150
+ const peg::ast_node& selection)
1151
+ : data(std::move(data))
1152
+ , fieldNames(std::move(fieldNames))
1153
+ , query(std::move(query))
1154
+ , operationName(std::move(operationName))
1155
+ , callback(std::move(callback))
1156
+ , selection(selection)
1157
+ {
1158
+ }
1159
+
1154
1160
// SubscriptionDefinitionVisitor visits the AST collects the fields referenced in the subscription at the point
1155
1161
// where we create a subscription.
1156
1162
class SubscriptionDefinitionVisitor
@@ -1159,15 +1165,15 @@ class SubscriptionDefinitionVisitor
1159
1165
SubscriptionDefinitionVisitor (SubscriptionParams&& params, SubscriptionCallback&& callback, FragmentMap&& fragments);
1160
1166
1161
1167
const peg::ast_node& getRoot () const ;
1162
- SubscriptionRegistration getRegistration ();
1168
+ std::shared_ptr<SubscriptionData> getRegistration ();
1163
1169
1164
1170
void visit (const peg::ast_node& operationDefinition);
1165
1171
1166
1172
private:
1167
1173
SubscriptionParams _params;
1168
1174
SubscriptionCallback _callback;
1169
1175
FragmentMap _fragments;
1170
- std::unique_ptr<SubscriptionRegistration > _result;
1176
+ std::shared_ptr<SubscriptionData > _result;
1171
1177
};
1172
1178
1173
1179
SubscriptionDefinitionVisitor::SubscriptionDefinitionVisitor (SubscriptionParams&& params, SubscriptionCallback&& callback, FragmentMap&& fragments)
@@ -1182,7 +1188,7 @@ const peg::ast_node& SubscriptionDefinitionVisitor::getRoot() const
1182
1188
return *_params.query ->root ;
1183
1189
}
1184
1190
1185
- SubscriptionRegistration SubscriptionDefinitionVisitor::getRegistration ()
1191
+ std::shared_ptr<SubscriptionData> SubscriptionDefinitionVisitor::getRegistration ()
1186
1192
{
1187
1193
if (!_result)
1188
1194
{
@@ -1198,7 +1204,7 @@ SubscriptionRegistration SubscriptionDefinitionVisitor::getRegistration()
1198
1204
throw schema_exception ({ error.str () });
1199
1205
}
1200
1206
1201
- auto result = std::move (* _result);
1207
+ auto result = std::move (_result);
1202
1208
1203
1209
_result.reset ();
1204
1210
@@ -1274,13 +1280,16 @@ void SubscriptionDefinitionVisitor::visit(const peg::ast_node& operationDefiniti
1274
1280
});
1275
1281
});
1276
1282
1277
- _result.reset (new SubscriptionRegistration {
1278
- std::move (_params),
1279
- std::move (_callback),
1280
- selection,
1283
+ _result = std::make_shared<SubscriptionData>(
1284
+ std::make_shared<OperationData>(
1285
+ std::move (_params.state ),
1286
+ std::move (_params.variables ),
1287
+ std::move (_fragments)),
1281
1288
std::move (fieldNames),
1282
- std::move (_fragments)
1283
- });
1289
+ std::move (_params.query ),
1290
+ std::move (_params.operationName ),
1291
+ std::move (_callback),
1292
+ selection);
1284
1293
}
1285
1294
1286
1295
Request::Request (TypeMap&& operationTypes)
@@ -1332,7 +1341,7 @@ SubscriptionKey Request::subscribe(SubscriptionParams&& params, SubscriptionCall
1332
1341
auto registration = subscriptionVisitor.getRegistration ();
1333
1342
auto key = _nextKey++;
1334
1343
1335
- for (const auto & name : registration. fieldNames )
1344
+ for (const auto & name : registration-> fieldNames )
1336
1345
{
1337
1346
_listeners[name].insert (key);
1338
1347
}
@@ -1351,7 +1360,7 @@ void Request::unsubscribe(SubscriptionKey key)
1351
1360
return ;
1352
1361
}
1353
1362
1354
- for (const auto & name : itrSubscription->second . fieldNames )
1363
+ for (const auto & name : itrSubscription->second -> fieldNames )
1355
1364
{
1356
1365
auto itrListener = _listeners.find (name);
1357
1366
@@ -1390,20 +1399,20 @@ void Request::deliver(const SubscriptionName& name, const std::shared_ptr<Object
1390
1399
for (const auto & key : itrListeners->second )
1391
1400
{
1392
1401
auto itrSubscription = _subscriptions.find (key);
1393
- const auto & registration = itrSubscription->second ;
1402
+ auto registration = itrSubscription->second ;
1394
1403
std::future<response::Value> result;
1395
1404
1396
1405
try
1397
1406
{
1398
1407
result = std::async (std::launch::deferred,
1399
- [](std::future<response::Value> data)
1408
+ [registration ](std::future<response::Value> data)
1400
1409
{
1401
1410
response::Value document (response::Type::Map);
1402
1411
1403
1412
document.emplace_back (" data" , data.get ());
1404
1413
1405
1414
return document;
1406
- }, optionalOrDefaultSubscription->resolve (registration. params . state , registration. selection , registration. fragments , registration. params . variables ));
1415
+ }, optionalOrDefaultSubscription->resolve (registration-> data -> state , registration-> selection , registration-> data -> fragments , registration-> data -> variables ));
1407
1416
}
1408
1417
catch (const schema_exception& ex)
1409
1418
{
@@ -1416,7 +1425,7 @@ void Request::deliver(const SubscriptionName& name, const std::shared_ptr<Object
1416
1425
result = promise.get_future ();
1417
1426
}
1418
1427
1419
- registration. callback (std::move (result));
1428
+ registration-> callback (std::move (result));
1420
1429
}
1421
1430
}
1422
1431
0 commit comments