From 75f6ebdcf58bff33b8e4bcdea31425aa29294762 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Mon, 26 Mar 2012 10:51:26 -0400 Subject: [PATCH] have node retrieve js files via ws instead of http --- lib/flowerbox/reporter/console_base.rb | 2 ++ lib/flowerbox/reporter/verbose.rb | 2 ++ lib/flowerbox/runner/base.rb | 4 +++ lib/flowerbox/runner/node.rb | 48 ++++++++++---------------- lib/flowerbox/server.rb | 4 ++- 5 files changed, 30 insertions(+), 30 deletions(-) diff --git a/lib/flowerbox/reporter/console_base.rb b/lib/flowerbox/reporter/console_base.rb index cfaaf96..36289d2 100644 --- a/lib/flowerbox/reporter/console_base.rb +++ b/lib/flowerbox/reporter/console_base.rb @@ -1,3 +1,5 @@ +require 'flowerbox/reporter/base' + module Flowerbox::Reporter class ConsoleBase < Base include FileDisplay diff --git a/lib/flowerbox/reporter/verbose.rb b/lib/flowerbox/reporter/verbose.rb index 02f8e71..f26d245 100644 --- a/lib/flowerbox/reporter/verbose.rb +++ b/lib/flowerbox/reporter/verbose.rb @@ -1,3 +1,5 @@ +require 'flowerbox/reporter/console_base' + module Flowerbox::Reporter class Verbose < ConsoleBase def initialize diff --git a/lib/flowerbox/runner/base.rb b/lib/flowerbox/runner/base.rb index 9f472ee..37809de 100644 --- a/lib/flowerbox/runner/base.rb +++ b/lib/flowerbox/runner/base.rb @@ -32,6 +32,10 @@ module Flowerbox Flowerbox.reporters end + def load(file) + sprockets.find_asset(file.first, :bundle => false).body + end + def ensure_alive while @count < MAX_COUNT && !finished? @count += 1 if @timer_running diff --git a/lib/flowerbox/runner/node.rb b/lib/flowerbox/runner/node.rb index ee9c34f..b6194aa 100644 --- a/lib/flowerbox/runner/node.rb +++ b/lib/flowerbox/runner/node.rb @@ -66,27 +66,16 @@ jsdom.env( var gotFlowerbox = false; - var files = #{sprockets.files.to_json}; - var fileRunner = function() { - if (files.length > 0) { - var file = files.shift(); + var socket = new ws('ws://localhost:#{server.port + 1}/'); + socket.onopen = function() { + var files = #{sprockets.files.to_json}; - var options = { - host: "localhost", - port: #{server.port}, - path: "/__F__/" + file, - method: "GET" - }; + var fileLoader = function() { + if (files.length > 0) { + var file = files.shift(); - var request = http.request(options, function(response) { - var data = ''; - - response.on('data', function(chunk) { - data += chunk; - }); - - response.on('end', function() { - vm.runInNewContext(data, context, file); + socket.onmessage = function(data) { + vm.runInNewContext(data.data, context, file); for (thing in window) { if (!context[thing]) { context[thing] = window[thing] } @@ -95,18 +84,18 @@ jsdom.env( if (!gotFlowerbox && context.Flowerbox) { context.Flowerbox.environment = 'node'; context.Flowerbox.UNKNOWN = '#{Flowerbox::Result::FileInfo::UNKNOWN}'; + context.Flowerbox.socket = socket; gotFlowerbox = true; } - fileRunner(); - }); - }); + fileLoader(); + }; + + socket.send(JSON.stringify(['load', file])); + } else { + socket.onmessage = null; - request.end(); - } else { - context.Flowerbox.socket = new ws('ws://localhost:#{server.port + 1}/'); - context.Flowerbox.socket.onopen = function() { #{env} var waitForFinish; @@ -118,10 +107,11 @@ jsdom.env( } }; waitForFinish(); - }; - } + } + }; + + fileLoader(); }; - fileRunner(); }); JS end diff --git a/lib/flowerbox/server.rb b/lib/flowerbox/server.rb index 833c469..3bf7475 100644 --- a/lib/flowerbox/server.rb +++ b/lib/flowerbox/server.rb @@ -43,7 +43,9 @@ module Flowerbox ws.onmessage { |message| command, data = JSON.parse(message) - runner.send(command, data.flatten) + output = runner.send(command, [ data ].flatten) + + ws.send(output) if command == 'load' } end