概述
本篇開始介紹游戲業(yè)務架構相關的內容。在游戲業(yè)務層,所有需要隔離的系統(tǒng)和模塊間通信都可以通過消息分發(fā)解耦。例如網(wǎng)絡返回通知、數(shù)據(jù)更新同步到界面等。
消息分發(fā)基于觀察者模式設計。需要處理消息的地方向消息中心注冊監(jiān)聽回調,派發(fā)消息時,調用消息中心的派發(fā)接口遍歷該消息的監(jiān)聽隊列,調用對應的回調方法。
具體方案
先定義監(jiān)聽回調類型
1
2
3
4
|
/** * 消息監(jiān)聽回調方法 */ export type NotifyListener = (src: any, data: any) => void; |
通過key-value方式保存監(jiān)聽隊列
1
|
private static msg2listDict: Dictionary< string, Array<NotifyListenerInfo> > = new Dictionary< string, Array<NotifyListenerInfo> >(); |
接口定義
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
/** * 添加多次監(jiān)聽者,需要手動移除 * @param msg * @param listener * @param target */ public static addListener(msg: string, listener: NotifyListener, target?: any): void {} /** * 添加單次監(jiān)聽者,事件觸發(fā)后即移除 * @param msg * @param listener * @param target */ public static addOnceListener(msg: string, listener: NotifyListener, target?: any): void {} /** * 移除指定消息指定的監(jiān)聽者 * @param msg * @param listener */ public static removeMsgListener(msg: string, listener: NotifyListener): void {} /** * 移除指定消息所有監(jiān)聽者 * @param msg */ public static removeMsgAllListeners(msg: string): void {} /** * 移除指定目標對指定消息的監(jiān)聽 * @param msg * @param target */ public static removeTargetMsgListen(msg: string, target: any): void {} /** * 移除指定目標所有消息監(jiān)聽 * @param target */ public static removeTargetAllMsgListen(target: any): void {} /** * 派發(fā)消息 * @param msg * @param src * @param data */ public static notify(msg: string, src: any, data: any): void {} |
在添加移除實現(xiàn)中,需要注意某消息可能正在派發(fā)。
對于一個消息新添加的監(jiān)聽者,應該在當前隊列消息派發(fā)完后再派發(fā),因此,添加一個待添加隊列
1
|
private static listener2add: Array<NotifyListenerInfo> = []; |
在添加監(jiān)聽者時做以下判斷
1
2
3
4
5
|
// 該消息正在派發(fā),放入待添加隊列 if (NotifyCenter.notifyMsgs.indexOf(msg) >= 0) { NotifyCenter.listener2add.push(info); return ; } |
同樣在移除監(jiān)聽者時,可能正在派發(fā)消息,避免對隊列的修改導致for循環(huán)異常,添加一個待移除隊列,派發(fā)消息時,如果該監(jiān)聽者在移除隊列則不派發(fā)。在消息派發(fā)完后再將其移出隊列
1
|
private static listener2remove: Array<NotifyListenerInfo> = []; |
在移除監(jiān)聽者時做以下判斷
1
2
3
4
5
6
|
// 該消息正在派發(fā),放入待移除隊列 if (NotifyCenter.notifyMsgs.indexOf(msg) >= 0) { NotifyCenter.listener2remove.push(list[i]); } else { list.splice(i, 1); } |
派發(fā)消息時遍歷指定消息下的隊列
1
2
3
4
5
6
7
8
9
10
11
12
13
|
// 隊列不存在,不需要處理 let list = NotifyCenter.msg2listDict.get(msg); if (!list) { return ; } // 標記消息正在派發(fā),多個消息可能同時在派發(fā),同一消息可能標記多次 NotifyCenter.notifyMsgs.push(msg); // 處理消息派發(fā) for (let i = 0, n = list.length; i < n; i++) { NotifyCenter._dispatch(list[i], src, data, false ); } |
派發(fā)消息時先判斷是否在移除隊列
1
2
3
4
|
// 在移除隊列,不派發(fā) if (NotifyCenter.listener2remove.indexOf(info) >= 0) { return ; } |
當前隊列派發(fā)完后檢查待添加隊列
1
2
3
4
5
6
|
// 處理待添加隊列派發(fā) for (let i = 0, n = msg2add.length; i < n; i++) { if (listener2add[i].msg == msg) { NotifyCenter._dispatch(listener2add[i], src, data, true ); } } |
引入消息分發(fā)中心,隔離的系統(tǒng)、模塊間通過消息監(jiān)聽和派發(fā)通信,避免互相引用耦合。
以上就是詳解CocosCreator消息分發(fā)機制的詳細內容,更多關于CocosCreator消息分發(fā)的資料請關注服務器之家其它相關文章!
原文鏈接:https://blog.csdn.net/houjia159/article/details/108450241