Skip to content

Commit 976c59a

Browse files
add comments
1 parent 7b44e33 commit 976c59a

File tree

5 files changed

+19
-8
lines changed

5 files changed

+19
-8
lines changed

src/components/Provider.js

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import PropTypes from 'prop-types'
33
import { ReactReduxContext } from './Context'
44
import Subscription from '../utils/Subscription'
55

6+
// Provider实际上是用高阶组件(HOCHigher-order component)包裹App结合react的Context进行状态管理
67
class Provider extends Component {
78
constructor(props) {
89
super(props)
@@ -36,6 +37,7 @@ class Provider extends Component {
3637
}
3738

3839
componentDidUpdate(prevProps) {
40+
// 重新订阅
3941
if (this.props.store !== prevProps.store) {
4042
this.state.subscription.tryUnsubscribe()
4143
const subscription = new Subscription(this.props.store)

src/connect/connect.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export function createConnect({
7474
'mapDispatchToProps'
7575
)
7676
const initMergeProps = match(mergeProps, mergePropsFactories, 'mergeProps')
77-
77+
// 调用时传入组件
7878
return connectHOC(selectorFactory, {
7979
// used in error messages
8080
methodName: 'connect',
@@ -101,4 +101,4 @@ export function createConnect({
101101
}
102102
}
103103

104-
export default createConnect()
104+
export default createConnect() // 实际export的是connect createConnect() 可以加入更多配置项

src/utils/Subscription.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { getBatch } from './batch'
33
// encapsulates the subscription logic for connecting a component to the redux store, as
44
// well as nesting subscriptions of descendant components, so that we can ensure the
55
// ancestor components re-render before descendants
6-
6+
// 订阅逻辑的封装
7+
// 嵌套订阅
78
const CLEARED = null
89
const nullListeners = { notify() {} }
910

@@ -15,11 +16,12 @@ function createListenerCollection() {
1516
let next = []
1617

1718
return {
19+
// 订阅清空
1820
clear() {
1921
next = CLEARED
2022
current = CLEARED
2123
},
22-
24+
// 发布订阅
2325
notify() {
2426
const listeners = (current = next)
2527
batch(() => {
@@ -28,7 +30,7 @@ function createListenerCollection() {
2830
}
2931
})
3032
},
31-
33+
3234
get() {
3335
return next
3436
},
@@ -58,12 +60,12 @@ export default class Subscription {
5860

5961
this.handleChangeWrapper = this.handleChangeWrapper.bind(this)
6062
}
61-
63+
//嵌套订阅
6264
addNestedSub(listener) {
6365
this.trySubscribe()
6466
return this.listeners.subscribe(listener)
6567
}
66-
68+
//嵌套发布
6769
notifyNestedSubs() {
6870
this.listeners.notify()
6971
}

src/utils/batch.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Default to a dummy "batch" implementation that just runs the callback
2+
// 批处理
23
function defaultNoopBatch(callback) {
34
callback()
45
}

src/utils/isPlainObject.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@
22
* @param {any} obj The object to inspect.
33
* @returns {boolean} True if the argument appears to be a plain object.
44
*/
5+
6+
// 简单对象只能是{}和new object()
7+
// typeof [] === "object",但不是简单对象
58
export default function isPlainObject(obj) {
69
if (typeof obj !== 'object' || obj === null) return false
710

811
let proto = Object.getPrototypeOf(obj)
912
if (proto === null) return true
10-
13+
// 基于原型链查找
14+
// new出来的对象的原型是function
15+
// function的原型是object
16+
// 二者不相等 所以不是简单对象
1117
let baseProto = proto
1218
while (Object.getPrototypeOf(baseProto) !== null) {
1319
baseProto = Object.getPrototypeOf(baseProto)

0 commit comments

Comments
 (0)