0.1.4 • Published 6 years ago

ember-rewind-stack v0.1.4

Weekly downloads
411
License
MIT
Repository
-
Last release
6 years ago

ember-rewind-stack

Many applications have code that orchestrates a LOT of API calls. Particularly related to creation of complex solutions, it is quite reasonable to expect that one user action will result in 5 or more API calls, some of which will result in the creation of ancillary records/entities.

The question is - what happens when some critical path call fails in the middle of one of these giant chains?

Enter the rewind-stack-service, which allows you to manage multiple, possibly parallel, operation stacks. In the event of a failure, your .catch() block can ask the service for all the completed operations, and then take action to roll-back the changes.

Installation

ember install ember-rewind-stack

When should I use this?

Only use this when you are orchestrating a lot of calls. If you are just issuing a single save operation, this is massive overkill. Basically, if you can't easily figure out how to clean things up in a .catch(), then consider using this.

API

methodreturnsdescription
addOperation (stackName, operationObj)n/aadd an operation to a named stack. Creates the stack if it does not exist. Sets the .current to the passed in operation. If there is a .current, it is pushed into the stack's .completed array. A .startedAt property is added to the operation when pushed in. A .completedAt property is set when it's pushed into the .completed array.
completeOperation (stackName, options)n/aMoves the current operation onto the .completed array, adding the .completedAt property, and clearing the stack's .current property. The options are merged into the current operation, allowing useful stuff like itemId's to be added after an operation completes.
getCompleted (stackName)array of completed operations, reversedUsed by exception handlers to rewind the completed actions
removeStack (stackName)n/aclean up a stack
getCurrent (stackName)Current Operation ObjReturns the current operation
getStack (stackName)stack objectreturns the entire stack object

Operation Object

The operation object can be whatever you need it to be. The intent here is that you provide enough info in that object to allow you to un-do the operation.

{
  "name": "create site item", // some sort of name that makes sense to you
  "type": "create-item",      // something that would make sense in a log
  "cleanup": "remove-item",   // a key for your clean up code to use...
  "inputs": {                 // whatever your cleanup code needs...
    "id": "3ef...",           // in this case, to delete an item we need the id and owner
    "owner": "dcadmin"
  }
}
0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

8 years ago

0.1.0

8 years ago