-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce
use_context
to inject mini_racer
This allow devs to customize the mini_racer context, e.g, make use of snapshots, additional options, `attach`es, etc.
- Loading branch information
Showing
9 changed files
with
145 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Changelog | ||
|
||
All notable changes to this project will be documented in this file. | ||
|
||
## Upcoming | ||
|
||
Prefer injection of the custom mini_racer context |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module Humid | ||
class Humid | ||
module ControllerRuntime | ||
extend ActiveSupport::Concern | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
class Humid | ||
class Renderer | ||
cattr_accessor :context, instance_writer: false, instance_reader: false | ||
|
||
class << self | ||
def use_context(ctx) | ||
self.context = ctx | ||
|
||
ctx.attach("console.log", proc { |err| logger.debug(err.to_s) }) | ||
ctx.attach("console.info", proc { |err| logger.info(err.to_s) }) | ||
ctx.attach("console.error", proc { |err| logger.error(err.to_s) }) | ||
ctx.attach("console.warn", proc { |err| logger.warn(err.to_s) }) | ||
|
||
js = "" | ||
js << remove_functions | ||
js << renderer | ||
ctx.eval(js) | ||
|
||
source_path = config.application_path | ||
map_path = config.source_map_path | ||
|
||
if map_path | ||
ctx.attach("readSourceMap", proc { File.read(map_path) }) | ||
end | ||
|
||
filename = File.basename(source_path.to_s) | ||
ctx.eval(File.read(source_path), filename: filename) | ||
end | ||
|
||
def dispose | ||
if context | ||
context.dispose | ||
self.context = nil | ||
end | ||
end | ||
|
||
def render(*args) | ||
ActiveSupport::Notifications.instrument("render.humid") do | ||
context.call("__renderer", *args) | ||
rescue MiniRacer::RuntimeError => e | ||
message = ([e.message] + e.backtrace.filter { |x| x.starts_with? "JavaScript" }).join("\n") | ||
render_error = Humid::RenderError.new(message) | ||
|
||
if config.raise_render_errors | ||
raise render_error | ||
else | ||
logger.error(render_error.inspect) | ||
"" | ||
end | ||
end | ||
end | ||
|
||
def logger | ||
config.logger | ||
end | ||
|
||
private | ||
|
||
def remove_functions | ||
<<~JS | ||
delete this.setTimeout; | ||
delete this.setInterval; | ||
delete this.clearTimeout; | ||
delete this.clearInterval; | ||
delete this.setImmediate; | ||
delete this.clearImmediate; | ||
JS | ||
end | ||
|
||
def config | ||
Humid.config | ||
end | ||
|
||
def renderer | ||
<<~JS | ||
var __renderer; | ||
function setHumidRenderer(fn) { | ||
__renderer = fn; | ||
} | ||
JS | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module Humid | ||
class Humid | ||
VERSION = "0.0.6".freeze | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.