antlers 1.6.0 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 106cf5f2c5134ed5b4a852ca6eca4679578d5766d6202324f319bec6b39a957d
4
- data.tar.gz: d23315642c39cfeefc0580b44bce3b24e03559a7e37c37848550a1bdbba80e0e
3
+ metadata.gz: ba6f3d5d712defbfb1f3a86de94c5270a469bdd9b54f64aa588c213836e65387
4
+ data.tar.gz: 64f9d2e971e5bff2cec674d8918c2c2c49a2d77ade9d63f17c8b07810abe6389
5
5
  SHA512:
6
- metadata.gz: 71f494a2316f1f4825e9716208ef29c374b7426cd95c42993eef030bc66d458d02330bfc3a4988ce539f85b8eff2cd9ba0ac131866937682e4bedd407d576549
7
- data.tar.gz: 273f0a4992a51eb3731cd9dbeb14d349823c8facf263eae08aa3bcd1202058912193d3abf1512829df3cad3dd8bd152f7d93696e4f10c94d651a46991da9e445
6
+ metadata.gz: c9f55cab92eb709fd8d711b893068d32cde5a3e38a7c3987dfdb096db05aff94d201c4e75ea787d8b5cd7f00b37efd06da98cd9ec6a28694db7bfbb96331d654
7
+ data.tar.gz: 59c94fa478ee7ad43932de8f41aadb37ff11dab8de007df1a0ebebfe155b7530d06d383a2e1c4d622064f33e9038e24748e6ba525ebed4bcab9a8b46fd9d1705
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'translations'
4
+
5
+ module Antlers
6
+ # Keeps what a component rendered for a while, so <{ HomeGallery }> renders once and the next renders reuse its HTML
7
+ # without making it again. A component class opts in with cache_seconds, like LowNode's "cache 300". Its HTML is kept
8
+ # by its class and props, the locale and the context an app sets for each request, like the site it came to.
9
+ #
10
+ # A render that reads something only one visitor should see calls ComponentCache.skip!, like reading the logged in
11
+ # user, so it isn't kept. That skips every component around it too, as their HTML holds it.
12
+ module ComponentCache
13
+ MAX_ENTRIES = 1_000
14
+
15
+ Entry = Data.define(:html, :expires_at)
16
+
17
+ # One component rendering to be kept. It's skipped when something in it calls skip!.
18
+ class Frame
19
+ attr_accessor :skipped
20
+ end
21
+
22
+ @entries = {}
23
+ @mutex = Mutex.new
24
+
25
+ module_function
26
+
27
+ # The component's HTML from the cache or else from the block, kept when its class asks to be.
28
+ def fetch(klass, props, &)
29
+ seconds = klass.cache_seconds if klass.respond_to?(:cache_seconds)
30
+ return yield unless seconds
31
+
32
+ key = [klass, props.except(:event), Translations.locale, context]
33
+ cached = entry(key)
34
+ return cached.html if cached
35
+
36
+ html, skipped = rendered(&)
37
+ store(key, Entry.new(html:, expires_at: now + seconds)) unless skipped
38
+ html
39
+ end
40
+
41
+ # Stops every component rendering now from being kept, as what they render is for one visitor only.
42
+ def skip!
43
+ frames.each { it.skipped = true }
44
+ end
45
+
46
+ # Renders the block with what an app keeps components apart by, like the site's host, for this fiber's renders.
47
+ def with_context(*parts)
48
+ previous = context
49
+ Fiber[:antlers_cache_context] = parts
50
+ yield
51
+ ensure
52
+ Fiber[:antlers_cache_context] = previous
53
+ end
54
+
55
+ def context
56
+ Fiber[:antlers_cache_context]
57
+ end
58
+
59
+ def clear
60
+ @mutex.synchronize { @entries.clear }
61
+ end
62
+
63
+ def size
64
+ @mutex.synchronize { @entries.size }
65
+ end
66
+
67
+ # The HTML the block renders and whether something in it called skip!.
68
+ def rendered
69
+ frame = Frame.new
70
+ frames.push(frame)
71
+ html = yield
72
+ [html, frame.skipped]
73
+ ensure
74
+ frames.pop
75
+ end
76
+
77
+ # This fiber's components rendering now, outermost first. Each fiber has its own, as it renders its own request.
78
+ def frames
79
+ Fiber[:antlers_cache_frames] ||= []
80
+ end
81
+
82
+ def entry(key)
83
+ cached = @mutex.synchronize { @entries[key] }
84
+ cached if cached && cached.expires_at > now
85
+ end
86
+
87
+ # Keeps an entry, dropping the oldest when full, as entries are kept in the order they were added.
88
+ def store(key, entry)
89
+ @mutex.synchronize do
90
+ @entries.delete(key)
91
+ @entries.shift while @entries.size >= MAX_ENTRIES
92
+ @entries[key] = entry
93
+ end
94
+ end
95
+
96
+ def now
97
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
98
+ end
99
+ end
100
+ end
@@ -17,6 +17,8 @@ module Antlers
17
17
  @translations = (translations || {}).to_h do |locale, mappings|
