ione 1.2.0.pre0 → 1.2.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b1ae6f32b5b3e404b07f6ec0a0b20856a0458719
4
- data.tar.gz: 6a1e98dcd2cb657b2cbbe7dcc29514e38c672608
3
+ metadata.gz: 48f64612b814b95e3c12619aad2ad8da60d00f7b
4
+ data.tar.gz: 6094996f6fedbe6449c0033cae40ccc19cafe46c
5
5
  SHA512:
6
- metadata.gz: b57e6559e0be8dca588b6f4b7d1cf069041848d368de377ad0c4db94f4863c0d0194d66843190fc18f1de2c92a0465429d32773d4994c8aee405a584aa167544
7
- data.tar.gz: 8a30c9516df0d52376ebda8958446fdb35d4fb9adbe4028d180afb438f31061bb77ede9ecebde1e7a640ba19364a2a2a35acc691288585de35e2ea64453d5526
6
+ metadata.gz: 00e1194cf9716e8f5030b634f21740d622c96862682a0427266dd76fa453290ed60460b2fdae08f9165d28df12bbfa3ca24de2d03043935c3407335a5e85afd5
7
+ data.tar.gz: 9dda73caa92ee3a8f5853f3edf81496082e9c748663295b2f6f5c1f6c5a21f324d54d8e921a3afb4ec580e8253b0d1c5e24ecf2985a7684a10a962b926f25612
data/lib/ione/future.rb CHANGED
@@ -395,56 +395,14 @@ module Ione
395
395
  end
396
396
 
397
397
  module FutureCallbacks
398
- # Registers a listener that will be called when this future completes,
399
- # i.e. resolves or fails. The listener will be called with the future as
400
- # solve argument
401
- #
402
- # @yieldparam [Ione::Future] future the future
403
- def on_complete(&listener)
404
- run_immediately = false
405
- if @state != :pending
406
- run_immediately = true
407
- else
408
- @lock.lock
409
- begin
410
- if @state == :pending
411
- @complete_listeners << listener
412
- else
413
- run_immediately = true
414
- end
415
- ensure
416
- @lock.unlock
417
- end
418
- end
419
- if run_immediately
420
- listener.call(self, @value, @error) rescue nil
421
- end
422
- nil
423
- end
424
-
425
398
  # Registers a listener that will be called when this future becomes
426
399
  # resolved. The listener will be called with the value of the future as
427
400
  # sole argument.
428
401
  #
429
402
  # @yieldparam [Object] value the value of the resolved future
430
403
  def on_value(&listener)
431
- run_immediately = false
432
- if @state == :resolved
433
- run_immediately = true
434
- else
435
- @lock.lock
436
- begin
437
- if @state == :pending
438
- @value_listeners << listener
439
- elsif @state == :resolved
440
- run_immediately = true
441
- end
442
- ensure
443
- @lock.unlock
444
- end
445
- end
446
- if run_immediately
447
- listener.call(value) rescue nil
404
+ on_complete do |f, value|
405
+ listener.call(value) if f.resolved?
448
406
  end
449
407
  nil
450
408
  end
@@ -455,23 +413,8 @@ module Ione
455
413
  #
456
414
  # @yieldparam [Error] error the error that failed the future
457
415
  def on_failure(&listener)
458
- run_immediately = false
459
- if @state == :failed
460
- run_immediately = true
461
- else
462
- @lock.lock
463
- begin
464
- if @state == :pending
465
- @failure_listeners << listener
466
- elsif @state == :failed
467
- run_immediately = true
468
- end
469
- ensure
470
- @lock.unlock
471
- end
472
- end
473
- if run_immediately
474
- listener.call(@error) rescue nil
416
+ on_complete do |f, _, error|
417
+ listener.call(error) if f.failed?
475
418
  end
476
419
  nil
477
420
  end
@@ -488,9 +431,34 @@ module Ione
488
431
  def initialize
489
432
  @lock = Mutex.new
490
433
  @state = :pending
491
- @failure_listeners = []
492
- @value_listeners = []
493
- @complete_listeners = []
434
+ @listeners = []
435
+ end
436
+
437
+ # Registers a listener that will be called when this future completes,
438
+ # i.e. resolves or fails. The listener will be called with the future as
439
+ # solve argument
440
+ #
441
+ # @yieldparam [Ione::Future] future the future
442
+ def on_complete(&listener)
443
+ run_immediately = false
444
+ if @state != :pending
445
+ run_immediately = true
446
+ else
447
+ @lock.lock
448
+ begin
449
+ if @state == :pending
450
+ @listeners << listener
451
+ else
452
+ run_immediately = true
453
+ end
454
+ ensure
455
+ @lock.unlock
456
+ end
457
+ end
458
+ if run_immediately
459
+ listener.call(self, @value, @error) rescue nil
460
+ end
461
+ nil
494
462
  end
495
463
 
496
464
  # Returns the value of this future, blocking until it is available if
@@ -510,8 +478,7 @@ module Ione
510
478
  return @value if @state == :resolved
511
479
  semaphore = Queue.new
512
480
  u = proc { semaphore << :unblock }
513
- @value_listeners << u
514
- @failure_listeners << u
481
+ @listeners << u
515
482
  ensure
516
483
  @lock.unlock
517
484
  end
@@ -564,50 +531,36 @@ module Ione
564
531
  # @private
565
532
  class CompletableFuture < Future
566
533
  def resolve(v=nil)
567
- value_listeners = nil
568
- complete_listeners = nil
534
+ listeners = nil
569
535
  @lock.lock
570
536
  begin
571
537
  raise FutureError, 'Future already completed' unless @state == :pending
572
538
  @value = v
573
539
  @state = :resolved
574
- value_listeners = @value_listeners
575
- complete_listeners = @complete_listeners
576
- @value_listeners = nil
577
- @failure_listeners = nil
578
- @complete_listeners = nil
540
+ listeners = @listeners
541
+ @listeners = nil
579
542
  ensure
580
543
  @lock.unlock
581
544
  end
582
- value_listeners.each do |listener|
583
- listener.call(v) rescue nil
584
- end
585
- complete_listeners.each do |listener|
545
+ listeners.each do |listener|
586
546
  listener.call(self, v, nil) rescue nil
587
547
  end
588
548
  nil
589
549
  end
590
550
 
591
551
  def fail(error)
592
- failure_listeners = nil
593
- complete_listeners = nil
552
+ listeners = nil
594
553
  @lock.lock
595
554
  begin
596
555
  raise FutureError, 'Future already completed' unless @state == :pending
597
556
  @error = error
598
557
  @state = :failed
599
- failure_listeners = @failure_listeners
600
- complete_listeners = @complete_listeners
601
- @value_listeners = nil
602
- @failure_listeners = nil
603
- @complete_listeners = nil
558
+ listeners = @listeners
559
+ @listeners = nil
604
560
  ensure
605
561
  @lock.unlock
606
562
  end
607
- failure_listeners.each do |listener|
608
- listener.call(error) rescue nil
609
- end
610
- complete_listeners.each do |listener|
563
+ listeners.each do |listener|
611
564
  listener.call(self, nil, error) rescue nil
612
565
  end
613
566
  nil
data/lib/ione/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Ione
4
- VERSION = '1.2.0.pre0'.freeze
4
+ VERSION = '1.2.0.pre1'.freeze
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ione
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0.pre0
4
+ version: 1.2.0.pre1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Theo Hultberg
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-20 00:00:00.000000000 Z
11
+ date: 2014-08-21 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Reactive programming framework for Ruby, painless evented IO, futures
14
14
  and an efficient byte buffer