-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
038091e
commit 53b0ed0
Showing
6 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |