From 40bbafe202c19022058193eb35a3a4a562789d19 Mon Sep 17 00:00:00 2001 From: FedericoAmura Date: Wed, 27 Nov 2024 20:29:08 +0100 Subject: [PATCH] feat: add ids in state machines --- packages/automation/src/lib/listeners/fetch.spec.ts | 4 +++- packages/automation/src/lib/state-machine.spec.ts | 7 +++++++ packages/automation/src/lib/state-machine.ts | 8 +++++++- packages/automation/tsconfig.json | 7 ++----- packages/wrapped-keys-lit-actions/tsconfig.json | 2 +- 5 files changed, 20 insertions(+), 8 deletions(-) diff --git a/packages/automation/src/lib/listeners/fetch.spec.ts b/packages/automation/src/lib/listeners/fetch.spec.ts index ed0098ff0..3f881cc83 100644 --- a/packages/automation/src/lib/listeners/fetch.spec.ts +++ b/packages/automation/src/lib/listeners/fetch.spec.ts @@ -28,7 +28,9 @@ describe('FetchListener', () => { it('should fetch data and emit the correct value', async () => { let callbackCalled: () => void; - const callbackPromise = new Promise(resolve => callbackCalled = resolve); + const callbackPromise = new Promise( + (resolve) => (callbackCalled = resolve) + ); const callback = jest.fn(async () => { callbackCalled(); diff --git a/packages/automation/src/lib/state-machine.spec.ts b/packages/automation/src/lib/state-machine.spec.ts index ff7410b9e..7d3649fc4 100644 --- a/packages/automation/src/lib/state-machine.spec.ts +++ b/packages/automation/src/lib/state-machine.spec.ts @@ -38,6 +38,13 @@ describe('StateMachine', () => { }); }); + it('should generate a unique id for each state machine instance', () => { + const anotherStateMachine = new StateMachine(); + expect(stateMachine.id).toBeDefined(); + expect(anotherStateMachine.id).toBeDefined(); + expect(stateMachine.id).not.toEqual(anotherStateMachine.id); + }); + it('should add states and transitions correctly', () => { stateMachine.addTransition({ fromState: 'A', diff --git a/packages/automation/src/lib/state-machine.ts b/packages/automation/src/lib/state-machine.ts index c85dcd816..e915ae269 100644 --- a/packages/automation/src/lib/state-machine.ts +++ b/packages/automation/src/lib/state-machine.ts @@ -18,7 +18,8 @@ type MachineStatus = 'running' | 'stopped'; * A StateMachine class that manages states and transitions between them. */ export class StateMachine { - private status: MachineStatus = 'stopped'; + public id: string; + public status: MachineStatus = 'stopped'; private states = new Map(); private transitions = new Map>(); private currentState?: State; @@ -26,6 +27,7 @@ export class StateMachine { private debug = false; constructor(params: BaseStateMachineParams = {}) { + this.id = this.generateId(); this.debug = params.debug ?? false; } @@ -173,4 +175,8 @@ export class StateMachine { throw new Error(`Could not enter state ${stateKey}`); } } + + private generateId(): string { + return Math.random().toString(36).substring(2); + } } diff --git a/packages/automation/tsconfig.json b/packages/automation/tsconfig.json index d3187ebee..8cb12823a 100644 --- a/packages/automation/tsconfig.json +++ b/packages/automation/tsconfig.json @@ -1,16 +1,13 @@ { "extends": "../../tsconfig.base.json", "compilerOptions": { - "module": "system", + "module": "commonjs", "forceConsistentCasingInFileNames": true, "strict": true, "noImplicitOverride": true, "noPropertyAccessFromIndexSignature": true, "noImplicitReturns": true, - "noFallthroughCasesInSwitch": true, - "allowJs": true, - "checkJs": false, - "resolveJsonModule": false + "noFallthroughCasesInSwitch": true }, "files": [], "include": ["global.d.ts"], diff --git a/packages/wrapped-keys-lit-actions/tsconfig.json b/packages/wrapped-keys-lit-actions/tsconfig.json index d3187ebee..2e798eb14 100644 --- a/packages/wrapped-keys-lit-actions/tsconfig.json +++ b/packages/wrapped-keys-lit-actions/tsconfig.json @@ -1,7 +1,7 @@ { "extends": "../../tsconfig.base.json", "compilerOptions": { - "module": "system", + "module": "commonjs", "forceConsistentCasingInFileNames": true, "strict": true, "noImplicitOverride": true,