From 038091e88f51c917023b0a92f6649cc579d4e564 Mon Sep 17 00:00:00 2001 From: Ricardo Boss Date: Mon, 12 Feb 2024 21:22:47 +0100 Subject: [PATCH] Added SerializationWriterProxyFactory --- lib/kiota_abstractions.dart | 1 + .../serialization/serialization_writer.dart | 4 +- .../serialization_writer_proxy_factory.dart | 47 +++++++++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 lib/src/serialization/serialization_writer_proxy_factory.dart diff --git a/lib/kiota_abstractions.dart b/lib/kiota_abstractions.dart index 55255c9..47b501a 100644 --- a/lib/kiota_abstractions.dart +++ b/lib/kiota_abstractions.dart @@ -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'; diff --git a/lib/src/serialization/serialization_writer.dart b/lib/src/serialization/serialization_writer.dart index 0559ab5..b389640 100644 --- a/lib/src/serialization/serialization_writer.dart +++ b/lib/src/serialization/serialization_writer.dart @@ -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; diff --git a/lib/src/serialization/serialization_writer_proxy_factory.dart b/lib/src/serialization/serialization_writer_proxy_factory.dart new file mode 100644 index 0000000..f51f55a --- /dev/null +++ b/lib/src/serialization/serialization_writer_proxy_factory.dart @@ -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; + } +}