@@ -8,6 +8,7 @@ import "dart:typed_data";
88
99import "../definitions/data_schema.dart" ;
1010import "../definitions/form.dart" ;
11+ import "../exceptions.dart" ;
1112import "../scripting_api.dart" as scripting_api;
1213import "content.dart" ;
1314import "content_serdes.dart" ;
@@ -16,17 +17,24 @@ import "content_serdes.dart";
1617class InteractionOutput implements scripting_api.InteractionOutput {
1718 /// Creates a new [InteractionOutput] based on a [Content] object.
1819 ///
19- /// A [ContentSerdes] object has to be passed for decoding the raw
20- /// payload contained in the [Content] object.
20+ /// A [_contentSerdes] object has to be passed for decoding the raw
21+ /// payload contained in the [_content] object.
22+ ///
23+ /// In contrast to the interface definition in the
24+ /// [Scripting API specification] , [_form] is defined as non-nullable here,
25+ /// since other parts of the code never pass a `null` value as an argument for
26+ /// this parameter.
27+ ///
28+ /// [Scripting API specification] : https://w3c.github.io/wot-scripting-api/#the-interactionoutput-interface
2129 InteractionOutput (
2230 this ._content,
23- this ._contentSerdes, [
31+ this ._contentSerdes,
2432 this ._form,
2533 this ._schema,
26- ] ) : _data = _content.body;
34+ ) : _data = _content.body;
2735
2836 final Content _content;
29- final Form ? _form;
37+ final Form _form;
3038 final DataSchema ? _schema;
3139 final Stream <List <int >> _data;
3240
@@ -38,6 +46,10 @@ class InteractionOutput implements scripting_api.InteractionOutput {
3846
3947 @override
4048 Future <ByteBuffer > arrayBuffer () async {
49+ if (dataUsed) {
50+ throw NotReadableException ("Data has already been read" );
51+ }
52+
4153 _dataUsed = true ;
4254 return _content.byteBuffer;
4355 }
@@ -52,8 +64,11 @@ class InteractionOutput implements scripting_api.InteractionOutput {
5264 return existingValue.value;
5365 }
5466
55- // TODO(JKRhb): Should a NotReadableError be thrown if schema is null?
56- // C.f. https://w3c.github.io/wot-scripting-api/#the-value-function
67+ if (schema == null ) {
68+ throw NotReadableException (
69+ "Can't convert data to a value because no DataSchema is present." ,
70+ );
71+ }
5772
5873 final value = await _contentSerdes.contentToValue (
5974 _content,
@@ -72,5 +87,5 @@ class InteractionOutput implements scripting_api.InteractionOutput {
7287 DataSchema ? get schema => _schema;
7388
7489 @override
75- Form ? get form => _form;
90+ Form get form => _form;
7691}
0 commit comments