raindeer 1.0.67 → 1.1.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: fe0d92621da81e79fa4a838e90d6534bcdbe0dca3f18f6bd84e118e6364ac8a7
4
- data.tar.gz: 795d5ef5d272f8964af7b4cadfda713858042211b1b8f6e40ec6699ddff3ea44
3
+ metadata.gz: 82fcde177d95de3f5ebd23739f9aa7a57e7577e791500294fb6299780f6417b1
4
+ data.tar.gz: 63531aa3bde4df4780e3c3f3a59d39a13998dba5fe9eb0f76b4da5f11869d2f4
5
5
  SHA512:
6
- metadata.gz: 8aed89b06baeb1f5034b2f8d3326aa35bf5c18552b0a0a644a967c21f2f4fee10b5b9c1bc75c54e88a925f11dc3fecf2e6288f388cccaab2f5db78bfa5efa2dd
7
- data.tar.gz: '06240344926087b3b200abe5973851f8f857d627d4b8eee451869c8ac3350fbfb6265c4473f634c3bdab7d530cc2e06e2ed8362f175257ce5cc34f0a4aa5f621'
6
+ metadata.gz: 8eb828025ace56b36618553a251fd8c8768821532580154c6cc7f3424cfa6fe04c77d036c378ba12167d061629b7fbf1db6fe9d80e969908f63dcef30d83d635
7
+ data.tar.gz: fc551499692cdaa0e325fcd7f3831c9d848c9d71ec53123bc6eea20ca209cad76eaa619806a83802075a800129204ae22fb7fc5ed0355d8284354867f41efbc0
data/lib/app/app.rb ADDED
@@ -0,0 +1,112 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rain
4
+ # The app folder. While debugging it's loaded again whenever one of its files is saved, added or deleted, and the
5
+ # framework and gems stay loaded. Only the app's classes go, with the observers, routes and cached components they added.
6
+ class App
7
+ MODULE_NAME = Module.instance_method(:name)
8
+
9
+ @lock = Thread::Mutex.new
10
+ @changed = Thread::ConditionVariable.new
11
+ @serving = 0
12
+ @reloading = false
13
+
14
+ class << self
15
+ attr_accessor :watching
16
+
17
+ # Runs a request or an interval's job once any reload under way is done, so neither sees the app half loaded.
18
+ def serving
19
+ return yield unless watching
20
+
21
+ @lock.synchronize do
22
+ @changed.wait(@lock) while @reloading
23
+ @serving += 1
24
+ end
25
+ begin
26
+ yield
27
+ ensure
28
+ @lock.synchronize do
29
+ @serving -= 1
30
+ @changed.broadcast
31
+ end
32
+ end
33
+ end
34
+
35
+ # Waits for the requests and jobs under way to finish then holds new ones back until the block's done.
36
+ def reloading
37
+ @lock.synchronize do
38
+ @changed.wait(@lock) while @reloading
39
+ @reloading = true
40
+ @changed.wait(@lock) while @serving.positive?
41
+ end
42
+ yield
43
+ ensure
44
+ @lock.synchronize do
45
+ @reloading = false
46
+ @changed.broadcast
47
+ end
48
+ end
49
+ end
50
+
51
+ # An interval's job runs on a thread of its own, so it waits for a reload as a request does.
52
+ module ServingTicks
53
+ def tick(...)
54
+ App.serving { super }
55
+ end
56
+ end
57
+
58
+ def initialize(path:)
59
+ @path = path
60
+ # The routes there are before the app loads, like the system pages', which stay when it loads again.
61
+ @framework_routes = Providers['rain.router'].routes.keys
62
+ end
63
+
64
+ def load
65
+ metadata = dirload(@path)
66
+ return unless Dir.exist?(File.expand_path('pages', @path))
67
+
68
+ Providers.define('rain.pages', eager: true) do
69
+ require_relative '../pages/pages'
70
+ Rain::Pages.new(metadata:)
71
+ end
72
+ end
73
+
74
+ def reload
75
+ App.reloading do
76
+ names = Dirload.unload(@path)
77
+ Observers.clear { |object| app_object?(object, names) }
78
+ Providers['rain.router'].keep_routes(@framework_routes)
79
+ Antlers::ComponentCache.clear
80
+ load
81
+ ensure
82
+ Lowkey.clear
83
+ end
84
+ end
85
+
86
+ # Reloads in the background until the process exits. An error is shown and the next save tries again.
87
+ def watch
88
+ require 'listen'
89
+
90
+ LowNode.singleton_class.prepend(ServingTicks)
91
+ App.watching = true
92
+ Listen.to(@path) { reload_and_report }.start
93
+ end
94
+
95
+ private
96
+
97
+ def reload_and_report
98
+ started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
99
+ reload
100
+ puts format('Reloaded the app in %.2fs', Process.clock_gettime(Process::CLOCK_MONOTONIC) - started)
101
+ rescue ScriptError, StandardError => e
102
+ puts "The app couldn't be reloaded: #{e.message}\n#{e.backtrace&.first}"
103
+ end
104
+
105
+ # Whether an observer or a key is one of the app's classes or modules or made from one.
106
+ def app_object?(object, names)
107
+ # Module#name itself, as an app class can define its own "name".
108
+ name = MODULE_NAME.bind_call(object.is_a?(Module) ? object : object.class).to_s
109
+ names.any? { |app_name| name == app_name || name.start_with?("#{app_name}::") }
110
+ end
111
+ end
112
+ end
data/lib/raindeer/boot.rb CHANGED
@@ -52,6 +52,7 @@ module Rain
52
52
  assets.build
