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

Setup provisional state scoped to current render #29

Merged
merged 2 commits into from
Nov 8, 2022
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
26 changes: 26 additions & 0 deletions lib/turbo_reflex/provisional_state.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

require_relative "state"

class TurboReflex::ProvisionalState
def initialize(state_manager)
@state_manager = state_manager
@keys = Set.new
end

attr_reader :keys

def [](*keys, default: nil)
state_manager[*keys, default: default]
end

def []=(*keys, value)
key = TurboReflex::State.key_for(*keys)
value.nil? ? self.keys.delete(key) : self.keys.add(key)
state_manager[key] = value
end

private

attr_reader :state_manager
end
15 changes: 8 additions & 7 deletions lib/turbo_reflex/state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,22 @@ def deserialize(string)
rescue => error
raise TurboReflex::StateDeserializationError, "Unable to decode, inflate, and load Base64 string! \"#{string}\" #{error.message}"
end

def key_for(*keys)
keys.map { |key| key.try(:cache_key) || key.to_s }.join("/")
end
end

def initialize(ordinal_payload = nil)
@internal_keys = []
@internal_data = {}.with_indifferent_access

self.class.deserialize(ordinal_payload).each do |(key, value)|
deserialize(ordinal_payload).each do |(key, value)|
write key, value
end
end

delegate :deserialize, :key_for, :serialize, :serialize_base64, to: "self.class"
delegate :size, to: :internal_data
delegate :include?, :has_key?, :key?, :member?, to: :internal_data

Expand All @@ -60,11 +65,11 @@ def write(*keys, value)
end

def payload
self.class.serialize_base64 internal_data
serialize_base64 internal_data
end

def ordinal_payload
self.class.serialize internal_list
serialize internal_list
end

def shrink!
Expand Down Expand Up @@ -100,10 +105,6 @@ def internal_list
internal_keys.map { |key| [key, internal_data[key]] }
end

def key_for(*keys)
keys.map { |key| key.try(:cache_key) || key.to_s }.join("/")
end

def shrink(obj)
case obj
when Array
Expand Down
13 changes: 13 additions & 0 deletions lib/turbo_reflex/state_manager.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require_relative "state"
require_relative "provisional_state"

# Class used to hold ephemeral state related to the rendered UI.
#
Expand Down Expand Up @@ -109,8 +110,15 @@ def []=(*keys, value)
state.write(*keys, value)
end

def provisional_state
@provisional_state ||= TurboReflex::ProvisionalState.new(self)
end

alias_method :now, :provisional_state

def write_cookie
return unless changed?
clear_provisional_state!
state.shrink!
state.prune! max_bytesize: TurboReflex::StateManager.cookie_max_bytesize
cookies.signed["turbo_reflex.state"] = {value: state.ordinal_payload, path: "/", expires: 1.day.from_now}
Expand All @@ -137,4 +145,9 @@ def header
def cookie
cookies.signed["turbo_reflex.state"]
end

def clear_provisional_state!
provisional_state.keys.each { |key| state.write key, nil }
@provisional_state = nil
end
end
2 changes: 1 addition & 1 deletion test/dummy/app/reflexes/demos_reflex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class DemosReflex < TurboReflex::Base
prevent_controller_action

def toggle
state[:active_demo] = visible? ? nil : element.aria.controls
state.now[:active_demo] = visible? ? nil : element.aria.controls
morph "##{demo_id}-demos", render("demos/#{demo_id}/demos")
end

Expand Down
8 changes: 4 additions & 4 deletions test/system/demos/increment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class IncrementTest < ApplicationSystemTestCase
# open demo
find_by_id("#{name}-demo").find("[data-turbo-reflex='DemosReflex#toggle']").click
assert page.evaluate_script("document.cookie").include?("turbo_reflex.state")
assert_equal "eyJhY3RpdmVfZGVtbyI6ImxpbmstaW4tZnJhbWUifQ", find_by_id("turbo-reflex", visible: false)["data-state"]
assert_equal name, page.evaluate_script("TurboReflex.state.active_demo")
# assert_equal "eyJhY3RpdmVfZGVtbyI6ImxpbmstaW4tZnJhbWUifQ", find_by_id("turbo-reflex", visible: false)["data-state"]
# assert_equal name, page.evaluate_script("TurboReflex.state.active_demo")
assert_equal 0, find_by_id("#{name}-demo").find("[data-role='counter']").text.to_i
assert_equal "N/A", find_by_id("#{name}-demo").find("[data-role='http-fingerprint']").text
assert_equal "N/A", find_by_id("#{name}-demo").find("[data-role='http-method']").text
Expand Down Expand Up @@ -46,8 +46,8 @@ class IncrementTest < ApplicationSystemTestCase
# open demo
find_by_id("#{name}-demo").find("[data-turbo-reflex='DemosReflex#toggle']").click
assert page.evaluate_script("document.cookie").include?("turbo_reflex.state")
assert_equal "eyJhY3RpdmVfZGVtbyI6ImJ1dHRvbi1pbi1mcmFtZSJ9", find_by_id("turbo-reflex", visible: false)["data-state"]
assert_equal name, page.evaluate_script("TurboReflex.state.active_demo")
# assert_equal "eyJhY3RpdmVfZGVtbyI6ImJ1dHRvbi1pbi1mcmFtZSJ9", find_by_id("turbo-reflex", visible: false)["data-state"]
# assert_equal name, page.evaluate_script("TurboReflex.state.active_demo")
assert_equal 0, find_by_id("#{name}-demo").find("[data-role='counter']").text.to_i
assert_equal "N/A", find_by_id("#{name}-demo").find("[data-role='http-fingerprint']").text
assert_equal "N/A", find_by_id("#{name}-demo").find("[data-role='http-method']").text
Expand Down