public void TestGlobalReaction() { var counter = CounterType.Create(); counter.Unprotected(); var effectCount = 0; var disposable = Reactions.Reaction(() => { var _1 = counter.Count; var _2 = counter.Count; }, () => { effectCount++; }); counter.Increment(); counter.Increment(); disposable.Dispose(); counter.Increment(); counter.Increment(); Assert.Equal(4, counter.Count); Assert.Equal(2, effectCount); }
private void AddSnapshotReaction() { if (!_hasSnapshotReaction) { _hasSnapshotReaction = true; var disposer = Reactions.Reaction((r) => Snapshot, (snapshot, r) => EmitSnapshot(snapshot)); AddDisposer(() => disposer.Dispose()); } }
public ObjectNode(IType type, ObjectNode parent, string subpath, IEnvironment environment, object initialValue, object storedValue, bool canAttachTreeNode, Action <INode, object> finalize) { NodeId = ++NextNodeId; Type = type; StoredValue = storedValue; _SubPath = ObservableValue <string> .From(); Subpath = subpath; _Parent = ObservableValue <ObjectNode> .From(); Parent = parent; Environment = environment; _IsRunningAction = false; IsProtectionEnabled = true; AutoUnbox = true; State = NodeLifeCycle.INITIALIZING; PreBoot(); PreCompute(); if (parent == null) { IdentifierCache = new IdentifierCache(); } if (canAttachTreeNode) { NodeCache.Add(StoredValue, new StateTreeNode(this)); } bool sawException = true; try { _IsRunningAction = true; finalize?.Invoke(this, initialValue); _IsRunningAction = false; if (parent != null) { parent.Root.IdentifierCache?.AddNodeToCache(this); } else { IdentifierCache?.AddNodeToCache(this); } FireHook("afterCreate"); State = NodeLifeCycle.CREATED; sawException = false; } finally { if (sawException) { // short-cut to die the instance, to avoid the snapshot computed starting to throw... State = NodeLifeCycle.DEAD; } } var disposer = Reactions.Reaction <object>((r) => Snapshot, (snapshot, r) => EmitSnapshot(snapshot)); AddDisposer(() => disposer.Dispose()); }