Change events for AtomHashMap and the STM system (via Ref) #1007
louthy
announced in
Announcements
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
As requested by @CK-LinoPro in this Issue.
AtomHashMap
andRef
now haveChange
events.Change<A>
Change
is a new union-type that represents change to a value, and is used byAtomHashMap
andTrackingHashMap
.You can pattern-match on the
Change
value to find out what happened:EntryMapped<A, B>
is derived fromEntryMappedFrom<A>
andEntryMappedTo<B>
, so for anyA -> B
mapping change, you can just match on the destination value:There are also various 'helper' properties and methods for working with the derived types:
HasNoChange
true
if the derived-type is aNoChange<A>
HasChanged
true
if the derived-type is one-ofEntryRemoved<A>
orEntryAdded<A>
orEntryMapped<_, A>
HasAdded
true
if the derived-type is aEntryAdded<A>
HasRemoved
true
if the derived-type is aEntryRemoved<A>
HasMapped
true
if the derived-type is aEntryMapped<A, A>
HasMappedFrom<FROM>()
true
if the derived-type is aEntryMapped<FROM, A>
ToOption()
Change
, as long as theChange
is one-ofEntryAdded
orEntryMapped
orEntryMappedTo
There are also constructor functions to build your own
Change
values.AtomHashMap<K, V>
andAtomHashMap<EqK, K, V>
The two variants of
AtomHashMap
both now haveChange
events that can be subscribed to. They emit aHashMapPatch<K, V>
value, which contains three fields:From
HashMap<K, V>
that is the state before the changeTo
HashMap<K, V>
that is the state after the changeChanges
HashMap<K, Change<V>>
that describes the changes to each keyThere are three related
Rx.NET
extensions in theLanguageExt.Rx
package:AtomHashMap
ExtensionOnChange()
HashMapPatch<K, V>
OnMapChange()
HashMap<K, V>
, which represents the latest snapshot of theAtomHashMap
OnEntryChange()
(K, Change<V>)
, which represents the change to any key within theAtomHashMap
Example
Running the code above yields:
Swap
method (potential) breaking-changeThe implementation of
Swap
has changed. It now expects aFunc<TrackingHashMap<K, V>, TrackingHashMap<K, V>>
delegate instead of aFunc<HashMap<K, V>, HashMap<K, V>>
delegate. This is so theSwap
method can keep track of arbitrary changes during the invocation of the delegate, and then emit them as events after successfully committing the result.Ref<A>
Refs are used with the
atomic(() => ...)
,snapshot(() => ...)
, andserial(() => ...)
STM transactions. Their changes are now tracked during a transaction, and are then (if the transaction is successful) emitted on theRef<A>.Change
event. These simply publish the latest value.As before there's
Rx
extensions for this:Ref
ExtensionOnChange()
Example
This outputs:
TrackingHashMap
This is a new immutable data-structure which is mostly a clone of
HashMap
; but one that allows for changes to be tracked. This is completely standalone, and not related to theAtomHashMap
in any way other than it's used by theAtomHashMap.Swap
method. And so, this has use-cases of its own.Changes are tracked as a
HashMap<K, Change<V>>
. That means there's at most one change-value stored per-key. So, there's no risk of an ever expanding log of changes, because there is no log! The changes that are tracked are from the 'last snapshot point'. Which is from the empty-map or from the last state wheretrackingHashMap.Snapshot()
is called.Example 1
This will output:
Note the
+
, this indicates anChange.EntryAdded
. And so there has been a single 'add' of the key-value pair(100, World)
. The"Hello"
value is ignored, because from the point of the snapshot: a value has been added. That's all we care about.If I take a snapshot halfway through, you can see how this changes the output:
This outputs:
So the snapshot is from when there was a
(100, World)
pair in the map.Hopefully that gives an indication of how this works!
This discussion was created from the release Change events for AtomHashMap and the STM system (via Ref).
Beta Was this translation helpful? Give feedback.
All reactions