OperationStack
Allows an application to track a series of operations, storing information about the arguments passes in and the results returned
import { OperationStack } from '@esri/solution-common';
const stack = new OperationStack();
// start an operation by type
const id = stack.start('getItem');
//.. work happens...
stack.finish(id);
// start an operation with an Operation object
stack.start({
  id: 'createItem_1',
  type: 'createItem',
  inputs: {
   item: {...truncated...},
   portal: 'https://www.arcgis.com',
   username: 'jsmith'
  },
});
// make the call
stack.finish('createItem_1', {newItemId: '00cf213'});
// later you can get that information back out of the stack
const prevOp = stack.getOperation('createItem_1);
// and if you need to roll back you can use the
// .cleanup and .output properties to help orchestrate
Can be used to implement "atomic" operations in an environment that does not have this as a core feature
Constructors
Constructor Parameters
| Parameter | Type | Default | Notes | 
|---|
Methods
| Method | Returns | Notes | 
|---|---|---|
|  | void | Inform the stack that an operation has finished. This will append in a duration property, and mark the state as 'completed'. | 
|  | IOperation[] | Get a list of the completed operations | 
|  | IOperation | Returns a reference to an Operation | 
|  | IOperation[] | Returns reference to the operations array | 
|  | IOperation[] | Return an array of working operations | 
|  | void | Merge a serialized operation stack into a stack instance Typically used to create a comprehensive list of operations
when a function returns a  | 
|  | ISerializedOperationStack | Serialize the stack into simple objects | 
|  | string | Start an operation without requiring a full operation  | 
|  | void | Start an Operation  | 
|  | string | Serialize the completed operations into a set of human readable messages, sorted by the startedAt timestamp | 
finish
Parameters
| Parameter | Type | Default | Notes | 
|---|---|---|---|
| id Required | string | Unique identifier of the Operation | |
| options Optional | Record<stringunknown> | 
Returns
This will append in a duration property, and mark the state as 'completed'.
merge
Merge a serialized operation stack into a stack instance
Parameters
| Parameter | Type | Default | Notes | 
|---|---|---|---|
| stack Required | ISerializedOperationStack | 
Returns
   import { OperationStack } from '@esri/solution-common';
   function someFunction() {
     const stack = new OperationStack();
     stack.start('getItem', {id: 'get-bc3'});
     // do some work...
     stack.finish('get-bc3');
     const itm = {title: 'Fake Item', type: 'Web Map'};
     // create an entry for the function we are about to call...
     stack.start('createItem', {id: 'createItem_01', inputs: {item: itm}});
     // call a function that does work, and has it's own stack
     // and returns a serialized version as part of it's results
     return createItem(itm)
     .then((result) => {
       // tell the stack the last operation finished...
       stack.finish('createItem_01');
       // merge in the stack from the function we called
       stack.merge(result.stack);
       // > stack.getCompleted().length === 3
     });
   }
   function createItem (itm) {
     const otherStack = new OperationStack();
     const id = otherStack.start('createItem');
     // make calls to create item etc
     otherStack.finish(id, {itemId: newItem.id});
     otherStack.start('protectItem', {id: 'protect-00c'});
     // make call to protect item...
     otherStack.finish('protect-00c');
     // all done... return a result with a stack
     return Promise.resolve({
       success:true,
       stack: otherStack.serialize()
     });
   }
Typically used to create a comprehensive list of operations
when a function returns a SerializedOperationStack as part of
it's response
start
Start an operation without requiring a full operation
Parameters
| Parameter | Type | Default | Notes | 
|---|---|---|---|
| type Required | string | Type of the operation. i.e. getItem | |
| params Optional | Record<stringunknown> | 
Returns
Identifier of the new stack entry
const opId = stack.start('getItems');
//...work happens
stack.finish(opId);
startOperation
Start an Operation
Parameters
| Parameter | Type | Default | Notes | 
|---|---|---|---|
| operation Required | IOperation | 
Returns
const stack = new OperationStack();
stack.startOperation({
 id: 'get-bc3',
 type: 'getItem',
 cleanup: 'n/a',
 inputs: {
   id: 'bc3',
   owner: 'vader'
 }
});
// do work
stack.finish('get-bc3');
toString
Serialize the completed operations into a set of human readable messages, sorted by the startedAt timestamp
Returns
Class defined in packages/common/src/OperationStack.ts:47
Inform the stack that an operation has finished.