Class: Dommy::Rack::History
- Inherits:
-
Object
- Object
- Dommy::Rack::History
- Defined in:
- lib/dommy/rack/history.rb,
sig/dommy/rack.rbs
Overview
Browser-tab-style history stack.
Defined Under Namespace
Classes: Entry
Instance Method Summary collapse
-
#back ⇒ String?
Move the cursor back/forward one entry and return that Entry, or nil at the edge (Session picks the traversal mechanism from it).
-
#current ⇒ String?
URL-shaped views, kept for compatibility with existing callers.
- #current_entry ⇒ Object
- #entries ⇒ Array[String]
- #forward ⇒ String?
-
#initialize ⇒ History
constructor
A new instance of History.
- #push(url, window: nil, windex: nil) ⇒ void
-
#rebind_current(window:, windex:) ⇒ Object
Re-bind the current entry to a fresh window (a revisit re-loaded the URL into a new document), so later same-document sync matches it.
-
#replace_current_url(url) ⇒ Object
replaceState: the current entry's URL changes in place.
-
#sync_to(window, windex) ⇒ Object
Mirror a traversal the page itself performed (JS history.back()): move the cursor to the entry recorded for (window, windex).
Constructor Details
#initialize ⇒ History
Returns a new instance of History.
17 18 19 20 |
# File 'lib/dommy/rack/history.rb', line 17 def initialize @stack = [] @index = -1 end |
Instance Method Details
#back ⇒ String?
Move the cursor back/forward one entry and return that Entry, or nil at the edge (Session picks the traversal mechanism from it).
31 32 33 34 35 36 |
# File 'lib/dommy/rack/history.rb', line 31 def back return nil if @index <= 0 @index -= 1 current_entry end |
#current ⇒ String?
URL-shaped views, kept for compatibility with existing callers.
75 |
# File 'lib/dommy/rack/history.rb', line 75 def current = current_entry&.url |
#current_entry ⇒ Object
70 71 72 |
# File 'lib/dommy/rack/history.rb', line 70 def current_entry @stack[@index] if @index >= 0 end |
#entries ⇒ Array[String]
77 78 79 |
# File 'lib/dommy/rack/history.rb', line 77 def entries @stack.map(&:url) end |
#forward ⇒ String?
38 39 40 41 42 43 |
# File 'lib/dommy/rack/history.rb', line 38 def forward return nil if @index >= @stack.size - 1 @index += 1 current_entry end |
#push(url, window: nil, windex: nil) ⇒ void
This method returns an undefined value.
22 23 24 25 26 27 |
# File 'lib/dommy/rack/history.rb', line 22 def push(url, window: nil, windex: nil) kept = @index >= 0 ? @stack[0..@index] : [] @stack = kept + [Entry.new(url, window, windex)] @index = @stack.size - 1 url end |
#rebind_current(window:, windex:) ⇒ Object
Re-bind the current entry to a fresh window (a revisit re-loaded the URL into a new document), so later same-document sync matches it.
53 54 55 56 57 58 59 |
# File 'lib/dommy/rack/history.rb', line 53 def rebind_current(window:, windex:) entry = current_entry return unless entry entry.window = window entry.windex = windex end |
#replace_current_url(url) ⇒ Object
replaceState: the current entry's URL changes in place.
46 47 48 49 |
# File 'lib/dommy/rack/history.rb', line 46 def replace_current_url(url) current_entry&.url = url url end |
#sync_to(window, windex) ⇒ Object
Mirror a traversal the page itself performed (JS history.back()): move the cursor to the entry recorded for (window, windex). No-op when unknown (e.g. an entry created before sync was installed).
64 65 66 67 68 |
# File 'lib/dommy/rack/history.rb', line 64 def sync_to(window, windex) i = @stack.index { |e| e.window.equal?(window) && e.windex == windex } @index = i if i current_entry end |