Class: Dommy::Navigation::JointHistory

Inherits:
Object
  • Object
show all
Defined in:
lib/dommy/navigation.rb

Overview

Browser-tab-style joint history: ONE ordered stack over both full-document navigations and same-document (pushState) entries. Each entry remembers its window and that window's own history index, so back/forward can choose between a same-document popstate traversal (the entry's window is still live) and a full re-fetch (document boundary). Mirrors dommy-rack's Rack::History; kept in core so the embeddable Browser has a tab history without depending on Rack.

Defined Under Namespace

Classes: Entry

Instance Method Summary collapse

Constructor Details

#initializeJointHistory

Returns a new instance of JointHistory.



211
212
213
214
# File 'lib/dommy/navigation.rb', line 211

def initialize
  @stack = []
  @index = -1
end

Instance Method Details

#backObject



223
224
225
226
227
228
# File 'lib/dommy/navigation.rb', line 223

def back
  return nil if @index <= 0

  @index -= 1
  current_entry
end

#currentObject



258
# File 'lib/dommy/navigation.rb', line 258

def current = current_entry&.url

#current_entryObject



257
# File 'lib/dommy/navigation.rb', line 257

def current_entry = (@stack[@index] if @index >= 0)

#entriesObject



260
# File 'lib/dommy/navigation.rb', line 260

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

#forwardObject



230
231
232
233
234
235
# File 'lib/dommy/navigation.rb', line 230

def forward
  return nil if @index >= @stack.size - 1

  @index += 1
  current_entry
end

#lengthObject



259
# File 'lib/dommy/navigation.rb', line 259

def length = @stack.size

#push(url, window: nil, windex: nil) ⇒ Object



216
217
218
219
220
221
# File 'lib/dommy/navigation.rb', line 216

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(url: nil, window:, windex:) ⇒ Object

Re-bind the current entry to a fresh window + url (a reload / revisit re-loaded into a new document), so later same-document sync matches it.



239
240
241
242
243
244
245
246
# File 'lib/dommy/navigation.rb', line 239

def rebind_current(url: nil, window:, windex:)
  entry = current_entry
  return unless entry

  entry.url = url if url
  entry.window = window
  entry.windex = windex
end

#sync_to(window, windex) ⇒ Object

Mirror a same-document traversal the page performed itself (JS history.back()): move the cursor to the entry recorded for (window, windex). No-op when unknown.



251
252
253
254
255
# File 'lib/dommy/navigation.rb', line 251

def sync_to(window, windex)
  i = @stack.index { |e| e.window.equal?(window) && e.windex == windex }
  @index = i if i
  current_entry
end