File tree 5 files changed +19
-8
lines changed
5 files changed +19
-8
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ import PropTypes from 'prop-types'
3
3
import { ReactReduxContext } from './Context'
4
4
import Subscription from '../utils/Subscription'
5
5
6
+ // Provider实际上是用高阶组件(HOCHigher-order component)包裹App结合react的Context进行状态管理
6
7
class Provider extends Component {
7
8
constructor ( props ) {
8
9
super ( props )
@@ -36,6 +37,7 @@ class Provider extends Component {
36
37
}
37
38
38
39
componentDidUpdate ( prevProps ) {
40
+ // 重新订阅
39
41
if ( this . props . store !== prevProps . store ) {
40
42
this . state . subscription . tryUnsubscribe ( )
41
43
const subscription = new Subscription ( this . props . store )
Original file line number Diff line number Diff line change @@ -74,7 +74,7 @@ export function createConnect({
74
74
'mapDispatchToProps'
75
75
)
76
76
const initMergeProps = match ( mergeProps , mergePropsFactories , 'mergeProps' )
77
-
77
+ // 调用时传入组件
78
78
return connectHOC ( selectorFactory , {
79
79
// used in error messages
80
80
methodName : 'connect' ,
@@ -101,4 +101,4 @@ export function createConnect({
101
101
}
102
102
}
103
103
104
- export default createConnect ( )
104
+ export default createConnect ( ) // 实际export的是connect createConnect() 可以加入更多配置项
Original file line number Diff line number Diff line change @@ -3,7 +3,8 @@ import { getBatch } from './batch'
3
3
// encapsulates the subscription logic for connecting a component to the redux store, as
4
4
// well as nesting subscriptions of descendant components, so that we can ensure the
5
5
// ancestor components re-render before descendants
6
-
6
+ // 订阅逻辑的封装
7
+ // 嵌套订阅
7
8
const CLEARED = null
8
9
const nullListeners = { notify ( ) { } }
9
10
@@ -15,11 +16,12 @@ function createListenerCollection() {
15
16
let next = [ ]
16
17
17
18
return {
19
+ // 订阅清空
18
20
clear ( ) {
19
21
next = CLEARED
20
22
current = CLEARED
21
23
} ,
22
-
24
+ // 发布订阅
23
25
notify ( ) {
24
26
const listeners = ( current = next )
25
27
batch ( ( ) => {
@@ -28,7 +30,7 @@ function createListenerCollection() {
28
30
}
29
31
} )
30
32
} ,
31
-
33
+
32
34
get ( ) {
33
35
return next
34
36
} ,
@@ -58,12 +60,12 @@ export default class Subscription {
58
60
59
61
this . handleChangeWrapper = this . handleChangeWrapper . bind ( this )
60
62
}
61
-
63
+ //嵌套订阅
62
64
addNestedSub ( listener ) {
63
65
this . trySubscribe ( )
64
66
return this . listeners . subscribe ( listener )
65
67
}
66
-
68
+ //嵌套发布
67
69
notifyNestedSubs ( ) {
68
70
this . listeners . notify ( )
69
71
}
Original file line number Diff line number Diff line change 1
1
// Default to a dummy "batch" implementation that just runs the callback
2
+ // 批处理
2
3
function defaultNoopBatch ( callback ) {
3
4
callback ( )
4
5
}
Original file line number Diff line number Diff line change 2
2
* @param {any } obj The object to inspect.
3
3
* @returns {boolean } True if the argument appears to be a plain object.
4
4
*/
5
+
6
+ // 简单对象只能是{}和new object()
7
+ // typeof [] === "object",但不是简单对象
5
8
export default function isPlainObject ( obj ) {
6
9
if ( typeof obj !== 'object' || obj === null ) return false
7
10
8
11
let proto = Object . getPrototypeOf ( obj )
9
12
if ( proto === null ) return true
10
-
13
+ // 基于原型链查找
14
+ // new出来的对象的原型是function
15
+ // function的原型是object
16
+ // 二者不相等 所以不是简单对象
11
17
let baseProto = proto
12
18
while ( Object . getPrototypeOf ( baseProto ) !== null ) {
13
19
baseProto = Object . getPrototypeOf ( baseProto )
You can’t perform that action at this time.
0 commit comments