Skip to content

Commit

Permalink
add observable dictionary docs page
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniil Korostelev committed Jun 20, 2024
1 parent df90c79 commit 4e53998
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
43 changes: 42 additions & 1 deletion docs/articles/collections-dict.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,44 @@
# Observable Dictionary

> TODO: document
`ObservableDictionary<T>` is an observable wrapper around the standard `Dictionary<T>`, providing the same API for managing the collection.

In addition to the standard dictionary methods, it provides the [`Observe`](xref:TinkState.ObservableDictionary`2.Observe) method to expose itself as a read-only observable so one can bind to it and react to its changes.

```csharp
interface ObservableDictionary<TKey, TValue> : IDictionary<TKey, TValue>
{
Observable<IReadOnlyDictionary<TKey, TValue>> Observe();
}
```

Like most of observables, `ObservableDictionary<T>` can be created by a static function in the `Observable` helper class:

```csharp
static class Observable
{
static ObservableDictionary<TKey, TValue> Dictionary<TKey, TValue>();
}
```

Adding and removing items will naturally trigger its bindings and propagate through auto-observables.

## Example

```csharp
var dict = Observable.Dictionary<string, int>();
dict["a"] = 1;
dict["b"] = 2;

var auto = Observable.Auto(() => string.Join(",", dict.Select(kvp => $"{kvp.Key}:{kvp.Value}")));
Console.WriteLine(auto.Value); // a:1,b:2
// initial binding prints a:1 and b:2
dict.Observe().Bind(readOnlyDict =>
{
foreach (var (key, value) in readOnlyDict) Console.WriteLine($"{key}:{value}");
});

dict["c"] = 3; // binding prints a:1, b:2 and c:3
Console.WriteLine(auto.Value); // a:1,b:2,c:3
```
2 changes: 1 addition & 1 deletion docs/articles/collections-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

`ObservableList<T>` is an observable wrapper around the standard `List<T>`, providing the same API for managing the collection.

In addition to the standard list method, it provides the [`Observe`](xref:TinkState.ObservableList`1.Observe) method to expose itself
In addition to the standard list methods, it provides the [`Observe`](xref:TinkState.ObservableList`1.Observe) method to expose itself
as a read-only observable so one can bind to it and react to its changes.

```csharp
Expand Down

0 comments on commit 4e53998

Please sign in to comment.