Class: Wands::JavaScript::Queue
- Inherits:
-
Object
- Object
- Wands::JavaScript::Queue
- Defined in:
- lib/wands/java_script/queue.rb
Overview
Asynchronous event queuing mechanism utilizing JavaScript’s Promise; since it uses JavaScript’s API, only objects that can be converted to JavaScript objects can pass through this queue.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize ⇒ Queue
constructor
A new instance of Queue.
-
#pop ⇒ Object
The popped object is a JavaScript object.
-
#push(message) ⇒ Object
(also: #<<)
Only objects that can be converted to JavaScript object can be pushed.
Constructor Details
#initialize ⇒ Queue
Returns a new instance of Queue.
15 16 17 18 |
# File 'lib/wands/java_script/queue.rb', line 15 def initialize @waiter = nil @buffer = [] end |
Class Method Details
.wait_once {|queue| ... } ⇒ Object
9 10 11 12 13 |
# File 'lib/wands/java_script/queue.rb', line 9 def self.wait_once queue = new yield queue queue.pop end |
Instance Method Details
#pop ⇒ Object
The popped object is a JavaScript object. For example, when you push Ruby’s false, the pop method will return JS::True.
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/wands/java_script/queue.rb', line 37 def pop # message is received # return message return @buffer.shift unless @buffer.empty? # message is not received # set promise and wait resolve = nil promise = JS.global[:Promise].new { resolve = it } @waiter = resolve promise.await end |
#push(message) ⇒ Object Also known as: <<
Only objects that can be converted to JavaScript object can be pushed.
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/wands/java_script/queue.rb', line 21 def push() js_object = JS.try_convert() raise TypeError, "#{.class} is not a JS::Object like object" unless js_object if @waiter @waiter.apply @waiter = nil else @buffer << js_object end end |