medikoo / memoizee

Complete memoize/cache solution for JavaScript
ISC License
1.75k stars 61 forks source link

Idea: memoizing observables #98

Open Rush opened 6 years ago

Rush commented 6 years ago

I have just figured out a way to use memoizee for RxJS:

import { Observable } from 'rxjs';
import { publishReplay, refCount } from 'rxjs/operators';
import * as memoizee from 'memoizee';

type ObservableFunction = <T>(args: any[]) => Observable<T>;
export function memoizeObservable(toWrap: ObservableFunction, options: memoizee.Options) {
  const newOptions = Object.assign({}, options);

  return memoizee(function(this: any, ...args: any[]) {
    const result$ = toWrap.apply(this, args);

    return result$.pipe(publishReplay(1)).pipe(refCount());
  }, newOptions);
}

Is there a chance you're going to support this out of the box or would you rather see a library "memoizee-rxjs" or something along these lines?

Anyway - sharing this snippet here in case somebody finds it useful.

medikoo commented 6 years ago

@Rush can you reedit your message, so example is in plain JavaScript? Otherwise it's hard for me to relate to it.

Rush commented 6 years ago

Will do