Class: Esbuild::BuildState

Inherits:
Object
  • Object
show all
Defined in:
lib/esbuild/build_state.rb

Instance Method Summary collapse

Constructor Details

#initialize(service, on_rebuild) ⇒ BuildState

Returns a new instance of BuildState.



3
4
5
6
7
8
# File 'lib/esbuild/build_state.rb', line 3

def initialize(service, on_rebuild)
  @service = service
  @rebuild_id = nil
  @watch_id = nil
  @on_rebuild = on_rebuild
end

Instance Method Details

#disposeObject



16
17
18
19
20
21
# File 'lib/esbuild/build_state.rb', line 16

def dispose
  return unless @rebuild_id
  res = @service.send_request("command" => "rebuild-dispose", "rebuildID" => @rebuild_id)
  @rebuild_id = nil
  res
end

#handle_watch(error, response) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/esbuild/build_state.rb', line 29

def handle_watch(error, response)
  return @on_rebuild.call(error, nil) if error
  unless response["errors"].empty?
    error = BuildFailureError.new(response["errors"], response["warnings"])
    @on_rebuild.call(error, nil)
    return
  end

  result = BuildResult.new(response, self)
  @on_rebuild.call(nil, result)
end

#rebuildObject



23
24
25
26
27
# File 'lib/esbuild/build_state.rb', line 23

def rebuild
  raise "Cannot rebuild" if @rebuild_id.nil?
  rebuild_response = @service.send_request("command" => "rebuild", "rebuildID" => @rebuild_id)
  response_to_result(rebuild_response)
end

#response_to_result(res) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/esbuild/build_state.rb', line 41

def response_to_result(res)
  unless res["errors"].empty?
    raise BuildFailureError.new(res["errors"], res["warnings"])
  end
  if res["writeToStdout"]
    $stdout.puts res["writeToStdout"].rstrip
  end

  result = BuildResult.new(res, self)

  # Handle incremental rebuilds
  if res["rebuildID"] && !@rebuild_id
    @rebuild_id = res["rebuildID"]
  end

  # Handle watch mode
  if res["watchID"] && !@watch_id
    @watch_id = res["watchID"]
    if @on_rebuild
      @service.start_watch(@watch_id, ->(error, watch_response) { handle_watch(error, watch_response) })
    end
  end
  result
end

#stopObject



10
11
12
13
14
# File 'lib/esbuild/build_state.rb', line 10

def stop
  return unless @watch_id
  @service.stop_watch(@watch_id)
  @watch_id = nil
end