New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

rxnotify

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rxnotify

rx notify operator for rxjs

latest
npmnpm
Version
0.0.17
Version published
Maintainers
1
Created
Source

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);
  });

FAQs

Package last updated on 05 Jun 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts