Class: Dommy::Rack::History

Inherits:
Object
  • Object
show all
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

Constructor Details

#initializeHistory

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

#backString?

Move the cursor back/forward one entry and return that Entry, or nil at the edge (Session picks the traversal mechanism from it).

Returns:

  • (String, nil)


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

#currentString?

URL-shaped views, kept for compatibility with existing callers.

Returns:

  • (String, nil)


75
# File 'lib/dommy/rack/history.rb', line 75

def current = current_entry&.url

#current_entryObject



70
71
72
# File 'lib/dommy/rack/history.rb', line 70

def current_entry
  @stack[@index] if @index >= 0
end

#entriesArray[String]

Returns:

  • (Array[String])


77
78
79
# File 'lib/dommy/rack/history.rb', line 77

def entries
  @stack.map(&:url)
end

#forwardString?

Returns:

  • (String, nil)


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.

Parameters:

  • url (String)


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