Created
November 1, 2018 13:40
-
-
Save kolodny/a68280380918450bc04d3046dd50bbbf to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function cache<Query, Result>( | |
factory: (query: Query) => Observable<Result>, | |
cache: Map<Query, Result> = new Map()): (query: Query) => | |
Observable<Result> { | |
const subject = new Subject<Result>(); | |
return query => { | |
if (cache.has(query)) { | |
subject.next(cache.get(query)); | |
} else { | |
factory(query).subscribe(results => { | |
cache.set(query, results); | |
subject.next(results); | |
}); | |
} | |
return subject.asObservable(); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment