Class: Porous::Server::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/porous/server/builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(environment = :development) ⇒ Builder

Returns a new instance of Builder.



8
9
10
11
12
13
# File 'lib/porous/server/builder.rb', line 8

def initialize(environment = :development)
  @environment = environment
  @build_queue = Queue.new
  @last_build = nil
  @latest_change = Dir.glob(File.join('**', '*.rb')).map { |f| File.mtime f }.max
end

Instance Method Details

#buildObject

rubocop:todo Metrics/AbcSize



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/porous/server/builder.rb', line 15

def build # rubocop:todo Metrics/AbcSize
  components = Dir.glob(File.join('**', '*.rb')).map do |relative_path|
    modified = File.mtime relative_path
    @latest_change = modified if modified > @latest_change
    "require '#{relative_path}'"
  end
  # Porous
  builder = Opal::Builder.new scheduler: Opal::BuilderScheduler::Sequential, cache: false
  builder.build_str "require 'porous'", '(inline)'
  File.binwrite "#{Dir.pwd}/static/porous.js", builder.to_s
  # App
  build_string = "#{components.join ";"}; ".gsub '.rb', ''
  build_string << inject_socket_connection
  builder = Opal::Builder.new scheduler: Opal::BuilderScheduler::Sequential, cache: false
  builder.build_str build_string, '(inline)'
  File.binwrite "#{Dir.pwd}/static/app.js", builder.to_s
  @last_build = Time.now
  self
end

#inject_socket_connectionObject

rubocop:enable Metrics/AbcSize



58
59
60
61
# File 'lib/porous/server/builder.rb', line 58

def inject_socket_connection
  uri = @environment == :production ? 'wss://localhost/connect' : 'ws://localhost:9292/connect'
  "$connection = '#{uri}'; "
end

#startObject

rubocop:disable Metrics/AbcSize



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/porous/server/builder.rb', line 36

def start
  loop do
    sleep 0.25
    next unless @build_queue.empty?

    modified = Dir.glob(File.join('**', '*.rb')).map { |f| File.mtime f }.max
    next unless modified > @last_build

    @build_queue.push modified
    # Load for server
    Dir.glob(File.join('**', '*.rb')).map { |f| load File.expand_path("#{Dir.pwd}/#{f}") }

    # Rebuild for browser
    Thread.new { build }.join

    # Notify clients
    $socket&.public 'build', @last_build.inspect
    @build_queue.clear
  end
end