0.0.17 • Published 3 years ago

rxnotify v0.0.17

Weekly downloads
-
License
GPL-3.0
Repository
-
Last release
3 years ago

Description

A simple wrapper for notification/logging operations inside rxjs pipe

Basic usage

import { from } from 'rxjs';
import { Notifier } from './index';

const numbers = [1, 2, 3, 4, 5];

const notifier = Notifier.createNotifier();

from(numbers)
    .pipe(
        notifier.error('Test error'),
        notifier.success('Test success'),
        notifier.notify('Test notify'),
        notifier.warning('Test warning'),
    )
    .subscribe((res) => {
        console.log(res);
    });

Use with your logger/notification manager

import { from } from 'rxjs';
import { Notifier, INotifier } from './index';

export class ConsoleNotifier implements INotifier {
  error(...args): void {
    console.error(...args);
  }

  notify(...args): void {
    console.log(...args);
  }

  success(...args): void {
    console.info(...args);
  }

  warning(...args): void {
    console.warn(...args);
  }
}

const numbers = [1, 2, 3, 4, 5];

const notifier = Notifier.createNotifier(new ConsoleNotifier());

from(numbers)
    .pipe(
        notifier.error('Test error'),
        notifier.success('Test success'),
        notifier.notify('Test notify'),
        notifier.warning('Test warning'),
    )
    .subscribe((res) => {
        console.log(res);
    });

Use with angular material

// notifier.service.ts
import {Injectable} from "@angular/core";
import {MatSnackBar} from "@angular/material/snack-bar";
import {Notifier, INotifier} from "rxnotify";

class SnackbarNotifier implements INotifier {
  constructor(private _snackBar: MatSnackBar) {
  }

  error(message: string): void {
    this._snackBar.open(message, 'Close', {
      duration: 5000,
    });
  }

  notify(message: string): void {
    this._snackBar.open(message, 'Close', {
      duration: 5000,
    });
  }

  success(message: string): void {
    this._snackBar.open(message, 'Close', {
      duration: 5000,
    });
  }

  warning(message: string): void {
    this._snackBar.open(message, 'Close', {
      duration: 5000,
    });
  }
}

@Injectable()
export class NotifierService extends Notifier {
  constructor(private _snackBar: MatSnackBar) {
    super(new SnackbarNotifier(_snackBar));
  }
}

// app.controller.ts
import {Component, OnInit} from '@angular/core';
import {NotifierService} from "../services/notifier.service";
import { HttpClient } from '@angular/common/http';
import {catchError} from "rxjs/operators";
import {EMPTY} from "rxjs";

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
  title = 'angular-tour-of-heroes';

  constructor(private readonly notifier: NotifierService, private readonly httpClient: HttpClient) {
  }

  testFetch() {
    return this.httpClient.get('https://jsonplaceholder.typicode.com/todos/1').pipe(
      this.notifier.success("Request successful"),
      catchError(() => EMPTY.pipe(
        this.notifier.error("Request failure"),
      ))
    )
  }

  ngOnInit() {
    this.testFetch().subscribe((res) => {
      console.log(res);
    })
  }
}

Create your custom notifier

// A Notifier class for make observable notify method
class CustomNotifier extends Notifier {
  static CUSTOM_NOTIFIER = 'custom';

  custom = (...args: any[]) =>
    this.makeObservable(args, CustomNotifier.CUSTOM_NOTIFIER);
}

// A class with implementation of notification logic
class CustomConsoleNotifier extends ConsoleNotifier {
  custom(...args) {
    console.log('Test of custom');
  }
}

// Create
const customNotifier = Notifier.createCustomNotifier<CustomNotifier>(
  new CustomConsoleNotifier(),
  CustomNotifier,
);

from(numbers)
  .pipe(
    notifier.error('Test of error'),
    notifier.success('Test of success'),
    notifier.notify('Test of notify'),
    notifier.warning('Test of warning'),
    customNotifier.custom(),
  )
  .subscribe((res) => {
    console.log(res);
  });
0.0.17

3 years ago

0.0.16

3 years ago

0.0.15

3 years ago

0.0.14

3 years ago

0.0.13

3 years ago

0.0.12

3 years ago

0.0.11

3 years ago

0.0.10

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago