Skip to content

Commit

Permalink
Added missing interfaces and implementations for backing stores
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardoboss committed Feb 14, 2024
1 parent bd3c576 commit 8749570
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/kiota_abstractions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
9 changes: 9 additions & 0 deletions lib/src/store/backing_store_factory_singleton.dart
Original file line number Diff line number Diff line change
@@ -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;
}
28 changes: 28 additions & 0 deletions lib/src/store/backing_store_parse_node_factory.dart
Original file line number Diff line number Diff line change
@@ -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;
}
}
},
);
}
Original file line number Diff line number Diff line change
@@ -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);
});
}
}
},
);
}
7 changes: 7 additions & 0 deletions lib/src/store/in_memory_backing_store_factory.dart
Original file line number Diff line number Diff line change
@@ -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();
}

0 comments on commit 8749570

Please sign in to comment.