Skip to content

Commit

Permalink
Started implementing backing stores
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardoboss committed Feb 12, 2024
1 parent 038091e commit 53b0ed0
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/kiota_abstractions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,8 @@ 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';
part 'src/store/backed_model.dart';
part 'src/store/backing_store.dart';
part 'src/store/backing_store_factory.dart';
part 'src/store/backing_store_subscription_callback.dart';
part 'src/store/in_memory_backing_store.dart';
7 changes: 7 additions & 0 deletions lib/src/store/backed_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
part of '../../kiota_abstractions.dart';

/// Defines the contract for a model that is backed by a store.
abstract class BackedModel {
/// Gets the store that is backing the model.
BackingStore? get backingStore;
}
46 changes: 46 additions & 0 deletions lib/src/store/backing_store.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
part of '../../kiota_abstractions.dart';

/// Stores model information in a different location than the object properties.
///
/// Implementations can provide dirty tracking and caching capabilities or
/// integration with 3rd party stores.
abstract class BackingStore {
/// Gets a value from the backing store based on its key. Returns `null` if
/// the value hasn't changed and [returnOnlyChangedValues] is `true`.
T? get<T>(String key);

/// Sets or updates the stored value for the given key.
///
/// Will trigger subscriptions callbacks.
void set<T>(String key, T value);

/// Iterates all the values stored in the backing store. Values will be
/// filtered if [returnOnlyChangedValues] is `true`.
Iterable<MapEntry<String, Object?>> iterate();

/// Iterates the keys for all values that changed to `null`.
Iterable<String> iterateKeysForValuesChangedToNull();

/// Creates a subscription to any data change happening, optionally specifying
/// a [subscriptionId] to be able to unsubscribe later.
///
/// The given [callback] is invoked on data changes.
String subscribe(
BackingStoreSubscriptionCallback callback, [
String? subscriptionId,
]);

/// Unsubscribes a subscription by its [subscriptionId].
void unsubscribe(String subscriptionId);

/// Clears all the stored values. Doesn't trigger any subscription callbacks.
void clear();

/// Whether to return only values that have changed since the initialization
/// of the object when calling [get] and [iterate] methods.
abstract bool returnOnlyChangedValues;

/// Whether the initialization of the object and/or the initial
/// deserialization has been competed to track whether objects have changed.
abstract bool initializationCompleted;
}
7 changes: 7 additions & 0 deletions lib/src/store/backing_store_factory.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
part of '../../kiota_abstractions.dart';

/// Defines the contract for a factory that creates a [BackingStore].
abstract class BackingStoreFactory {
/// Creates a new instance of the [BackingStore].
BackingStore createBackingStore();
}
9 changes: 9 additions & 0 deletions lib/src/store/backing_store_subscription_callback.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
part of '../../kiota_abstractions.dart';

/// Defines the contract for a callback that is invoked when a value in the
/// [BackingStore] changes.
typedef BackingStoreSubscriptionCallback = void Function(
String dataKey,
Object? previousValue,
Object? newValue,
);
79 changes: 79 additions & 0 deletions lib/src/store/in_memory_backing_store.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
part of '../../kiota_abstractions.dart';

class InMemoryBackingStore implements BackingStore {
final Map<String, (bool, Object?)> _store = {};
final Map<String, BackingStoreSubscriptionCallback> _subscriptions = {};

bool _initializationCompleted = true;

bool get initializationCompleted => _initializationCompleted;

set initializationCompleted(bool value) {
_initializationCompleted = value;

for (final key in _store.keys) {
final tuple = _store[key]!;
final obj = tuple.$2;

if (obj is BackedModel) {
obj.backingStore?.initializationCompleted = value;
}

ensureCollectionPropertyIsConsistent(key, obj);

_store[key] = (!value, obj);
}
}

void ensureCollectionPropertyIsConsistent(String key, Object? value) {
// TODO(rbo): Implement this method.
}

@override
bool returnOnlyChangedValues = false;

@override
void clear() => _store.clear();

@override
T? get<T>(String key) {
if (key.isEmpty) {
throw ArgumentError('The key cannot be empty.');
}

if (!_store.containsKey(key)) {
return null;
}

final value = _store[key];
}

@override
Iterable<MapEntry<String, Object?>> iterate() {
// TODO: implement iterate
throw UnimplementedError();
}

@override
Iterable<String> iterateKeysForValuesChangedToNull() {
// TODO: implement iterateKeysForValuesChangedToNull
throw UnimplementedError();
}

@override
void set<T>(String key, T value) {
// TODO: implement set
}

@override
String subscribe(BackingStoreSubscriptionCallback callback,
[String? subscriptionId]) {
// TODO: implement subscribe
throw UnimplementedError();
}

@override
void unsubscribe(String subscriptionId) {
// TODO: implement unsubscribe
}
}

0 comments on commit 53b0ed0

Please sign in to comment.