Created
August 24, 2016 12:46
-
-
Save MikeRyanDev/94703818dd83cb87cfb5d41e7a6fa261 to your computer and use it in GitHub Desktop.
ngrx/store reselect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How does this work?