├── _post_build.ts ├── projects └── rx-service │ ├── src │ ├── public-api.ts │ ├── lib │ │ ├── rx-cleanup.ts │ │ ├── utils.ts │ │ ├── rx-cleanup.spec.ts │ │ ├── rx-service.ts │ │ └── rx-service.spec.ts │ └── test.ts │ ├── ng-package.json │ ├── tsconfig.lib.prod.json │ ├── tslint.json │ ├── tsconfig.spec.json │ ├── tsconfig.lib.json │ ├── package.json │ └── karma.conf.js ├── .editorconfig ├── CHANGELOG.md ├── .gitignore ├── .github └── workflows │ └── npm-publish.yml ├── tsconfig.json ├── LICENSE ├── angular.json ├── package.json ├── README.md └── tslint.json /_post_build.ts: -------------------------------------------------------------------------------- 1 | const fs = require("fs"); 2 | // add additional files 3 | fs.copyFileSync("README.md", "dist/rx-service/README.md"); 4 | -------------------------------------------------------------------------------- /projects/rx-service/src/public-api.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * Public API Surface of rx-store 3 | */ 4 | 5 | export * from './lib/rx-service'; 6 | export * from './lib/rx-cleanup'; 7 | 8 | -------------------------------------------------------------------------------- /projects/rx-service/ng-package.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "../../node_modules/ng-packagr/ng-package.schema.json", 3 | "dest": "../../dist/rx-service", 4 | "lib": { 5 | "entryFile": "src/public-api.ts" 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /projects/rx-service/tsconfig.lib.prod.json: -------------------------------------------------------------------------------- 1 | /* To learn more about this file see: https://angular.io/config/tsconfig. */ 2 | { 3 | "extends": "./tsconfig.lib.json", 4 | "compilerOptions": { 5 | "declarationMap": false 6 | }, 7 | "angularCompilerOptions": { 8 | "compilationMode": "partial" 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # Editor configuration, see https://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 | trim_trailing_whitespace = true 10 | 11 | [*.ts] 12 | quote_type = single 13 | 14 | [*.md] 15 | max_line_length = off 16 | trim_trailing_whitespace = false 17 | -------------------------------------------------------------------------------- /projects/rx-service/src/lib/rx-cleanup.ts: -------------------------------------------------------------------------------- 1 | import { Injectable, OnDestroy } from '@angular/core'; 2 | import { Subject } from 'rxjs'; 3 | 4 | @Injectable({ 5 | providedIn: 'root', 6 | }) 7 | export class RxCleanup extends Subject implements OnDestroy { 8 | ngOnDestroy(): void { 9 | this.next(); 10 | this.complete(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /projects/rx-service/tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../../tslint.json", 3 | "rules": { 4 | "directive-selector": [ 5 | true, 6 | "attribute", 7 | "lib", 8 | "camelCase" 9 | ], 10 | "component-selector": [ 11 | true, 12 | "element", 13 | "lib", 14 | "kebab-case" 15 | ] 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /projects/rx-service/src/lib/utils.ts: -------------------------------------------------------------------------------- 1 | export function isFunction(value: T): value is T { 2 | return typeof value === 'function'; 3 | } 4 | 5 | export function isObject(value: T): boolean { 6 | return ( 7 | value && value instanceof Object && !isArray(value) && !isFunction(value) 8 | ); 9 | } 10 | 11 | export function isArray(value: T): value is T { 12 | return Array.isArray(value); 13 | } 14 | -------------------------------------------------------------------------------- /projects/rx-service/tsconfig.spec.json: -------------------------------------------------------------------------------- 1 | /* To learn more about this file see: https://angular.io/config/tsconfig. */ 2 | { 3 | "extends": "../../tsconfig.json", 4 | "compilerOptions": { 5 | "outDir": "../../out-tsc/spec", 6 | "types": [ 7 | "jasmine" 8 | ] 9 | }, 10 | "files": [ 11 | "src/test.ts" 12 | ], 13 | "include": [ 14 | "**/*.spec.ts", 15 | "**/*.d.ts" 16 | ] 17 | } 18 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | ## [0.3.0] - 12 Dec 2022 3 | ### Breaking 4 | - Rename RxService to Rx 5 | ### Feature 6 | - Added RxCleanup 7 | ### Maintenance 8 | - Updated dependencies 9 | 10 | ## [0.2.0] - 05 Nov 2021 11 | ### Maintenance 12 | - Update dependencies 13 | 14 | ## [0.1.0] - 14 May 2021 15 | 16 | ### Maintenance 17 | - Update dependencies 18 | 19 | ## [0.0.9] - 27 Aug 2020 20 | 21 | - Published initial version 22 | -------------------------------------------------------------------------------- /projects/rx-service/src/test.ts: -------------------------------------------------------------------------------- 1 | // This file is required by karma.conf.js and loads recursively all the .spec and framework files 2 | import 'zone.js/dist/zone'; 3 | import 'zone.js/dist/zone-testing'; 4 | import { getTestBed } from '@angular/core/testing'; 5 | import { 6 | BrowserDynamicTestingModule, 7 | platformBrowserDynamicTesting 8 | } from '@angular/platform-browser-dynamic/testing'; 9 | 10 | // First, initialize the Angular testing environment. 11 | getTestBed().initTestEnvironment( 12 | BrowserDynamicTestingModule, 13 | platformBrowserDynamicTesting(), { 14 | teardown: { destroyAfterEach: false } 15 | } 16 | ); 17 | -------------------------------------------------------------------------------- /projects/rx-service/tsconfig.lib.json: -------------------------------------------------------------------------------- 1 | /* To learn more about this file see: https://angular.io/config/tsconfig. */ 2 | { 3 | "extends": "../../tsconfig.json", 4 | "compilerOptions": { 5 | "outDir": "../../out-tsc/lib", 6 | "declarationMap": true, 7 | "declaration": true, 8 | "inlineSources": true, 9 | "types": [], 10 | "lib": [ 11 | "dom", 12 | "es2018" 13 | ] 14 | }, 15 | "angularCompilerOptions": { 16 | "skipTemplateCodegen": true, 17 | "strictMetadataEmit": true, 18 | "enableResourceInlining": true 19 | }, 20 | "exclude": [ 21 | "src/test.ts", 22 | "**/*.spec.ts" 23 | ] 24 | } 25 | -------------------------------------------------------------------------------- /projects/rx-service/src/lib/rx-cleanup.spec.ts: -------------------------------------------------------------------------------- 1 | import { TestBed } from '@angular/core/testing'; 2 | import { RxCleanup } from './rx-cleanup'; 3 | 4 | describe('RxCleanup', () => { 5 | let service: RxCleanup; 6 | 7 | beforeEach(() => { 8 | TestBed.configureTestingModule({}); 9 | service = TestBed.inject(RxCleanup); 10 | }); 11 | 12 | it('should be created', () => { 13 | expect(service).toBeTruthy(); 14 | }); 15 | 16 | it('should emit a value on destruction', (done) => { 17 | service.subscribe(() => { 18 | done(); 19 | }); 20 | service.ngOnDestroy(); 21 | }); 22 | 23 | it('should complete on destruction', (done) => { 24 | service.subscribe({ 25 | complete: () => { 26 | done(); 27 | }, 28 | }); 29 | service.ngOnDestroy(); 30 | }); 31 | }); 32 | -------------------------------------------------------------------------------- /.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 | # Only exists if Bazel was run 8 | /bazel-out 9 | 10 | # dependencies 11 | node_modules 12 | 13 | # profiling files 14 | chrome-profiler-events*.json 15 | speed-measure-plugin*.json 16 | 17 | # IDEs and editors 18 | /.idea 19 | .project 20 | .classpath 21 | .c9/ 22 | *.launch 23 | .settings/ 24 | *.sublime-workspace 25 | 26 | # IDE - VSCode 27 | .vscode/* 28 | !.vscode/settings.json 29 | !.vscode/tasks.json 30 | !.vscode/launch.json 31 | !.vscode/extensions.json 32 | .history/* 33 | 34 | # misc 35 | /.angular/cache 36 | /.sass-cache 37 | /connect.lock 38 | /coverage 39 | /libpeerconnection.log 40 | npm-debug.log 41 | yarn-error.log 42 | testem.log 43 | /typings 44 | __ngcc_entry_points__.json 45 | 46 | # System Files 47 | .DS_Store 48 | Thumbs.db 49 | -------------------------------------------------------------------------------- /projects/rx-service/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rx-service", 3 | "version": "0.3.0", 4 | "description": "Simple RxJS BehaviorSubject wrapper that simplifies component communication in your applications", 5 | "author": "Angular Consulting", 6 | "license": "MIT", 7 | "repository": { 8 | "type": "git", 9 | "url": "https://github.com/angularconsulting/rx-service.git" 10 | }, 11 | "bugs": { 12 | "url": "https://github.com/angularconsulting/rx-service/issues" 13 | }, 14 | "homepage": "https://github.com/angularconsulting/rx-service#readme", 15 | "keywords": [ 16 | "reactive-services", 17 | "state-management", 18 | "component-communication", 19 | "rxjs", 20 | "services", 21 | "reactive", 22 | "rx" 23 | ], 24 | "peerDependencies": { 25 | "rxjs": "^6.5.0 || ^7.5.0" 26 | }, 27 | "dependencies": { 28 | "tslib": "^2.3.0" 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /projects/rx-service/src/lib/rx-service.ts: -------------------------------------------------------------------------------- 1 | import { BehaviorSubject, Observable } from 'rxjs'; 2 | import { isFunction } from './utils'; 3 | 4 | export abstract class Rx { 5 | private localState$: BehaviorSubject; 6 | private default: T; 7 | 8 | protected constructor(defaults: T) { 9 | this.localState$ = new BehaviorSubject(defaults); 10 | this.default = JSON.parse(JSON.stringify(defaults)); 11 | } 12 | 13 | public get state$(): Observable { 14 | return this.localState$.asObservable(); 15 | } 16 | 17 | public setState(state: ((prevState: T) => Partial) | Partial): void { 18 | if (isFunction(state)) { 19 | state = (state as (prevState: T) => Partial)(this.getState()); 20 | this.localState$.next(state as T); 21 | } else { 22 | this.localState$.next(state); 23 | } 24 | } 25 | 26 | public getState(): T { 27 | return this.localState$.getValue(); 28 | } 29 | 30 | public resetState(): void { 31 | this.localState$.next(this.default); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /.github/workflows/npm-publish.yml: -------------------------------------------------------------------------------- 1 | name: npm-publish 2 | on: 3 | push: 4 | branches: 5 | - master # Change this to your default branch 6 | jobs: 7 | npm-publish: 8 | name: npm-publish 9 | runs-on: ubuntu-latest 10 | steps: 11 | - name: Checkout repository 12 | uses: actions/checkout@master 13 | - name: Set up Node.js 14 | uses: actions/setup-node@master 15 | with: 16 | node-version: 10.0.0 17 | - name: Publish if version has been updated 18 | uses: pascalgn/npm-publish-action@06e0830ea83eea10ed4a62654eeaedafb8bf50fc 19 | with: # All of theses inputs are optional 20 | tag_name: "v%s" 21 | tag_message: "v%s" 22 | commit_pattern: "^Release (\\S+)" 23 | workspace: "dist/rx-service" 24 | env: # More info about the environment variables in the README 25 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Leave this as is, it's automatically generated 26 | NPM_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }} # You need to set this in your repo settings 27 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compileOnSave": false, 3 | "compilerOptions": { 4 | "baseUrl": "./", 5 | "outDir": "./dist/out-tsc", 6 | "forceConsistentCasingInFileNames": true, 7 | "strict": true, 8 | "noImplicitOverride": true, 9 | "noPropertyAccessFromIndexSignature": true, 10 | "noImplicitReturns": true, 11 | "noFallthroughCasesInSwitch": true, 12 | "sourceMap": true, 13 | "declaration": false, 14 | "downlevelIteration": true, 15 | "experimentalDecorators": true, 16 | "moduleResolution": "node", 17 | "importHelpers": true, 18 | "target": "ES2022", 19 | "module": "ES2022", 20 | "useDefineForClassFields": false, 21 | "lib": ["ES2022", "dom"], 22 | "paths": { 23 | "rx-service": [ 24 | "dist/rx-service/rx-service", 25 | "dist/rx-service" 26 | ] 27 | } 28 | }, 29 | "angularCompilerOptions": { 30 | "enableI18nLegacyMessageIdFormat": false, 31 | "strictInjectionParameters": true, 32 | "strictInputAccessModifiers": true, 33 | "strictTemplates": true 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Angular Consulting, angularconsulting.au 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 | -------------------------------------------------------------------------------- /projects/rx-service/karma.conf.js: -------------------------------------------------------------------------------- 1 | // Karma configuration file, see link for more information 2 | // https://karma-runner.github.io/1.0/config/configuration-file.html 3 | 4 | module.exports = function (config) { 5 | config.set({ 6 | basePath: '', 7 | frameworks: ['jasmine', '@angular-devkit/build-angular'], 8 | plugins: [ 9 | require('karma-jasmine'), 10 | require('karma-chrome-launcher'), 11 | require('karma-jasmine-html-reporter'), 12 | require('karma-coverage-istanbul-reporter'), 13 | require('@angular-devkit/build-angular/plugins/karma') 14 | ], 15 | client: { 16 | clearContext: false // leave Jasmine Spec Runner output visible in browser 17 | }, 18 | coverageIstanbulReporter: { 19 | dir: require('path').join(__dirname, '../../coverage/rx-service'), 20 | reports: ['html', 'lcovonly', 'text-summary'], 21 | fixWebpackSourcePaths: true 22 | }, 23 | reporters: ['progress', 'kjhtml'], 24 | port: 9876, 25 | colors: true, 26 | logLevel: config.LOG_INFO, 27 | autoWatch: true, 28 | browsers: ['Chrome'], 29 | singleRun: false, 30 | restartOnFileChange: true 31 | }); 32 | }; 33 | -------------------------------------------------------------------------------- /angular.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "./node_modules/@angular/cli/lib/config/schema.json", 3 | "version": 1, 4 | "newProjectRoot": "projects", 5 | "projects": { 6 | "rx-service": { 7 | "projectType": "library", 8 | "root": "projects/rx-service", 9 | "sourceRoot": "projects/rx-service/src", 10 | "prefix": "lib", 11 | "architect": { 12 | "build": { 13 | "builder": "@angular-devkit/build-angular:ng-packagr", 14 | "options": { 15 | "tsConfig": "projects/rx-service/tsconfig.lib.json", 16 | "project": "projects/rx-service/ng-package.json" 17 | }, 18 | "configurations": { 19 | "production": { 20 | "tsConfig": "projects/rx-service/tsconfig.lib.prod.json" 21 | } 22 | } 23 | }, 24 | "test": { 25 | "builder": "@angular-devkit/build-angular:karma", 26 | "options": { 27 | "main": "projects/rx-service/src/test.ts", 28 | "tsConfig": "projects/rx-service/tsconfig.spec.json", 29 | "karmaConfig": "projects/rx-service/karma.conf.js" 30 | } 31 | } 32 | } 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rx-service", 3 | "version": "0.3.0", 4 | "scripts": { 5 | "ng": "ng", 6 | "start": "ng serve", 7 | "build": "ng build && ts-node _post_build.ts", 8 | "test": "ng test", 9 | "lint": "ng lint", 10 | "e2e": "ng e2e", 11 | "prepublish": "ng build --configuration production && ts-node _post_build.ts" 12 | }, 13 | "private": true, 14 | "dependencies": { 15 | "rxjs": "~7.8.0" 16 | }, 17 | "devDependencies": { 18 | "@angular-devkit/build-angular": "^15.2.0", 19 | "@angular/cli": "^15.2.0", 20 | "@angular/common": "^15.2.0", 21 | "@angular/compiler": "^15.2.0", 22 | "@angular/compiler-cli": "^15.2.0", 23 | "@angular/core": "^15.2.0", 24 | "@angular/platform-browser": "^15.2.0", 25 | "@angular/platform-browser-dynamic": "^15.2.0", 26 | "@types/jasmine": "~4.3.1", 27 | "@types/jasminewd2": "~2.0.10", 28 | "@types/node": "^18.14.1", 29 | "codelyzer": "^6.0.2", 30 | "jasmine-core": "~4.5.0", 31 | "jasmine-spec-reporter": "~7.0.0", 32 | "karma": "~6.4.1", 33 | "karma-chrome-launcher": "~3.1.1", 34 | "karma-coverage-istanbul-reporter": "~3.0.2", 35 | "karma-jasmine": "~5.1.0", 36 | "karma-jasmine-html-reporter": "^2.0.0", 37 | "ng-packagr": "^15.2.2", 38 | "protractor": "~7.0.0", 39 | "ts-node": "~10.9.1", 40 | "tslint": "~6.1.3", 41 | "typescript": "~4.9.4", 42 | "zone.js": "^0.12.0" 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /projects/rx-service/src/lib/rx-service.spec.ts: -------------------------------------------------------------------------------- 1 | import { TestBed } from '@angular/core/testing'; 2 | import { Rx } from './rx-service'; 3 | 4 | interface Counter { 5 | value: number; 6 | } 7 | 8 | const initialState: Counter = { value: 0 }; 9 | 10 | class CounterService extends Rx { 11 | constructor() { 12 | super(initialState); 13 | } 14 | } 15 | 16 | describe('Rx (RxService)', () => { 17 | let service: CounterService; 18 | 19 | beforeEach(() => { 20 | TestBed.configureTestingModule({ 21 | providers: [{ provide: CounterService }], 22 | }); 23 | service = TestBed.inject(CounterService); 24 | }); 25 | 26 | it('should be created', () => { 27 | expect(service).toBeTruthy(); 28 | }); 29 | 30 | it('should return 0', () => { 31 | expect(service.getState().value).toEqual(0); 32 | }); 33 | 34 | it('should increase count to 1', () => { 35 | service.setState((old) => ({ value: old.value + 1 })); 36 | expect(service.getState().value).toEqual(1); 37 | }); 38 | 39 | it('should reset to initial', () => { 40 | service.setState((old) => ({ value: old.value + 1 })); 41 | service.resetState(); 42 | expect(service.getState().value).toEqual(0); 43 | }); 44 | 45 | it('should reset count', () => { 46 | const state = service.getState(); 47 | service.setState({ value: state.value + 1 }); 48 | service.resetState(); 49 | expect(service.getState().value).toEqual(0); 50 | }); 51 | }); 52 | 53 | class PrimitiveService extends Rx { 54 | constructor() { 55 | super(0); 56 | } 57 | } 58 | 59 | describe('Rx (RxService)', () => { 60 | let service: PrimitiveService; 61 | 62 | beforeEach(() => { 63 | TestBed.configureTestingModule({ 64 | providers: [{ provide: PrimitiveService }], 65 | }); 66 | service = TestBed.inject(PrimitiveService); 67 | }); 68 | 69 | it('should be created', () => { 70 | expect(service).toBeTruthy(); 71 | }); 72 | 73 | it('should return 0', () => { 74 | expect(service.getState()).toEqual(0); 75 | }); 76 | 77 | it('should increase count to 1', () => { 78 | const state = service.getState(); 79 | service.setState(state + 1); 80 | expect(service.getState()).toEqual(1); 81 | }); 82 | 83 | it('should reset to initial', () => { 84 | service.setState(1); 85 | service.resetState(); 86 | expect(service.getState()).toEqual(0); 87 | }); 88 | 89 | it('should reset count', () => { 90 | const state = service.getState(); 91 | service.setState(state + 1); 92 | service.resetState(); 93 | expect(service.getState()).toEqual(0); 94 | }); 95 | }); 96 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 🔥 Rx Service 2 | 3 | Enhance your application services with Rx Service. This is a simple yet powerful library that adds reactivity and consistency to your services while streamlining component communication within your application using the reliable RxJS BehaviorSubject 🐱🦸‍♂️ 4 | 5 | ## 👨‍💻 Example 6 | 7 | ### service 8 | ``` typescript 9 | import { Injectable } from '@angular/core'; 10 | import { Rx } from 'rx-service'; 11 | 12 | interface Counter { 13 | value: number; 14 | } 15 | 16 | const initialState: Counter = { value: 0 }; 17 | 18 | @Injectable({ 19 | providedIn: 'root', 20 | }) 21 | export class CounterService extends Rx { 22 | constructor() { 23 | super(initialState); 24 | } 25 | } 26 | ``` 27 | ### component class 28 | ``` typescript 29 | import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core'; 30 | import { Observable } from 'rxjs'; 31 | import { map } from 'rxjs/operators'; 32 | import { CounterService } from './counter.service'; 33 | 34 | @Component({ 35 | selector: 'app-root', 36 | templateUrl: './app.component.html', 37 | styleUrls: ['./app.component.css'], 38 | changeDetection: ChangeDetectionStrategy.OnPush, 39 | }) 40 | export class AppComponent implements OnInit { 41 | counter$!: Observable; 42 | constructor(private service: CounterService) {} 43 | 44 | ngOnInit(): void { 45 | this.counter$ = this.service.state$.pipe(map((x) => x.value)); 46 | } 47 | 48 | update(value: number): void { 49 | this.service.setState((state) => ({ value: state.value + value })); 50 | } 51 | } 52 | ``` 53 | ### component template 54 | ``` html 55 | 56 | {{ counter$ | async }} 57 | 58 | ``` 59 | 60 | ## 💡 Also you can just use primitives 61 | ``` typescript 62 | import { Rx } from "rx-service"; 63 | 64 | const initialState = 0; 65 | 66 | export class CounterService extends Rx { 67 | constructor() { 68 | super(initialState); 69 | } 70 | } 71 | ``` 72 | 73 | ## 🧹 Clean up subscriptions for edge cases 74 | ``` typescript 75 | 76 | import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core'; 77 | import { RxCleanup } from 'rx-service'; 78 | import { takeUntil } from 'rxjs'; 79 | import { CounterService } from './counter.service'; 80 | 81 | @Component({ 82 | selector: 'app-root', 83 | templateUrl: './app.component.html', 84 | styleUrls: ['./app.component.css'], 85 | changeDetection: ChangeDetectionStrategy.OnPush, 86 | }) 87 | export class AppComponent implements OnInit { 88 | constructor( 89 | private service: CounterService, 90 | private readonly cleanup$: RxCleanup 91 | ) {} 92 | 93 | ngOnInit(): void { 94 | this.service.state$ 95 | .pipe( 96 | // more operators here 97 | takeUntil(this.cleanup$) 98 | ) 99 | .subscribe((result) => { 100 | // more magic here 101 | }); 102 | } 103 | } 104 | ``` 105 | 106 | ## 🧞‍♂️ Install 107 | ``` 108 | yarn add rx-service 109 | ``` 110 | or 111 | ``` 112 | npm i rx-service 113 | ``` 114 | created by [angularconsulting.au](https://angularconsulting.au) 115 | -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "tslint:recommended", 3 | "rulesDirectory": [ 4 | "codelyzer" 5 | ], 6 | "rules": { 7 | "align": { 8 | "options": [ 9 | "parameters", 10 | "statements" 11 | ] 12 | }, 13 | "array-type": false, 14 | "arrow-return-shorthand": true, 15 | "curly": true, 16 | "deprecation": { 17 | "severity": "warning" 18 | }, 19 | "eofline": true, 20 | "import-blacklist": [ 21 | true, 22 | "rxjs/Rx" 23 | ], 24 | "import-spacing": true, 25 | "indent": { 26 | "options": [ 27 | "spaces" 28 | ] 29 | }, 30 | "max-classes-per-file": false, 31 | "max-line-length": [ 32 | true, 33 | 140 34 | ], 35 | "member-ordering": [ 36 | true, 37 | { 38 | "order": [ 39 | "static-field", 40 | "instance-field", 41 | "static-method", 42 | "instance-method" 43 | ] 44 | } 45 | ], 46 | "no-any": true, 47 | "no-console": [ 48 | true, 49 | "debug", 50 | "info", 51 | "time", 52 | "timeEnd", 53 | "trace" 54 | ], 55 | "no-empty": false, 56 | "no-inferrable-types": [ 57 | true, 58 | "ignore-params" 59 | ], 60 | "no-non-null-assertion": true, 61 | "no-redundant-jsdoc": true, 62 | "no-switch-case-fall-through": true, 63 | "no-var-requires": false, 64 | "object-literal-key-quotes": [ 65 | true, 66 | "as-needed" 67 | ], 68 | "quotemark": [ 69 | true, 70 | "single" 71 | ], 72 | "semicolon": { 73 | "options": [ 74 | "always" 75 | ] 76 | }, 77 | "space-before-function-paren": { 78 | "options": { 79 | "anonymous": "never", 80 | "asyncArrow": "always", 81 | "constructor": "never", 82 | "method": "never", 83 | "named": "never" 84 | } 85 | }, 86 | "typedef": [ 87 | true, 88 | "call-signature" 89 | ], 90 | "typedef-whitespace": { 91 | "options": [ 92 | { 93 | "call-signature": "nospace", 94 | "index-signature": "nospace", 95 | "parameter": "nospace", 96 | "property-declaration": "nospace", 97 | "variable-declaration": "nospace" 98 | }, 99 | { 100 | "call-signature": "onespace", 101 | "index-signature": "onespace", 102 | "parameter": "onespace", 103 | "property-declaration": "onespace", 104 | "variable-declaration": "onespace" 105 | } 106 | ] 107 | }, 108 | "variable-name": { 109 | "options": [ 110 | "ban-keywords", 111 | "check-format", 112 | "allow-pascal-case" 113 | ] 114 | }, 115 | "whitespace": { 116 | "options": [ 117 | "check-branch", 118 | "check-decl", 119 | "check-operator", 120 | "check-separator", 121 | "check-type", 122 | "check-typecast" 123 | ] 124 | }, 125 | "component-class-suffix": true, 126 | "contextual-lifecycle": true, 127 | "directive-class-suffix": true, 128 | "no-conflicting-lifecycle": true, 129 | "no-host-metadata-property": true, 130 | "no-input-rename": true, 131 | "no-inputs-metadata-property": true, 132 | "no-output-native": true, 133 | "no-output-on-prefix": true, 134 | "no-output-rename": true, 135 | "no-outputs-metadata-property": true, 136 | "template-banana-in-box": true, 137 | "template-no-negated-async": true, 138 | "use-lifecycle-interface": true, 139 | "use-pipe-transform-interface": true 140 | } 141 | } 142 | --------------------------------------------------------------------------------