Skip to content

Commit

Permalink
feat: add ids in state machines
Browse files Browse the repository at this point in the history
  • Loading branch information
FedericoAmura committed Nov 27, 2024
1 parent 327964e commit 40bbafe
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 8 deletions.
4 changes: 3 additions & 1 deletion packages/automation/src/lib/listeners/fetch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ describe('FetchListener', () => {

it('should fetch data and emit the correct value', async () => {
let callbackCalled: () => void;
const callbackPromise = new Promise<void>(resolve => callbackCalled = resolve);
const callbackPromise = new Promise<void>(
(resolve) => (callbackCalled = resolve)
);

const callback = jest.fn(async () => {
callbackCalled();
Expand Down
7 changes: 7 additions & 0 deletions packages/automation/src/lib/state-machine.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
8 changes: 7 additions & 1 deletion packages/automation/src/lib/state-machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ 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<string, State>();
private transitions = new Map<string, Map<string, Transition>>();
private currentState?: State;
private onStopCallback?: () => Promise<void>;
private debug = false;

constructor(params: BaseStateMachineParams = {}) {
this.id = this.generateId();
this.debug = params.debug ?? false;
}

Expand Down Expand Up @@ -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);
}
}
7 changes: 2 additions & 5 deletions packages/automation/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"],
Expand Down
2 changes: 1 addition & 1 deletion packages/wrapped-keys-lit-actions/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"module": "system",
"module": "commonjs",
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitOverride": true,
Expand Down

0 comments on commit 40bbafe

Please sign in to comment.