| am_counter_increment | R Documentation |
Increments an Automerge counter by the specified delta. Counters are CRDT types that support concurrent increments from multiple actors. Unlike regular integers, counter increments are commutative and do not conflict when merged.
am_counter_increment(doc, obj, key, delta)
doc |
An Automerge document |
obj |
An Automerge object ID (map or list), or |
key |
For maps: a character string key. For lists: an integer index (1-based) |
delta |
Integer value to add to the counter (can be negative) |
The delta can be negative to decrement the counter.
The document (invisibly), allowing for chaining with pipes
# Counter in document root (map)
doc <- am_create()
doc$score <- am_counter(0)
am_counter_increment(doc, AM_ROOT, "score", 10)
doc$score # 10
am_counter_increment(doc, AM_ROOT, "score", 5)
doc$score # 15
# Decrement with negative delta
am_counter_increment(doc, AM_ROOT, "score", -3)
doc$score # 12
# Counter in a nested map
doc$stats <- am_map(views = am_counter(0))
stats_obj <- doc$stats
am_counter_increment(doc, stats_obj, "views", 100)
# Counter in a list (1-based indexing)
doc$counters <- list(am_counter(0), am_counter(5))
counters_obj <- doc$counters
am_counter_increment(doc, counters_obj, 1, 1) # Increment first counter
am_counter_increment(doc, counters_obj, 2, 2) # Increment second counter
am_close(doc)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.