operationkit
Inspired by Operation and OperationQueue classes from Apple's Foundation framework for macOS, iOS, watchOS, and tvOS.
Universal package for the browser and node.js
Installation
with node:
npm install operationkit
with yarn:
yarn add operationkit
with cdn:
<script src="https://unpkg.com/operationkit/dist/operationkit.min.js"></script>
// then
window.operationkit
Documentation
Classes
Use
const Operation OpreationQueue BlockOperation GroupOperation OperationEvent QueueEvent QueuePriority } = ;
Operation
An abstract class that represents a single task.
const operation = ; const result = await operationstart;
Add dependencies
The operation will execute only when all dependencies are resolved.
operation1dependencies = operation2;operation1start
Console:
// operation2 started
// operation2 done
// operation1 started
// operation1 done
Extend the Operation class for complex tasks
/** * Method to implement to run custom task * @override * @returns */ async { let token = localStorage; if !token const response = await ; token = reponsedata; localStorage; return token; }
The run
function must always return a promise.
{ return { const response = await axios; ; }}
or simply use async keyword when overriding the function.
async { const response = await axios; return responsedata}
Example
/** * @override */ async { try const response = await axios; return responsedata; catch e this; }
TO NOTE: you must always return the result from the async function or Promise.resolve. You will have access to the result from the the result property or from resolving your promise.
const downloadOperation = ; // result is returned from your promise hereconst data = await ; // and the same result is available fromdownloadOperationresult;
Create a collection of complex operations that represents your application
const validateToken = ;const getUsersApi = ; getUsersApidependencies = validateToken;getUsersApi { // operation.result;}; getUsersApistart ;
Or set the dependency directly in the subclass:
{ thisdependencies = ; }
Operation Events
- OperationEvent.START Operation has started to run the task
- OperationEvent.READY Operation is ready to be executed
- OperationEvent.DONE Operation is finished running the task
- OperationEvent.CANCEL Operation was cancelled
- OperationEvent.ERROR An error occured while running the task
apiOperation
OperationQueue
A queue orchestrates the execution of operations. An operation queue executes its queued Operation objects based on their priority and readiness*.
const operationQueue = ; const operation1 = ; const operation2 = ; operationQueue;
Utilizing OperationQueue's maximum concurrent operations
operationQueuemaximumConcurrentOperations = 2;
Console:
// operation1 started
// operation2 started
// operation1 done
// operation2 done
Queue Priority
Specify the relative ordering of operations that are waiting to be started in an operation queue.
QueuePriorityveryHighQueuePriorityhighQueuePrioritynormalQueuePrioritylowQueuePriorityveryLow
These constants let you prioritize the order in which operations execute:
const getCacheData = ;getCacheDataqueuePriority = QueuePriorityhigh; const downloaHighRestImage = ;getCacheDataqueuePriority = QueuePrioritynormal; operationQueue;
Console:
// getCacheData started
// getCacheData done
// downloaHighRestImage started
// downloaHighRestImage done
Inserting operations in a queue to control flow
const blockOperation6 = 6 async { const response = await axios; const data = responsedata; return data;}; const operation = 1;const operation2 = 2;const operation3 = 3;const operation4 = 4 2000;const operation5 = 5 1000;const operation7 = 7 8000;const operation8 = 8 1500; operationdependencies = operation2 operation7;operation2dependencies = operation4 operation8;operation3dependencies = operation operation5 operation2;operation4dependencies = operation5 blockOperation6;operation5dependencies = blockOperation6;operation8dependencies = operation7; const operationQueue = ;operationQueuemaximumConcurrentOperations = 10; operationQueue { console;}; operationQueue;
OperationQueue Events
- OperationEvent.DONE OperationQueue has finished running all operations
- OperationEvent.PAUSED OperationQueue has paused
- OperationEvent.RESUMED OperationQueue has resumed
queue
BlockOperation
A helper class that accepts a function which will be exexuted as the operation's task.
const operation = async { const data = await api; ... return data;}; const result = await operationstartconsole // 'my operation result'
You have acces to the operation in the function:
const operation = async { console;};
GroupOperation
Group multiple operations and return the result of each operation when they are all resolved.
const groupOperation = ; groupOperation;groupOperation; const users posts = await groupOperationstart;
QueuePriorities are also considered when using GroupOperation.
Run tests
npm run test
Run coverage
npm run test-coverage
Run new build
npm run build
Run generate documentation
npm run docs