Skip to content

Instantly share code, notes, and snippets.

@MikeRyanDev
Created August 24, 2016 12:46
Show Gist options
  • Save MikeRyanDev/94703818dd83cb87cfb5d41e7a6fa261 to your computer and use it in GitHub Desktop.
Save MikeRyanDev/94703818dd83cb87cfb5d41e7a6fa261 to your computer and use it in GitHub Desktop.
ngrx/store reselect
import { defaultMemoize as memoize } from 'reselect';
export interface StreamSelector<A,B> {
(input$: Observable<T>): Observable<R>;
}
export interface Share {
<A,B>(): StreamSelector<A,B>;
<A,B,C>(c: C): StreamSelector<A,B>;
<A,B,C,D>(c: C, d: D): StreamSelector<A,B>;
<A,B,C,D,E>(c: C, d: D, e: E): StreamSelector<A,B>;
<A,B,C,D,E,F>(c: C, d: D, e: E, f: F): StreamSelector<A,B>;
<A,B,C,D,E,F,G>(c: C, d: D, e: E, f: F, g: G): StreamSelector<A,B>;
<A,B,C,D,E,F,G,H>(c: C, d: D, e: E, f: F, g: G, h: H): StreamSelector<A,B>;
<A,B,C,D,E,F,G,H,I>(c: C, d: D, e: E, f: F, g: G, h: H, i: I): StreamSelector<A,B>;
<A,B,C,D,E,F,G,H,I,J>(c: C, d: D, e: E, f: F, g: G, h: H, i: I, j: J): StreamSelector<A,B>;
}
export const share: Share = function(fn) {
let last, memoized;
const factory = memoize(source);
return function (...args) {
let result = factory(...args);
if (result !== last) {
last = result;
memoized = memoize((source) => result(source).share());
}
return memoized;
}
}
@fxck
Copy link

fxck commented Dec 14, 2016

How does this work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment