Class: Signalize::Computed
Instance Attribute Summary collapse
Attributes inherited from Signal
#_node, #_targets, #_version
Instance Method Summary
collapse
Methods inherited from Signal
#inspect, #subscribe, #to_s, #value=
Constructor Details
#initialize(compute) ⇒ Computed
Returns a new instance of Computed.
461
462
463
464
465
466
467
468
|
# File 'lib/signalize.rb', line 461
def initialize(compute)
super(nil)
@_compute = compute
@_sources = nil
@_flags = OUTDATED
end
|
Instance Attribute Details
#_compute ⇒ Object
Returns the value of attribute _compute.
459
460
461
|
# File 'lib/signalize.rb', line 459
def _compute
@_compute
end
|
#_flags ⇒ Object
Returns the value of attribute _flags.
459
460
461
|
# File 'lib/signalize.rb', line 459
def _flags
@_flags
end
|
#_sources ⇒ Object
Returns the value of attribute _sources.
459
460
461
|
# File 'lib/signalize.rb', line 459
def _sources
@_sources
end
|
Instance Method Details
#_notify ⇒ Object
557
558
559
560
561
562
563
564
565
566
567
|
# File 'lib/signalize.rb', line 557
def _notify
unless (@_flags & NOTIFIED).nonzero?
@_flags |= OUTDATED | NOTIFIED
node = @_targets
while node.nil?.!
node._target._notify
node = node._next_target
end
end
end
|
#_refresh ⇒ Object
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
|
# File 'lib/signalize.rb', line 470
def _refresh
@_flags &= ~NOTIFIED
return false if (@_flags & RUNNING).nonzero?
return true if (@_flags & (OUTDATED | TRACKING)) == TRACKING
@_flags &= ~OUTDATED
@_flags |= RUNNING
if @_version > 0 && !Signalize.needs_to_recompute(self)
@_flags &= ~RUNNING
return true
end
prev_context = Signalize.eval_context
begin
Signalize.prepare_sources(self)
Signalize.eval_context = self
value = @_compute.()
if (@_flags & HAS_ERROR).nonzero? || @value != value || @_version == 0
@value = value
@_flags &= ~HAS_ERROR
@_version += 1
end
rescue StandardError => err
@value = err;
@_flags |= HAS_ERROR
@_version += 1
end
Signalize.eval_context = prev_context
Signalize.cleanup_sources(self)
@_flags &= ~RUNNING
true
end
|
#_subscribe(node) ⇒ Object
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
|
# File 'lib/signalize.rb', line 519
def _subscribe(node)
if @_targets.nil?
@_flags |= OUTDATED | TRACKING
snode = @_sources
while snode.nil?.!
snode._source._subscribe(snode)
snode = snode._next_source
end
end
super(node)
end
|
#_unsubscribe(node) ⇒ Object
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
|
# File 'lib/signalize.rb', line 537
def _unsubscribe(node)
unless @_target.nil?
super(node)
if @_targets.nil?
@_flags &= ~TRACKING
node = @_sources
while node.nil?.!
node._source._unsubscribe(node)
node = node._next_source
end
end
end
end
|
#peek ⇒ Object
569
570
571
572
573
574
575
|
# File 'lib/signalize.rb', line 569
def peek
Signalize.cycle_detected unless _refresh
raise @value if (@_flags & HAS_ERROR).nonzero?
@value
end
|
#value ⇒ Object
577
578
579
580
581
582
583
584
585
586
587
588
|
# File 'lib/signalize.rb', line 577
def value
Signalize.cycle_detected if (@_flags & RUNNING).nonzero?
node = Signalize.add_dependency(self)
_refresh
node._version = @_version unless node.nil?
raise @value if (@_flags & HAS_ERROR).nonzero?
@value
end
|