Skip to content

Commit

Permalink
Added SerializationWriterProxyFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardoboss committed Feb 12, 2024
1 parent 46133cf commit 038091e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/kiota_abstractions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ part 'src/serialization/parse_node_proxy_factory.dart';
part 'src/serialization/serialization_writer.dart';
part 'src/serialization/serialization_writer_factory.dart';
part 'src/serialization/serialization_writer_factory_registry.dart';
part 'src/serialization/serialization_writer_proxy_factory.dart';
4 changes: 2 additions & 2 deletions lib/src/serialization/serialization_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ abstract class SerializationWriter {
Uint8List getSerializedContent();

/// Callback called before the serialization process starts.
void Function(Parsable)? onBeforeObjectSerialization;
ParsableHook? onBeforeObjectSerialization;

/// Callback called after the serialization process ends.
void Function(Parsable)? onAfterObjectSerialization;
ParsableHook? onAfterObjectSerialization;

/// Callback called right after the serialization process starts.
void Function(Parsable, SerializationWriter)? onStartObjectSerialization;
Expand Down
47 changes: 47 additions & 0 deletions lib/src/serialization/serialization_writer_proxy_factory.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
part of '../../kiota_abstractions.dart';

abstract class SerializationWriterProxyFactory
implements SerializationWriterFactory {
SerializationWriterProxyFactory({
required SerializationWriterFactory concrete,
ParsableHook? onBefore,
ParsableHook? onAfter,
void Function(Parsable, SerializationWriter)? onStart,
}) : _concrete = concrete,
_onBefore = onBefore,
_onAfter = onAfter,
_onStart = onStart;

final SerializationWriterFactory _concrete;
final ParsableHook? _onBefore;
final ParsableHook? _onAfter;
final void Function(Parsable, SerializationWriter)? _onStart;

@override
String get validContentType => _concrete.validContentType;

@override
SerializationWriter getSerializationWriter(String contentType) {
final writer = _concrete.getSerializationWriter(contentType);

final originalBefore = writer.onBeforeObjectSerialization;
writer.onBeforeObjectSerialization = (p) {
_onBefore?.call(p);
originalBefore?.call(p);
};

final originalAfter = writer.onAfterObjectSerialization;
writer.onAfterObjectSerialization = (p) {
_onAfter?.call(p);
originalAfter?.call(p);
};

final originalStart = writer.onStartObjectSerialization;
writer.onStartObjectSerialization = (p, w) {
_onStart?.call(p, w);
originalStart?.call(p, w);
};

return writer;
}
}

0 comments on commit 038091e

Please sign in to comment.