# operations-js

> Inspried by [Operation](https://developer.apple.com/documentation/foundation/operation) and [OperationQueue](https://developer.apple.com/documentation/foundation/operationqueue) classes from the iOS Framework.

Latest version **0.1.9** (published 2019-06-12) · ISC license · 0 weekly downloads

## Install

```sh
npm install operations-js
pnpm add operations-js
yarn add operations-js
bun add operations-js
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.9 |
| Published | 2019-06-12 |
| First published | 2019-06-04 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 35 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 3 |
| Maintainers | ohdannyboy |

## Links

- npm: https://www.npmjs.com/package/operations-js
- Repository: https://github.com/dannyYassine/operations.js
- Homepage: https://github.com/dannyYassine/operations.js#readme
- Issues: https://github.com/dannyYassine/operations.js/issues
- npm.io page: https://npm.io/package/operations-js

## Dependencies (2)

- [jest](https://npm.io/package/jest.md) ^24.8.0
- [uuid](https://npm.io/package/uuid.md) ^3.3.2

## Recent versions

- 0.1.9 (latest) — 2019-06-12
- 0.1.8 — 2019-06-11
- 0.1.7 — 2019-06-11
- 0.1.6 — 2019-06-05
- 0.1.5 — 2019-06-05
- 0.1.4 — 2019-06-05
- 0.1.3 — 2019-06-04
- 0.1.2 — 2019-06-04
- 0.1.1 — 2019-06-04
- 0.1.0 — 2019-06-04

## README

# operations-js

Inspried by [Operation](https://developer.apple.com/documentation/foundation/operation) and [OperationQueue](https://developer.apple.com/documentation/foundation/operationqueue) classes from the iOS Framework.

### Classes

* Operation
* BlockOperation
* OperationQueue
* GroupOperation

### Operation

```
const operation = new BlockOperation(6, async () => {
    return new Promise((resolve, reject) => {
            setTimeout(() => {
                resolve();
            }, 1000);
        })
});

operation.start()
```

### OperationQueue

```
const operationQueue = new OperationQueue();

const operation1 = new BlockOperation(1, async () => {
    [...]
});

const operation2 = new BlockOperation(2, async () => {
    [...]
});

operationQueue.addOperations([operation1, operation2]);
```

### Utilizing OperationQueue's maximum concurrent operations

```
operationQueue.maximumConcurentOperations = 2;
```

Console:
```
// operation1 started
// operation2 started
// operation1 done
// operation2 done
```

### Add dependencies

```
operation1.dependencies = [operation2];
operation1.start()
```

Console:
```
// operation2 started
// operation2 done
// operation1 started
// operation1 done
```

### Extend the Operation class for complex tasks

```
class ValidateTokenOperation extends Operation {
    
    /**
     * Method to implement to run custom task
     * @override
     */
    run() {
        [...]
    }
    
}
```

### Create a list of complex operations that represents your application

```
const validateToken = new ValidateTokenOperation();
const getUsersApi = new GetUsersApi();

getUsersApi.dependencies = [validateToken];
getUsersApi.completionCallback = operation => {
    // operation.result;
};
getUsersApi.start()
    .then(result => { ... })
    .catch(e => { ... });
```

Or set the dependency directly in the subclass:

```
class GetUsersApi extends Operation {
    constructor() {
        this.dependencies = [new ValidateTokenOperation()];
    }
}
```

### Inserting operations in a queue to control flow

```
const blockOperation6 = new BlockOperation(6, async () => {
    const response = await axios.get('https://samples.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid=b6907d289e10d714a6e88b30761fae22');
    const data = response.data;
    return data;
});

const operation = new DownloadDataOperation(1);
const operation2 = new DownloadDataOperation(2);
const operation3 = new DownloadDataOperation(3);
const operation4 = new TimeOutOperation(4, 2000);
const operation5 = new TimeOutOperation(5, 1000);
const operation7 = new TimeOutOperation(7, 8000);
const operation8 = new TimeOutOperation(8, 1500);

operation.dependencies = [operation2, operation7];
operation2.dependencies = [operation4, operation8];
operation3.dependencies = [operation, operation5, operation2];
operation4.dependencies = [operation5, blockOperation6];
operation5.dependencies = [blockOperation6];
operation8.dependencies = [operation7];

const operationQueue = new OperationQueue();
operationQueue.maximumConcurentOperations = 10;

operationQueue.completionCallback = () => {
    console.log('queue done');
};

operationQueue.addOperations([operation3])
    .then(result => {
        console.log(result)
    })
    .catch(e => {
        console.log(e)
    });
```

### GroupOperation:

Group multiple operations and return the result of each operation when they are all resolved.

```
const groupOperation = new GroupOperation();

groupOperation.addOperation(new GetUsersApi());
groupOperation.addOperation(new GetPostsApi());

const [users, posts] = await groupOperation.start();
```

---
_Source: https://npm.io/package/operations-js · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
