Skip to content

Commit

Permalink
feat(scripting_api)!: clean up Subscription interface
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Jan 18, 2022
1 parent 68caba9 commit 0cfcf72
Showing 1 changed file with 65 additions and 5 deletions.
70 changes: 65 additions & 5 deletions lib/src/scripting_api/subscription.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import '../definitions/form.dart';
import '../definitions/interaction_affordances/interaction_affordance.dart';
import 'consumed_thing.dart';
import 'interaction_options.dart';

/// Indicates the type of the subscription.
enum SubscriptionType {
Expand All @@ -23,11 +23,71 @@ enum SubscriptionType {

/// Represents a subscription to Property change and Event interactions.
abstract class Subscription {
/// Indicates what WoT Interaction this [Subscription] refers to.
SubscriptionType? type;
/// Denotes whether the subsciption is active, i.e. it is not stopped because
/// of an error or because of invocation of the [stop] method.
bool get active;

/// The Property or Event name.
String? name;
/// Stops delivering notifications for the subscription.
/// It takes an optional parameter [options] and returns a [Future].
Future<void> stop([InteractionOptions? options]);
}

/// Finds a matching unsubscribe [Form] for a subscription [form].
///
/// Uses either a dedicated [formIndex] or determines the [Form] using
/// the [interaction] Affordance and the [type] of subscription it belongs to.
// TODO(JKRhb): Using an index does not seem the best idea to me.
Form findUnsubscribeForm(InteractionAffordance interaction,
SubscriptionType type, Form form, int? formIndex) {
if (formIndex != null) {
interaction.forms[formIndex];
}

final operationType = _determineOpType(type);
final formOperations = form.op;

// The default op value also contains the unsubscribe/unobserve operation.
if (formOperations == null || formOperations.contains(operationType)) {
return form;
}

final unsubscribeForm = _findFormByScoring(interaction, form, operationType);

if (unsubscribeForm == null) {
// TODO(JKRhb): Add appropriate Exception type.
throw Exception("Could not find matching form for unsubscribe");
}

return unsubscribeForm;
}

String _determineOpType(SubscriptionType? subscriptionType) {
switch (subscriptionType) {
case SubscriptionType.event:
return "unsubscribeevent";
case SubscriptionType.property:
return "unobserveproperty";
default:
throw Exception();
}
}

Form? _findFormByScoring(
InteractionAffordance interaction, Form form, String operationType) {
int maxScore = 0;
Form? foundForm;

for (Form currentForm in interaction.augmentedForms) {
int score;
if (form.op!.contains(operationType)) {
score = 1;
} else {
continue;
}

if (Uri.parse(form.href).origin == Uri.parse(currentForm.href).origin) {
score++;
}

/// The Thing Description fragment that describes the WoT [interaction].
InteractionAffordance? interaction;
Expand Down

0 comments on commit 0cfcf72

Please sign in to comment.