├── src ├── ng-package.json ├── index.ts ├── tsconfig.lib.json ├── lib │ ├── waitUntil.ts │ ├── finalizeWithValue.ts │ ├── forkJoinWithProgress.ts │ └── helpers │ │ └── timeRange.ts └── package.json ├── tsconfig.json ├── wallaby.js ├── .editorconfig ├── CHANGELOG.md ├── .gitignore ├── package.json ├── LICENSE ├── README.md └── tslint.json /src/ng-package.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "../node_modules/ng-packagr/ng-package.schema.json", 3 | "dest": "../dist/lib", 4 | "lib": { 5 | "entryFile": "index.ts" 6 | } 7 | } -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Public API surface of rxjs-multi-scan 3 | */ 4 | 5 | export * from './lib/forkJoinWithProgress'; 6 | export * from './lib/helpers/timeRange'; 7 | export * from './lib/finalizeWithValue'; 8 | export * from './lib/waitUntil' 9 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compileOnSave": false, 3 | "compilerOptions": { 4 | "module": "commonjs", 5 | "target": "es5", 6 | "sourceMap": true, 7 | "typeRoots": [ 8 | "node_modules/@types" 9 | ] 10 | }, 11 | "include": [ 12 | "src/**/*.ts", 13 | "src/**/*.d.ts" 14 | ] 15 | } 16 | -------------------------------------------------------------------------------- /wallaby.js: -------------------------------------------------------------------------------- 1 | module.exports = function () { 2 | return { 3 | files: [ 4 | 'src/**/*.ts', 5 | { pattern: 'src/**/*.spec.ts', ignore: true }, 6 | ], 7 | tests: [ 8 | 'src/**/*.spec.ts', 9 | ], 10 | env: { 11 | type: 'node', 12 | runner: 'node' 13 | }, 14 | testFramework: 'jest' 15 | }; 16 | }; 17 | -------------------------------------------------------------------------------- /.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 | insert_final_newline = true 9 | max_line_length = 140 10 | trim_trailing_whitespace = true 11 | 12 | [*.md] 13 | max_line_length = off 14 | trim_trailing_whitespace = false 15 | 16 | [*.json] 17 | insert_final_newline = false 18 | max_line_length = off 19 | 20 | [*.ts] 21 | quote_type = single 22 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog of `RxJS-toolbox` 2 | ## 16.0.0 (2024-02-14) 3 | ### waitUntil 4 | Added waitUntil operator from auth0-angular interceptor codebase. 5 | 6 | ## 2.0.0 (2019-06-07) 7 | ### breaking change 8 | * forkJOinTransparent was renamed to forkJoinWithPercent 9 | * now it returns higher-order observable that emits percent$ observable and finalResult$ observable. 10 | 11 | ## 1.0.0 (2019-05-30) 12 | ### Features 13 | * added `forkJoinTransparent` operator 14 | -------------------------------------------------------------------------------- /src/tsconfig.lib.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tsconfig.json", 3 | "compilerOptions": { 4 | "baseUrl": "./", 5 | "declaration": false, 6 | "emitDecoratorMetadata": true, 7 | "experimentalDecorators": true, 8 | "lib": [ 9 | "es2015", "es6", "dom" 10 | ], 11 | "moduleResolution": "node", 12 | "outDir": "../dist/lib", 13 | "types": [] 14 | }, 15 | "exclude": [ 16 | ".ng_build", 17 | "../node_modules/@types/*", 18 | "node_modules/@types/*" 19 | ] 20 | } 21 | -------------------------------------------------------------------------------- /src/lib/waitUntil.ts: -------------------------------------------------------------------------------- 1 | import {first, mapTo, mergeMap, Observable} from 'rxjs'; 2 | 3 | /** 4 | * I found an interesting #rxjs custom operator in auth0-angular interceptor codebase: waitUntil 5 | * https://github.com/auth0/auth0-angular/blob/v2.2.3/projects/auth0-angular/src/lib/auth.interceptor.ts 6 | * It holds until the param observable emits - and then switches to the source 7 | */ 8 | export const waitUntil = 9 | (signal$: Observable) => 10 | (source$: Observable) => 11 | source$.pipe(mergeMap((value) => signal$.pipe(first(), mapTo(value)))); 12 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See http://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # compiled output 4 | /dist 5 | /tmp 6 | /out-tsc 7 | 8 | # dependencies 9 | /node_modules 10 | 11 | # IDEs and editors 12 | /.idea 13 | .project 14 | .classpath 15 | .c9/ 16 | *.launch 17 | .settings/ 18 | *.sublime-workspace 19 | 20 | # IDE - VSCode 21 | .vscode/* 22 | !.vscode/settings.json 23 | !.vscode/tasks.json 24 | !.vscode/launch.json 25 | !.vscode/extensions.json 26 | 27 | # misc 28 | /.sass-cache 29 | /connect.lock 30 | /coverage 31 | /libpeerconnection.log 32 | npm-debug.log 33 | yarn-error.log 34 | testem.log 35 | /typings 36 | 37 | # System Files 38 | .DS_Store 39 | Thumbs.db 40 | -------------------------------------------------------------------------------- /src/lib/finalizeWithValue.ts: -------------------------------------------------------------------------------- 1 | import {defer, Observable} from 'rxjs'; 2 | import {finalize, tap} from 'rxjs/operators'; 3 | 4 | /** 5 | finalizeWithValue - provides (unlike original finalize from rxjs) source$'s last emitted value (if any) in format {value: } 6 | If source$ completes with noe emitted value - provide undefined. 7 | Author - Benlesh, taken here: https://github.com/ReactiveX/rxjs/issues/4803#issuecomment-496711335 8 | */ 9 | export function finalizeWithValue(callback: (value: T|any) => void): (source: Observable) => Observable { 10 | return (source: Observable): Observable => defer(() => { 11 | let lastValue: T; 12 | return source.pipe( 13 | tap(value => lastValue = value), 14 | finalize(() => callback(lastValue ? {value: lastValue} : lastValue)), 15 | ); 16 | }); 17 | } 18 | -------------------------------------------------------------------------------- /src/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rxjs-toolbox", 3 | "description": "RxJS-toolbox - set of custom operators and handy factory functions for RxJS.", 4 | "version": "16.0.0", 5 | "license": "MIT", 6 | "author": { 7 | "name": "Oleksandr Poshtaruk", 8 | "email": "kievsash@ukr.net", 9 | "url": "https://github.com/kievsash" 10 | }, 11 | "homepage": "https://github.com/kievsash/rxjs-toolbox", 12 | "bugs": "https://github.com/kievsash/rxjs-toolbox/issues", 13 | "repository": { 14 | "type": "git", 15 | "url": "git+https://github.com/kievsash/rxjs-toolbox.git" 16 | }, 17 | "publishConfig": { 18 | "registry": "https://registry.npmjs.org/", 19 | "tag": "latest" 20 | }, 21 | "keywords": [ 22 | "forkJoinTransparent", 23 | "timeRange", 24 | "finalizeWithValue", 25 | "rxjs", 26 | "operator", 27 | "operators", 28 | "combination" 29 | ], 30 | "peerDependencies": { 31 | "rxjs": "^6.0.0" 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/lib/forkJoinWithProgress.ts: -------------------------------------------------------------------------------- 1 | import {defer, forkJoin, Observable, of, Subject} from 'rxjs'; 2 | import {finalize, tap} from 'rxjs/operators'; 3 | 4 | export function forkJoinWithProgress(arrayOfObservables: any[]): Observable { 5 | 6 | return defer(() => { 7 | let counter: number = 0; 8 | const percent$: Subject = new Subject(); 9 | 10 | const modilefiedObservablesList: Observable[] = arrayOfObservables.map( 11 | (item, index) => item.pipe( 12 | finalize(() => { 13 | const percentValue: number = ++counter * 100 / arrayOfObservables.length; 14 | percent$.next(percentValue); 15 | }))); 16 | 17 | const finalResult$: Observable = forkJoin(modilefiedObservablesList).pipe( 18 | tap(() => { 19 | percent$.next(100); 20 | percent$.complete(); 21 | })); 22 | 23 | return of([finalResult$, percent$.asObservable()]); 24 | }); 25 | 26 | } 27 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "private": true, 3 | "version": "16.0.0", 4 | "scripts": { 5 | "build": "ng-packagr -p src/ng-package.json", 6 | "postbuild": "npm run build:readme", 7 | "build:readme": "copyfiles README.md dist/lib", 8 | "lint": "tslint --project tsconfig.json" 9 | }, 10 | "jest": { 11 | "collectCoverage": false, 12 | "moduleFileExtensions": [ 13 | "ts", 14 | "js", 15 | "json" 16 | ], 17 | "transform": { 18 | "^.+\\.ts$": "/node_modules/ts-jest/preprocessor.js" 19 | }, 20 | "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(js|ts)$" 21 | }, 22 | "dependencies": { 23 | "rxjs": "^7.8.0" 24 | }, 25 | "devDependencies": { 26 | "@angular/compiler": "^16.0.5", 27 | "@angular/compiler-cli": "^16.0.5", 28 | "@angular/core": "^16.0.5", 29 | "@types/node": "^8.10.21", 30 | "copyfiles": "^2.1.0", 31 | "ng-packagr": "^16.0.5", 32 | "ts-node": "^10.9.1", 33 | "tslint": "^6.1.3", 34 | "typescript": "5.0.4", 35 | "zone.js": "^0.13.0" 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/lib/helpers/timeRange.ts: -------------------------------------------------------------------------------- 1 | import {concat, merge, Observable, of} from 'rxjs'; 2 | import {delay} from 'rxjs/operators'; 3 | 4 | // timeRange - Function to create Observable that will emit values with specified delays 5 | // Params: 6 | // range - array of objects with special structure [{value: , delay: },...] 7 | // isRelative - if true = next emissions is scheduled only after previous is complete (so delays are summarized) 8 | // if false - all values are scheduled at once (delay values are absolute in relation to the moment of subscription) 9 | // 10 | // Usage 11 | // timeRange([ 12 | // {value: 15, delay: 1500}, // 1500ms 13 | // {value: 15, delay: 2500} // 2500ms 14 | // ]) 15 | // 16 | // timeRange([ 17 | // {value: 15, delay: 1500}, // 1500ms 18 | // {value: 15, delay: 2500} // 1500+2500 19 | // ], true); 20 | 21 | export function timeRange(range: any[], isRelative: boolean = false): Observable { 22 | const obsArray: Observable[] = range.map(item => of(item.value).pipe(delay(item.delay))); 23 | return isRelative ? concat(...obsArray) : merge(...obsArray); 24 | } 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Lars Gyrup Brink Nielsen 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RxJS-toolbox - set of custom operators and handy factory functions for RxJS 2 | 3 | *Note: updated to Angular 16.x in version 2.2.0 4 | 5 | ## Installation 6 | Install using NPM CLI 7 | ``` 8 | npm install --save rxjs-toolbox 9 | ``` 10 | 11 | ### forkJoin-transparent 12 | A combination operator that combines multiple sources and returns their last emitted data as well as percentage of their completion. 13 | 14 | ### Usage 15 | 16 | #### forkJoinWithProgress 17 | ```typescript 18 | import { ajax } from 'rxjs/ajax'; 19 | import { merge } from 'rxjs'; 20 | `import { forkJoinWithProgress } from 'rxjs-toolbox';` 21 | import {tap, mergeMap, ignoreElements} from 'rxjs/operators'; 22 | 23 | const getUserDetails = userIdsList => { 24 | 25 | const arrayOfObservables = userIdsList.map(userId => 26 | ajax('https://jsonplaceholder.typicode.com/comments/' + userId) 27 | ) 28 | 29 | return forkJoinWithProgress(arrayOfObservables) 30 | } 31 | 32 | 33 | const result$ = getUserDetails([1, 2, 15]); 34 | 35 | result$.pipe( 36 | mergeMap(([finalResult, progress]) => merge( 37 | progress.pipe( 38 | tap((value) => console.log(`${value} completed`)), 39 | ignoreElements() 40 | ), 41 | finalResult 42 | )) 43 | ).subscribe(values => console.log(values), console.warn); 44 | 45 | // Output: 46 | // 33.333333333333336 completed 47 | // 66.66666666666667 completed 48 | // 100 completed 49 | // final value: (3) [{…}, {…}, {…}] 50 | ``` 51 | 52 | #### waitUntil 53 | I found an interesting #rxjs custom operator in [auth0-angular interceptor](https://github.com/auth0/auth0-angular/blob/v2.2.3/projects/auth0-angular/src/lib/auth.interceptor.ts) codebase: waitUntil 54 | 55 | It holds until the param observable emits first value- and then switches to the source 56 | 57 | ######Params: 58 | isLoaded$ 59 | ```typescript 60 | isLoaded$ // - some observable we wait for 61 | ... 62 | of(route).pipe( 63 | waitUntil(isLoaded$) 64 | ) 65 | ... 66 | ``` 67 | 68 | #### Helper functions 69 | ##### timeRange 70 | Function to create Observable that will emit values with specified delays 71 | ######Params: 72 | *range* - array of objects with special structure [{value: , delay: },...] 73 | 74 | *isRelative* - if true = next emissions is scheduled only after previous is complete (so delays are summarized). 75 | 76 | if false - all values are scheduled at once (delay values are absolute in relation to the moment of subscription) 77 | 78 | ```typescript 79 | const range$ = timeRange([ 80 | {value: 15, delay: 1500}, // 1500ms 81 | {value: 15, delay: 2500} // 2500ms 82 | ]) 83 | 84 | const range2$ = timeRange([ 85 | {value: 15, delay: 1500}, // 1500ms 86 | {value: 15, delay: 2500} // 1500+2500 87 | ], true); 88 | 89 | ``` 90 | 91 | ##### finalizeWithValue 92 | Provides (unlike original [finalize](https://rxjs.dev/api/operators/finalize) from RxJS) source$'s last emitted value (if any) in format {value: }. 93 | 94 | If source$ completes with noe emitted value - provides undefined. 95 | 96 | Author - Ben Lesh, taken [here](https://github.com/ReactiveX/rxjs/issues/4803#issuecomment-496711335) 97 | 98 | ```typescript 99 | 100 | from([1,3]).pipe( 101 | finalizeWithValue((lastEmittedValue) => console.log(lastEmittedValue)) // 3 102 | ) 103 | 104 | ``` 105 | 106 | ### Want to learn RxJS? 107 | Try my ["Hands-on RxJS for Web Development"](https://www.udemy.com/course/hands-on-rxjs-for-web-development/) video-course! 108 | -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "tslint:latest", 4 | "tslint-eslint-rules" 5 | ], 6 | "rules": { 7 | "arrow-return-shorthand": true, 8 | "callable-types": true, 9 | "class-name": true, 10 | "comment-format": [ 11 | true, 12 | "check-space" 13 | ], 14 | "curly": true, 15 | "deprecation": { 16 | "severity": "warn" 17 | }, 18 | "eofline": true, 19 | "forin": true, 20 | "import-blacklist": [ 21 | true, 22 | "rxjs/Rx" 23 | ], 24 | "import-spacing": true, 25 | "indent": [ 26 | true, 27 | "spaces" 28 | ], 29 | "interface-over-type-literal": true, 30 | "label-position": true, 31 | "max-line-length": [ 32 | true, 33 | 140 34 | ], 35 | "member-access": false, 36 | "member-ordering": [ 37 | true, 38 | { 39 | "order": [ 40 | "static-field", 41 | "instance-field", 42 | "static-method", 43 | "instance-method" 44 | ] 45 | } 46 | ], 47 | "no-arg": true, 48 | "no-bitwise": true, 49 | "no-console": [ 50 | true, 51 | "debug", 52 | "info", 53 | "time", 54 | "timeEnd", 55 | "trace" 56 | ], 57 | "no-construct": true, 58 | "no-debugger": true, 59 | "no-duplicate-super": true, 60 | "no-empty": false, 61 | "no-empty-interface": true, 62 | "no-eval": true, 63 | "no-misused-new": true, 64 | "no-shadowed-variable": true, 65 | "no-string-literal": false, 66 | "no-string-throw": true, 67 | "no-switch-case-fall-through": true, 68 | "no-trailing-whitespace": true, 69 | "no-unnecessary-initializer": true, 70 | "no-var-keyword": true, 71 | "one-line": [ 72 | true, 73 | "check-open-brace", 74 | "check-catch", 75 | "check-else", 76 | "check-whitespace" 77 | ], 78 | "prefer-const": true, 79 | "radix": true, 80 | "semicolon": [ 81 | true, 82 | "always" 83 | ], 84 | "triple-equals": [ 85 | true, 86 | "allow-null-check" 87 | ], 88 | "typedef-whitespace": [ 89 | true, 90 | { 91 | "call-signature": "nospace", 92 | "index-signature": "nospace", 93 | "parameter": "nospace", 94 | "property-declaration": "nospace", 95 | "variable-declaration": "nospace" 96 | } 97 | ], 98 | "unified-signatures": true, 99 | "variable-name": false, 100 | "whitespace": [ 101 | true, 102 | "check-branch", 103 | "check-decl", 104 | "check-operator", 105 | "check-separator", 106 | "check-type" 107 | ], 108 | "typedef": [ 109 | true, 110 | "call-signature", 111 | "arrow-call-signature", 112 | "parameter", 113 | "variable-declaration" 114 | ], 115 | "array-type": [ 116 | true, 117 | "array" 118 | ], 119 | "arrow-parens": [ 120 | true, 121 | "ban-single-arg-parens" 122 | ], 123 | "interface-name": [ 124 | true, 125 | "never-prefix" 126 | ], 127 | "new-parens": true, 128 | "no-conditional-assignment": true, 129 | "no-consecutive-blank-lines": true, 130 | "no-submodule-imports": [ 131 | true, 132 | "@angular/common", 133 | "@angular/core", 134 | "@angular/platform-browser", 135 | "@angular/platform-browser-dynamic", 136 | "core-js", 137 | "rxjs/operators", 138 | "zone.js/dist" 139 | ], 140 | "object-literal-shorthand": false, 141 | "object-literal-sort-keys": [ 142 | true, 143 | "ignore-case" 144 | ], 145 | "quotemark": [ 146 | true, 147 | "single", 148 | "avoid-escape", 149 | "avoid-template" 150 | ], 151 | "space-before-function-paren": [ 152 | true, 153 | { 154 | "anonymous": "always" 155 | } 156 | ], 157 | "ter-max-len": [ 158 | true, 159 | 140, 160 | { 161 | "ignoreRegExpLiterals": true, 162 | "ignoreStrings": true, 163 | "ignoreTemplateLiterals": true, 164 | "ignoreUrls": true 165 | } 166 | ], 167 | "trailing-comma": [ 168 | true, 169 | { 170 | "multiline": "always", 171 | "singleline": "never" 172 | } 173 | ], 174 | "no-implicit-dependencies": [ 175 | false, 176 | "dev" 177 | ], 178 | "no-inferrable-types": false, 179 | "no-non-null-assertion": false, 180 | "no-unused-expression": false, 181 | "no-use-before-declare": false 182 | } 183 | } 184 | --------------------------------------------------------------------------------