generated from DevilTea/starter-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
228 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
{ | ||
"name": "@deviltea/tiny-state-machine-mono", | ||
"type": "module", | ||
"version": "0.0.1", | ||
"private": true, | ||
"packageManager": "[email protected]", | ||
"description": "A simple state machine with tiny size and type safe.", | ||
|
@@ -16,7 +15,7 @@ | |
"keywords": [], | ||
"scripts": { | ||
"build": "lerna run build", | ||
"release": "lerna exec publint && pnpm run typecheck && lerna version && lerna run build && lerna publish", | ||
"release": "lerna exec publint && pnpm run typecheck && lerna run build && lerna publish", | ||
"lint": "eslint --fix .", | ||
"typecheck": "tsc -b --noEmit", | ||
"publint": "lerna exec publint", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
# @deviltea/tiny-state-machine | ||
|
||
[![npm version][npm-version-src]][npm-version-href] | ||
[![npm downloads][npm-downloads-src]][npm-downloads-href] | ||
[![bundle][bundle-src]][bundle-href] | ||
[![License][license-src]][license-href] | ||
|
||
## Installation | ||
|
||
To install the package, use either `npm`, `yarn`, or `pnpm`: | ||
|
||
```bash | ||
npm install @deviltea/tiny-state-machine | ||
``` | ||
|
||
```bash | ||
yarn add @deviltea/tiny-state-machine | ||
``` | ||
|
||
```bash | ||
pnpm add @deviltea/tiny-state-machine | ||
``` | ||
|
||
## Usage | ||
|
||
Here is an example of how to create a simple state machine using `@deviltea/tiny-state-machine`. | ||
|
||
### Example: Basic State Machine | ||
|
||
```typescript | ||
import { createMachine } from '@deviltea/tiny-state-machine' | ||
|
||
// Define a simple state machine with three states: idle, loading, and finished | ||
const machine = createMachine({ | ||
initial: 'idle', | ||
states: { | ||
idle: { | ||
on: { | ||
start: 'loading', | ||
}, | ||
}, | ||
loading: { | ||
on: { | ||
done: 'finished', | ||
}, | ||
}, | ||
finished: {}, | ||
}, | ||
}) | ||
|
||
console.log(machine.currentState) // "idle" | ||
|
||
// Transition to the next state | ||
machine.send('start') | ||
console.log(machine.currentState) // "loading" | ||
|
||
// Finish the transition | ||
machine.send('done') | ||
console.log(machine.currentState) // "finished" | ||
``` | ||
|
||
### Example: Adding Transition Handlers | ||
|
||
You can also add handlers that will be called during state transitions. | ||
|
||
```typescript | ||
import { createMachine } from '@deviltea/tiny-state-machine' | ||
|
||
const machine = createMachine({ | ||
initial: 'idle', | ||
states: { | ||
idle: { | ||
on: { | ||
start: 'loading', | ||
}, | ||
}, | ||
loading: { | ||
on: { | ||
done: 'finished', | ||
}, | ||
}, | ||
finished: {}, | ||
}, | ||
}) | ||
|
||
const unsubscribe = machine.onTransition(({ transition }) => { | ||
console.log( | ||
`Transitioned from ${transition.source} to ${transition.target} via event '${transition.event}'` | ||
) | ||
}) | ||
|
||
machine.send('start') // Logs: Transitioned from idle to loading via event 'start' | ||
machine.send('done') // Logs: Transitioned from loading to finished via event 'done' | ||
|
||
unsubscribe() | ||
``` | ||
|
||
## License | ||
|
||
[MIT](./LICENSE) License © 2023-PRESENT [DevilTea](https://github.com/DevilTea) | ||
|
||
<!-- Badges --> | ||
|
||
[npm-version-src]: https://img.shields.io/npm/v/@deviltea/tiny-state-machine?style=flat&colorA=080f12&colorB=1fa669 | ||
[npm-version-href]: https://npmjs.com/package/@deviltea/tiny-state-machine | ||
[npm-downloads-src]: https://img.shields.io/npm/dm/@deviltea/tiny-state-machine?style=flat&colorA=080f12&colorB=1fa669 | ||
[npm-downloads-href]: https://npmjs.com/package/@deviltea/tiny-state-machine | ||
[bundle-src]: https://img.shields.io/bundlephobia/minzip/@deviltea/tiny-state-machine?style=flat&colorA=080f12&colorB=1fa669&label=minzip | ||
[bundle-href]: https://bundlephobia.com/result?p=@deviltea/tiny-state-machine | ||
[license-src]: https://img.shields.io/github/license/DevilTea/tiny-state-machine.svg?style=flat&colorA=080f12&colorB=1fa669 | ||
[license-href]: https://github.com/DevilTea/tiny-state-machine/blob/main/LICENSE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
# @deviltea/tiny-state-machine-vue | ||
|
||
[![npm version][npm-version-src]][npm-version-href] | ||
[![npm downloads][npm-downloads-src]][npm-downloads-href] | ||
[![bundle][bundle-src]][bundle-href] | ||
[![License][license-src]][license-href] | ||
|
||
## Installation | ||
|
||
To install the package, use either `npm`, `yarn`, or `pnpm`: | ||
|
||
```bash | ||
npm install @deviltea/tiny-state-machine | ||
``` | ||
|
||
```bash | ||
yarn add @deviltea/tiny-state-machine | ||
``` | ||
|
||
```bash | ||
pnpm add @deviltea/tiny-state-machine | ||
``` | ||
|
||
## Usage | ||
|
||
Here is an example of how to create a simple state machine using `@deviltea/tiny-state-machine`. | ||
|
||
### Example: Basic State Machine | ||
|
||
```typescript | ||
import { createMachine } from '@deviltea/tiny-state-machine' | ||
|
||
// Define a simple state machine with three states: idle, loading, and finished | ||
const machine = createMachine({ | ||
initial: 'idle', | ||
states: { | ||
idle: { | ||
on: { | ||
start: 'loading', | ||
}, | ||
}, | ||
loading: { | ||
on: { | ||
done: 'finished', | ||
}, | ||
}, | ||
finished: {}, | ||
}, | ||
}) | ||
|
||
console.log(machine.currentState) // "idle" | ||
|
||
// Transition to the next state | ||
machine.send('start') | ||
console.log(machine.currentState) // "loading" | ||
|
||
// Finish the transition | ||
machine.send('done') | ||
console.log(machine.currentState) // "finished" | ||
``` | ||
|
||
### Example: Adding Transition Handlers | ||
|
||
You can also add handlers that will be called during state transitions. | ||
|
||
```typescript | ||
import { createMachine } from '@deviltea/tiny-state-machine' | ||
|
||
const machine = createMachine({ | ||
initial: 'idle', | ||
states: { | ||
idle: { | ||
on: { | ||
start: 'loading', | ||
}, | ||
}, | ||
loading: { | ||
on: { | ||
done: 'finished', | ||
}, | ||
}, | ||
finished: {}, | ||
}, | ||
}) | ||
|
||
const unsubscribe = machine.onTransition(({ transition }) => { | ||
console.log( | ||
`Transitioned from ${transition.source} to ${transition.target} via event '${transition.event}'` | ||
) | ||
}) | ||
|
||
machine.send('start') // Logs: Transitioned from idle to loading via event 'start' | ||
machine.send('done') // Logs: Transitioned from loading to finished via event 'done' | ||
|
||
unsubscribe() | ||
``` | ||
|
||
## License | ||
|
||
[MIT](./LICENSE) License © 2023-PRESENT [DevilTea](https://github.com/DevilTea) | ||
|
||
<!-- Badges --> | ||
|
||
[npm-version-src]: https://img.shields.io/npm/v/@deviltea/tiny-state-machine-vue?style=flat&colorA=080f12&colorB=1fa669 | ||
[npm-version-href]: https://npmjs.com/package/@deviltea/tiny-state-machine-vue | ||
[npm-downloads-src]: https://img.shields.io/npm/dm/@deviltea/tiny-state-machine-vue?style=flat&colorA=080f12&colorB=1fa669 | ||
[npm-downloads-href]: https://npmjs.com/package/@deviltea/tiny-state-machine-vue | ||
[bundle-src]: https://img.shields.io/bundlephobia/minzip/@deviltea/tiny-state-machine-vue?style=flat&colorA=080f12&colorB=1fa669&label=minzip | ||
[bundle-href]: https://bundlephobia.com/result?p=@deviltea/tiny-state-machine-vue | ||
[license-src]: https://img.shields.io/github/license/DevilTea/tiny-state-machine.svg?style=flat&colorA=080f12&colorB=1fa669 | ||
[license-href]: https://github.com/DevilTea/tiny-state-machine/blob/main/LICENSE |