{"id":7979,"date":"2024-06-28T16:01:05","date_gmt":"2024-06-28T08:01:05","guid":{"rendered":""},"modified":"2024-06-28T16:01:05","modified_gmt":"2024-06-28T08:01:05","slug":"\u3010RxJs\u3011\u6bcf\u5929\u5b66\u4e60\u4e00\u70b9\u64cd\u4f5c\u7b26 merge\u3001mergeAll\u3001mergeMap\u3001mergeMapTo\u3001mergeScan","status":"publish","type":"post","link":"https:\/\/mushiming.com\/7979.html","title":{"rendered":"\u3010RxJs\u3011\u6bcf\u5929\u5b66\u4e60\u4e00\u70b9\u64cd\u4f5c\u7b26 merge\u3001mergeAll\u3001mergeMap\u3001mergeMapTo\u3001mergeScan"},"content":{"rendered":"

merge<\/strong><\/p>\n

\u5b98\u65b9\u89e3\u91ca\uff1a<\/strong><\/p>\n

merge(observables: ...ObservableInput, concurrent: number, scheduler: Scheduler): Observable<\/p>\n

\u521b\u5efa\u4e00\u4e2a\u8f93\u51fa Observable \uff0c\u5b83\u53ef\u4ee5\u540c\u65f6\u53d1\u51fa\u6bcf\u4e2a\u7ed9\u5b9a\u7684\u8f93\u5165 Observable \u4e2d\u503c\u3002\u3001<\/p>\n

\u6d4b\u8bd5\u4ee3\u7801\uff1a<\/strong><\/p>\n

const sourceOne$ = of(1, 2, 3); const sourceTwo$ = of(5, 6); const sourceThree$ = of(7, 8); sourceOne$ .pipe( merge(sourceTwo$, sourceThree$) ) .subscribe(val => { console.log(val); }); \/\/\u8f93\u51fa 1 2 3 5 6 7 8 const button = document.querySelector('button'); const click$ = fromEvent(button, 'click'); const timer$ = interval(1000); click$.pipe( merge(timer$) ).subscribe(x => console.log(x)); 0 1 2 \/\/\u70b9\u51fb\u6309\u94ae MouseEvent {isTrusted: true, screenX: 41, screenY: 254, clientX: 41, clientY: 143, \u2026} 3 4 5 \/\/\u70b9\u51fb\u6309\u94ae MouseEvent {isTrusted: true, screenX: 41, screenY: 254, clientX: 41, clientY: 143, \u2026} 6 7 <\/code><\/pre>\n

\u5f39\u73e0\u56fe\uff1a<\/strong><\/p>\n

\"\u3010RxJs\u3011\u6bcf\u5929\u5b66\u4e60\u4e00\u70b9\u64cd\u4f5c\u7b26<\/p>\n

merge<\/code> \u8ba2\u9605\u6bcf\u4e2a\u7ed9\u5b9a\u7684\u8f93\u5165 Observable (\u4f5c\u4e3a\u53c2\u6570)\uff0c\u5c06\u6240\u6709\u8f93\u5165 Observables \u7684\u6240\u6709\u503c\u53d1\u9001(\u4e0d\u8fdb\u884c\u4efb\u4f55\u8f6c\u6362)\u5230\u8f93\u51fa Observable \u3002\u6240\u6709\u7684\u8f93\u5165 Observable \u90fd\u5b8c\u6210\u4e86\uff0c\u8f93\u51fa Observable \u624d \u80fd\u5b8c\u6210\u3002\u4efb\u4f55\u7531\u8f93\u5165 Observable \u53d1\u51fa\u7684\u9519\u8bef\u90fd\u4f1a\u7acb\u5373\u5728\u8f93\u51fa Observalbe \u4e0a\u53d1\u51fa\u3002<\/p>\n

\u5c06\u591a\u4e2aObservable\u7684\u503c\u8f93\u51fa\u5230\u4e00\u4e2aobservable\u3002<\/p>\n

 <\/p>\n

mergeAll<\/strong><\/p>\n

\u5b98\u65b9\u89e3\u91ca\uff1a<\/strong><\/p>\n

mergeAll(concurrent: number): Observable<\/p>\n

\u5c06\u9ad8\u9636 Observable \u8f6c\u6362\u6210\u4e00\u9636 Observable \uff0c\u4e00\u9636 Observable \u4f1a\u540c\u65f6\u53d1\u51fa\u5728\u5185\u90e8 Observables \u4e0a\u53d1\u51fa\u7684\u6240\u6709\u503c\u3002<\/p>\n

\u6d4b\u8bd5\u4ee3\u7801\uff1a<\/strong><\/p>\n

 const button = document.querySelector('button'); const click$ = fromEvent(button, 'click'); const interval$ = interval(1000); click$.pipe( map(() => interval$), mergeAll() ).subscribe(num => console.log(num)); \/\/\u70b9\u51fbbutton 0 1 \/\/\u70b9\u51fbbutton 0 2 1 3 2 4 \/\/\u70b9\u51fbbutton 0 3 5 \u3002\u3002\u3002<\/code><\/pre>\n

\u5f39\u73e0\u56fe\uff1a<\/strong><\/p>\n

\"\u3010RxJs\u3011\u6bcf\u5929\u5b66\u4e60\u4e00\u70b9\u64cd\u4f5c\u7b26<\/p>\n

mergeMap<\/strong><\/p>\n

\u5b98\u65b9\u89e3\u91ca\uff1a<\/strong><\/p>\n

mergeMap(project: function(value: T, ?index: number): ObservableInput, resultSelector: function(outerValue: T, innerValue: I, outerIndex: number, innerIndex: number): any, concurrent: number): Observable<\/p>\n

\u5c06\u6bcf\u4e2a\u6e90\u503c\u6295\u5c04\u6210 Observable \uff0c\u8be5 Observable \u4f1a\u5408\u5e76\u5230\u8f93\u51fa Observable \u4e2d\u3002<\/p>\n

\u6d4b\u8bd5\u4ee3\u7801\uff1a<\/strong><\/p>\n

const letters$ = of('a', 'b', 'c'); letters$.pipe( mergeMap(x => interval(1000).pipe( map(i => x + i) ) ) ).subscribe(x => console.log(x)); \/\/\u8f93\u51fa a0 b0 c0 a1 b1 c1 ...<\/code><\/pre>\n

\u5f39\u73e0\u56fe<\/strong><\/p>\n

\"\u3010RxJs\u3011\u6bcf\u5929\u5b66\u4e60\u4e00\u70b9\u64cd\u4f5c\u7b26<\/strong><\/p>\n

\u5c06\u6bcf\u4e2a\u503c\u6620\u5c04\u6210 Observable \uff0c\u7136\u540e\u4f7f\u7528 mergeAll \u6253\u5e73\u6240\u6709\u7684\u5185\u90e8 Observables \u3002<\/p>\n

\u53ef\u4ee5\u770b\u4f5c\u662fmap\u4e0emergeAll\u7684\u7ed3\u5408\u3002<\/strong><\/p>\n

 <\/p>\n

mergeMapTo:<\/strong><\/p>\n

\u5b98\u65b9\u89e3\u91ca\uff1a<\/strong><\/p>\n

mergeMapTo(innerObservable: ObservableInput, resultSelector: function(outerValue: T, innerValue: I, outerIndex: number, innerIndex: number): any, concurrent: number): Observable<\/p>\n

\u5c06\u6bcf\u4e2a\u6e90\u503c\u6295\u5c04\u6210\u540c\u4e00\u4e2a Observable \uff0c\u8be5 Observable \u4f1a\u591a\u6b21\u5408\u5e76\u5230\u8f93\u51fa Observable \u4e2d\u3002<\/p>\n

\u6d4b\u8bd5\u4ee3\u7801\uff1a<\/p>\n

const button = document.querySelector('button'); const click$ = fromEvent(button, 'click'); click$.pipe( mergeMapTo(interval(1000)) ).subscribe(x => console.log(x)); \/\/\u70b9\u51fbbutton 0 1 \/\/\u70b9\u51fbbutton 0 2 1 3 2 4 \/\/\u70b9\u51fbbutton 0 3 5 const sourceOne$ = of(1, 2, 3); const sourceTwo$ = of(5, 6); sourceOne$ .pipe( mergeMapTo(sourceTwo$) ) .subscribe(val => { console.log(val); }); \/\/\u8f93\u51fa 5 6 5 6 5 6<\/code><\/pre>\n

\u5f39\u73e0\u56fe\uff1a<\/p>\n

\"\u3010RxJs\u3011\u6bcf\u5929\u5b66\u4e60\u4e00\u70b9\u64cd\u4f5c\u7b26<\/p>\n

\u5c06\u6bcf\u4e2a\u6e90\u503c\u6620\u5c04\u6210\u7ed9\u5b9a\u7684 Observable \uff1ainnerObservable<\/code> \uff0c\u800c\u65e0\u8bba\u6e90\u503c\u662f\u4ec0\u4e48\uff0c\u7136\u540e \u5c06\u8fd9\u4e9b\u7ed3\u679c Observables \u5408\u5e76\u5230\u5355\u4e2a\u7684 Observable \uff0c\u4e5f\u5c31\u662f\u8f93\u51fa Observable \u3002<\/p>\n

\u53ef\u4ee5\u770b\u4f5cmergeAll\u548cmapTo\u7684\u7ed3\u5408<\/strong><\/p>\n

 <\/p>\n

mergeScan<\/strong><\/p>\n

\u5b98\u65b9\u89e3\u91ca\uff1a<\/strong><\/p>\n

mergeScan(accumulator: function(acc: R, value: T): Observable<R>, seed: *, concurrent: number): Observable<R><\/p>\n

\u5728\u6e90 Observable \u4e0a\u5e94\u7528 accumulator \u51fd\u6570\uff0c\u5176\u4e2d accumulator \u51fd\u6570\u672c\u8eab\u8fd4\u56de Observable \uff0c\u7136\u540e\u6bcf\u4e2a\u8fd4\u56de\u7684\u4e2d\u95f4 Observable \u4f1a\u88ab\u5408\u5e76\u5230\u8f93\u51fa Observable \u4e2d\u3002<\/p>\n

\u6d4b\u8bd5\u4ee3\u7801<\/strong><\/p>\n

const button = document.querySelector('button'); const click$ = fromEvent(button, 'click'); const seed = 1; \/\/ \u521d\u59cb\u7684\u7d2f\u52a0\u503c\u3002 const fn = (acc, one) => of(acc * one); \/\/ \u5728\u6bcf\u4e2a\u6e90\u503c\u4e0a\u8c03\u7528\u7684\u7d2f\u52a0\u5668\u51fd\u6570\u3002 click$ .pipe( mapTo(2), mergeScan(fn, seed) ).subscribe(x => console.log(x)); \/\/\u70b9\u51fbbutton 2 \/\/\u70b9\u51fbbutton 4 \/\/\u70b9\u51fbbutton 8 \/\/\u70b9\u51fbbutton 16<\/code><\/pre>\n

 <\/p>\n

\u53c2\u8003\uff1a<\/p>\n

https:\/\/cn.rx.js.org\/class\/es6\/Observable.js~Observable.html#instance-method-mergeScan<\/p>\n","protected":false},"excerpt":{"rendered":"\u3010RxJs\u3011\u6bcf\u5929\u5b66\u4e60\u4e00\u70b9\u64cd\u4f5c\u7b26 merge\u3001mergeAll\u3001mergeMap\u3001mergeMapTo\u3001mergeScanmerge\u5b98\u65b9\u89e3\u91ca\uff1amerge(observables:...Observ...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/7979"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=7979"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/7979\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=7979"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=7979"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=7979"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}