53
53
  assets.watch if config.debug_mode
54
54
  end
55
+ Providers['rain.app'].watch if config.debug_mode && Providers.all.key?('rain.app')
55
56
 
56
57
  require 'lowloop'
57
58
  # The system pages' own requests go in a buffer of their own like interval ticks so they never push the app's out.
@@ -94,17 +95,13 @@ require 'antlers' # Adds antlers support to dirload.
94
95
  require 'dirload'
95
96
  dirload(File.expand_path('../system', __dir__))
96
97
 
97
- application_path = File.expand_path('app', Dir.pwd)
98
+ require_relative '../app/app'
99
+ app_path = File.expand_path('app', Dir.pwd)
98
100
 
99
- if Dir.exist?(application_path)
100
- metadata = dirload(application_path)
101
-
102
- if Dir.exist?(File.expand_path('pages', application_path))
103
- Providers.define('rain.pages', eager: true) do
104
- require_relative '../pages/pages'
105
- Rain::Pages.new(metadata:)
106
- end
107
- end
101
+ if Dir.exist?(app_path)
102
+ app = Rain::App.new(path: app_path)
103
+ app.load
104
+ Providers.define('rain.app') { app }
108
105
  end
109
106
 
110
107
  #################################################
data/lib/router/router.rb CHANGED
@@ -9,6 +9,7 @@ require_relative 'route'
9
9
  require_relative 'trie'
10
10
  require_relative '../translations/translations'
11
11
  require_relative 'events/sites'
12
+ require_relative '../app/app'
12
13
 
13
14
  module Rain
14
15
  # Defines routes and routes requests to a matching route event.
@@ -42,6 +43,13 @@ module Rain
42
43
  @current_level.pop
43
44
  end
44
45
 
46
+ # Forgets every route but these paths, like the app's when it's loaded again, and keeps the rest in their order.
47
+ def keep_routes(paths)
48
+ @routes = @routes.slice(*paths)
49
+ @trie = Trie.new(handled_only: true)
50
+ @routes.each_value { |route| @trie.merge(route:) }
51
+ end
52
+
45
53
  # A route for each HTTP verb, like "put '/path'".
46
54
  VERBS.each do |verb|
