rubymotion-community / BubbleWrap

Cocoa wrappers and helpers for RubyMotion (Ruby for iOS and OS X) - Making Cocoa APIs more Ruby like, one API at a time. Fork away and send your pull requests
Other
1.18k stars 208 forks source link

Deferrable Callback Thread/Queue #410

Closed jonmorehouse closed 9 years ago

jonmorehouse commented 9 years ago

This is kind of a weird case I ran into ... but sometimes there is weird behavior when using DefaultDeferrables in the context of GCD.


Queue = Dispatch::Queue.new(:some_queue.to_s)
local_promise = EM::DefaultDeferrable.new

queue.async do
    # NOTE do something in the background
    local_promise.succeed :passed

end

local_promise.callback do
  # NOTE not necessarily invoked on the main thread, as one would assume
end

So my understanding here is that while we would assume that the callback is invoked in the thread it was declared on, but that doesn't seem to be the case. I ran into some weird edge cases with this writing tests, using wait and wait_max.

What do you think of changing up the deferrable to automatically call the callback blocks on their origin thread?

clayallsopp commented 9 years ago

Hm yeah, it looks like the callbacks are invoked only on the thread that .succeed etc are invoked on.

I think a new subclass, like EM::ThreadAwareDeferrable, could solve this need (naming things is hard, not strongly opinionated about that one)

jonmorehouse commented 9 years ago

Cool. I'm working on something like this right now. Do you think its safe to store the queue or should we use a weakref?

I'm just thinking, if for some reason the original queue is garbage collected, how should we handle that?

clayallsopp commented 9 years ago

Storing a reference to the queue should be fine - the queues themselves are lightweight, I believe