├── src ├── config.ts ├── version.ts ├── version.spec.ts ├── mod.ts ├── rx-queue.spec.ts ├── debounce-queue │ ├── debounce-queue.ts │ └── debounce-queue.spec.ts ├── throttle-queue │ ├── throttle-queue.ts │ └── throttle-queue.spec.ts ├── concurrency-executor │ ├── concurrency-executer.spec.ts │ └── concurrency-executer.ts ├── delay-queue │ ├── delay-queue.ts │ ├── delay-queue-executor.ts │ ├── delay-queue.spec.ts │ └── delay-queue-executor.spec.ts └── rx-queue.ts ├── docs └── images │ ├── delay.png │ ├── queue.png │ ├── debounce.png │ └── throttle.png ├── .eslintrc.cjs ├── tsconfig.cjs.json ├── .editorconfig ├── tsconfig.json ├── scripts ├── package-publish-config-tag.sh ├── generate-version.sh └── npm-pack-testing.sh ├── tests ├── fixtures │ └── smoke-testing.ts └── rx.spec.ts ├── .gitignore ├── package.json ├── .vscode └── settings.json ├── .github └── workflows │ └── npm.yml ├── README.md └── LICENSE /src/config.ts: -------------------------------------------------------------------------------- 1 | export { VERSION } from './version.js' 2 | -------------------------------------------------------------------------------- /docs/images/delay.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huan/rx-queue/HEAD/docs/images/delay.png -------------------------------------------------------------------------------- /docs/images/queue.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huan/rx-queue/HEAD/docs/images/queue.png -------------------------------------------------------------------------------- /docs/images/debounce.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huan/rx-queue/HEAD/docs/images/debounce.png -------------------------------------------------------------------------------- /docs/images/throttle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huan/rx-queue/HEAD/docs/images/throttle.png -------------------------------------------------------------------------------- /.eslintrc.cjs: -------------------------------------------------------------------------------- 1 | 2 | const rules = { 3 | } 4 | 5 | module.exports = { 6 | extends: '@chatie', 7 | rules, 8 | } 9 | -------------------------------------------------------------------------------- /tsconfig.cjs.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "compilerOptions": { 4 | "module": "CommonJS", 5 | "outDir": "dist/cjs", 6 | }, 7 | } 8 | -------------------------------------------------------------------------------- /src/version.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * This file will be overwrite when we publish NPM module 3 | * by scripts/generate_version.ts 4 | */ 5 | 6 | export const VERSION = '0.0.0' 7 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # Editor configuration, see http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | indent_style = space 7 | indent_size = 2 8 | end_of_line = lf 9 | insert_final_newline = true 10 | trim_trailing_whitespace = true 11 | 12 | [*.md] 13 | max_line_length = 0 14 | trim_trailing_whitespace = false 15 | -------------------------------------------------------------------------------- /src/version.spec.ts: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env -S node --no-warnings --loader ts-node/esm 2 | 3 | import { test } from 'tstest' 4 | 5 | import { VERSION } from './version.js' 6 | 7 | test('Make sure the VERSION is fresh in source code', async (t) => { 8 | t.equal(VERSION, '0.0.0', 'version should be 0.0.0 in source code, only updated before publish to NPM') 9 | }) 10 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "@chatie/tsconfig", 3 | "compilerOptions": { 4 | "outDir": "dist/esm", 5 | }, 6 | "exclude": [ 7 | "node_modules/", 8 | "dist/", 9 | "tests/fixtures/", 10 | ], 11 | "include": [ 12 | "bin/*.ts", 13 | "examples/**/*.ts", 14 | "scripts/**/*.ts", 15 | "src/**/*.ts", 16 | "tests/**/*.spec.ts", 17 | ], 18 | } 19 | -------------------------------------------------------------------------------- /scripts/package-publish-config-tag.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -e 3 | 4 | VERSION=$(npx pkg-jq -r .version) 5 | 6 | if npx --package @chatie/semver semver-is-prod $VERSION; then 7 | npx pkg-jq -i '.publishConfig.tag="latest"' 8 | echo "production release: publicConfig.tag set to latest." 9 | else 10 | npx pkg-jq -i '.publishConfig.tag="next"' 11 | echo 'development release: publicConfig.tag set to next.' 12 | fi 13 | 14 | -------------------------------------------------------------------------------- /scripts/generate-version.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -e 3 | 4 | SRC_VERSION_TS_FILE='src/version.ts' 5 | 6 | [ -f ${SRC_VERSION_TS_FILE} ] || { 7 | echo ${SRC_VERSION_TS_FILE}" not found" 8 | exit 1 9 | } 10 | 11 | VERSION=$(npx pkg-jq -r .version) 12 | 13 | cat <<_SRC_ > ${SRC_VERSION_TS_FILE} 14 | /** 15 | * This file was auto generated from scripts/generate-version.sh 16 | */ 17 | export const VERSION: string = '${VERSION}' 18 | _SRC_ 19 | -------------------------------------------------------------------------------- /scripts/npm-pack-testing.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -e 3 | 4 | 5 | npm run dist 6 | npm pack 7 | 8 | TMPDIR="/tmp/npm-pack-testing.$$" 9 | mkdir "$TMPDIR" 10 | mv *-*.*.*.tgz "$TMPDIR" 11 | cp tests/fixtures/smoke-testing.ts "$TMPDIR" 12 | 13 | cd $TMPDIR 14 | npm init -y 15 | npm install *-*.*.*.tgz \ 16 | rxjs \ 17 | brolog \ 18 | typescript 19 | 20 | ./node_modules/.bin/tsc \ 21 | --lib esnext,dom \ 22 | --noEmitOnError \ 23 | --noImplicitAny \ 24 | --skipLibCheck \ 25 | smoke-testing.ts 26 | 27 | node smoke-testing.js 28 | -------------------------------------------------------------------------------- /src/mod.ts: -------------------------------------------------------------------------------- 1 | export { RxQueue } from './rx-queue.js' 2 | 3 | export { DelayQueueExecutor } from './delay-queue/delay-queue-executor.js' 4 | export { DebounceQueue } from './debounce-queue/debounce-queue.js' 5 | export { DelayQueue } from './delay-queue/delay-queue.js' 6 | export { ThrottleQueue } from './throttle-queue/throttle-queue.js' 7 | export { 8 | concurrencyExecuter, 9 | } from './concurrency-executor/concurrency-executer.js' 10 | 11 | export { VERSION } from './version.js' 12 | -------------------------------------------------------------------------------- /tests/fixtures/smoke-testing.ts: -------------------------------------------------------------------------------- 1 | import { 2 | DelayQueue, 3 | RxQueue, 4 | ThrottleQueue, 5 | VERSION, 6 | } from 'rx-queue' 7 | 8 | if (VERSION === '0.0.0') { 9 | throw new Error('version should be set before publishing') 10 | } 11 | 12 | const rq = new RxQueue() 13 | console.info(`RxQueue v${rq.version()}`) 14 | 15 | const dq = new DelayQueue() 16 | console.info(`DelayQueue v${dq.version()}`) 17 | 18 | const tq = new ThrottleQueue() 19 | console.info(`ThrottleQueue v${tq.version()}`) 20 | 21 | console.info('Smoke Testing PASSED!') 22 | -------------------------------------------------------------------------------- /src/rx-queue.spec.ts: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env -S node --no-warnings --loader ts-node/esm 2 | 3 | // tslint:disable:no-shadowed-variable 4 | import { 5 | test, 6 | sinon, 7 | } from 'tstest' 8 | 9 | import RxQueue from './rx-queue.js' 10 | 11 | test('RxQueue subscribe & next', async t => { 12 | const EXPECTED_ITEM = { test: 'testing123' } 13 | const spy = sinon.spy() 14 | 15 | const q = new RxQueue() 16 | 17 | q.subscribe(spy) 18 | q.next(EXPECTED_ITEM) 19 | 20 | t.ok(spy.calledOnce, 'should received 1 call') 21 | t.deepEqual(spy.firstCall.args[0], EXPECTED_ITEM, 'should received EXPECTED_ITEM') 22 | }) 23 | 24 | test('RxQueue version()', async t => { 25 | const q = new RxQueue() 26 | t.ok(/^\d+\.\d+\.\d+$/.test(q.version()), 'get version') 27 | }) 28 | -------------------------------------------------------------------------------- /src/debounce-queue/debounce-queue.ts: -------------------------------------------------------------------------------- 1 | import { 2 | interval, 3 | Subject, 4 | Subscription, 5 | } from 'rxjs' 6 | import { 7 | debounce, 8 | } from 'rxjs/operators' 9 | 10 | import RxQueue from '../rx-queue.js' 11 | 12 | /** 13 | * DebounceQueue drops a item if there's another one comes in a period of time. 14 | * 15 | * T: item type 16 | */ 17 | export class DebounceQueue extends RxQueue { 18 | 19 | private subscription : Subscription 20 | private subject : Subject 21 | 22 | /** 23 | * 24 | * @param period milliseconds 25 | */ 26 | constructor ( 27 | period?: number, // milliseconds 28 | ) { 29 | super(period) 30 | 31 | this.subject = new Subject() 32 | this.subscription = this.subject.pipe( 33 | debounce(() => interval(this.period)), 34 | ).subscribe((item: T) => super.next(item)) 35 | } 36 | 37 | override next (item: T) { 38 | this.subject.next(item) 39 | } 40 | 41 | override unsubscribe () { 42 | this.subscription.unsubscribe() 43 | super.unsubscribe() 44 | } 45 | 46 | } 47 | 48 | export default DebounceQueue 49 | -------------------------------------------------------------------------------- /src/throttle-queue/throttle-queue.ts: -------------------------------------------------------------------------------- 1 | import { 2 | interval, 3 | Subject, 4 | Subscription, 5 | } from 'rxjs' 6 | import { 7 | throttle, 8 | } from 'rxjs/operators' 9 | 10 | import RxQueue from '../rx-queue.js' 11 | 12 | /** 13 | * ThrottleQueue 14 | * 15 | * passes one item and then drop all the following items in a period of time. 16 | * 17 | * T: item type 18 | */ 19 | export class ThrottleQueue extends RxQueue { 20 | 21 | private subscription : Subscription 22 | private subject : Subject 23 | 24 | /** 25 | * 26 | * @param period milliseconds 27 | */ 28 | constructor ( 29 | period?: number, // milliseconds 30 | ) { 31 | super(period) 32 | 33 | this.subject = new Subject() 34 | this.subscription = this.subject.pipe( 35 | throttle(() => interval(this.period)), 36 | ).subscribe((item: T) => super.next(item)) 37 | } 38 | 39 | override next (item: T) { 40 | this.subject.next(item) 41 | } 42 | 43 | override unsubscribe () { 44 | this.subscription.unsubscribe() 45 | super.unsubscribe() 46 | } 47 | 48 | } 49 | 50 | export default ThrottleQueue 51 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (http://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # Typescript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | /package-lock.json 61 | t.* 62 | /bundles/ 63 | /dist/ 64 | -------------------------------------------------------------------------------- /src/concurrency-executor/concurrency-executer.spec.ts: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env -S node --no-warnings --loader ts-node/esm 2 | 3 | import { 4 | test, 5 | sinon, 6 | } from 'tstest' 7 | 8 | import { concurrencyExecuter } from './concurrency-executer.js' 9 | 10 | test('concurrencyExecuter() smoke testing', async t => { 11 | const sandbox = sinon.createSandbox({ 12 | useFakeTimers: true, 13 | }) 14 | 15 | const INPUT_LIST = [1, 2, 3, 4, 5, 6, 7, 8, 9] 16 | const CONCURRENCY = 2 17 | const SLEEP_MS = 10 18 | 19 | const task = async (v: number) => { 20 | await new Promise(resolve => setTimeout(resolve, SLEEP_MS)) 21 | return v * 10 22 | } 23 | 24 | const iterator = concurrencyExecuter( 25 | CONCURRENCY, 26 | )( 27 | task, 28 | )( 29 | INPUT_LIST, 30 | ) 31 | 32 | const outputList: number[] = [] 33 | 34 | ;(async () => { 35 | for await (const item of iterator) { 36 | outputList.push(item) 37 | } 38 | })().catch(e => t.fail(e)) 39 | 40 | for (let i = 0; i < 3; i++) { 41 | t.equal(outputList.length, i * CONCURRENCY, 'should has ' + i * CONCURRENCY + ' output item(s) after ' + i + ' iteration(s)') 42 | await sandbox.clock.tickAsync(SLEEP_MS + 1) 43 | } 44 | 45 | t.pass('smoke testing passed') 46 | 47 | sandbox.restore() 48 | }) 49 | -------------------------------------------------------------------------------- /tests/rx.spec.ts: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env -S node --no-warnings --loader ts-node/esm 2 | 3 | import { test } from 'tstest' 4 | import { 5 | asyncScheduler, 6 | interval, 7 | } from 'rxjs' 8 | import { 9 | map, 10 | take, 11 | } from 'rxjs/operators' 12 | import { 13 | TestScheduler, 14 | } from 'rxjs/testing' 15 | 16 | /** 17 | * See: https://github.com/ReactiveX/rxjs/blob/master/doc/writing-marble-tests.md 18 | * See Also: https://github.com/ohjames/rxjs-websockets/blob/master/src/index.spec.ts 19 | */ 20 | test('marble smoke testing', async t => { 21 | 22 | function timeRange ( 23 | start: number, 24 | end: number, 25 | step = 1000, 26 | schedulerX = asyncScheduler, 27 | ) { 28 | return interval(step, schedulerX).pipe( 29 | map(n => n + start), 30 | take(end - start + 1), 31 | ) 32 | } 33 | 34 | const scheduler = new TestScheduler((actual, expected) => { 35 | // console.log('Actual:', actual, '\n\n', 'Expected:', expected); 36 | t.ok( 37 | JSON.stringify(actual) === JSON.stringify(expected), 38 | 'two observable should be equal to the defination from marble diagram', 39 | ) 40 | }) 41 | 42 | const source = timeRange(2, 8, 50, scheduler) 43 | const values = { 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8 } 44 | 45 | scheduler.expectObservable(source).toBe('-----2----3----4----5----6----7----(8|)', values) 46 | 47 | scheduler.flush() 48 | 49 | }) 50 | -------------------------------------------------------------------------------- /src/delay-queue/delay-queue.ts: -------------------------------------------------------------------------------- 1 | import { 2 | concat, 3 | of, 4 | Subject, 5 | Subscription, 6 | timer, 7 | } from 'rxjs' 8 | import { 9 | concatMap, 10 | ignoreElements, 11 | } from 'rxjs/operators' 12 | 13 | import RxQueue from '../rx-queue.js' 14 | 15 | /** 16 | * DelayQueue passes all the items and add delays between items. 17 | * T: item type 18 | */ 19 | export class DelayQueue extends RxQueue { 20 | 21 | private subscription : Subscription 22 | private subject : Subject 23 | 24 | /** 25 | * 26 | * @param period milliseconds 27 | */ 28 | constructor ( 29 | period?: number, // milliseconds 30 | ) { 31 | super(period) 32 | 33 | this.subject = new Subject() 34 | this.subscription = this.subject.pipe( 35 | concatMap(x => concat( 36 | of(x), // emit first item right away 37 | /** 38 | * Issue #71 - DelayQueue failed: behavior breaking change after RxJS from v6 to v7 39 | * https://github.com/huan/rx-queue/issues/71 40 | */ 41 | timer(this.period).pipe( 42 | ignoreElements(), 43 | ), 44 | )), 45 | ).subscribe((item: T) => super.next(item)) 46 | } 47 | 48 | override next (item: T) { 49 | this.subject.next(item) 50 | } 51 | 52 | override unsubscribe () { 53 | this.subscription.unsubscribe() 54 | super.unsubscribe() 55 | } 56 | 57 | } 58 | 59 | export default DelayQueue 60 | -------------------------------------------------------------------------------- /src/delay-queue/delay-queue-executor.ts: -------------------------------------------------------------------------------- 1 | import type { Subscription } from 'rxjs' 2 | 3 | import DelayQueue from './delay-queue.js' 4 | 5 | export interface ExecutionUnit { 6 | fn : () => T, 7 | name : string, 8 | resolve : (value: T | PromiseLike) => void, 9 | reject : (e?: any) => void, 10 | } 11 | 12 | /** 13 | * DelayQueueExecutor calls functions one by one with a delay time period between calls. 14 | */ 15 | export class DelayQueueExecutor extends DelayQueue> { 16 | 17 | private readonly delayQueueSubscription: Subscription 18 | 19 | /** 20 | * 21 | * @param period milliseconds 22 | */ 23 | constructor ( 24 | period: number, 25 | ) { 26 | super(period) 27 | 28 | this.delayQueueSubscription = this.subscribe(unit => { 29 | try { 30 | const ret = unit.fn() 31 | return unit.resolve(ret) 32 | } catch (e) { 33 | return unit.reject(e) 34 | } 35 | }) 36 | } 37 | 38 | async execute ( 39 | fn: () => T, 40 | name?: string, 41 | ): Promise { 42 | return new Promise((resolve, reject) => { 43 | const unit: ExecutionUnit = { 44 | fn, 45 | name: name || fn.name, 46 | reject, 47 | resolve, 48 | } 49 | this.next(unit) 50 | }) 51 | } 52 | 53 | override unsubscribe () { 54 | this.delayQueueSubscription.unsubscribe() 55 | super.unsubscribe() 56 | } 57 | 58 | } 59 | 60 | export default DelayQueueExecutor 61 | -------------------------------------------------------------------------------- /src/rx-queue.ts: -------------------------------------------------------------------------------- 1 | import { 2 | PartialObserver, 3 | Subject, 4 | Subscription, 5 | } from 'rxjs' 6 | 7 | import { 8 | VERSION, 9 | } from './config.js' 10 | 11 | // default set to 500 milliseconds 12 | const DEFAULT_PERIOD_TIME = 500 13 | 14 | // https://codepen.io/maindg/pen/xRwGvL 15 | export class RxQueue extends Subject { 16 | 17 | private itemList: T[] = [] 18 | 19 | constructor ( 20 | public period = DEFAULT_PERIOD_TIME, 21 | ) { 22 | super() 23 | } 24 | 25 | override next (item: T) { 26 | if (this.observers.length > 0) { 27 | super.next(item) 28 | } else { 29 | this.itemList.push(item) 30 | } 31 | } 32 | 33 | override subscribe (observer: PartialObserver) : Subscription 34 | override subscribe (next: (value: T) => void, error?: (error: any) => void, complete?: () => void) : Subscription 35 | 36 | override subscribe (...args: never[]): never 37 | 38 | override subscribe ( 39 | nextOrObserver: ((value: T) => void) | PartialObserver, 40 | error?: (error: any) => void, 41 | complete?: () => void, 42 | ) { 43 | let subscription: Subscription // TypeScript strict require strong typing differenciation 44 | if (typeof nextOrObserver === 'function') { 45 | subscription = super.subscribe(nextOrObserver, error, complete) 46 | } else { 47 | subscription = super.subscribe(nextOrObserver) 48 | } 49 | this.itemList.forEach(item => this.next(item)) 50 | this.itemList = [] 51 | return subscription 52 | } 53 | 54 | public version (): string { 55 | return VERSION 56 | } 57 | 58 | } 59 | 60 | export default RxQueue 61 | -------------------------------------------------------------------------------- /src/concurrency-executor/concurrency-executer.ts: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env -S node --no-warnings --loader ts-node/esm 2 | 3 | /** 4 | * If you know how iterators work and how they are consumed you would't need any extra library, 5 | * since it can become very easy to build your own concurrency yourself. 6 | * — @Endless 7 | * 8 | * Inspired by: @link https://stackoverflow.com/a/51020535/1123955 9 | */ 10 | 11 | /** 12 | * Huan's stackoverflow answer (code example) for `merge`: 13 | * @link https://stackoverflow.com/a/69985103/1123955 14 | */ 15 | import { 16 | merge, 17 | } from 'ix/asynciterable/index.js' 18 | 19 | type ExecuterTask = (value: S) => T | Promise 20 | 21 | const executeTask = (task: ExecuterTask) => async function * ( 22 | iterator: IterableIterator, 23 | ): AsyncIterableIterator { 24 | for (const one of iterator) { 25 | const result = await task(one) 26 | yield result 27 | } 28 | } 29 | 30 | /** 31 | * Execute task with the concurrency on an iterator 32 | * The order will not be guaranteed. (mostly will be different) 33 | */ 34 | const concurrencyExecuter = (concurrency = 1) => 35 | (task : ExecuterTask) => 36 | async function * ( 37 | iterator: Array | IterableIterator, 38 | ): AsyncIterableIterator { 39 | if (Array.isArray(iterator)) { 40 | iterator = iterator.values() 41 | } 42 | 43 | const executer = executeTask(task) 44 | 45 | const resultIteratorList = new Array(concurrency) 46 | .fill(iterator) 47 | .map(executer) as [AsyncIterableIterator, ... AsyncIterableIterator[]] 48 | 49 | yield * merge(...resultIteratorList) 50 | } 51 | 52 | export { 53 | concurrencyExecuter, 54 | } 55 | -------------------------------------------------------------------------------- /src/throttle-queue/throttle-queue.spec.ts: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env -S node --no-warnings --loader ts-node/esm 2 | 3 | import { 4 | test, 5 | sinon, 6 | } from 'tstest' 7 | 8 | import ThrottleQueue from './throttle-queue.js' 9 | 10 | const EXPECTED_ITEM1 = { test: 'testing123' } 11 | const EXPECTED_ITEM2 = { mol: 42 } 12 | const EXPECTED_ITEM3 = 42 13 | 14 | const THROTTLE_PERIOD_TIME = 10 // milliseconds 15 | 16 | test('ThrottleQueue 1 item', async t => { 17 | const q = new ThrottleQueue(THROTTLE_PERIOD_TIME) 18 | 19 | const spy = sinon.spy() 20 | q.subscribe(spy) 21 | 22 | q.next(EXPECTED_ITEM1) 23 | 24 | t.ok(spy.calledOnce, 'should called right after first item') 25 | t.deepEqual(spy.firstCall.args[0], EXPECTED_ITEM1, 'should get the first item immediately') 26 | }) 27 | 28 | test('ThrottleQueue 2 item', async t => { 29 | const q = new ThrottleQueue(THROTTLE_PERIOD_TIME) 30 | 31 | const spy = sinon.spy() 32 | q.subscribe(spy) 33 | 34 | q.next(EXPECTED_ITEM1) 35 | q.next(EXPECTED_ITEM2) 36 | 37 | t.ok(spy.calledOnce, 'should only be called once right after next two items') 38 | t.deepEqual(spy.firstCall.args[0], EXPECTED_ITEM1, 'should get the first item') 39 | 40 | await new Promise(resolve => setTimeout(resolve, THROTTLE_PERIOD_TIME + 3)) 41 | t.ok(spy.calledOnce, 'should drop the second call after period because of throttle') 42 | }) 43 | 44 | test('ThrottleQueue 3 items', async t => { 45 | const q = new ThrottleQueue(THROTTLE_PERIOD_TIME) 46 | 47 | const spy = sinon.spy() 48 | q.subscribe(spy) 49 | 50 | q.next(EXPECTED_ITEM1) 51 | q.next(EXPECTED_ITEM2) 52 | 53 | await new Promise(resolve => setTimeout(resolve, THROTTLE_PERIOD_TIME + 3)) 54 | 55 | q.next(EXPECTED_ITEM3) 56 | t.ok(spy.calledTwice, 'should received the third item after THROTTLE_TIME') 57 | t.deepEqual(spy.secondCall.args[0], EXPECTED_ITEM3, 'should received EXPECTED_ITEM3 (not the ITEM2!)') 58 | }) 59 | -------------------------------------------------------------------------------- /src/debounce-queue/debounce-queue.spec.ts: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env -S node --no-warnings --loader ts-node/esm 2 | 3 | import { 4 | test, 5 | sinon, 6 | } from 'tstest' 7 | 8 | import DebounceQueue from './debounce-queue.js' 9 | 10 | const EXPECTED_ITEM1 = { test: 'testing123' } 11 | const EXPECTED_ITEM2 = { mol: 42 } 12 | const EXPECTED_ITEM3 = 42 13 | 14 | const DELAY_PERIOD_TIME = 10 // milliseconds 15 | 16 | test('DebounceQueue 1 item', async t => { 17 | const q = new DebounceQueue(DELAY_PERIOD_TIME) 18 | 19 | const spy = sinon.spy() 20 | q.subscribe(spy) 21 | 22 | q.next(EXPECTED_ITEM1) 23 | t.ok(spy.notCalled, 'should not called right after first item') 24 | 25 | await new Promise(resolve => setTimeout(resolve, DELAY_PERIOD_TIME + 3)) 26 | t.ok(spy.calledOnce, 'should be called after the DELAY_PERIOD_TIME') 27 | t.deepEqual(spy.firstCall.args[0], EXPECTED_ITEM1, 'should get the first item immediately') 28 | }) 29 | 30 | test('DebounceQueue 2 item', async t => { 31 | const q = new DebounceQueue(DELAY_PERIOD_TIME) 32 | 33 | const spy = sinon.spy() 34 | q.subscribe(spy) 35 | 36 | q.next(EXPECTED_ITEM1) 37 | q.next(EXPECTED_ITEM2) 38 | 39 | await new Promise(resolve => setTimeout(resolve, DELAY_PERIOD_TIME + 3)) 40 | t.equal(spy.callCount, 1, 'should be called only once after DELAY_PERIOD_TIME because its debounced') 41 | t.deepEqual(spy.lastCall.args[0], EXPECTED_ITEM2, 'should get the EXPECTED_ITEM2') 42 | }) 43 | 44 | test('DebounceQueue 3 items', async t => { 45 | const q = new DebounceQueue(DELAY_PERIOD_TIME) 46 | 47 | const spy = sinon.spy() 48 | q.subscribe(spy) 49 | 50 | q.next(EXPECTED_ITEM1) 51 | q.next(EXPECTED_ITEM2) 52 | 53 | await new Promise(resolve => setTimeout(resolve, DELAY_PERIOD_TIME + 3)) 54 | 55 | q.next(EXPECTED_ITEM3) 56 | t.equal(spy.callCount, 1, 'should called once right after next(EXPECTED_ITEM3)') 57 | t.deepEqual(spy.lastCall.args[0], EXPECTED_ITEM2, 'the first call should receive EXPECTED_ITEM2') 58 | 59 | await new Promise(resolve => setTimeout(resolve, DELAY_PERIOD_TIME + 3)) 60 | t.equal(spy.callCount, 2, 'should be called twice after the DELAY_PERIOD_TIME') 61 | t.deepEqual(spy.lastCall.args[0], EXPECTED_ITEM3, 'should get EXPECTED_ITEM3') 62 | }) 63 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rx-queue", 3 | "version": "1.0.5", 4 | "description": "Easy to Use ReactiveX Queue that Supports Delay/DelayExecutor/Throttle/Debounce/Concurrency Features Powered by RxJS/IxJS", 5 | "type": "module", 6 | "exports": { 7 | ".": { 8 | "import": "./dist/esm/src/mod.js", 9 | "require": "./dist/cjs/src/mod.js" 10 | } 11 | }, 12 | "typings": "./dist/esm/src/mod.d.ts", 13 | "engines": { 14 | "node": ">=16", 15 | "npm": ">=7" 16 | }, 17 | "scripts": { 18 | "clean": "shx rm -fr dist/*", 19 | "dist": "npm-run-all clean build dist:commonjs", 20 | "build": "tsc && tsc -p tsconfig.cjs.json", 21 | "dist:commonjs": "jq -n \"{ type: \\\"commonjs\\\" }\" > dist/cjs/package.json", 22 | "lint": "npm-run-all lint:es lint:ts", 23 | "lint:ts": "tsc --isolatedModules --noEmit", 24 | "test": "npm-run-all lint test:unit", 25 | "test:unit": "cross-env NODE_OPTIONS=\"--no-warnings --loader=ts-node/esm\" tap \"src/**/*.spec.ts\" \"tests/*.spec.ts\"", 26 | "test:pack": "bash -x scripts/npm-pack-testing.sh", 27 | "lint:es": "eslint --ignore-pattern fixtures/ \"src/**/*.ts\" \"tests/**/*.ts\"" 28 | }, 29 | "repository": { 30 | "type": "git", 31 | "url": "git+https://github.com/huan/rx-queue.git" 32 | }, 33 | "keywords": [ 34 | "queue", 35 | "delay", 36 | "executor", 37 | "throttle", 38 | "debounce", 39 | "rxjs", 40 | "rx", 41 | "fifo" 42 | ], 43 | "author": "Huan LI ", 44 | "license": "Apache-2.0", 45 | "bugs": { 46 | "url": "https://github.com/huan/rx-queue/issues" 47 | }, 48 | "homepage": "https://github.com/huan/rx-queue#readme", 49 | "devDependencies": { 50 | "@chatie/eslint-config": "^1.0.4", 51 | "@chatie/git-scripts": "^0.6.2", 52 | "@chatie/semver": "^0.4.7", 53 | "@chatie/tsconfig": "^4.5.3", 54 | "@types/sinon": "^10.0.6", 55 | "source-map-support": "^0.5.21", 56 | "tstest": "^1.0.1", 57 | "typescript": "^4.5.2" 58 | }, 59 | "files": [ 60 | "dist/", 61 | "src/" 62 | ], 63 | "publishConfig": { 64 | "access": "public", 65 | "tag": "next" 66 | }, 67 | "git": { 68 | "scripts": { 69 | "pre-push": "npx git-scripts-pre-push" 70 | } 71 | }, 72 | "dependencies": { 73 | "ix": "^4.5.2", 74 | "rxjs": "^7.5.5" 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/delay-queue/delay-queue.spec.ts: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env -S node --no-warnings --loader ts-node/esm 2 | 3 | import { 4 | test, 5 | sinon, 6 | } from 'tstest' 7 | 8 | import DelayQueue from './delay-queue.js' 9 | 10 | const EXPECTED_ITEM1 = { test: 'testing123' } 11 | const EXPECTED_ITEM2 = { mol: 42 } 12 | const EXPECTED_ITEM3 = 42 13 | 14 | const DELAY_PERIOD_TIME = 10 // milliseconds 15 | 16 | test('DelayQueue 1 item', async t => { 17 | const q = new DelayQueue(DELAY_PERIOD_TIME) 18 | 19 | const spy = sinon.spy() 20 | q.subscribe(spy) 21 | 22 | q.next(EXPECTED_ITEM1) 23 | 24 | t.equal(spy.callCount, 1, 'should called right after first item') 25 | t.deepEqual(spy.lastCall.args[0], EXPECTED_ITEM1, 'should get the first item immediately') 26 | }) 27 | 28 | test('DelayQueue 2 item', async t => { 29 | const q = new DelayQueue(DELAY_PERIOD_TIME) 30 | 31 | const spy = sinon.spy() 32 | q.subscribe(spy) 33 | 34 | q.next(EXPECTED_ITEM1) 35 | q.next(EXPECTED_ITEM2) 36 | 37 | t.equal(spy.callCount, 1, 'should get one item after next two item') 38 | t.deepEqual(spy.lastCall.args[0], EXPECTED_ITEM1, 'should get the first item only') 39 | 40 | await new Promise(resolve => setTimeout(resolve, DELAY_PERIOD_TIME + 3)) 41 | t.equal(spy.callCount, 2, 'should get the second item after period delay') 42 | t.deepEqual(spy.lastCall.args[0], EXPECTED_ITEM2, 'should get the second item for last call') 43 | }) 44 | 45 | test('DelayQueue 3 items', async t => { 46 | const q = new DelayQueue(DELAY_PERIOD_TIME) 47 | 48 | const spy = sinon.spy() 49 | q.subscribe(spy) 50 | 51 | q.next(EXPECTED_ITEM1) 52 | q.next(EXPECTED_ITEM2) 53 | q.next(EXPECTED_ITEM3) 54 | 55 | t.equal(spy.callCount, 1, 'get first item immediatelly') 56 | t.deepEqual(spy.lastCall.args[0], EXPECTED_ITEM1, 'should received EXPECTED_ITEM1 immediatelly') 57 | 58 | await new Promise(resolve => setTimeout(resolve, DELAY_PERIOD_TIME + 3)) 59 | t.equal(spy.callCount, 2, 'get second item after period') 60 | t.deepEqual(spy.lastCall.args[0], EXPECTED_ITEM2, 'should received EXPECTED_ITEM2 after 1 x period') 61 | 62 | await new Promise(resolve => setTimeout(resolve, DELAY_PERIOD_TIME + 3)) 63 | t.equal(spy.callCount, 3, 'should get the third item after 2 x period') 64 | t.deepEqual(spy.lastCall.args[0], EXPECTED_ITEM3, 'should received EXPECTED_ITEM3 after 2 x period') 65 | }) 66 | -------------------------------------------------------------------------------- /src/delay-queue/delay-queue-executor.spec.ts: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env -S node --no-warnings --loader ts-node/esm 2 | 3 | import { 4 | test, 5 | sinon, 6 | } from 'tstest' 7 | 8 | import DelayExecutor from './delay-queue-executor.js' 9 | 10 | const DELAY_PERIOD_TIME = 10 11 | 12 | const EXPECTED_VAL1 = 1 13 | const EXPECTED_VAL2 = 2 14 | const EXPECTED_VAL3 = 3 15 | 16 | const MEANING_OF_LIFE = 42 17 | 18 | test('DelayQueueExecutor execute once', async t => { 19 | const spy = sinon.spy() 20 | 21 | const delay = new DelayExecutor(DELAY_PERIOD_TIME) 22 | 23 | delay 24 | .execute(() => spy(EXPECTED_VAL1)) 25 | .catch(() => { /* */ }) 26 | 27 | t.ok(spy.calledOnce, 'should received 1 call immediately') 28 | t.equal(spy.firstCall.args[0], EXPECTED_VAL1, 'should get EXPECTED_VAL1') 29 | }) 30 | 31 | test('DelayQueueExecutor execute thrice', async t => { 32 | const spy = sinon.spy() 33 | 34 | const delay = new DelayExecutor(DELAY_PERIOD_TIME) 35 | 36 | delay.execute(() => spy(EXPECTED_VAL1)).catch(() => { /* */ }) 37 | delay.execute(() => spy(EXPECTED_VAL2)).catch(() => { /* */ }) 38 | delay.execute(() => spy(EXPECTED_VAL3)).catch(() => { /* */ }) 39 | 40 | t.equal(spy.callCount, 1, 'should call once immediately') 41 | t.equal(spy.lastCall.args[0], EXPECTED_VAL1, 'should get EXPECTED_VAL1') 42 | 43 | await new Promise(resolve => setTimeout(resolve, DELAY_PERIOD_TIME + 3)) 44 | t.equal(spy.callCount, 2, 'should call twice after DELAY_PERIOD_TIME') 45 | t.equal(spy.lastCall.args[0], EXPECTED_VAL2, 'should get EXPECTED_VAL2') 46 | 47 | await new Promise(resolve => setTimeout(resolve, DELAY_PERIOD_TIME + 3)) 48 | t.equal(spy.callCount, 3, 'should call thrice after 2 x DELAY_PERIOD_TIME') 49 | t.equal(spy.lastCall.args[0], EXPECTED_VAL3, 'should get EXPECTED_VAL3') 50 | 51 | await new Promise(resolve => setTimeout(resolve, DELAY_PERIOD_TIME + 3)) 52 | t.equal(spy.callCount, 3, 'should keep third call...') 53 | }) 54 | 55 | test('DelayQueueExecutor return Promise', async t => { 56 | const delay = new DelayExecutor(0) 57 | 58 | const mol = await delay.execute(() => MEANING_OF_LIFE) 59 | t.equal(mol, MEANING_OF_LIFE, 'should get the function return value') 60 | 61 | const p = delay.execute(() => Promise.resolve(MEANING_OF_LIFE)) 62 | t.ok(p instanceof Promise, 'should get the function return value(promise)') 63 | 64 | const value = await p 65 | t.equal(value, MEANING_OF_LIFE, 'should get the function return value by await') 66 | 67 | }) 68 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "typescript.tsdk": "./node_modules/typescript/lib", 3 | 4 | "editor.fontFamily": "Consolas, 'Courier New', monospace", 5 | "editor.fontLigatures": true, 6 | 7 | "editor.tokenColorCustomizations": { 8 | "textMateRules": [ 9 | { 10 | "scope": [ 11 | //following will be in italics (=Pacifico) 12 | "comment", 13 | // "entity.name.type.class", //class names 14 | "keyword", //import, export, return… 15 | "support.class.builtin.js", //String, Number, Boolean…, this, super 16 | "storage.modifier", //static keyword 17 | "storage.type.class.js", //class keyword 18 | "storage.type.function.js", // function keyword 19 | "storage.type.js", // Variable declarations 20 | "keyword.control.import.js", // Imports 21 | "keyword.control.from.js", // From-Keyword 22 | "entity.name.type.js", // new … Expression 23 | "keyword.control.flow.js", // await 24 | "keyword.control.conditional.js", // if 25 | "keyword.control.loop.js", // for 26 | "keyword.operator.new.js", // new 27 | ], 28 | "settings": { 29 | "fontStyle": "italic", 30 | }, 31 | }, 32 | { 33 | "scope": [ 34 | //following will be excluded from italics (My theme (Monokai dark) has some defaults I don't want to be in italics) 35 | "invalid", 36 | "keyword.operator", 37 | "constant.numeric.css", 38 | "keyword.other.unit.px.css", 39 | "constant.numeric.decimal.js", 40 | "constant.numeric.json", 41 | "entity.name.type.class.js" 42 | ], 43 | "settings": { 44 | "fontStyle": "", 45 | }, 46 | } 47 | ] 48 | }, 49 | "files.exclude": { 50 | "dist/": true, 51 | "doc/": true, 52 | "node_modules/": true, 53 | "package/": true, 54 | }, 55 | "alignment": { 56 | "operatorPadding": "right", 57 | "indentBase": "firstline", 58 | "surroundSpace": {, 59 | "colon": [1, 1], // The first number specify how much space to add to the left, can be negative. The second number is how much space to the right, can be negative. 60 | "assignment": [1, 1], // The same as above. 61 | "arrow": [1, 1], // The same as above. 62 | "comment": 2, // Special how much space to add between the trailing comment and the code. 63 | // If this value is negative, it means don't align the trailing comment. 64 | } 65 | }, 66 | "editor.formatOnSave": false, 67 | "python.pythonPath": "python3", 68 | "eslint.validate": [ 69 | "javascript", 70 | "typescript", 71 | ], 72 | 73 | } 74 | -------------------------------------------------------------------------------- /.github/workflows/npm.yml: -------------------------------------------------------------------------------- 1 | name: NPM 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | build: 7 | name: Build 8 | strategy: 9 | matrix: 10 | os: 11 | - ubuntu-latest 12 | - macos-latest 13 | - windows-latest 14 | node-version: 15 | - 16 16 | 17 | runs-on: ${{ matrix.os }} 18 | steps: 19 | - uses: actions/checkout@v2 20 | - name: Use Node.js ${{ matrix.node-version }} 21 | uses: actions/setup-node@v2 22 | with: 23 | node-version: ${{ matrix.node-version }} 24 | cache: npm 25 | cache-dependency-path: package.json 26 | - name: Install Dependencies 27 | run: npm install 28 | 29 | - name: Test 30 | run: npm test 31 | 32 | pack: 33 | name: Pack 34 | needs: build 35 | runs-on: ubuntu-latest 36 | steps: 37 | - uses: actions/checkout@v2 38 | - uses: actions/setup-node@v2 39 | with: 40 | node-version: 16 41 | cache: npm 42 | cache-dependency-path: package.json 43 | - name: Install Dependencies 44 | run: npm install 45 | 46 | - name: Generate Version 47 | run: ./scripts/generate-version.sh 48 | 49 | - name: Pack Testing 50 | run: ./scripts/npm-pack-testing.sh 51 | 52 | publish: 53 | if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/heads/v')) 54 | name: Publish 55 | needs: [build, pack] 56 | runs-on: ubuntu-latest 57 | steps: 58 | - uses: actions/checkout@v2 59 | - uses: actions/setup-node@v2 60 | with: 61 | node-version: 16 62 | registry-url: https://registry.npmjs.org/ 63 | cache: npm 64 | cache-dependency-path: package.json 65 | - name: Install Dependencies 66 | run: npm install 67 | 68 | - name: Generate Version 69 | run: ./scripts/generate-version.sh 70 | 71 | - name: Set Publish Config 72 | run: ./scripts/package-publish-config-tag.sh 73 | 74 | - name: Build Dist 75 | run: npm run dist 76 | 77 | - name: Check Branch 78 | id: check-branch 79 | run: | 80 | if [[ ${{ github.ref }} =~ ^refs/heads/(main|v[0-9]+\.[0-9]+.*)$ ]]; then 81 | echo ::set-output name=match::true 82 | fi # See: https://stackoverflow.com/a/58869470/1123955 83 | - name: Is A Publish Branch 84 | if: steps.check-branch.outputs.match == 'true' 85 | run: | 86 | NAME=$(npx pkg-jq -r .name) 87 | VERSION=$(npx pkg-jq -r .version) 88 | if npx version-exists "$NAME" "$VERSION" 89 | then echo "$NAME@$VERSION exists on NPM, skipped." 90 | else npm publish 91 | fi 92 | env: 93 | NODE_AUTH_TOKEN: ${{ secrets.NODE_AUTH_TOKEN }} 94 | - name: Is Not A Publish Branch 95 | if: steps.check-branch.outputs.match != 'true' 96 | run: echo 'Not A Publish Branch' 97 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RX-QUEUE 2 | 3 | [![NPM](https://github.com/huan/rx-queue/actions/workflows/npm.yml/badge.svg)](https://github.com/huan/rx-queue/actions/workflows/npm.yml) 4 | [![Windows Build status](https://img.shields.io/appveyor/ci/zixia/rx-queue/master.svg?label=Windows)](https://ci.appveyor.com/project/zixia/rx-queue) 5 | [![NPM Version](https://badge.fury.io/js/rx-queue.svg)](https://badge.fury.io/js/rx-queue) 6 | [![Downloads](http://img.shields.io/npm/dm/rx-queue.svg?style=flat-square)](https://npmjs.org/package/rx-queue) 7 | [![Powered by TypeScript](https://img.shields.io/badge/Powered%20By-TypeScript-blue.svg)](https://www.typescriptlang.org/) 8 | 9 | Easy to Use ReactiveX Queue that Supports Delay/DelayExecutor/Throttle/Debounce Features Powered by RxJS. 10 | 11 | ![RxQueue](https://huan.github.io/rx-queue/images/queue.png) 12 | > Picture Credit: [Queues in JavaScript](https://www.kirupa.com/html5/queues_in_javascript.htm) 13 | 14 | ## API 15 | 16 | ### Class 17 | 18 | 1. [RxQueue](#rxqueue) 19 | 1. [DelayQueue](#delayqueue) 20 | 1. [ThrottleQueue](#throttlequeue) 21 | 1. [DebounceQueue](#debouncequeue) 22 | 1. [DelayQueueExecutor](#DelayQueueExecutor) 23 | 24 | ### Function 25 | 26 | 1. [concurrencyExecuter()](#concurrencyexecuter) 27 | 28 | ### RxQueue 29 | 30 | `RxQueue` is the base class of all other queues. It extends from RxJS Subject. 31 | 32 | **Example:** 33 | 34 | ```ts 35 | import { RxQueue } from 'rx-queue' 36 | 37 | const queue = new RxQueue() 38 | queue.next(1) 39 | queue.next(2) 40 | queue.next(3) 41 | 42 | queue.subscribe(console.log) 43 | // Output: 1 44 | // Output: 2 45 | // Output: 3 46 | ``` 47 | 48 | ### DelayQueue 49 | 50 | `DelayQueue` passes all the items and add delays between items. 51 | 52 | ![DelayQueue](https://huan.github.io/rx-queue/images/delay.png) 53 | > Picture Credit: [ReactiveX Single Operator Delay](http://reactivex.io/documentation/single.html) 54 | 55 | Practical examples of `DelayQueue`: 56 | 57 | 1. We are calling a HTTP API which can only be called no more than ten times per second, or it will throw a `500` error. 58 | 59 | **Example:** 60 | 61 | ```ts 62 | import { DelayQueue } from 'rx-queue' 63 | 64 | const delay = new DelayQueue(500) // set delay period time to 500 milliseconds 65 | delay.subscribe(console.log) 66 | 67 | delay.next(1) 68 | delay.next(2) 69 | delay.next(3) 70 | 71 | // Output: 1 72 | // Paused 500 millisecond... 73 | // Output: 2 74 | // Paused 500 millisecond... 75 | // Output: 3 76 | ``` 77 | 78 | ### ThrottleQueue 79 | 80 | `ThrottleQueue` passes one item and then drop all the following items in a period of time. 81 | 82 | ![ThrottleQueue](https://huan.github.io/rx-queue/images/throttle.png) 83 | > Picture Credit: [ReactiveX Observable Throttle](http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#instance-method-throttle) 84 | 85 | By using throttle, we don't allow to our queue to pass more than once every X milliseconds. 86 | 87 | Practical examples of `ThrottleQueue`: 88 | 89 | 1. User is typing text in a textarea. We want to call auto-save function when user is typing, and want it only run at most once every five minutes. 90 | 91 | **Example:** 92 | 93 | ```ts 94 | import { ThrottleQueue } from 'rx-queue' 95 | 96 | const throttle = new ThrottleQueue(500) // set period time to 500 milliseconds 97 | throttle.subscribe(console.log) 98 | 99 | throttle.next(1) 100 | throttle.next(2) 101 | throttle.next(3) 102 | 103 | // Output: 1 104 | ``` 105 | 106 | ### DebounceQueue 107 | 108 | `DebounceQueue` drops a item if there's another one comes in a period of time. 109 | 110 | ![DebounceQueue](https://huan.github.io/rx-queue/images/debounce.png) 111 | > Picture Credit: [ReactiveX Observable Debounce](http://reactivex.io/documentation/operators/debounce.html) 112 | 113 | The Debounce technique allow us to deal with multiple sequential items in a time period to only keep the last one. 114 | 115 | Debouncing enforces that no more items will be passed again until a certain amount of time has passed without any new items coming. 116 | 117 | Practical examples of `DebounceQueue`: 118 | 119 | 1. User is typing text in a search box. We want to make an auto-complete function call only after the user stop typing for 500 milliseconds. 120 | 121 | **Example:** 122 | 123 | ```ts 124 | import { DebounceQueue } from 'rx-queue' 125 | 126 | const debounce = new DebounceQueue(500) // set period time to 500 milliseconds 127 | debounce.subscribe(console.log) 128 | 129 | debounce.next(1) 130 | debounce.next(2) 131 | debounce.next(3) 132 | 133 | // Paused 500 millisecond... 134 | // Output: 3 135 | ``` 136 | 137 | ### DelayQueueExecutor 138 | 139 | `DelayQueueExecutor` calls functions one by one with a delay time period between calls. 140 | 141 | > If you want this feature but do not want rxjs dependencies, you can have a look on a zero dependencies alternative: [BottleNeck](https://github.com/SGrondin/bottleneck) 142 | 143 | ![DelayQueueExecutor](https://huan.github.io/rx-queue/images/delay.png) 144 | > Picture Credit: [ReactiveX Single Operator Delay](http://reactivex.io/documentation/single.html) 145 | 146 | Practical examples of `DelayQueueExecutor`: 147 | 148 | 1. We are calling a HTTP API which can only be called no more than ten times per second, or it will throw a `500` error. 149 | 150 | **Example:** 151 | 152 | ```ts 153 | import { DelayQueueExecutor } from 'rx-queue' 154 | 155 | const delay = new DelayQueueExecutor(500) // set delay period time to 500 milliseconds 156 | 157 | delay.execute(() => console.log(1)) 158 | delay.execute(() => console.log(2)) 159 | delay.execute(() => console.log(3)) 160 | 161 | // Output: 1 162 | // Paused 500 millisecond... 163 | // Output: 2 164 | // Paused 500 millisecond... 165 | // Output: 3 166 | ``` 167 | 168 | ### `concurrencyExecuter()` 169 | 170 | When we have a array and need to use an async function to get the result of them, we can use `Promise.all()`: 171 | 172 | ```ts 173 | const asyncTask = async function (item) { 174 | /** 175 | * Some heavy task, like: 176 | * 1. requires XXX MB of memory 177 | * 2. make 10+ new network connections and each takes 10+ seconds 178 | * 3. etc. 179 | */ 180 | } 181 | 182 | const result = await Promise.all( 183 | hugeArray.map(item => asyncTask), 184 | ) 185 | ``` 186 | 187 | Because the above example `asyncTask` requires lots of resource for each task, 188 | so if the `hugeArray` has many items, like 1,000+, 189 | then to use the `Promise.all` will very likely to crash the system. 190 | 191 | The solution is that we can use `concurrencyExecuter()` to execute them in parallel with a concurrency limitation. 192 | 193 | ```ts 194 | // async task: 195 | const heavyTask = (n: number) => Promise.resolve(resolve => setTimeout(resolve(n^2), 100)) 196 | 197 | const results = concurrencyExecuter( 198 | 2, // concurrency 199 | )( 200 | heavyTask, // task async function 201 | )( 202 | [1, 2, 3], // task arguments 203 | ) 204 | 205 | /** 206 | * in the following `for` loop, we will have 2 currency tasks running at the same time. 207 | */ 208 | for await (const result of results) { 209 | console.log(result) 210 | } 211 | ``` 212 | 213 | That's it. 214 | 215 | ## SEE ALSO 216 | 217 | * [Writing Marble Tests](https://github.com/ReactiveX/rxjs/blob/master/doc/writing-marble-tests.md) 218 | 219 | ## CHANGELOG 220 | 221 | ### main v1.0 (Nov 23, 2021) 222 | 223 | 1. ES Module Support 224 | 1. TypeScript 4.5 225 | 1. `concurrencyExecuter()` method added 226 | 227 | ### v0.12 - May 2021 228 | 229 | 1. Upgrade RxJS to v7.1 230 | 1. Upgrade TypeScript to v4.3 231 | 1. Fix RxJS breaking changes [#71](https://github.com/huan/rx-queue/issues/71) 232 | 233 | ### v0.8 - Mar 2019 234 | 235 | 1. Fix typo: issue [#40](https://github.com/huan/rx-queue/issues/40) - rename `DelayQueueExector` to `DelayQueueExecutor` 236 | 237 | ### v0.6 - Sep 2018 238 | 239 | 1. fix exception bug in browser(ie. Angular) 240 | 241 | ### v0.4 - May 2018 242 | 243 | 1. Upgrade to RxJS 6 244 | 1. Moved CI from Travis-ci.org to Travis-ci.com 245 | 246 | ### v0.2 - Oct 30, 2017 247 | 248 | 1. Support: `DelayQueue`, `ThrottleQueue`, `DebounceQueue`, `DelayQueueExecutor`. 249 | 1. first version 250 | 251 | ## AUTHOR 252 | 253 | [Huan LI (李卓桓)](http://linkedin.com/in/zixia) \ 254 | 255 | [![Profile of Huan LI (李卓桓) on StackOverflow](https://stackexchange.com/users/flair/265499.png)](https://stackexchange.com/users/265499) 256 | 257 | ## COPYRIGHT & LICENSE 258 | 259 | * Code & Docs © 2017-now Huan LI \ 260 | * Code released under the Apache-2.0 License 261 | * Docs released under Creative Commons 262 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2017-2018 Huan LI 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------