Last active
August 29, 2015 14:02
-
-
Save cloud8421/ee8f36eb74224ded9aa5 to your computer and use it in GitHub Desktop.
Lightweight futures in Ruby (with Rails example)
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
class Future < Thread | |
def result | |
join | |
value | |
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
class HomeController < ApplicationController | |
def index | |
@page = HomePage.new | |
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
class HomePage | |
attr_reader :data | |
def initialize | |
@data = { | |
featured_items: Future.new { http_fetch(:featured_items) }, | |
grid_items: Future.new { http_fetch(:grid_items) } | |
} | |
end | |
def featured_items | |
@featured_items ||= data[:featured_items].result | |
end | |
def grid_items | |
@grid_items ||= data[:grid_items].result | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment