English | 日本語
日々怠ける処の開発者、日没すれども帰れぬ処の社畜開発者に致す。 ステートマシンを1から作ったり貧弱なステートマシンのお世話があなたの仕事ですか? 怠けましょう
Poor state (Before) |
if (fsm.current === 'Doing' || fsm.current === 'Waiting' || ...) { showProgress(); } |
---|---|
Rich state (After) |
if (fsm.current.inProgress) { showProgress(); } |
JavaScriptとTypeScript用のステートマシンです
- 読みやすい: ステートマシン定義は読みやすく、簡単に遷移を把握できます
- ジェネリック型対応: ステート、アクション、オプション引数すべて
- Rich state: ステートオブジェクトにユーザー定義クラスが使えます
- ライフサイクル: 生成/破棄
- 状態遷移図のエクスポート: PlantUML
- でも学習コストがお高いんでしょ?: 基本機能は1ステップ、Rich stateは更に2ステップ、全機能は更に3ステップのみで習得できます
import { StateMachine } from '@working-sloth/state-machine';
enum SlothState {
Idle = 'Idle',
...
}
enum SlothAction {
Sleep = 'Sleep',
...
}
const fsm = StateMachine.fromString<SlothState, SlothAction>(
'Sloth State', // state machine name
SlothState.Idle, // start state
{
state: SlothState.Idle,
transitions: [
[SlothAction.Sleep, SlothState.Sleeping],
[SlothAction.Eat, SlothState.Eating],
]
}, {
state: SlothState.Sleeping,
transitions: [
[SlothAction.Wake, SlothState.Idle],
]
},
...
);
fsm.start(); // Don't forget
console.log(fsm.current); // You can get current state
if (fsm.can(SlothAction.Sleep)) {
fsm.do(SlothAction.Sleep);
}
真に驚くべきサンプルがあるが、ここに書くには余白が狭すぎる See samples
真に驚くべきサンプルがあるが、ここに書くには余白が狭すぎる See samples
- PlantUMLからの逆変換: 明日やる
- ドキュメントの充実: 明日やる
- CLIからの遷移図エクスポート: 明日やる
- 休憩: 毎日
- おふとん: 毎日
- 有能な怠け者になる: もうすぐ
- 無能な働き者になる: 一昨日
- 「question」または「enhancement」としてIssueを作成
- e-mail:koba.work.work1127@gmail.com