From 0095a41a237fd347bc115bb340b08692f3cc7740 Mon Sep 17 00:00:00 2001 From: zksu <565338107@qq.com> Date: Thu, 23 May 2024 09:23:40 +0800 Subject: [PATCH 1/6] =?UTF-8?q?chore(docs):=20=E7=BA=A0=E6=AD=A3=20(=20?= =?UTF-8?q?=E5=B0=8F=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/quick-starts.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/quick-starts.md b/docs/quick-starts.md index 146a860..d7d2c9f 100644 --- a/docs/quick-starts.md +++ b/docs/quick-starts.md @@ -47,10 +47,10 @@ class TcpClient extends FlexStateMachine{ },options)) } - @state{ + @state({ when:["Initial","Disconnected","Error"], // 代表只能当处于此三种状态时才允许调用连接方法 pending:"Connecting", // 执行后进入正在连接中的状态 - } + }) connect(){ this._socket.connect(this.options) } -- Gitee From ba23df055c00a86f50ed8d0de490b100e3c07916 Mon Sep 17 00:00:00 2001 From: zksu <565338107@qq.com> Date: Thu, 23 May 2024 09:41:22 +0800 Subject: [PATCH 2/6] =?UTF-8?q?chore:=20=E6=B7=BB=E5=8A=A0=E4=BC=A0?= =?UTF-8?q?=E5=85=A5=E4=BB=BB=E6=84=8F=E5=8F=82=E6=95=B0,=E4=BB=A5?= =?UTF-8?q?=E4=BE=BF=E5=8F=AF=E4=BB=A5=E4=BC=A0=E5=85=A5=20onState1Enter?= =?UTF-8?q?=E8=BF=99=E6=A0=B7=E7=9A=84=E5=87=BD=E6=95=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/flexstate.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/flexstate.ts b/src/flexstate.ts index 33b85c1..c1639b3 100644 --- a/src/flexstate.ts +++ b/src/flexstate.ts @@ -155,8 +155,8 @@ export interface FlexStateMachineContext extends FlexStateTransitionHooks{ * 状态机构造参数 */ export interface FlexStateOptions extends FlexStateTransitionHooks,FlexEventOptions{ - name? : string, // 当前状态机名称 - states? : FlexStateMap, // 状态声明 + name? : string, // 当前状态机名称 + states? : FlexStateMap, // 状态声明 parent? : FlexState, // 父状态实例 context? : any, // 当执行动作或状态转换事件时的this指向 autoStart? : boolean, // 是否自动开始运行状态机,=false需要调用.start() @@ -166,6 +166,7 @@ export interface FlexStateOptions extends FlexStateTransitionHooks,FlexEventOpti injectStateValue? : boolean, // 在实例中注入:大写状态名称的字段,其值 =状态值 history? : number // 记录状态转换历史,0=不记录,N=最大记录N条历史 scope? : FlexStateMachine, // 内部独立子状态域 + [props: string] : any // 主要方便添加随机函数(onState1Enter....).不然ts 使用传入会报错(实例化时传入任意函数) } -- Gitee From da74ad9f022a7017d141a17a3418aeb566ed5157 Mon Sep 17 00:00:00 2001 From: zksu <565338107@qq.com> Date: Thu, 23 May 2024 10:55:24 +0800 Subject: [PATCH 3/6] chore: ignore --- .gitignore | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 2758f31..c0e2ece 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,6 @@ /.yalc /yarn.lock /yalc.lock -/coverage \ No newline at end of file +/coverage + +dist -- Gitee From 5b4e51eba2f407965a21c233f6566e8e1f6c1527 Mon Sep 17 00:00:00 2001 From: zksu <565338107@qq.com> Date: Thu, 23 May 2024 10:55:53 +0800 Subject: [PATCH 4/6] =?UTF-8?q?chore:=20=E6=B7=BB=E5=8A=A0=E6=9C=AC?= =?UTF-8?q?=E5=9C=B0=E5=8F=91=E5=B8=83script?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 5f3d821..824d80c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "flexstate", - "version": "1.0.20", + "version": "1.0.21", "description": "Flexible, friendly and easy-to-use finite state machine", "main": "./dist/index.js", "types": "./dist/index.d.ts", @@ -10,7 +10,8 @@ "scripts": { "test": "vitest --coverage", "build": "tsup", - "release": "tsup && npm version patch && npm publish" + "release": "tsup && npm version patch && npm publish", + "publish:local": "yalc publish" }, "author": "wxzhang@126.com", "license": "MIT", -- Gitee From 8b4fe2feb6998d4010ac6bfb42d115084f483a03 Mon Sep 17 00:00:00 2001 From: zksu <565338107@qq.com> Date: Thu, 23 May 2024 10:56:29 +0800 Subject: [PATCH 5/6] =?UTF-8?q?chore:=20=E6=B7=BB=E5=8A=A0=E6=B3=A8?= =?UTF-8?q?=E9=87=8A.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/flexstate.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/flexstate.ts b/src/flexstate.ts index c1639b3..7251c1b 100644 --- a/src/flexstate.ts +++ b/src/flexstate.ts @@ -816,7 +816,7 @@ export class FlexStateMachine extends FlexEvent{ // 保存冲突方法的引用,当执行unregister(action)时可以恢复 if(!this.#conflictMethods) this.#conflictMethods=[this.context[actionName]] this.#conflictMethods[actionName] = this.context[actionName] - console.warn("异步状态机注入的动作在实例上已经存在同名方法") + console.warn("异步状态机注入的动作在实例上已经存在同名方法,方法名为: "+ actionName) } // 通过触发事件的方式来动作执行 this.context[actionName] = (...args:any[])=>{ -- Gitee From ead3ea6b14c1fc5c9d84a5e2e7f80b1e7983ebc9 Mon Sep 17 00:00:00 2001 From: zksu <565338107@qq.com> Date: Thu, 23 May 2024 13:46:11 +0800 Subject: [PATCH 6/6] =?UTF-8?q?test:=20=E6=B7=BB=E5=8A=A0=E7=9B=B4?= =?UTF-8?q?=E6=8E=A5=E5=AE=9E=E4=BE=8B=E7=8A=B6=E6=80=81=E6=9C=BA=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/__tests__/index.test.ts | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/__tests__/index.test.ts b/src/__tests__/index.test.ts index 287ab72..744ee41 100644 --- a/src/__tests__/index.test.ts +++ b/src/__tests__/index.test.ts @@ -97,6 +97,41 @@ describe("状态机",()=>{ }) describe("状态转换",()=>{ + test("直接实例状态机并指定上下文, 切换状态", async () => { + enum STATUS { + A = "A", + B = "B", + C = "C", + } + class Context { + static states = { + [STATUS.A]: { value: 1, initial: true, next: [STATUS.B] }, + [STATUS.B]: { value: 2, next: STATUS.C }, + [STATUS.C]: { value: 3, final: true, next: [STATUS.A] }, + }; + @state({ + when: [STATUS.A], + pending: [STATUS.B], + resolved: [STATUS.C], + }) + async load() { + const timeout = 1000; + await new Promise((resolve) => setTimeout(resolve, timeout)); + } + fsm: FlexStateMachine; + constructor() { + this.fsm = new FlexStateMachine({ + context: this, + }); + } + onTransition() { + console.log(`b`); + } + } + const c = new Context(); + await c.load(); + expect(c.fsm.current.name).toBe(STATUS.C); + }); test("正常状态切换",async () => { let beginStates:string[] = [], endStates:string[] = [] @@ -994,4 +1029,4 @@ describe("错误处理", () => { } }) -}) \ No newline at end of file +}) -- Gitee