RAC的原始碼閱讀(二)

2021-08-20 03:26:09 字數 1482 閱讀 2639

上篇已經介紹了racsignal 冷熱訊號轉換的原始碼跟蹤,本篇主要介紹racsubject 訊號訂閱及訊號傳送的原始碼跟蹤

先貼一段示例**,待會就對這段**進行原始碼跟蹤

//1️⃣racsubject 建立

racsubject * subject = [racsubject subject];

//3️⃣ 訂閱訊號

[subject subscribenext:^(id x) ];

//7️⃣ 傳送資料

[subject sendnext:@"123"];

貼上我自繪的流程圖

這個就是原始碼的執行步驟,接下來跟著這些步驟,就能弄懂整個原始碼流程

進入[[racsubject alloc] init]

- (instancetype)init
進入[subject subscribenext] -> [racsubscriber subscriberwithnext:nextblock error:null completed:null]

+ (instancetype)subscriberwithnext:(void (^)(id x))next error:(void (^)(nserror *error))error completed:(void (^)(void))completed
進入[self subscribe:o]

- (racdisposable *)subscribe:(id)subscriber 

[disposable adddisposable:[racdisposable disposablewithblock:^];

if (index != nsnotfound) [subscribers removeobjectatindex:index];

}}]];

return disposable;

}

進入[subject sendnext]

- (void)sendnext:(id)value ];

}- (void)enumeratesubscribersusingblock:(void (^)(idsubscriber))block

//遍歷所有的訂閱者,將訂閱者返回

for (idsubscriber in subscribers)

}

進入 [subscriber sendnext:value]

- (void)sendnext:(id)value 

}

ps:本篇subject sendnext舉例,請跟著流程圖對原始碼進行閱讀,關鍵點本人已加上注釋。本人能力有限,有什麼錯誤或者表達不清的還望指正。

《原始碼閱讀》原始碼閱讀技巧,原始碼閱讀工具

檢視某個類的完整繼承關係 選中類的名稱,然後按f4 quick type hierarchy quick type hierarchy可以顯示出類的繼承結構,包括它的父類和子類 supertype hierarchy supertype hierarchy可以顯示出類的繼承和實現結構,包括它的父類和...

Webpack 原始碼(二) 如何閱讀原始碼

如果想要了解 webpack 的流程,只要閱讀 七珏 細說 webpack 之流程篇 所述的內容就夠了,講解地比較全面了 本文就不對 webpack 流程再做重複的描述,而是從另外乙個角度補充分析 webpack 原始碼 webpack 中最為重要的無非是 compiler compilation ...

STL原始碼閱讀 二

vector的記憶體分配基類 template class vector alloc base vector alloc base的偏特化版本,不需要儲存記憶體分配器 template class vector alloc base tp,allocator,true template struct...