From 874957053c21766613dbad19c6118f380bcc65b2 Mon Sep 17 00:00:00 2001 From: Ricardo Boss Date: Wed, 14 Feb 2024 01:02:57 +0100 Subject: [PATCH] Added missing interfaces and implementations for backing stores --- lib/kiota_abstractions.dart | 4 ++ .../backing_store_factory_singleton.dart | 9 ++++ .../backing_store_parse_node_factory.dart | 28 +++++++++++++ ...re_serialization_writer_proxy_factory.dart | 42 +++++++++++++++++++ .../in_memory_backing_store_factory.dart | 7 ++++ 5 files changed, 90 insertions(+) create mode 100644 lib/src/store/backing_store_factory_singleton.dart create mode 100644 lib/src/store/backing_store_parse_node_factory.dart create mode 100644 lib/src/store/backing_store_serialization_writer_proxy_factory.dart create mode 100644 lib/src/store/in_memory_backing_store_factory.dart diff --git a/lib/kiota_abstractions.dart b/lib/kiota_abstractions.dart index 306a9d8..fa130cf 100644 --- a/lib/kiota_abstractions.dart +++ b/lib/kiota_abstractions.dart @@ -46,5 +46,9 @@ part 'src/serialization/serialization_writer_proxy_factory.dart'; part 'src/store/backed_model.dart'; part 'src/store/backing_store.dart'; part 'src/store/backing_store_factory.dart'; +part 'src/store/backing_store_factory_singleton.dart'; +part 'src/store/backing_store_parse_node_factory.dart'; +part 'src/store/backing_store_serialization_writer_proxy_factory.dart'; part 'src/store/backing_store_subscription_callback.dart'; part 'src/store/in_memory_backing_store.dart'; +part 'src/store/in_memory_backing_store_factory.dart'; diff --git a/lib/src/store/backing_store_factory_singleton.dart b/lib/src/store/backing_store_factory_singleton.dart new file mode 100644 index 0000000..f69b1cc --- /dev/null +++ b/lib/src/store/backing_store_factory_singleton.dart @@ -0,0 +1,9 @@ +part of '../../kiota_abstractions.dart'; + +/// This class is used to register the backing store factory. +class BackingStoreFactorySingleton { + static final BackingStoreFactory _instance = InMemoryBackingStoreFactory(); + + /// The backing store factory singleton instance. + static BackingStoreFactory get instance => _instance; +} diff --git a/lib/src/store/backing_store_parse_node_factory.dart b/lib/src/store/backing_store_parse_node_factory.dart new file mode 100644 index 0000000..2d18b1e --- /dev/null +++ b/lib/src/store/backing_store_parse_node_factory.dart @@ -0,0 +1,28 @@ +part of '../../kiota_abstractions.dart'; + +/// Proxy implementation of [ParseNodeFactory] that allows for the +/// [BackingStore] that automatically sets the state of the [BackingStore] +/// when deserializing. +class BackingStoreParseNodeFactory extends ParseNodeProxyFactory { + /// Creates a new instance of the [BackingStoreParseNodeFactory] class. + BackingStoreParseNodeFactory({ + required super.concrete, + }) : super( + onBefore: (parsable) { + if (parsable is BackedModel) { + final model = parsable as BackedModel; + if (model.backingStore != null) { + model.backingStore!.initializationCompleted = false; + } + } + }, + onAfter: (parsable) { + if (parsable is BackedModel) { + final model = parsable as BackedModel; + if (model.backingStore != null) { + model.backingStore!.initializationCompleted = true; + } + } + }, + ); +} diff --git a/lib/src/store/backing_store_serialization_writer_proxy_factory.dart b/lib/src/store/backing_store_serialization_writer_proxy_factory.dart new file mode 100644 index 0000000..71a4b86 --- /dev/null +++ b/lib/src/store/backing_store_serialization_writer_proxy_factory.dart @@ -0,0 +1,42 @@ +part of '../../kiota_abstractions.dart'; + +/// Proxy implementation of [SerializationWriterFactory] for the [BackingStore] +/// that automatically sets the state of the backing store when serializing. +class BackingStoreSerializationWriterProxyFactory + extends SerializationWriterProxyFactory { + /// Creates a new instance of [BackingStoreSerializationWriterProxyFactory] + /// with the provided concrete factory. + BackingStoreSerializationWriterProxyFactory({ + required super.concrete, + }) : super( + onBefore: (p) { + if (p is BackedModel) { + final model = p as BackedModel; + if (model.backingStore != null) { + model.backingStore!.returnOnlyChangedValues = true; + } + } + }, + onAfter: (p) { + if (p is BackedModel) { + final model = p as BackedModel; + if (model.backingStore != null) { + model.backingStore!.returnOnlyChangedValues = false; + model.backingStore!.initializationCompleted = true; + } + } + }, + onStart: (p, writer) { + if (p is BackedModel) { + final model = p as BackedModel; + if (model.backingStore != null) { + model.backingStore! + .iterateKeysForValuesChangedToNull() + .forEach((element) { + writer.writeNullValue(element); + }); + } + } + }, + ); +} diff --git a/lib/src/store/in_memory_backing_store_factory.dart b/lib/src/store/in_memory_backing_store_factory.dart new file mode 100644 index 0000000..6e8e405 --- /dev/null +++ b/lib/src/store/in_memory_backing_store_factory.dart @@ -0,0 +1,7 @@ +part of '../../kiota_abstractions.dart'; + +/// This class is used to create instances of [InMemoryBackingStore]. +class InMemoryBackingStoreFactory implements BackingStoreFactory { + @override + BackingStore createBackingStore() => InMemoryBackingStore(); +}