# propserve

> **propserve** let's you subscribe to changes of other properties of the same class by using decorators.

Latest version **0.1.1** (published 2020-08-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install propserve
pnpm add propserve
yarn add propserve
bun add propserve
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.1 |
| Published | 2020-08-17 |
| First published | 2019-12-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Aleix Casanovas |
| Maintainers | aleics |

## Links

- npm: https://www.npmjs.com/package/propserve
- npm.io page: https://npm.io/package/propserve

## Dependencies (1)

- [rxjs](https://npm.io/package/rxjs.md) ^6.5.3

## Recent versions

- 0.1.1 (latest) — 2020-08-17
- 0.1.0 — 2019-12-18

## README

# propserve
**propserve** let's you subscribe to changes of other properties of the same class by using decorators.

## Usage
Using the [`@ObserveOn`](https://github.com/aleics/propserve/blob/397fc239a3bbcc8242313141f057c0d42f8d3c5e/src/observe.ts#L3) decorator, an observer property is defined. The observer is able to subscribe to changes of other properties.

```ts
class Test {
  @ObserveOn<number>('bar') foo!: Observable<number>;
  bar: number = 1;
}

const test = new Test();
test.foo.subscribe(value => {
  console.log('Received: ' + value);
});
```

### Use case: Angular
The main motivation of this library was the lack to provide [lifecycle hooks](https://angular.io/guide/lifecycle-hooks) as [`Observable`](https://rxjs.dev/guide/observable) in the Angular framework. Specifically for [`OnChanges`](https://angular.io/api/core/OnChanges). The different component's lifecycle phases are provided by using callback methods. For instance:

```ts
@Component({
  selector: 'some',
  templateUrl: './some.component.html'
})
export class SomeComponent implements OnChanges {
  @Input() foo: number;
  @Input() bar: number;

  result: number;

  ngOnChanges(changes: SimpleChanges) {
    if (changes.foo || changes.bar) {
      const sum = this.foo * this.bar;
      if (sum > 0) {
        this.result = sum;
      }
    }
  }
}
```

Two inputs are multiplied, and if the result is higher than `0`, the result is displayed.

Using **propserve**, you can react to the changes of a single or multiple component's properties, and use the full power of reactive streams:

```ts
@Component({
  selector: 'some',
  templateUrl: './some.component.html'
})
export class SomeComponent {
  @Input() foo: number;
  @ObserveOn<number>('foo') fooChanges$!: Observable<number>;

  @Input() bar: number;
  @ObserveOn<number>('bar') barChanges$!: Observable<number>;

  result$ = combineLatest(this.fooChanges$, this.barChanges$).pipe(
    map(([first, second]) => first * second),
    filter(value => value > 0)
  );
}
```

By using the Angular [`async`](https://angular.io/api/common/AsyncPipe) pipe in the template, you can extract the value of `result$` once the `Observable` returns a value. Thus, the handling of both properties' changes is much simplified and functional-like.

---
_Source: https://npm.io/package/propserve · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
