Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore workspace snapshot from running app #268

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
47 changes: 46 additions & 1 deletion R/server.R
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@
id = id,
function(input, output, session) {

vals <- reactiveValues(stacks = list(), new_block = list())
vals <- reactiveValues(stacks = list(), new_block = list(), save = list())

Check warning on line 460 in R/server.R

View check run for this annotation

Codecov / codecov/patch

R/server.R#L460

Added line #L460 was not covered by tests

# Init existing stack modules
init(x, vals, session)
Expand Down Expand Up @@ -525,6 +525,51 @@
write(to_json(), file)
}
)

observeEvent(input$save, {
message("SAVING WORKSPACE")
vals$save <- blockr_serialize(x)

Check warning on line 531 in R/server.R

View check run for this annotation

Codecov / codecov/patch

R/server.R#L529-L531

Added lines #L529 - L531 were not covered by tests
})

# restore
observeEvent(input$restore, {
message("RESTORING WORKSPACE")
saved_stacks <- blockr_deserialize(vals$save)
current_stacks_names <- list_workspace_stacks()
saved_stacks_names <- names(dropNulls(lapply(saved_stacks, \(el) {
if (inherits(el, "stack")) get_stack_name(el)

Check warning on line 540 in R/server.R

View check run for this annotation

Codecov / codecov/patch

R/server.R#L535-L540

Added lines #L535 - L540 were not covered by tests
})))

# If length saved is < length current, we have to remove elements
if (length(current_stacks_names) > length(saved_stacks_names)) {
to_remove <- current_stacks_names[!(saved_stacks_names %in% current_stacks_names)]
lapply(to_remove, \(stack) {
removeUI(
selector = session$ns(stack)

Check warning on line 548 in R/server.R

View check run for this annotation

Codecov / codecov/patch

R/server.R#L544-L548

Added lines #L544 - L548 were not covered by tests
)
})
} else {
to_restore <- saved_stacks_names[!(saved_stacks_names %in% current_stacks_names)]

Check warning on line 552 in R/server.R

View check run for this annotation

Codecov / codecov/patch

R/server.R#L552

Added line #L552 was not covered by tests

lapply(to_restore, \(stack) {
tmp <- saved_stacks[[stack]]
stack_ui <- inject_remove_button(
tmp,
session$ns(stack)

Check warning on line 558 in R/server.R

View check run for this annotation

Codecov / codecov/patch

R/server.R#L554-L558

Added lines #L554 - L558 were not covered by tests
)

insertUI(
selector = ".stacks",
ui = stack_ui

Check warning on line 563 in R/server.R

View check run for this annotation

Codecov / codecov/patch

R/server.R#L561-L563

Added lines #L561 - L563 were not covered by tests
)

})
}

restore_workspace(blockr_deserialize(vals$save))

Check warning on line 569 in R/server.R

View check run for this annotation

Codecov / codecov/patch

R/server.R#L569

Added line #L569 was not covered by tests
# Reinit all stack modules
init(x, vals, session)

Check warning on line 571 in R/server.R

View check run for this annotation

Codecov / codecov/patch

R/server.R#L571

Added line #L571 was not covered by tests
})
}
)
}
Expand Down
12 changes: 12 additions & 0 deletions R/ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -423,9 +423,21 @@ generate_ui.workspace <- function(x, id, ...) {
),
downloadButton(
ns("serialize"),
"Export",
class = "mx-2"
),
actionButton(
ns("save"),
"Save",
icon("save"),
class = "mx-2"
),
actionButton(
ns("restore"),
"Restore",
icon("reload"),
class = "mx-2"
)
),
div(class = "m-2 row workspace", stack_ui)
)
Expand Down
Loading