Module: DeferrableGratification::Combinators
- Defined in:
- lib/deferrable_gratification/combinators.rb,
lib/deferrable_gratification/combinators/bind.rb
Overview
Combinators for building up higher-level asynchronous abstractions by composing simpler asynchronous operations, without having to manually wire callbacks together and remember to propagate errors correctly.
Defined Under Namespace
Modules: ClassMethods Classes: Bind
Class Method Summary collapse
-
.included(base) ⇒ Object
Boilerplate hook to extend ClassMethods.
Instance Method Summary collapse
-
#>>(prok) ⇒ Deferrable
Alias for #bind!.
-
#bind!(&block) ⇒ Deferrable
Register callbacks so that when this Deferrable succeeds, its result will be passed to the block, which is assumed to return another Deferrable representing the status of a second operation.
-
#map(&block) ⇒ Deferrable
Transform the result of this Deferrable by invoking
block
, returning a Deferrable which succeeds with the transformed result.
Class Method Details
.included(base) ⇒ Object
Boilerplate hook to extend ClassMethods.
147 148 149 |
# File 'lib/deferrable_gratification/combinators.rb', line 147 def self.included(base) base.send :extend, ClassMethods end |
Instance Method Details
#>>(prok) ⇒ Deferrable
71 72 73 |
# File 'lib/deferrable_gratification/combinators.rb', line 71 def >>(prok) Bind.setup!(self, &prok) end |
#bind!(&block) ⇒ Deferrable
Register callbacks so that when this Deferrable succeeds, its result will be passed to the block, which is assumed to return another Deferrable representing the status of a second operation.
If this operation fails, the block will not be run. If either operation fails, the compound Deferrable returned will fire its errbacks, meaning callers don’t have to know about the inner operations and can just subscribe to the result of #bind!.
If you find yourself writing lots of nested #bind! calls, you can equivalently rewrite them as a chain and remove the nesting: e.g.
a.bind! do |x|
b(x).bind! do |y|
c(y).bind! do |z|
d(z)
end
end
end
has the same behaviour as
a.bind! do |x|
b(x)
end.bind! do |y|
c(y)
end.bind! do |z|
d(y)
end
As well as being more readable due to avoiding left margin inflation, this prevents introducing bugs due to inadvertent local variable capture by the nested blocks.
123 124 125 |
# File 'lib/deferrable_gratification/combinators.rb', line 123 def bind!(&block) Bind.setup!(self, &block) end |
#map(&block) ⇒ Deferrable
Transform the result of this Deferrable by invoking block
, returning a Deferrable which succeeds with the transformed result.
If this operation fails, the operation will not be run, and the returned Deferrable will also fail.
operation in some way.
141 142 143 |
# File 'lib/deferrable_gratification/combinators.rb', line 141 def map(&block) bind!(&block) end |