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

Commitd33fbda

Browse files
committed
removed reaction and swaped state, o in action
1 parent3af8e85 commitd33fbda

File tree

3 files changed

+9
-81
lines changed

3 files changed

+9
-81
lines changed

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name":"juicr.js",
3-
"version":"1.0.1",
3+
"version":"1.0.2",
44
"scripts": {
55
"test":"mocha",
66
"build":"babel src -d lib; uglifyjs lib/juicr.js -o lib/juicr.min.js -c -m; gzip < lib/juicr.min.js > lib/juicr.min.js.gz;"

‎src/juicr.js‎

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
constructor(o={}){
44
this.actions={}
55
this.listeners=[]
6-
this.reactions=[]
76
this._state=o.initialState||{}
87
this.dev=o.dev
98

@@ -16,13 +15,12 @@
1615
this.dispatch=this.dispatch.bind(this)
1716
this.action=this.action.bind(this)
1817
this.listen=this.listen.bind(this)
19-
this.reaction=this.reaction.bind(this)
2018
}
2119

2220
action(actionName,fn){
23-
this.actions[actionName]=(o,_state)=>{
21+
this.actions[actionName]=(_state,o)=>{
2422
returnnewPromise((resolve,reject)=>{
25-
constres=fn(o,_state)
23+
constres=fn(_state,o)
2624

2725
if(res||res===undefined){
2826
resolve(res)
@@ -49,28 +47,6 @@
4947
returnlistener.unsubscribe
5048
}
5149

52-
reaction(props,fn){
53-
constreaction={
54-
props:typeofprops==='string' ?[props] :props,
55-
fn
56-
}
57-
58-
this.reactions.push(reaction)
59-
60-
// run reaction first time
61-
consts={}
62-
63-
reaction.props.forEach((p)=>{
64-
s[p]=this._state[p]
65-
})
66-
67-
constcomputedState=fn(s,this._state)
68-
this._updateState(computedState)
69-
//////
70-
71-
returnreaction
72-
}
73-
7450
_updateState(changedState){
7551
this._state={ ...this._state, ...changedState}
7652
constchangedKeys=Object.keys(changedState)
@@ -86,15 +62,6 @@
8662
}
8763
}
8864
})
89-
90-
this.reactions.forEach(({ props, fn})=>{
91-
letdoReaction=props.filter(element=>changedKeys.includes(element)).length>0
92-
93-
if(doReaction){
94-
constcomputedState=fn(changedState,this._state)
95-
if(computedState)this._updateState(computedState)
96-
}
97-
})
9865
}
9966

10067
dispatch(actionName,o){
@@ -105,7 +72,7 @@
10572
return
10673
}
10774

108-
returnaction(o||{},this._state).then((changedState)=>{
75+
returnaction(this._state,o||{}).then((changedState)=>{
10976
if(changedState){
11077
this._updateState(changedState)
11178
returnchangedState

‎test/test.juicr.js‎

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe('Juicr', () => {
1515
it('should be able to add action and dispatch with listener',function(done){
1616
constjuicr=newJuicr({initialState:{text:''}})
1717

18-
juicr.action("setText",(text)=>{
18+
juicr.action("setText",(state,text)=>{
1919
return{ text}
2020
})
2121

@@ -30,7 +30,7 @@ describe('Juicr', () => {
3030
it('should be able to add async action and dispatch with listener',function(done){
3131
constjuicr=newJuicr({initialState:{text:''}})
3232

33-
juicr.action("setText",(text)=>{
33+
juicr.action("setText",(state,text)=>{
3434
returnnewPromise((resolve)=>{
3535
setTimeout(()=>{
3636
resolve({ text})
@@ -47,11 +47,11 @@ describe('Juicr', () => {
4747
it('should be able to listen to many props',function(){
4848
constjuicr=newJuicr({initialState:{text:'',number:0}})
4949

50-
juicr.action("setText",(text)=>{
50+
juicr.action("setText",(state,text)=>{
5151
return{ text}
5252
})
5353

54-
juicr.action("setNumber",(number)=>{
54+
juicr.action("setNumber",(state,number)=>{
5555
return{ number}
5656
})
5757

@@ -72,7 +72,7 @@ describe('Juicr', () => {
7272
it('should be able to unsubscribe listener',function(){
7373
constjuicr=newJuicr({initialState:{text:''}})
7474

75-
juicr.action("setText",(text)=>{
75+
juicr.action("setText",(state,text)=>{
7676
return{ text}
7777
})
7878

@@ -81,43 +81,4 @@ describe('Juicr', () => {
8181

8282
expect(juicr.listeners.length).to.equal(0)
8383
});
84-
85-
it('should be able to add reaction',function(done){
86-
constjuicr=newJuicr({initialState:{text:''}})
87-
88-
juicr.action("setText",(text)=>{
89-
return{ text}
90-
})
91-
92-
juicr.reaction("text",(changedState)=>{
93-
return{textExtra:changedState.text+"2"}
94-
})
95-
96-
juicr.listen("textExtra",(changedState)=>{
97-
expect(changedState.textExtra).to.equal("hello2")
98-
done()
99-
})
100-
101-
juicr.dispatch("setText","hello")
102-
});
103-
104-
it('should run reaction when add',function(done){
105-
constjuicr=newJuicr({initialState:{text:'hello'}})
106-
107-
juicr.action("setText",(text)=>{
108-
return{ text}
109-
})
110-
111-
juicr.listen("textExtra",(changedState)=>{
112-
expect(changedState.textExtra).to.equal("hello2")
113-
done()
114-
})
115-
116-
juicr.reaction("text",(changedState)=>{
117-
return{textExtra:changedState.text+"2"}
118-
})
119-
});
120-
121-
// should throw error
122-
// should warn chain reaction
12384
})

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp