Class: Signalize::Computed
Instance Attribute Summary collapse
-
#_compute ⇒ Object
Returns the value of attribute _compute.
-
#_flags ⇒ Object
Returns the value of attribute _flags.
-
#_sources ⇒ Object
Returns the value of attribute _sources.
Attributes inherited from Signal
Instance Method Summary collapse
- #_notify ⇒ Object
- #_refresh ⇒ Object
- #_subscribe(node) ⇒ Object
- #_unsubscribe(node) ⇒ Object
-
#initialize(compute) ⇒ Computed
constructor
A new instance of Computed.
- #peek ⇒ Object
- #value ⇒ Object
Methods inherited from Signal
#inspect, #subscribe, #to_s, #value=
Constructor Details
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? # If this computed signal has subscribed to updates from its dependencies # (TRACKING flag set) and none of them have notified about changes (OUTDATED # flag not set), then the computed value can't have changed. return true if (@_flags & (OUTDATED | TRACKING)) == TRACKING @_flags &= ~OUTDATED # NOTE: performance optimization removed. # # if @_global_version == Signalize.global_version # return true # end # @_global_version = Signalize.global_version # Mark this computed signal running before checking the dependencies for value # changes, so that the RUNNING flag can be used to notice cyclical dependencies. @_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 # A computed signal subscribes lazily to its dependencies when the it # gets its first subscriber. # RUBY NOTE: if we redefine `node`` here, it messes with `node` top method scope! # So we'll use a new variable name `snode` 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) # Only run the unsubscribe step if the computed signal has any subscribers. unless @_target.nil? super(node) # Computed signal unsubscribes from its dependencies when it loses its last subscriber. # This makes it possible for unreferences subgraphs of computed signals to get garbage collected. 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 |