angelnikolov / ts-cacheable

Observable/Promise Cache Decorator
https://npmjs.com/package/ts-cacheable
ISC License
340 stars 42 forks source link

Cache not working on custom observables #21

Closed crjacinro closed 5 years ago

crjacinro commented 5 years ago

Is this library only compatible with HTTP observables? I have a custom observable in which i control the execution of next and complete methods.

This is my code.

loadApplicationsWithCache(enumValue, {date: "").subscribe(this.onLoadApiComplete, this.onLoadApiError)

@Cacheable() loadApplicationsWithCache(status: someEnumType, date: someObject) : Observable<Map<string, Data>> { return this.loadApplicationsFromAPI.loadApplications(status, date); }

At first, the actual API is being called. The second invocation emits empty map.

angelnikolov commented 5 years ago

Hi there. Can you get me some pseudo-code out of the loadApplications method? Thanks!

crjacinro commented 5 years ago

Its quite complex because it passes a lot of abstractions in our code.

Here is a snippet

`public loadApplications(membershipStatus: MembershipApplicationStatus, dateRange: DateRange) { this.membershipStatus = membershipStatus; this.dateRange = dateRange; return Observable.create(this.loadApplicationsObservable) }

loadApplicationsObservable = (observer: Observer<Map<string, MembershipApplicationData>>) => {
    this.observer = observer;

    this.getApplicationsService.getAllApplications(this.membershipStatus, this.dateRange).subscribe(
        this.onLoadApplicationsComplete, this.onLoadApplicationsFailed
    );
}`
angelnikolov commented 5 years ago

Hmm, this very basic repro works. Can you find something different with it versus your setup?

import { Observable, Observer } from 'rxjs';
import { Cacheable } from './ngx-cacheable';
class Testbed {
  @Cacheable()
  getData(id: number) {
    return Observable.create((observer: Observer<any>)=>{
      console.log("accessed from data source")
      setTimeout(()=>{
        observer.next([1,2,3])
      }, 2500);
    })
  }

}

const testbed = new Testbed();
testbed.getData(1).subscribe(console.log);
testbed.getData(1).subscribe(console.log);
angelnikolov commented 5 years ago

@crjacinro any updates?

crjacinro commented 5 years ago

hello, still not able to make it work until now.