Class: Dommy::LockManager

Inherits:
Object
  • Object
show all
Includes:
Bridge::Methods
Defined in:
lib/dommy/navigator.rb

Overview

navigator.locks — Web Locks API. Locks are scoped to the Navigator instance; serial execution per name. Real browsers coordinate across tabs; dommy is single-process so it just serializes calls within the same Window.

Spec: https://w3c.github.io/web-locks/

Instance Method Summary collapse

Methods included from Bridge::Methods

included

Constructor Details

#initialize(window) ⇒ LockManager

Returns a new instance of LockManager.



573
574
575
576
# File 'lib/dommy/navigator.rb', line 573

def initialize(window)
  @window = window
  @held = {}
end

Instance Method Details

#__js_call__(method, args) ⇒ Object



606
607
608
609
610
611
612
613
# File 'lib/dommy/navigator.rb', line 606

def __js_call__(method, args)
  case method
  when "request"
    request(args[0], args[1], args[2])
  when "query"
    query
  end
end

#queryObject



599
600
601
602
# File 'lib/dommy/navigator.rb', line 599

def query
  held = @held.map { |name, lock| {"name" => name, "mode" => lock.mode, "clientId" => "dommy"} }
  PromiseValue.resolve(@window, {"held" => held, "pending" => []})
end

#request(name, options_or_callback, callback = nil) ⇒ Object



578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
# File 'lib/dommy/navigator.rb', line 578

def request(name, options_or_callback, callback = nil)
  if options_or_callback.is_a?(Hash) || options_or_callback.nil?
    options = options_or_callback || {}
    cb = callback
  else
    options = {}
    cb = options_or_callback
  end

  key = name.to_s
  if @held[key] && options["ifAvailable"]
    return invoke_with_lock(cb, nil)
  end

  lock = Lock.new(key, options["mode"] || "exclusive")
  @held[key] = lock
  result = invoke_with_lock(cb, lock)
  @held.delete(key)
  result
end