Class: Dommy::LockManager

Inherits:
Object
  • Object
show all
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: w3c.github.io/web-locks/

Instance Method Summary collapse

Constructor Details

#initialize(window) ⇒ LockManager

Returns a new instance of LockManager.



533
534
535
536
# File 'lib/dommy/navigator.rb', line 533

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

Instance Method Details

#__js_call__(method, args) ⇒ Object



564
565
566
567
568
569
570
571
# File 'lib/dommy/navigator.rb', line 564

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

#queryObject



559
560
561
562
# File 'lib/dommy/navigator.rb', line 559

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



538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
# File 'lib/dommy/navigator.rb', line 538

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