https://github.com/maybemonad/task-queue-for-rxjs
For async execution in nested Observables
https://github.com/maybemonad/task-queue-for-rxjs
rxjs
Last synced: 16 days ago
JSON representation
For async execution in nested Observables
- Host: GitHub
- URL: https://github.com/maybemonad/task-queue-for-rxjs
- Owner: MaybeMonad
- Created: 2022-06-24T07:17:30.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2022-06-30T11:48:07.000Z (almost 4 years ago)
- Last Synced: 2025-03-03T01:44:02.163Z (over 1 year ago)
- Topics: rxjs
- Language: TypeScript
- Homepage: https://codesandbox.io/s/task-queue-for-rxjs-g1prs6?file=/src/App.js
- Size: 51.8 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Async Task Queue for RxJS
Checkout: [Code Sandbox Example](https://codesandbox.io/s/task-queue-for-rxjs-g1prs6?file=/src/App.js)

You can simply create an instance to get a new `Subject` with queued execution by applying `new TaskQueue()`, as well as wrap your exist `Subject`s with `TaskQueue` like `new TaskQueue(ob$)`.
```ts
const ob$ = new Subject()
const subOb$ = new Subject()
async function asyncFunc() {
await new Promise((resolve) => setTimeout(resolve, Math.random(1) * 1000))
}
// Without [TaskQueue]
ob$.subscribe(x => {
subOb$.next(x)
})
subOb$.current.pipe(switchMap(asyncFunc)).subscribe() // <- My main goal is to make sure the [asyncFunc] is executed asyncronously.
// With [TaskQueue]
const taskQueue = new TaskQueue(subOb$)
ob$.subscribe(x => {
taskQueue.next(x)
// or
taskQueue.nextAsync(x)
})
taskQueue.subscribe(asyncFunc)
```
## `TaskQueue`
```ts
interface Option {
taskPacakgeSize?: number; // Maximum tasks in an execution, default is 1
tickTime?: number; // Debounce Time(ms), default is 0
}
```
## `Reconciler`
Reconciler is a tool to schedule the executions between different Observables.
```ts
const ob1$ = new TaskQueue()
const ob2$ = new TaskQueue()
const ob3$ = new TaskQueue()
const asyncFunc = async (x) => {
await new Promise(resolve => setTimeout(resolve, Math.random(1) * 1000))
console.log(x)
}
// Without Reconciler
ob1$.next(1)
ob2$.next(2)
ob3$.next(3)
ob1$.subscribe(console.log)
ob2$.subscribe(console.log)
ob3$.subscribe(console.log)
// With Reconciler
const executor = new Reconciler(ob1$, ob2$, ob3$)
executor.next(1, ob$1)
executor.next(2, ob$2)
executor.next(3, ob$3)
ob1$.subscribe(console.log)
ob2$.subscribe(console.log)
ob3$.subscribe(console.log)
```