#Rxjs -> Signal not working.

11 messages · Page 1 of 1 (latest)

ripe scarab
#

Why does this not work? This works calling data from the api. But if I want to update the data, all the signals do not do anything. I thought simply sending a new emission from the observable will let all the signals react. What am I not understanding.

pageData$ = this.pageDataSubject.pipe(
        tap((data:any) =>{
            console.log(data)
        }),
        switchMap((data:any) => {
            if (data === null || data === undefined || data === '') {
              return this.apiData$; // get data from api
            } else {
                return of(data); // update data if any changes made by clicks etc
            }
        })
    ) as any;

    pageData = toSignal(this.pageData$) as any; // convert the above to signal

    checkPageChanges = effect((data) => { // this fires when api resolves. 
        console.log(this.pageData());
    });

Then when I click something

update(){
         this.pageDataSubject.next(newData);
}

All the console logs in the rxjs works but not the signals.

autumn badger
#

Add console.logs AFTER your switch map to confirm the stream is actually working.

I see you're using any a lot, is that possibly hiding the problem?

limpid fossil
#

My guess is that you're mutating the data instead of actually creating a new object or array.

ripe scarab
#

Thanks for this, I still cant work it out. Even putting raw string in as my update.

#

console logs work well.

#

How do I just get the rxjs to update a signal as as well as a function. So I would like to have one source of truth updated able by the rxjs or an input.

ripe scarab
ripe scarab
limpid fossil
ripe scarab
#

@limpid fossil I really appreciate your help. You cannot believe how helpful you are to the world.
I have forked your stack to this
https://stackblitz.com/edit/stackblitz-starters-wdzfmr?file=src%2Fmain.ts
The thing is I need to modify a large object based off the pageData(). So how could I modify the pageData and then emit a new value. The stackblitz is my exact problem. As you can see the

    // this fires when api resolves.
    console.log(this.pageData());
  });```
does not fire off anymore if I hit the update. So somehow I need to create a new reference to the pageData but how can I do pageData.update (which does not work). Is there a pageData.get() then I can create a brand new object?
limpid fossil