js_render 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3efa8f3b5071ab1391bc0f0884c7125ddfc875bc
4
- data.tar.gz: eddc754107e5f99f70b9cb48855781023379735b
3
+ metadata.gz: 44e4ca7607fdaa47edeb79eaa87ab02f4830b5c2
4
+ data.tar.gz: a8579d5be73c8c6935403a8ea28c6f4411269271
5
5
  SHA512:
6
- metadata.gz: f1a34fc4f3b80733f4b3b5335902a1ed43b40db6442cbc73e32f78b847bb263fdfaa9928dfcd5fa671910c95fb3340b6145b08039a0758170c8b969dd8426f84
7
- data.tar.gz: 5b4011ffccaae946dfac1903abee032074e0cda6392b27e6fcd5af4c73675d55c88de7ebac4a96718e9ef102fbdb1b692972a55b58adb0727e041324f4ec3c5f
6
+ metadata.gz: 6fdcd63206923591551212e3d0207f25d5d7cad3f8e24e14ac4844bf0271130e2aed72e819af42af0649568eb7b85f4b04ad9378744a6849b02692a7d60a0fc8
7
+ data.tar.gz: 6e03a1600adcf4669b3697449feed57f5849c1849d37a879d93cda900b2ca615aa49a5bf5768fc7aeab4ae3f20109cfda96a9c50d3016b397e1e7a13543c4d03
data/README.md CHANGED
@@ -101,8 +101,7 @@ This is the base path where your components live.
101
101
 
102
102
  **component_paths**
103
103
 
104
- These are the paths off of your base path that are searched to find your component (or more accurately your components' server render functions). Wildcards are supported.
105
-
104
+ These are the paths off of your base path that are searched to find your component (or more accurately your components' server render functions). Wildcards are supported.
106
105
  If you are using Rails AND the asset pipeline, the lookup path can point to your pre-built file and the asset pipeline will give JsRender the built file. If you are using another build tool, make sure you are pointing to the built assets. JsRender will NOT take care of any compile step for you, it expects these files to already be compiled to ES5 compatible with [ExecJS](https://github.com/rails/execjs).
107
106
 
108
107
  > Defaults to `['/**/*']`
@@ -178,6 +177,18 @@ This config option is a boolean that specifies if the server render function and
178
177
 
179
178
  > Defaults to `true`
180
179
 
180
+ **cache_size**
181
+
182
+ Since ExecJS is expensive, components are cached. This controls the number of items to keep in the LRU (least recently used) cache, where the least recently used is evicted.
183
+
184
+ > Defaults to 100
185
+
186
+ **cache_ttl**
187
+
188
+ Since ExecJS is expensive, components are cached. This controls the TTL (in seconds) of items in the LRU (least recently used) cache, where items are evicted once the TTL is reached.
189
+
190
+ > Defaults to 600 seconds (10 minutes)
191
+
181
192
  ## Development
182
193
 
183
194
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/js_render.gemspec CHANGED
@@ -23,4 +23,5 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency "rake", "~> 10.0"
24
24
  spec.add_development_dependency "rspec", "~> 3.0"
25
25
  spec.add_dependency "execjs"
26
+ spec.add_dependency "lru_redux"
26
27
  end
@@ -24,7 +24,9 @@ module JsRender
24
24
  :use_asset_pipeline,
25
25
  :asset_finder_class,
26
26
  :key_transforms,
27
- :should_server_render
27
+ :should_server_render,
28
+ :cache_size,
29
+ :cache_ttl
28
30
 
29
31
  def initialize
30
32
  @base_path = 'app/assets/javascripts'
@@ -36,6 +38,8 @@ module JsRender
36
38
  @asset_finder_class = nil
37
39
  @key_transforms = []
38
40
  @should_server_render = true
41
+ @cache_size = 100
42
+ @cache_ttl = 10 * 60
39
43
  end
40
44
  end
41
45
  end
@@ -1,10 +1,13 @@
1
1
  require 'execjs'
2
2
  require 'securerandom'
3
+ require 'lru_redux'
3
4
 
4
5
  module JsRender
5
6
  class Renderer
6
7
  attr_reader :component_name, :json_data, :uuid, :asset_finder
7
8
 
9
+ @@cache = LruRedux::TTL::ThreadSafeCache.new(JsRender.config.cache_size, JsRender.config.cache_ttl)
10
+
8
11
  GLOBAL_CONTEXT = <<-JS
9
12
  var global = global || this;
10
13
  var self = self || this;
@@ -38,8 +41,6 @@ module JsRender
38
41
  return '<span id="#{@uuid}">' + serverStr + '</span>';
39
42
  })()
40
43
  JS
41
- renderer_code = asset_finder.read_files(@component_name)
42
- context = ::ExecJS.compile(GLOBAL_CONTEXT + renderer_code)
43
44
  context.eval(server_code)
44
45
  rescue ExecJS::RuntimeError, ExecJS::ProgramError => error
45
46
  raise Errors::ServerRenderError::new(@component_name, @json_data, error)
@@ -57,6 +58,16 @@ module JsRender
57
58
 
58
59
  private
59
60
 
61
+ def context
62
+ base_path = JsRender.config.base_path
63
+ paths = JsRender.config.component_paths
64
+ suffix = JsRender.config.component_suffix
65
+ @@cache.getset(base_path + paths.join('') + suffix + @component_name) do
66
+ renderer_code = asset_finder.read_files(@component_name)
67
+ ExecJS.compile(GLOBAL_CONTEXT + renderer_code)
68
+ end
69
+ end
70
+
60
71
  def asset_finder
61
72
  @asset_finder ||=
62
73
  if JsRender.config.asset_finder_class
@@ -1,3 +1,3 @@
1
1
  module JsRender
2
- VERSION = '0.5.0'
2
+ VERSION = '0.6.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: js_render
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonathan Lehman
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-10-09 00:00:00.000000000 Z
11
+ date: 2018-04-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: lru_redux
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  description: Render JavaScript components on the server side with Ruby.
70
84
  email:
71
85
  - jonathan.lehman91@gmail.com
@@ -242,7 +256,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
242
256
  version: '0'
243
257
  requirements: []
244
258
  rubyforge_project:
245
- rubygems_version: 2.4.5.1
259
+ rubygems_version: 2.5.2
246
260
  signing_key:
247
261
  specification_version: 4
248
262
  summary: Render JavaScript components on the server side with Ruby.