lib/signalize.rb in signalize-1.1.0 vs lib/signalize.rb in signalize-1.2.0
- old
+ new
@@ -19,10 +19,14 @@
def self.cycle_detected
raise Signalize::Error, "Cycle detected"
end
+ def self.mutation_detected
+ raise Signalize::Error, "Computed cannot have side-effects"
+ end
+
RUNNING = 1 << 0
NOTIFIED = 1 << 1
OUTDATED = 1 << 2
DISPOSED = 1 << 3
HAS_ERROR = 1 << 4
@@ -32,10 +36,14 @@
# Computed | Effect | nil
global_map_accessor :eval_context
self.eval_context = nil
+ # Used by `untracked` method
+ global_map_accessor :untracked_depth
+ self.untracked_depth = 0
+
# Effects collected into a batch.
global_map_accessor :batched_effect
self.batched_effect = nil
global_map_accessor :batch_depth
self.batch_depth = 0
@@ -412,10 +420,12 @@
node._version = _version unless node.nil?
@value
end
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
@@ -482,11 +492,11 @@
if @_version > 0 && !Signalize.needs_to_recompute(self)
@_flags &= ~RUNNING
return true
end
- prevContext = Signalize.eval_context
+ 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
@@ -497,11 +507,11 @@
rescue StandardError => err
@value = err;
@_flags |= HAS_ERROR
@_version += 1
end
- Signalize.eval_context = prevContext
+ Signalize.eval_context = prev_context
Signalize.cleanup_sources(self)
@_flags &= ~RUNNING
true
end
@@ -663,9 +673,24 @@
begin
return yield
ensure
Signalize.end_batch
+ end
+ end
+
+ def untracked
+ return yield unless Signalize.untracked_depth.zero?
+
+ prev_context = Signalize.eval_context
+ Signalize.eval_context = nil
+ Signalize.untracked_depth += 1
+
+ begin
+ return yield
+ ensure
+ Signalize.untracked_depth -= 1
+ Signalize.eval_context = prev_context
end
end
end
extend API