How to trigger an rxjs observable

A. L

Using rxjs v6

So I have multiple variables that I want to track/observe, and when any of them change, trigger an Observable to call an API, which then updates something else based on those variables.

I thought I could maybe trigger an event on a div to force the observable to do something, but it's not returning results based on what I want

E.g.

var a, b, c, d // if any updates, should trigger div "update" event

let obs = fromEvent(this.$refs.updater, "update")
    .pipe(
        switchMap(i => {return this.callAnAPI()})
    )
obs.subscribe()

var update = new Event("update")
this.$refs.updater.dispatchEvent(update)

However, there is no subscribe method on the observable. I was trying to modify one of my other Observables (which works)

fromEvent(input, "keyup")
    .pipe(
        map(i => i.currentTarget.value),
        debounceTime(delay),
        distinctUntilChanged(),
        switchMap(value => 
        {
            this.page = 1
            if (ajax)
            {
                return ajax
            }   
            else
            {
                return this.axiosAjax({
                    path,
                    method,
                    data: {
                        ...data,
                        [term]: value
                    }
                })
            }    
        })
    )
Yoshi

Based on the title of your question, I'm speculating that your custom event is just a means to an end.

If so, I don't think it's actually necessary. If you want "outside" influence (or a trigger) over an observable, use a Subject.

For example:

const { fromEvent, merge, of, Subject } = rxjs;
const { switchMap, delay } = rxjs.operators;

// fake api
const fakeApi$ = of(Math.random()).pipe(
  delay(2000),
);

// store a reference to the subject
const triggerApi$ = new Subject();

// apply behavior
const api$ = triggerApi$.pipe(
  switchMap(
    () => {
      console.log('switching to api call');

      // delegate to api call
      return fakeApi$;
    }
  )
);

// activate (handle result)
api$.subscribe((result) => {
  console.log('api result', result);
});

// some outside triggers
const fooClicks$ = fromEvent(document.getElementById('foo'), 'click');
const barClicks$ = fromEvent(document.getElementById('bar'), 'click');
const bazChanges$ = fromEvent(document.getElementById('baz'), 'change');

// combined
const updates$ = merge(fooClicks$, barClicks$, bazChanges$);

// activate
updates$.subscribe(triggerApi$);
<script src="https://unpkg.com/[email protected]/bundles/rxjs.umd.min.js"></script>

<button id="foo">foo</button>
<button id="bar">bar</button>
<textarea id="baz"></textarea>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

In RxJS 5, is there a way to trigger an Observable before subscribing to it?

How to chain rxjs observable

How to create an Observable that depends on another Observable in RxJS

How to chain the result of observable to the below observable in rxjs

RXJS How to convert Observable<T[]> to Observable<T>

How to chain a list of RxJS observable

How to create a waterfall observable in rxjs?

rxjs how to pass observable to map

How to cancel a composed RxJS observable

How to finilize rxjs switchmap observable?

How to transform properties in a RXJS Observable

How to stop the share observable in rxjs

How filter a json with observable rxJS

Angular jasmine test not able to trigger Observable created with fromEvent rxjs operator

RxJS : how to transform an Observable<Observable<Thing>> into Observable<Thing[]>

How can I buffer an observable by a simple in Rxjs?

How to unsubscribe from an RxJS 5 observable?

How to mock RxJs Observable Websocket for Unit Tests

RxJS: How to test Observable.ajax() with nock?

how to subscribe to the last observable and execute a funtion in rxjs?

Rxjs: How to implement Observable.first()?

RxJs Observable with infinite scroll OR how to combine Observables

How to skip first value of an observable with RxJS?

How to start observable in the pipe in RxJS 6?

How to bind third party event to rxjs observable

RxJs how to merge two overlapping observable into one

RxJs: How to loop based on state of the observable?

How is it possible to stop a debounced Rxjs Observable?

How to extract data from RxJS observable