47
55
  define_method(verb) do |path, &block|
@@ -50,20 +58,24 @@ module Rain
50
58
  end
51
59
 
52
60
  def request(event: RequestEvent)
53
- site = Rain::Site.from_request(event.request)
54
- locale = Rain::Locale.from_request(event.request, site:).language
61
+ App.serving { respond_in_context(request: event.request) }
62
+ end
63
+
64
+ private
65
+
66
+ def respond_in_context(request:)
67
+ site = Rain::Site.from_request(request)
68
+ locale = Rain::Locale.from_request(request, site:).language
55
69
 
56
70
  # Cached components are kept apart by site, as each links back to its own and can show other things.
57
71
  response_event = Antlers::ComponentCache.with_context(site.host) do
58
- Antlers::Translations.with_locale(locale) { respond(request: event.request) }
72
+ Antlers::Translations.with_locale(locale) { respond(request:) }
59
73
  end
60
74
  language_headers(response: response_event.response, locale:) if response_event.is_a?(ResponseEvent)
61
75
 
62
76
  response_event
63
77
  end
64
78
 
65
- private
66
-
67
79
  def language_headers(response:, locale:)
68
80
  return unless response
69
81
  return if Antlers::Translations.locales.empty?
data/lib/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Raindeer
4
- VERSION = '1.0.67'
4
+ VERSION = '1.1.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: raindeer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.67
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - maedi
@@ -74,7 +74,7 @@ dependencies:
74
74
  version: '0.7'
75
75
  - - ">="
76
76
  - !ruby/object:Gem::Version
77
- version: 0.7.1
77
+ version: 0.7.4
78
78
  type: :runtime
79
79
  prerelease: false
80
80
  version_requirements: !ruby/object:Gem::Requirement
@@ -84,7 +84,7 @@ dependencies:
84
84
  version: '0.7'
85
85
  - - ">="
86
86
  - !ruby/object:Gem::Version
87
- version: 0.7.1
87
+ version: 0.7.4
88
88
  - !ruby/object:Gem::Dependency
89
89
  name: lowdata
90
90
  requirement: !ruby/object:Gem::Requirement
@@ -134,7 +134,7 @@ dependencies:
134
134
  version: '0.7'
135
135
  - - ">="
136
136
  - !ruby/object:Gem::Version
137
- version: 0.7.5
137
+ version: 0.7.7
138
138
  type: :runtime
139
139
  prerelease: false
140
140
  version_requirements: !ruby/object:Gem::Requirement
@@ -144,7 +144,7 @@ dependencies:
144
144
  version: '0.7'
145
145
  - - ">="
146
146
  - !ruby/object:Gem::Version
147
- version: 0.7.5
147
+ version: 0.7.7
148
148
  - !ruby/object:Gem::Dependency
149
149
  name: lowloop
150
150
  requirement: !ruby/object:Gem::Requirement
@@ -236,7 +236,7 @@ dependencies:
236
236
  version: '1.1'
237
237
  - - ">="
238
238
  - !ruby/object:Gem::Version
239
- version: 1.1.5
239
+ version: 1.1.6
240
240
  type: :runtime
241
241
  prerelease: false
242
242
  version_requirements: !ruby/object:Gem::Requirement
@@ -246,7 +246,7 @@ dependencies:
246
246
  version: '1.1'
247
247
  - - ">="
248
248
  - !ruby/object:Gem::Version
249
- version: 1.1.5
249
+ version: 1.1.6
250
250
  - !ruby/object:Gem::Dependency
251
251
  name: plugs
252
252
  requirement: !ruby/object:Gem::Requirement
@@ -321,6 +321,7 @@ extensions: []
321
321
  extra_rdoc_files: []
322
322
  files:
323
323
  - bin/rain
324
+ - lib/app/app.rb
324
325
  - lib/assets/assets.rb
325
326
  - lib/cli/cli.rb
326
327
  - lib/cli/static.rb