Skip to content

Commit

Permalink
refactor!: rework subscribeResource API
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Feb 19, 2022
1 parent 5fb3448 commit 6893e03
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 35 deletions.
12 changes: 6 additions & 6 deletions lib/src/binding_coap/coap_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,11 @@ class CoapClient extends ProtocolClient {

@override
Future<Subscription> subscribeResource(
Form form,
void Function() deregisterObservation,
void Function(Content content) next,
void Function(Exception error)? error,
void Function()? complete) async {
Form form, {
required void Function(Content content) next,
void Function(Exception error)? error,
required void Function() complete,
}) async {
OperationType operationType;
final op = form.op ?? ["observeproperty"];
// TODO(JKRhb): Create separate function for this.
Expand All @@ -290,7 +290,7 @@ class CoapClient extends ProtocolClient {

final request = _createRequest(form, operationType);

return await request.startObservation(next, deregisterObservation);
return await request.startObservation(next, complete);
}

@override
Expand Down
8 changes: 3 additions & 5 deletions lib/src/binding_http/http_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,10 @@ class HttpClient extends ProtocolClient {
}

@override
Future<Subscription> subscribeResource(
Form form,
void Function() deregisterObservation,
void Function(Content content) next,
Future<Subscription> subscribeResource(Form form,
{required void Function(Content content) next,
void Function(Exception error)? error,
void Function()? complete) async {
required void Function() complete}) async {
// TODO: implement subscribeResource
throw UnimplementedError();
}
Expand Down
36 changes: 17 additions & 19 deletions lib/src/core/consumed_thing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -277,25 +277,23 @@ class ConsumedThing implements scripting_api.ConsumedThing {
final form = clientAndForm.form; // TODO(JKRhb): Handle URI variables
final client = clientAndForm.client;

final subscription = await client.subscribeResource(
form, () => removeSubscription(affordanceName, subscriptionType),
(content) {
try {
listener(InteractionOutput(
content, servient.contentSerdes, form, dataSchema));
} on Exception {
// Exception is handled by onError function. Not sure if this is the
// best design, though.
// TODO(JKRhb): Check if this try-catch-block can be removed.
}
}, (error) {
if (onError != null) {
onError(error);
}
}, () {
// TODO(JKRhb): current scripting api cannot handle this (apparently)
});

final subscription = await client.subscribeResource(form,
next: (content) {
try {
listener(InteractionOutput(
content, servient.contentSerdes, form, dataSchema));
} on Exception {
// Exception is handled by onError function. Not sure if this is the
// best design, though.
// TODO(JKRhb): Check if this try-catch-block can be removed.
}
},
error: (error) {
if (onError != null) {
onError(error);
}
},
complete: () => removeSubscription(affordanceName, subscriptionType));
if (subscriptionType == SubscriptionType.property) {
_observedProperties[affordanceName] = subscription;
} else {
Expand Down
8 changes: 3 additions & 5 deletions lib/src/core/protocol_interfaces/protocol_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ abstract class ProtocolClient {

/// Requests the client to perform a `subscribeproperty` operation on a
/// [form].
Future<Subscription> subscribeResource(
Form form,
void Function() deregisterObservation,
void Function(Content content) next,
Future<Subscription> subscribeResource(Form form,
{required void Function(Content content) next,
void Function(Exception error)? error,
void Function()? complete);
required void Function() complete});
}

0 comments on commit 6893e03

Please sign in to comment.