Giter Club home page Giter Club logo

Comments (3)

victos avatar victos commented on September 2, 2024 2

You can use a custom operators, maybe something like this:

export function showBusy() {
  return function showBusyOperatorImplementation(source) {
    return Observable.create(subscriber => {
      const angularBusyService = ServiceLocator.injector.get(AngularBusyService);
      const subscription = source.subscribe(value => {
          try {
            subscriber.next(value);
          } catch (err) {
            subscriber.error(err);
          }
        },
        err => subscriber.error(err),
        () => subscriber.complete());
      angularBusyService.busyWithProgressBar = subscription;
      return subscription;
    });
  };
}

then you can use function showBusy() in pipe, something like this.getAlerts(page).pipe(showBusy(), map(...)) . The operator showBusy used to put the busy things into a service called angularBusyService, and the directive can get the busy things from the service.

from angular-opensource.

victos avatar victos commented on September 2, 2024 1

I use the ServiceLocator just because I don't want to pass it in to showBusy as a param.

from angular-opensource.

mtpultz avatar mtpultz commented on September 2, 2024

Thanks @victos I can't believe I didn't notice your answer. I've literally circled around 5 months later looking for the same answer so I can apply ng-busy between an Angular Material dialog that has been closed, and an exhaustMap of the actual request based on the response, which would work great using a pipe:

this.dialog
  .open(ConfirmDialogComponent, {
    data: { title: '...', message: `...` }
  })
  .afterClosed()
  .pipe(
    showBusy()
    exhaustMap((decision: boolean) =>
      (decision)
        ? this.resource.action(...)
        : empty()
    ),
    catchError((error: any) => { ... })
)

I understand roughly what you're doing in the above example having created few simple reusable operators, but nothing like your example, and I'm a bit confused regarding the last sentence.

Is this what you mean by the ServiceLocator and it will find the AngularBusyService? Are there any downsides to doing this... like performance? Seems to be an anit-pattern for DI based on some reading. Alternatively, I was trying to DI the AngularBusyService from the parent component and pass it into showBusy as a param, but can't seem to find or import it.

import {Injector} from "@angular/core";

export class ServiceLocator {
    static injector: Injector;
}

That gets used like:

import {ServiceLocator} from './locater.service.ts';

export class AppModule {
    constructor(private injector: Injector){    // Create global Service Injector
        ServiceLocator.injector = this.injector;
    }
}
export function showBusy() {
  return function showBusyOperatorImplementation(source) {
    return Observable.create(subscriber => {
      const angularBusyService = ServiceLocator.injector.get(AngularBusyService);
      const subscription = source.subscribe(value => {
          try {
            subscriber.next(value);
          } catch (err) {
            subscriber.error(err);
          }
        },
        err => subscriber.error(err),
        () => subscriber.complete());
      angularBusyService.busyWithProgressBar = subscription;
      return subscription;
    });
  };
}

from angular-opensource.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.