Skip to content

Commit

Permalink
# 4.2.5 2022-03-22
Browse files Browse the repository at this point in the history
* [bug] repair the problem about when all the connections from agents are disconnected, the method effect may still work, and change state, if it is a asynchronous method.
  • Loading branch information
wangyi committed Mar 21, 2022
1 parent 505bf9f commit 149f1be
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 5 deletions.
6 changes: 5 additions & 1 deletion docs/changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,8 @@ in this version, `runtime.cache` used in MiddleWare is independent.

# 4.2.4 2022-03-21

* [optimize] add validation about effect method getting from `agent`.
* [optimize] add validation about effect method getting from `agent`.

# 4.2.5 2022-03-22

* [bug] repair the problem about when all the connections from agents are disconnected, the method effect may still work, and change state, if it is a asynchronous method.
6 changes: 5 additions & 1 deletion docs/zh/changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,8 @@

# 4.2.4 2022-03-21

* [optimize] 新增对通过 `agent` 获取 effect 修饰方法的非法校验。
* [optimize] 新增对通过 `agent` 获取 effect 修饰方法的非法校验。

# 4.2.5 2022-03-22

* [bug] 修复异步副作用方法在所有代理断连后,依然可以修改 state 的问题。
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "agent-reducer",
"version": "4.2.4",
"version": "4.2.5",
"main": "dist/agent-reducer.mini.js",
"typings": "index.d.ts",
"author": "Jimmy.Harding",
Expand Down
3 changes: 3 additions & 0 deletions src/libs/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ function createEffect<S=any, T extends Model<S> = Model>(
}

export function runningNotInitialedModelEffects<S=any, T extends Model<S> = Model>(model:T):void {
if (!isConnecting<S, T>(model)) {
return;
}
const effects = model[agentEffectsKey] || [];
const notInitialedModelEffects = effects.filter(
(effect) => !effect.initialed,
Expand Down
2 changes: 1 addition & 1 deletion src/libs/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ function createMethodEffectBuilder<
T extends Model<S>= Model<S>
>(entity:T) {
return function methodEffectBuilder(effectMethod:EffectMethod<S, T>, args:any[]) {
return connect<S, T>(entity).run((ag) => effectMethod.apply(ag, args));
return connect<S, T>(entity).run((ag) => { effectMethod.apply(ag, args); });
};
}

Expand Down
70 changes: 69 additions & 1 deletion test/guard/effect.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {addEffect, create} from "../../src";
import {addEffect, create, effect} from "../../src";
import {EffectCallback, Model} from "../../src/libs/global.type";
import {agentEffectsKey} from "../../src/libs/defines";

Expand Down Expand Up @@ -214,4 +214,72 @@ describe("the trigger time about effect", () => {
expect(()=>addEffect(effectCallback, model, model.decrease)).toThrow();
});

});

describe('async effect',()=>{

type User = {
id:number|null,
username:string|null,
password:string|null,
nick:string,
name?:string,
sex?:'male'|'female',
age?:number
};

class UserModel implements Model<User>{

state:User = {
id:null,
username:null,
password:null,
nick:'guest'
};

login(username:string,password:string):User{
return {...this.state,id:null,username,password};
}

private initial(user:Omit<User,'password'>):User{
return {...user,password: null};
}

@effect(()=>UserModel.prototype.login)
private async fetchUser(){
const {username}=this.state;
const user:Omit<User,'password'> = await new Promise((resolve)=>setTimeout(()=>{
resolve({
id:1,
username,
nick:'nick',
name:'name',
sex:'male'
});
}));
this.initial(user);
}

}

test('test disconnect before effect starts action',async ()=>{
const model = new UserModel();
const {agent,connect,disconnect} = create(model);
connect();
agent.login('user','xxx');
disconnect();
await new Promise(resolve => setTimeout(resolve));
expect(model.state.id).toBe(null);
});

test('test disconnect after effect starts action',async ()=>{
const model = new UserModel();
const {agent,connect,disconnect} = create(model);
connect();
agent.login('user','xxx');
await new Promise(resolve => setTimeout(resolve));
disconnect();
expect(model.state.id).toBe(1);
});

});

0 comments on commit 149f1be

Please sign in to comment.