Class: Dato::Local::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/dato/local/loader.rb

Constant Summary collapse

PUSHER_API_KEY =
"75e6ef0fe5d39f481626"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, preview_mode = false) ⇒ Loader

rubocop:disable Style/OptionalBooleanParameter



16
17
18
19
20
21
# File 'lib/dato/local/loader.rb', line 16

def initialize(client, preview_mode = false)
  @client = client
  @preview_mode = preview_mode
  @entities_repo = EntitiesRepo.new
  @items_repo = ItemsRepo.new(@entities_repo)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



11
12
13
# File 'lib/dato/local/loader.rb', line 11

def client
  @client
end

#entities_repoObject (readonly)

Returns the value of attribute entities_repo.



11
12
13
# File 'lib/dato/local/loader.rb', line 11

def entities_repo
  @entities_repo
end

#items_repoObject (readonly)

Returns the value of attribute items_repo.



11
12
13
# File 'lib/dato/local/loader.rb', line 11

def items_repo
  @items_repo
end

#preview_modeObject (readonly)

Returns the value of attribute preview_mode.



11
12
13
# File 'lib/dato/local/loader.rb', line 11

def preview_mode
  @preview_mode
end

Instance Method Details

#loadObject

rubocop:enable Style/OptionalBooleanParameter



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dato/local/loader.rb', line 24

def load
  threads = [
    Thread.new { Thread.current[:output] = site },
    Thread.new { Thread.current[:output] = all_items },
    Thread.new { Thread.current[:output] = all_uploads },
  ]

  results = threads.map do |t|
    t.join
    t[:output]
  end

  @entities_repo = EntitiesRepo.new(*results)
  @items_repo = ItemsRepo.new(@entities_repo)
end

#stop_watchObject



64
65
66
# File 'lib/dato/local/loader.rb', line 64

def stop_watch
  pusher.disconnect if pusher && pusher.connected
end

#watch(&block) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/dato/local/loader.rb', line 40

def watch(&block)
  site_id = client.get("/site")["data"]["id"]

  return if pusher && pusher.connected

  channel_name = if client.environment
                   "private-site-#{site_id}-environment-#{client.environment}"
                 else
                   "private-site-#{site_id}"
                 end

  pusher.subscribe(channel_name)

  bind_on_site_upsert(&block)
  bind_on_item_destroy(&block)
  bind_on_item_upsert(&block)
  bind_on_item_type_upsert(&block)
  bind_on_item_type_destroy(&block)
  bind_on_upload_upsert(&block)
  bind_on_upload_destroy(&block)

  pusher.connect(true)
end