Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit86188bb

Browse files
committed
add code
1 parent421dba9 commit86188bb

File tree

7 files changed

+230
-0
lines changed

7 files changed

+230
-0
lines changed

‎.gitignore‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Logs
2+
logs
3+
*.log
4+
5+
# Runtime data
6+
pids
7+
*.pid
8+
*.seed
9+
10+
# Directory for instrumented libs generated by jscoverage/JSCover
11+
lib-cov
12+
13+
# Coverage directory used by tools like istanbul
14+
coverage
15+
16+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
17+
.grunt
18+
19+
# node-waf configuration
20+
.lock-wscript
21+
22+
# Compiled binary addons (http://nodejs.org/api/addons.html)
23+
build/Release
24+
25+
# Dependency directory
26+
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
27+
node_modules
28+
29+
# Bower
30+
bower_components/
31+
32+
# idea
33+
.idea
34+
35+
# do not keep dist
36+
dist
37+
.DS_Store

‎src/.gitignore‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Logs
2+
logs
3+
*.log
4+
5+
# Runtime data
6+
pids
7+
*.pid
8+
*.seed
9+
10+
# Directory for instrumented libs generated by jscoverage/JSCover
11+
lib-cov
12+
13+
# Coverage directory used by tools like istanbul
14+
coverage
15+
16+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
17+
.grunt
18+
19+
# node-waf configuration
20+
.lock-wscript
21+
22+
# Compiled binary addons (http://nodejs.org/api/addons.html)
23+
build/Release
24+
25+
# Dependency directory
26+
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
27+
node_modules
28+
29+
# Bower
30+
bower_components/
31+
32+
# idea
33+
.idea
34+
35+
# do not keep dist
36+
dist

‎src/Provider.js‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
constwarning=require('../utils/warning.js')
2+
3+
functioncheckStoreShape(store){
4+
constmissingMethods=['subscribe','dispatch','getState'].filter(m=>!store.hasOwnProperty(m));
5+
6+
if(missingMethods.length>0){
7+
warning(
8+
'Store似乎不是一个合法的Redux Store对象: '+
9+
'缺少这些方法: '+missingMethods.join(', ')+'。'
10+
)
11+
}
12+
}
13+
14+
functionProvider(store){
15+
checkStoreShape(store)
16+
returnfunction(appConfig){
17+
returnObject.assign({},appConfig,{store})
18+
}
19+
}
20+
21+
module.exports=Provider

‎src/connect.js‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
constshallowEqual=require('../utils/shallowEqual.js')
2+
constwarning=require('../utils/warning.js')
3+
constwrapActionCreators=require('../utils/wrapActionCreators.js')
4+
5+
constdefaultMapStateToProps=state=>({})// eslint-disable-line no-unused-vars
6+
constdefaultMapDispatchToProps=dispatch=>({dispatch})
7+
8+
functionconnect(mapStateToProps,mapDispatchToProps){
9+
constshouldSubscribe=Boolean(mapStateToProps)
10+
constmapState=mapStateToProps||defaultMapStateToProps
11+
constapp=getApp();
12+
13+
letmapDispatch
14+
if(typeofmapDispatchToProps==='function'){
15+
mapDispatch=mapDispatchToProps
16+
}elseif(!mapDispatchToProps){
17+
mapDispatch=defaultMapDispatchToProps
18+
}else{
19+
mapDispatch=wrapActionCreators(mapDispatchToProps)
20+
}
21+
22+
returnfunctionwrapWithConnect(pageConfig){
23+
24+
functionhandleChange(options){
25+
if(!this.unsubscribe){
26+
return
27+
}
28+
29+
conststate=this.store.getState()
30+
constmappedState=mapState(state,options);
31+
if(!this.data||shallowEqual(this.data,mappedState)){
32+
return;
33+
}
34+
this.setData(mappedState)
35+
}
36+
37+
functiononLoad(options){
38+
this.store=app.store;
39+
if(!this.store){
40+
warning("Store对象不存在!")
41+
}
42+
if(shouldSubscribe){
43+
this.unsubscribe=this.store.subscribe(handleChange.bind(this,options))
44+
handleChange.apply(this)
45+
}
46+
}
47+
48+
functiononUnload(){
49+
typeofthis.unsubscribe==='function'&&this.unsubscribe()
50+
}
51+
52+
returnObject.assign({},pageConfig,mapDispatch(app.store.dispatch),{onLoad, onUnload})
53+
}
54+
}
55+
56+
module.exports=connect

‎src/utils/shallowEqual.js‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
functionshallowEqual(objA,objB){
2+
if(objA===objB){
3+
returntrue
4+
}
5+
6+
constkeysA=Object.keys(objA)
7+
constkeysB=Object.keys(objB)
8+
9+
if(keysA.length!==keysB.length){
10+
returnfalse
11+
}
12+
13+
// Test for A's keys different from B.
14+
consthasOwn=Object.prototype.hasOwnProperty
15+
for(leti=0;i<keysA.length;i++){
16+
if(!hasOwn.call(objB,keysA[i])||
17+
objA[keysA[i]]!==objB[keysA[i]]){
18+
returnfalse
19+
}
20+
}
21+
22+
returntrue
23+
}
24+
25+
module.exports=shallowEqual

‎src/utils/warning.js‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Prints a warning in the console if it exists.
3+
*
4+
*@param {String} message The warning message.
5+
*@returns {void}
6+
*/
7+
functionwarning(message){
8+
/* eslint-disable no-console */
9+
if(typeofconsole!=='undefined'&&typeofconsole.error==='function'){
10+
console.error(message)
11+
}
12+
/* eslint-enable no-console */
13+
try{
14+
// This error was thrown as a convenience so that if you enable
15+
// "break on all exceptions" in your console,
16+
// it would pause the execution at this line.
17+
thrownewError(message)
18+
/* eslint-disable no-empty */
19+
}catch(e){}
20+
/* eslint-enable no-empty */
21+
}
22+
23+
module.exports=warning

‎src/utils/wrapActionCreators.js‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
functionbindActionCreator(actionCreator,dispatch){
2+
returnfunction(){
3+
returndispatch(actionCreator.apply(undefined,arguments));
4+
};
5+
}
6+
7+
functionbindActionCreators(actionCreators,dispatch){
8+
if(typeofactionCreators==='function'){
9+
returnbindActionCreator(actionCreators,dispatch);
10+
}
11+
12+
if(typeofactionCreators!=='object'||actionCreators===null){
13+
thrownewError('bindActionCreators expected an object or a function, instead received '+(actionCreators===null ?'null' :typeofactionCreators)+'. '+'Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?');
14+
}
15+
16+
varkeys=Object.keys(actionCreators);
17+
varboundActionCreators={};
18+
for(vari=0;i<keys.length;i++){
19+
varkey=keys[i];
20+
varactionCreator=actionCreators[key];
21+
if(typeofactionCreator==='function'){
22+
boundActionCreators[key]=bindActionCreator(actionCreator,dispatch);
23+
}
24+
}
25+
returnboundActionCreators;
26+
}
27+
28+
functionwrapActionCreators(actionCreators){
29+
returndispatch=>bindActionCreators(actionCreators,dispatch)
30+
}
31+
32+
module.exports=wrapActionCreators

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp