在后台作业队列中渲染 Rails 视图的部分内容。ActiveJob + ActionCable。
Oh, yeah! You gotta get schwifty. You gotta get schwifty in here.
get_schwifty is a Rails plugin for rendering slow view partials in a background job and sending them to the client over ActionCable websockets.
Slow-to-render HTML elements can be expensive (hosting) and unavoidable (technical debt, slow libs, expensive db queries, etc). Rendering in-line in your web server consumes a connection which could be serving other clients. Sometimes it's better to just return a minimal page quickly, and let the data backfill as it's generated.
get_schwifty is all about quick responses by utilizing background jobs to do the rendering for your and delivering it to the client with ActionCable.
get_schwifty was extracted from HireLoop.io, a platform for a more human hiring experience.
Make hiring delightful by closing the loop between hiring managers and every applicant. Automate the trival tasks associated with screening job applicants, lowering the barrier for easy and clear communication.
Add this line to your application's Gemfile:
gem "get_schwifty"And then execute:
$ bundleOr install it yourself as:
$ gem install get_schwiftyInstall the plugin
$ rails generate get_schwifty:installFollow the instructions printed.
Generate your first get_schwifty Cable
$ rails generate get_schwifty:cable Calculator fibonacciCables are a place to put your data access logic. Think Controllers for each get_schwifty cable.
# app/cables/calculator_cable.rb
class CalculatorCable is
Lastly, we need to tell the app where to render this chunk of HTML. get_schwifty uses a similar routing syntax to Rails routes of cablecontrollername#action.
# app/views/yourpage.html.erb
... Other HTML ...
This will get displayed while the cable is waiting to get schwifty.
Maybe markup for a loading animation?
... Other HTML ...Load the page, and you should get the whole page back quickly, while the Fibonacci comes later.
Random data isn't very useful. Unauthenticated data isn't very cool either. Usually in Rails data scoping belongs starts at a current_user method. get_schwifty will make any identefiers or params made available in the channel subscription within your cables. Let's take a look at an example.
Here we'll find the current user. This is copied form the Rails Guides.
# app/channels/connection.rb
module ApplicationCable
class Connection
... same as before ...
... Other HTML ...These params will be available in your cable action.
# app/cables/calculator_cable.rb
class CalculatorCable < BaseCable
def fibonacci
n = SecureRandom.rand(params[:min_start]..40)
... same code as before ..
end
endThe gem is available as open source under the terms of the MIT License.
暂无开放 Issues,或尚未同步最近议题。