Class: Dispatch::Promise

Inherits:
BasicObject
Defined in:
lib/futuristic/dispatch/promise.rb

Direct Known Subclasses

Future

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object (private)

like method_missing for objc without this ‘promise = Dispatch::Promise.new { NSData.dataWithContentsOfFile(file_name) }’ will not work NSString.alloc.initWithData(promise, encoding:NSUTF8StringEncoding) since promise will not respond to NSData#bytes and return a NSInvalidArgumentException



59
60
61
# File 'lib/futuristic/dispatch/promise.rb', line 59

def method_missing(meth, *args, &block)
  __value__.send(meth, *args, &block)
end

Class Method Details

.new(&block) ⇒ Object

new

Promise a -> Eval a

MacRuby and Rubymotion BasicObject#initialize doesn’t like blocks, so we have to do this



5
6
7
8
9
10
# File 'lib/futuristic/dispatch/promise.rb', line 5

def self.new(&block)
  unless block_given?
    ::Kernel.raise(::ArgumentError, "You cannot initalize a Dispatch::Promise without a block")
  end
  self.alloc.initialization(block)
end

Instance Method Details

#initialization(block) ⇒ Object

setup Grand Central Dispatch concurrent Queue and Group



14
15
16
17
18
19
20
21
22
23
# File 'lib/futuristic/dispatch/promise.rb', line 14

def initialization(block)
  init
  @computation = block
  # Groups are just simple layers on top of semaphores.
  @group = ::Dispatch::Group.new
  # Each thread gets its own FIFO queue upon which we will dispatch
  # the delayed computation passed in the &block variable.
  @promise_queue = ::Dispatch::Queue.concurrent("org.macruby.#{self.class}-0x#{hash.to_s(16)}") #
  self
end

#inspectObject



26
27
28
# File 'lib/futuristic/dispatch/promise.rb', line 26

def inspect
  __value__.inspect
end