Skip to content

Commit

Permalink
Replace Snapshot.prevStates with Machine.getPrevStates()
Browse files Browse the repository at this point in the history
  • Loading branch information
clebert committed Feb 18, 2024
1 parent 893974a commit 5ae3af0
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ seamless integration with TypeScript. It facilitates deterministic behavior by o
encapsulated state machine, user-defined actions and state transformers, as well as automatic stale
snapshot invalidation.

_508 B with all dependencies, minified and gzipped._
_511 B with all dependencies, minified and gzipped._

## Installation

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,19 @@
"size-limit": [
{
"name": "brotli compression",
"limit": "460 B",
"limit": "463 B",
"path": "./lib/mod.js",
"brotli": true
},
{
"name": "gzip compression",
"limit": "508 B",
"limit": "511 B",
"path": "./lib/mod.js",
"gzip": true
},
{
"name": "no compression",
"limit": "460 B",
"limit": "463 B",
"path": "./lib/mod.js",
"gzip": false
}
Expand Down
13 changes: 6 additions & 7 deletions src/create_machine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,17 @@ describe(`createMachine()`, () => {
});

test(`prev states`, () => {
const isRed = trafficLightMachine.assert(`isRed`);
const prevStates = trafficLightMachine.getPrevStates(`isRed`);

// @ts-expect-error ts(1360)
void (isRed.prevStates satisfies ReadonlyArray<'isRed'>);
void (prevStates satisfies ReadonlyArray<'isRed'>);
// @ts-expect-error ts(1360)
void (isRed.prevStates satisfies ReadonlyArray<'isTurningGreen'>);
void (prevStates satisfies ReadonlyArray<'isTurningGreen'>);
// @ts-expect-error ts(1360)
void (isRed.prevStates satisfies ReadonlyArray<'isGreen'>);
void (isRed.prevStates satisfies ReadonlyArray<'isTurningRed'>);
void (prevStates satisfies ReadonlyArray<'isGreen'>);
void (prevStates satisfies ReadonlyArray<'isTurningRed'>);

expect(isRed.prevStates).toEqual([`isTurningRed`]);
expect(prevStates).toEqual([`isTurningRed`]);
});

test(`subscriptions`, () => {
Expand Down Expand Up @@ -194,7 +194,6 @@ describe(`createMachine()`, () => {
expect(() => isRed.state).toThrow(errorMessage);
expect(() => isRed.value).toThrow(errorMessage);
expect(() => isRed.actions).toThrow(errorMessage);
expect(() => isRed.prevStates).toThrow(errorMessage);
expect(() => actions.turnGreen).toThrow(errorMessage);
expect(() => turnGreen(`#FFFF00`)).toThrow(errorMessage);
});
Expand Down
16 changes: 8 additions & 8 deletions src/create_machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export type Machine<
listener: () => void,
options?: { readonly signal?: AbortSignal | undefined },
): () => void;

getPrevStates<TState extends keyof TTransformerMap>(
state: TState,
): readonly InferPrevStateUnion<TTransformerMap, TTransitionsMap, TState>[];
};

export type TransformerMap = Readonly<Record<string, (...args: any[]) => any>>;
Expand All @@ -49,8 +53,6 @@ export interface Snapshot<
...args: Parameters<TTransformerMap[TTransitionsMap[TState][TActionName]]>
) => Snapshot<TTransformerMap, TTransitionsMap, TTransitionsMap[TState][TActionName]>;
};

readonly prevStates: readonly InferPrevStateUnion<TTransformerMap, TTransitionsMap, TState>[];
}

export type InferPrevStateUnion<
Expand Down Expand Up @@ -201,12 +203,6 @@ export function createMachine<

return actions;
},

get prevStates() {
assertVersion();

return prevStatesMap[currentState as string] as any;
},
};
}

Expand Down Expand Up @@ -237,5 +233,9 @@ export function createMachine<

return unsubscribe;
},

getPrevStates(state) {
return prevStatesMap[state as string] as any;
},
};
}

0 comments on commit 5ae3af0

Please sign in to comment.