Class: InstantRecord::Client::Notifier::JsClients

Inherits:
Object
  • Object
show all
Defined in:
lib/instant_record/client/notifier.rb

Overview

Tells every open tab that local data changed so pages re-render. Runs in the service worker context; indexes the client list rather than passing Ruby procs to JS (narrower interop surface).

Instance Method Summary collapse

Constructor Details

#initializeJsClients

Returns a new instance of JsClients.



8
9
10
# File 'lib/instant_record/client/notifier.rb', line 8

def initialize
  require "js"
end

Instance Method Details

#records_changedObject



12
13
14
15
16
17
18
19
# File 'lib/instant_record/client/notifier.rb', line 12

def records_changed
  clients = JS.global[:clients].matchAll.await
  message = JS.eval("return {type: 'records_changed'}")

  clients[:length].to_i.times do |i|
    clients[i].postMessage(message)
  end
end