Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tomas2d/promise-based-task
Create a task and resolve it later via a Promise approach. Run time-consuming processes only once.
https://github.com/tomas2d/promise-based-task
async async-task promise task-map typescript
Last synced: about 2 months ago
JSON representation
Create a task and resolve it later via a Promise approach. Run time-consuming processes only once.
- Host: GitHub
- URL: https://github.com/tomas2d/promise-based-task
- Owner: Tomas2D
- License: mit
- Created: 2021-12-27T22:51:40.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-11-29T00:05:34.000Z (2 months ago)
- Last Synced: 2024-12-02T08:16:25.512Z (2 months ago)
- Topics: async, async-task, promise, task-map, typescript
- Language: TypeScript
- Homepage:
- Size: 1.35 MB
- Stars: 8
- Watchers: 3
- Forks: 2
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# 🛤 promise-based-task
[![CI](https://github.com/Tomas2D/promise-based-task/actions/workflows/node.js.yml/badge.svg?branch=main)](https://github.com/Tomas2D/promise-based-task/actions/workflows/node.js.yml)
[![codecov](https://codecov.io/gh/Tomas2D/promise-based-task/branch/main/graph/badge.svg?token=SQA7VM6XIV)](https://codecov.io/gh/Tomas2D/promise-based-task)With this tiny library, you will be able to solve situations where you want to prevent
multiple requests to do time-consuming operations simultaneously.
With this approach, one request will take care of creating the final data and other processes
will asynchronously wait for the completion.**NOTE:** This library is useful if you need a lightweight solution without an extra service like Redis / PubSub etc.
Does this library help you? Please give it a ⭐️!
## ✨️ Features
- Later resolving of a task in a `Promise` way
- Auto rejecting promises removed from the shared data structure (`TaskMap`, which is an extension of a `Map` data structure)
- Zero dependencies
- Map with limited space (sliding-window)
- Task Queue mechanism## 🚀 Installation
```
yarn add promise-based-task
```
```
npm install promise-based-task
```## 🤘🏻 Usage
**Single task**
```typescript
import { Task } from 'promise-based-task'let pricesTask: Task | null = null
app.get('/prices', async function (req, res) {
if (pricesTask === null) {
pricesTask = new Task();const result = await longRunningTask()
pricesTask.resolve(result)
}const prices = await pricesTask
res.json(prices)
})
```**Multiple tasks (without removing)**
This type is useful when you want to cache data that are not so huge, but
their creation is expensive. Following code prevents multiple requests to react the
critical section of generating the data.```typescript
import { Task, TaskMap } from 'promise-based-task'let pricesTasks: TaskMap = new TaskMap()
app.get('/prices/:date', async function (req, res) {
const date = req.params.dateif (!pricesTasks.has(date)) {
const task = new Task();
pricesTasks.set(date, task)const result = await longRunningTask(date)
task.resolve(result)
}const prices = await pricesTasks.get(date)
res.json(prices)
})
```**Multiple tasks (with removing)**
Sometimes you do not want to store results in memory because results can be enormous in size
and thus, your server can run out of his memory pretty fast.Following code prevents multiple requests to trigger the process of transforming data to
desired shape and uploading them to S3.```typescript
import { Task, TaskMap } from 'promise-based-task'const tasks: TaskMap = new TaskMap()
app.get('/observations/:date', async function (req, res) {
const date = req.params.dateif (tasks.has(date)) {
await tasks.get(date)
} else {
const task = new Task()
tasks.set(date, task)const rawData = await fetchData(date)
const data = await analyzeData(rawData)
await uploadDataToS3(data)task.resolve()
tasks.delete(date)
}downloadDataFromS3(date).pipe(res)
})
```**Task Queue**
```typescript
// Type Definition
declare class TaskQueue {
constructor(queueSize?: number); // default is 1
clear(): Promise;
execute(fn: () => Promise): Promise;
getStats(): {
processingCount: number;
waitingCount: number;
};
}
```You want to use the Task Queue when you want to throttle requests/executions of given part of your system.
```typescript
const queue = new TaskQueue(5)app.get('/scrape/:source', async function (req, res) {
const source = req.params.source
const response = await queue.execute(async () => {
// this function will run by the queue mechanism
return service.scrape(source)
})
res.json(response)
})app.get('/scrape/status', async function (req, res) {
const { processingCount, waitingCount } = queue.getStats()
return res.json({
processingCount,
waitingCount,
})
})
```**Sliding window**
```typescript
// Type Definition
declare class SlidingTaskMap extends TaskMap {
constructor(windowSize: number, ttl?: number);
set(key: K, value: V, customTTL?: number): this; // create/override
delete(key: K): boolean; // remove single entry
clear(): void; // removes all entries
pop(): boolean; // removes oldest entry, false for an empty window
shift(): boolean; // removes newest entry, false for an empty window
}
```When your map size reaches a specified threshold, the oldest values will be
removed. You can be then sure that the size of the map will never overflow your memory.```typescript
import { Task, SlidingTaskMap } from 'promise-based-task'const WINDOW_SIZE = 10
const TTL = 60 * 1000 // data will persist 60 seconds in a cache
const tasks = new SlidingTaskMap(WINDOW_SIZE, TTL)app.get('/calculation/:date', async function () {
const date = req.params.dateif (!tasks.has(date)) {
const task = new Task()
tasks.set(date, task)const data = await fetchData(date)
task.resolve(data)
}return tasks.get(date)
})
```When your map size reaches a specified threshold, the oldest values will be
removed. You can be then sure that the size of the map will never overflow your memory.```typescript
import { Task, SlidingTaskMap } from 'promise-based-task'const WINDOW_SIZE = 10
const TTL = 60 * 1000 // data will persist 60 seconds in a cache
const tasks = new SlidingTaskMap(WINDOW_SIZE, TTL)app.get('/calculation/:date', async function () {
const date = req.params.dateif (!tasks.has(date)) {
const task = new Task()
tasks.set(date, task)
const data = await fetchData(date)
task.resolve(data)
}
return tasks.get(date)
})
```