日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

rxjs of操作符生成的Observable对象的执行详细分析

發布時間:2023/12/19 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 rxjs of操作符生成的Observable对象的执行详细分析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

代碼:

const a = of([1, 2, 3]); a.subscribe((data) => console.log('Fairy:' + data));

單步調試,首先執行of所在的index.ts:

of.js的實現很簡單:

import { isScheduler } from '../util/isScheduler'; import { fromArray } from './fromArray'; import { scheduleArray } from '../scheduled/scheduleArray'; export function of(...args) {let scheduler = args[args.length - 1];if (isScheduler(scheduler)) {args.pop();return scheduleArray(args, scheduler);}else {return fromArray(args);} } //# sourceMappingURL=of.js.map

沒有scheduler,走fromArray這條路:

fromArray.js的實現:

import { Observable } from '../Observable'; import { subscribeToArray } from '../util/subscribeToArray'; import { scheduleArray } from '../scheduled/scheduleArray'; export function fromArray(input, scheduler) {if (!scheduler) {return new Observable(subscribeToArray(input));}else {return scheduleArray(input, scheduler);} } //# sourceMappingURL=fromArray.js.map

這個subscribeToArray是一個函數構造器,接收一個數組,返回一個新的函數:

export const subscribeToArray = (array) => (subscriber) => {for (let i = 0, len = array.length; i < len && !subscriber.closed; i++) {subscriber.next(array[i]);}subscriber.complete(); }; //# sourceMappingURL=subscribeToArray.js.map

該函數和我們在應用代碼里調用Observable對象的subscribe方法時傳入的回調函數有何區別?

繼續往下調試。

調用Observable對象的subscribe方法:

ObserverOrNext就是上圖第63行的箭頭函數,error和complete為undefined:

新建一個Subscriber對象:

subscriber的父類是subscription:

subscriber的destination是一個SafeSubscriber:

SafeSubscriber也是一個Subscriber:

這個emptyObservable啥實現也沒有:

import { config } from './config'; import { hostReportError } from './util/hostReportError'; export const empty = {closed: true,next(value) { },error(err) {if (config.useDeprecatedSynchronousErrorHandling) {throw err;}else {hostReportError(err);}},complete() { } }; //# sourceMappingURL=Observer.js.map ExtensibilityExtensibility![](https://img-blog.csdnimg.cn/img_convert/fd30a74f127e31a732add12cbcbabc05.png)所以toSubscriber返回的實際是一個subscriber對象:![](https://img-blog.csdnimg.cn/img_convert/d755db210f9042167caa4c3f8e51fbae.png) ![](https://img-blog.csdnimg.cn/img_convert/d7a08902cbd7f7c2cf54a7e8e440be08.png)首先調用subscriber對象的add方法,目的是通過這個三元表達式,判斷到底應該調用subscribe方法,還是trySubscribe方法:![](https://img-blog.csdnimg.cn/img_convert/7842df68ea5df9dff9665ee002a0e0bc.png)在我的Angular10,執行后者:![](https://img-blog.csdnimg.cn/img_convert/fc35352eb8125afd3f4cb34c2b936136.png)記住這個語義:Observable的subscribe方法,輸入參數為subscriber:![](https://img-blog.csdnimg.cn/img_convert/39930d88fe8b1b7febc9db45584dd876.png)_trySubscribe調用_subscribe:![](https://img-blog.csdnimg.cn/img_convert/c8255b7f4b351bcc80ad7d008b684cf8.png)然后就執行到了之前用subscribeToArray返回的function內部:![](https://img-blog.csdnimg.cn/img_convert/cca17064851f45b8998808a521e16e92.png)注意在這個上下文里,我們既有輸入,又有應用程序傳入的subscribe函數,因此可以調用next了:![](https://img-blog.csdnimg.cn/img_convert/00b5a512ad77740193ee1d209d188051.png) ![](https://img-blog.csdnimg.cn/img_convert/d6a9930cec4e5d881e6b3a104bda7da3.png)next和_next的區別就在于有個this.isStopped的判斷:![](https://img-blog.csdnimg.cn/img_convert/4368c8a5e31f1c595616f2b666383b46.png)注意語義:Observable調用subscribe,而subscriber調用next.![](https://img-blog.csdnimg.cn/img_convert/ed88af507bbecd6b570cdb7dbd887e6f.png)subscriber的desination里包含了應用程序傳入的callback:![](https://img-blog.csdnimg.cn/img_convert/831d2d80553d2ee5723196289f8446a5.png) ![](https://img-blog.csdnimg.cn/img_convert/7934c01b71638f69ce2595b6ed3a976f.png)subscriber的_tryOrUnsub函數里,最終調用應用程序的callback:![](https://img-blog.csdnimg.cn/img_convert/4309fcce0db382818f54641af6e04d75.png) ![](https://img-blog.csdnimg.cn/img_convert/f3c9f2197cb9c5812245324c029aee24.png)更多Jerry的原創文章,盡在:"汪子熙": ![](https://img-blog.csdnimg.cn/img_convert/e4a8387dca5921e049fd05f96ef232ae.png)

總結

以上是生活随笔為你收集整理的rxjs of操作符生成的Observable对象的执行详细分析的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。