Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .Rprofile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
if (requireNamespace("devtools", quietly = TRUE)) {
try(devtools::load_all(".", quiet = TRUE))
}
15 changes: 14 additions & 1 deletion R/widget.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ REMOTE_ORIGIN <- NULL
if (inherits(value, "Prelim")) value else yr::Prelim$any(value)
}

# Compare a CRDT-stored value against a candidate R value for change detection.
# The CRDT normalizes types on round-trip (notably integers come back as doubles),
# so identical() alone reports spurious changes;
# fall back to a tolerant value-wise comparison when types differ.
.values_equal <- function(stored, value) {
if (identical(stored, value)) {
return(TRUE)
}
length(stored) == length(value) && isTRUE(all.equal(stored, value))
}

#' Holds the currently-active `yr::Transaction`, or NULL. Shared by a
#' WidgetBase and its storages so storage operations can join an ongoing
#' transaction instead of opening a nested one.
Expand Down Expand Up @@ -214,7 +225,9 @@ YAttrStorage <- R6::R6Class(
update = function(value) {
private$with_transaction(
function(trans) {
if (identical(private$attrs$get(trans, private$key), value)) {
# The CRDT may normalizes types on round-trip (e.g. an integer -> double)
# so a strict identical() would treat an unchanged value as changed.
if (.values_equal(private$attrs$get(trans, private$key), value)) {
return(FALSE)
}
private$attrs$insert(trans, private$key, .as_prelim(value))
Expand Down
2 changes: 0 additions & 2 deletions examples/widgets.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
"metadata": {},
"outputs": [],
"source": [
"# devtools::load_all(\".\")\n",
"\n",
"library(\"ywidgets\")"
]
},
Expand Down
Loading