18
18
  [locale.to_s, (mappings || {}).to_h { |source, translation| [source.to_s, translation.to_s] }.freeze]
19
19
  end.freeze
20
+ # Components kept with the old text render again.
21
+ ComponentCache.clear if defined?(ComponentCache)
20
22
  end
21
23
 
22
24
  def translations
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Antlers
4
- VERSION = '1.6.0'
4
+ VERSION = '1.7.0'
5
5
  end
data/lib/antlers.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'antlers/component_cache'
3
4
  require_relative 'antlers/elements'
4
5
  require_relative 'antlers/keywords'
5
6
  require_relative 'antlers/lexer'
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative '../antlers/component_cache'
3
4
  require_relative '../antlers/keywords'
4
5
  require_relative '../interfaces/leaf_node'
5
6
  require_relative '../modules/namespace'
@@ -30,13 +31,9 @@ module Antlers
30
31
  klass = node_class || raise(NameError, "uninitialized constant #{@name}")
31
32
  event = possible_render_event(props:, current_binding:)
32
33
 
33
- # Initialize and render each get the props they take, and the event if they take it.
34
+ # Initialize and render each get the props they take and the event if they take it. A cached component skips both.
34
35
  record_render(klass) do
35
- keywords = props.merge(event:)
36
- instance = klass.new(**Keywords.accepted(klass.instance_method(:initialize), keywords))
37
- next instance.render_template(event:, parent_binding:, props:, context:) if klass.respond_to?(:template) && klass.template
38
-
39
- instance.render(**Keywords.accepted(instance.method(:render), keywords))
36
+ ComponentCache.fetch(klass, props) { render_component(klass:, props:, event:, parent_binding:, context:) }
40
37
  end
41
38
  end
42
39
 
@@ -63,6 +60,14 @@ module Antlers
63
60
 
64
61
  private
65
62
 
63
+ def render_component(klass:, props:, event:, parent_binding:, context:)
64
+ keywords = props.merge(event:)
65
+ instance = klass.new(**Keywords.accepted(klass.instance_method(:initialize), keywords))
66
+ return instance.render_template(event:, parent_binding:, props:, context:) if klass.respond_to?(:template) && klass.template
67
+
68
+ instance.render(**Keywords.accepted(instance.method(:render), keywords))
69
+ end
70
+
66
71
  def node_class
67
72
  class_constant(namespace: @namespace&.split('::') || [], name: @name)
68
73
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: antlers
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.0
4
+ version: 1.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - maedi
@@ -48,6 +48,7 @@ files:
48
48
  - lib/antlers.rb
49
49
  - lib/antlers/api.rb
50
50
  - lib/antlers/compiler.rb
51
+ - lib/antlers/component_cache.rb
51
52
  - lib/antlers/elements.rb
52
53
  - lib/antlers/keywords.rb
53
54
  - lib/antlers/lexer.rb