vs. |
CommandModel.process("ValidateCreditcard"){
/* side effect */
var validation = fetch("https://.../check-card", ...);
if (validation == true) {
return Event("OrderPlaced");
} else {
return Event("OrderCanceled");
  }
}
c0: ValidateCreditcard()
e0: ValidationSuccessful
c1: PlaceOrder()
e1: OrderPlaced
commands.tags.applyDiscount = ["discount-modifications"]; commands.applyDiscount = function(request, state) { /* find total order price in global state object, apply discount */ var totalPrice = state.orders[request.orderId].totalPrice; state.orders[request.orderId].discount = totalPrice * 0.1; /* if order is changed retroactively at a past point this command needs to be replayed, since a different discount would then be computed */ var readDuringComputation = [ "state.orders[" + request.orderId + "].totalPrice" ]; return { success: true, newState: state, read: readDuringComputation, eventTags: ["applied-discount"] } }
commandTags.someCmd = [ "..." ]; commands.someCmd = function(request, state) { state.foo = state.bar + 1; var b = retroactive.createBranch("big bang"); b.deleteEvents(...); b.getState(); return { read: [ "state.bar" ], eventTags: [ "..." ] } }
Object.observe()
from ECMAScript 7
/* add product id 172 to the "state.cart" object with quantity 1 */
[ { "op": "add", "path": "/cart/172", "value": 1 } ]
/* update the quantity of the product "state.cart[172]" */
[ { "op": "replace", "path": "/cart/172", "value": 3 } ]
/* add an object to "state.orders" array and remove it from cart */
[{
"op": "add",
"path": "/orders/-",
"value": {
"timestamp": 1458061070313,
"content": { "172": 3 }
}
},
{
"op": "remove",
"path": "/cart/172"
}]
vertices["foo"].behaviorFn = function(state, msg) { // local computations state.foo = state.bar + 1; return [state, vertices["foo"].behaviorFn]; }
$ find . -name "question_*"
e0: Created Account
e1: +500
→ eX: -1000
e2: -500
PlaceOrder = FetchCurrencyRate + ProcessOrder + SendConfirmation
CommandModel.process("ValidateCreditcard"){ /* side effect */ var validation = fetch("https://.../check-card", ...); if (validation == true) { return Event("OrderPlaced"); } else { return Event("OrderCanceled"); } }
c0: ValidateCreditcard() e0: ValidationSuccessful c1: PlaceOrder() e1: OrderPlaced
/* add product id 172 to the "state.cart" object with quantity 1 */ [ { "op": "add", "path": "/cart/172", "value": 1 } ] /* update the quantity of the product "state.cart[172]" */ [ { "op": "replace", "path": "/cart/172", "value": 3 } ] /* add an object to "state.orders" array and remove it from cart */ [{ "op": "add", "path": "/orders/-", "value": { "timestamp": 1458061070313, "content": { "172": 3 } } }, { "op": "remove", "path": "/cart/172" }]
-state.shoppingCart = [1, 2, 3];
+state.shoppingCart = [1, 2, 3, 4];
{ "op": "add", "path": "/state/shoppingCart/-", "value": 4 }
state.foo[99] = "new last element"; // vs. state.foo.push("new last element");
commands.placeOrder = function(request, state) { var b = retroactive.createBranch("big bang"); b.deleteEvents("product-removed-from-cart", false); b.getState(); // ... return { newState: state, success: true, read: [] } }