Skip to content

Commit

Permalink
fixup! feat: implement exploreDirectory method
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Dec 23, 2023
1 parent 20f14b0 commit 9ff5152
Showing 1 changed file with 36 additions and 27 deletions.
63 changes: 36 additions & 27 deletions lib/src/core/wot.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,33 +127,7 @@ class WoT implements scripting_api.WoT {

final thingDescriptionStream = Stream.fromIterable(
rawThingDescriptions.whereType<Map<String, dynamic>>(),
).transform(
StreamTransformer<Map<String, dynamic>, ThingDescription>(
(stream, cancelOnError) {
final streamController = StreamController<ThingDescription>();

final streamSubscription = stream.listen(
(rawThingDescription) {
try {
streamController
.add(ThingDescription.fromJson(rawThingDescription));
} on Exception catch (exception) {
streamController.addError(exception);
}
},
onDone: streamController.close,
onError: streamController.addError,
cancelOnError: cancelOnError,
);
streamController
..onPause = streamSubscription.pause
..onResume = streamSubscription.resume
..onCancel = streamSubscription.cancel;

return streamController.stream.listen(null);
},
),
);
).toThingDescriptionStream();

return ThingDiscoveryProcess(thingDescriptionStream, filter);
}
Expand All @@ -179,3 +153,38 @@ extension _DirectoryValidationExtension on ThingDescription {
atTypes.contains(type);
}
}

extension _DirectoryTdDeserializationExtension on Stream<Map<String, dynamic>> {
Stream<ThingDescription> toThingDescriptionStream() {
const streamTransformer = StreamTransformer(_transformerMethod);

return transform(streamTransformer);
}

static StreamSubscription<ThingDescription> _transformerMethod(
Stream<Map<String, dynamic>> rawThingDescriptionStream,
bool cancelOnError,
) {
final streamController = StreamController<ThingDescription>();

final streamSubscription = rawThingDescriptionStream.listen(
(rawThingDescription) {
try {
streamController.add(ThingDescription.fromJson(rawThingDescription));
} on Exception catch (exception) {
streamController.addError(exception);
}
},
onDone: streamController.close,
onError: streamController.addError,
cancelOnError: cancelOnError,
);

streamController
..onPause = streamSubscription.pause
..onResume = streamSubscription.resume
..onCancel = streamSubscription.cancel;

return streamController.stream.listen(null);
}
}

0 comments on commit 9ff5152

Please sign in to comment.