|
| 1 | +require 'hyperstack/hot_loader/add_error_boundry' |
| 2 | +require 'hyperstack/hot_loader/stack-trace.js' |
| 3 | +require 'hyperstack/hot_loader/css_reloader' |
| 4 | +require 'opal-parser' # gives me 'eval', for hot-loading code |
| 5 | + |
| 6 | +require 'json' |
| 7 | +require 'hyperstack/hot_loader/short_cut.js' |
| 8 | + |
| 9 | +# Opal client to support hot reloading |
| 10 | +$eval_proc = proc do |file_name, s| |
| 11 | + $_hyperstack_reloader_file_name = file_name |
| 12 | + eval s |
| 13 | +end |
| 14 | + |
| 15 | +module Hyperstack |
| 16 | + |
| 17 | + class HotLoader |
| 18 | + def self.callbackmaps |
| 19 | + @@callbackmaps ||= Hash.new { |h, k| h[k] = Hash.new { |h1, k1| h1[k1] = Hash.new { |h2, k2| h2[k2] = Array.new }}} |
| 20 | + end |
| 21 | + |
| 22 | + def self.record(klass, instance_var, depth, *items) |
| 23 | + if $_hyperstack_reloader_file_name |
| 24 | + callbackmaps[$_hyperstack_reloader_file_name][klass][instance_var].concat items |
| 25 | + else |
| 26 | + callback = lambda do |stack_frames| |
| 27 | + file_name = `#{stack_frames[depth]}.fileName` |
| 28 | + match = /^(.+\/assets\/)(.+\/)\2/.match(file_name) |
| 29 | + if match |
| 30 | + file_name = file_name.gsub(match[1]+match[2], '') |
| 31 | + callbackmaps[file_name][klass][instance_var].concat items |
| 32 | + end |
| 33 | + end |
| 34 | + error = lambda do |err| |
| 35 | + `console.error(#{"hyperstack hot loader could not find source file for callback: #{err}"})` |
| 36 | + end |
| 37 | + `StackTrace.get().then(#{callback}).catch(#{error})` |
| 38 | + end |
| 39 | + end |
| 40 | + |
| 41 | + def self.remove(file_name) |
| 42 | + callbackmaps[file_name].each do |klass, instance_vars| |
| 43 | + instance_vars.each do |instance_var, items| |
| 44 | + klass.instance_variable_get(instance_var).reject! { |item| items.include? item } |
| 45 | + end |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + def connect_to_websocket(port) |
| 50 | + host = `window.location.host`.sub(/:\d+/, '') |
| 51 | + host = '127.0.0.1' if host == '' |
| 52 | + protocol = `window.location.protocol` == 'https:' ? 'wss:' : 'ws:' |
| 53 | + ws_url = "#{host}:#{port}" |
| 54 | + puts "Hot-Reloader connecting to #{ws_url}" |
| 55 | + ws = `new WebSocket(#{protocol} + '//' + #{ws_url})` |
| 56 | + `#{ws}.onmessage = #{lambda { |e| reload(e) }}` |
| 57 | + `setInterval(function() { #{ws}.send('') }, #{@ping * 1000})` if @ping |
| 58 | + end |
| 59 | + |
| 60 | + def notify_error(reload_request) |
| 61 | + msg = "HotLoader #{reload_request[:filename]} RELOAD ERROR:\n\n#{$!}" |
| 62 | + puts msg |
| 63 | + alert msg if use_alert? |
| 64 | + end |
| 65 | + |
| 66 | + @@USE_ALERT = true |
| 67 | + def self.alerts_on! |
| 68 | + @@USE_ALERT = true |
| 69 | + end |
| 70 | + |
| 71 | + def self.alerts_off! |
| 72 | + @@USE_ALERT = false |
| 73 | + end |
| 74 | + |
| 75 | + def use_alert? |
| 76 | + @@USE_ALERT |
| 77 | + end |
| 78 | + |
| 79 | + def reload(e) |
| 80 | + reload_request = JSON.parse(`e.data`) |
| 81 | + if reload_request[:type] == "ruby" |
| 82 | + puts "Reloading #{reload_request[:filename]} (asset_path: #{reload_request[:asset_path]})" |
| 83 | + begin |
| 84 | + #Hyperstack::Context.reset! false |
| 85 | + file_name = reload_request[:asset_path] #.gsub(/.+hyperstack\//, '') |
| 86 | + HotLoader.remove(file_name) |
| 87 | + $eval_proc.call file_name, reload_request[:source_code] |
| 88 | + rescue |
| 89 | + notify_error(reload_request) |
| 90 | + end |
| 91 | + if @reload_post_callback |
| 92 | + @reload_post_callback.call |
| 93 | + else |
| 94 | + puts "no reloading callback to call" |
| 95 | + end |
| 96 | + end |
| 97 | + if reload_request[:type] == "css" |
| 98 | + @css_reloader.reload(reload_request, `document`) |
| 99 | + end |
| 100 | + end |
| 101 | + |
| 102 | + # @param port [Integer] opal hot reloader port to connect to |
| 103 | + # @param reload_post_callback [Proc] optional callback to be called after re evaluating a file for example in react.rb files we want to do a React::Component.force_update! |
| 104 | + def initialize(port=25222, ping=nil, &reload_post_callback) |
| 105 | + @port = port |
| 106 | + @reload_post_callback = reload_post_callback |
| 107 | + @css_reloader = CssReloader.new |
| 108 | + @ping = ping |
| 109 | + end |
| 110 | + # Opens a websocket connection that evaluates new files and runs the optional @reload_post_callback |
| 111 | + def listen |
| 112 | + connect_to_websocket(@port) |
| 113 | + end |
| 114 | + |
| 115 | + def self.listen(port=25222, ping=nil) |
| 116 | + ::Hyperstack::Internal::Component::TopLevelRailsComponent.include AddErrorBoundry |
| 117 | + @server = HotLoader.new(port, ping) do |
| 118 | + # TODO: check this out when Operations are integrated |
| 119 | + # if defined?(Hyperloop::Internal::Operation::ClientDrivers) && |
| 120 | + # Hyperloop::ClientDrivers.respond_to?(:initialize_client_drivers_on_boot) |
| 121 | + # Hyperloop::ClientDrivers.initialize_client_drivers_on_boot |
| 122 | + # end |
| 123 | + Hyperstack::Component.force_update! |
| 124 | + end |
| 125 | + @server.listen |
| 126 | + end |
| 127 | + |
| 128 | + end |
| 129 | +end |
0 commit comments