我对整个 Cycle.js/RxJS 生态系统还很陌生,我希望有人能指导我中止正在进行的 ajax 调用的过程,因为它看起来似乎可行。
如果有一个从search-github-user中派生出来的小例子,那就太棒了。
为了避免 SO 狂热者投反对票,我在这里添加了该示例的副本:
import Cycle from '@cycle/core';
import {Observable} from 'rx';
import {div, label, input, hr, ul, li, a, makeDOMDriver} from '@cycle/dom';
import {makeHTTPDriver} from '@cycle/http';
function main(sources) {
// Requests for Github repositories happen when the input field changes,
// debounced by 500ms, ignoring empty input field.
const searchRequest$ = sources.DOM.select('.field').events('input')
.debounce(500)
.map(ev => ev.target.value)
.filter(query => query.length > 0)
.map(q => ({
url: 'https://api.github.com/search/repositories?q=' + encodeURI(q),
category: 'github',
}));
// Requests unrelated to the Github search. This is to demonstrate
// how filtering for the HTTP response category is necessary.
const otherRequest$ = Observable.interval(1000).take(2)
.map(() => 'http://www.google.com');
// Convert the stream of HTTP responses to virtual DOM elements.
const vtree$ = sources.HTTP
.filter(res$ => res$.request.category === 'github')
.flatMap(x => x)
.map(res => res.body.items)
.startWith([])
.map(results =>
div([
label({className: 'label'}, 'Search:'),
input({className: 'field', attributes: {type: 'text'}}),
hr(),
ul({className: 'search-results'}, results.map(result =>
li({className: 'search-result'}, [
a({href: result.html_url}, result.name)
])
))
])
);
const request$ = searchRequest$.merge(otherRequest$);
return {
DOM: vtree$,
HTTP: request$
};
}
Cycle.run(main, {
DOM: makeDOMDriver('#main-container'),
HTTP: makeHTTPDriver()
});
更新
感谢@user3743222 指出master分支上的变化,看来作者已经发布了一个新版本,现在abort部分在这里。