Class: Signalize::Signal

Inherits:
Object
  • Object
show all
Defined in:
lib/signalize.rb

Direct Known Subclasses

Computed

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Signal

Returns a new instance of Signal.



367
368
369
370
371
372
# File 'lib/signalize.rb', line 367

def initialize(value)
  @value = value
  @_version = 0;
  @_node = nil
  @_targets = nil
end

Instance Attribute Details

#_nodeObject

Returns the value of attribute _node.



365
366
367
# File 'lib/signalize.rb', line 365

def _node
  @_node
end

#_targetsObject

Returns the value of attribute _targets.



365
366
367
# File 'lib/signalize.rb', line 365

def _targets
  @_targets
end

#_versionObject

Returns the value of attribute _version.



365
366
367
# File 'lib/signalize.rb', line 365

def _version
  @_version
end

Instance Method Details

#_refreshObject



374
# File 'lib/signalize.rb', line 374

def _refresh = true

#_subscribe(node) ⇒ Object



376
377
378
379
380
381
382
# File 'lib/signalize.rb', line 376

def (node)
  if _targets != node && node._prev_target.nil?
    node._next_target = _targets
    _targets._prev_target = node if !_targets.nil?
    self._targets = node
  end
end

#_unsubscribe(node) ⇒ Object



384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/signalize.rb', line 384

def _unsubscribe(node)
  # Only run the unsubscribe step if the signal has any subscribers to begin with.
  if !_targets.nil?
    prev = node._prev_target
    nxt = node._next_target
    if !prev.nil?
      prev._next_target = nxt
      node._prev_target = nil
    end
    if !nxt.nil?
      nxt._prev_target = prev
      node._next_target = nil
    end
    self._targets = nxt if node == _targets
  end
end

#inspectObject



453
454
455
# File 'lib/signalize.rb', line 453

def inspect
  "#<#{self.class} value: #{peek.inspect}>"
end

#peekObject



451
# File 'lib/signalize.rb', line 451

def peek = @value

#subscribe(&fn) ⇒ Object



401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# File 'lib/signalize.rb', line 401

def subscribe(&fn)
  signal = self
  this = Effect.allocate
  this.send(:initialize, -> {
    value = signal.value
    flag = this._flags & TRACKING
    this._flags &= ~TRACKING;
    begin
      fn.(value)
    ensure
      this._flags |= flag
    end
  })

  Signalize.effect(this)
end

#to_sObject



447
448
449
# File 'lib/signalize.rb', line 447

def to_s
  @value.to_s
end

#valueObject



418
419
420
421
422
# File 'lib/signalize.rb', line 418

def value
  node = Signalize.add_dependency(self)
  node._version = _version unless node.nil?
  @value
end

#value=(value) ⇒ Object



424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# File 'lib/signalize.rb', line 424

def value=(value)
  Signalize.mutation_detected if Signalize.eval_context.is_a?(Signalize::Computed)

  if value != @value
    Signalize.cycle_detected if Signalize.batch_iteration > 100

    @value = value;
    @_version += 1
    # Signalize.global_version += 1

    Signalize.start_batch
    begin
      node = _targets
      while node.nil?.!
        node._target._notify
        node = node._next_target
      end
    ensure
      Signalize.end_batch
    end
  end
end