Last active
April 6, 2016 15:31
-
-
Save davidchase/86168abd32c3f38456c1c03ab8160e4c to your computer and use it in GitHub Desktop.
cacheFetch http://www.webpackbin.com/E1PlqgCRl
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"/> | |
</head> | |
<body> | |
<button class="btn">Click Me</button> | |
<script src="index.js"></script> | |
</body> | |
</html> |
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
import {click} from '@most/dom-event'; | |
import {fromEvent, fromPromise, combine, map} from 'most'; | |
const cachePromise = function(triggerStream, promiseFn, n = 1) { | |
const signal = triggerStream.multicast() | |
const promiseSink = map(promiseFn, signal).await().take(n); | |
return combine((events, resp) => resp, signal, promiseSink); | |
}; | |
const promiseF = () => fetch('http://reqres.in/api/users?page=2').then(r => r.json()); | |
const btn = click(document.body) | |
.filter(event => event.target.matches('.btn')) | |
cachePromise(btn, promiseF).observe(console.log.bind(console)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment