public void TestApplyActionCalls() { var model = ModelType.Create(); var calls = new ISerializedActionCall[] { new SerializedActionCall { Name = "SetTo", Path = "", Arguments = new object[] { "mars" } }, new SerializedActionCall { Name = "SetTo", Path = "", Arguments = new object[] { "universe" } } }; model.ApplyAction(calls); var snapshot = model.GetSnapshot <IModelSnapshot>(); Assert.NotNull(snapshot); Assert.Equal("universe", snapshot.To); }
public static object ApplyAction(this object target, ISerializedActionCall call) { if (!target.IsStateTreeNode()) { throw new InvalidOperationException("Can not listen for action on Non State Tree Node"); } var resolved = target.TryResolve(call.Path ?? ""); if (resolved == null) { throw new InvalidOperationException($"Invalid action path: {call.Path}"); } if (call.Name == "@APPLY_PATCHES") { resolved.ApplyPatch((IJsonPatch[])call.Arguments[0]); } if (call.Name == "@APPLY_SNAPSHOT") { resolved.ApplySnapshot(call.Arguments[0]); } var node = resolved.GetStateTreeNode(); if (resolved is IObservableObject observable) { var arguments = call.Arguments.ToArray(); if (observable.TryInvokeAction(call.Name, arguments, out object result)) { return(result); } } throw new InvalidOperationException($"Not able to apply the action: {call.Name} on target: {target} on path: {call.Path}"); }