Skip to content

Latest commit

 

History

History
125 lines (107 loc) · 4.09 KB

README-jp.md

File metadata and controls

125 lines (107 loc) · 4.09 KB

English | 日本語

State Machine

npm version Build Status Codacy Badge codecov MIT License

StateMachine

日々怠ける処の開発者、日没すれども帰れぬ処の社畜開発者に致す。 ステートマシンを1から作ったり貧弱なステートマシンのお世話があなたの仕事ですか? 怠けましょう

Poor state
(Before)
if (fsm.current === 'Doing' || fsm.current === 'Waiting' || ...) {
    showProgress();
}

if (fsm.current === 'Complete') { showResult(); } if (fsm.current === 'Error') { showError(); } if (fsm.current === 'Cancel') { showCanceled(); } ...
Rich state
(After)
if (fsm.current.inProgress) {
    showProgress();
}

fsm.current.show();

概要

JavaScriptとTypeScript用のステートマシンです

特徴

  • 読みやすい: ステートマシン定義は読みやすく、簡単に遷移を把握できます
  • ジェネリック型対応: ステート、アクション、オプション引数すべて
  • Rich state: ステートオブジェクトにユーザー定義クラスが使えます
  • ライフサイクル: 生成/破棄
  • 状態遷移図のエクスポート: PlantUML
  • でも学習コストがお高いんでしょ?: 基本機能は1ステップ、Rich stateは更に2ステップ、全機能は更に3ステップのみで習得できます

Quick start

case: String state (最もシンプル)

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);
}

case: Named static state (rich state)

真に驚くべきサンプルがあるが、ここに書くには余白が狭すぎる See samples

case: Typed dynamic state (ライフサイクル付きrich state)

真に驚くべきサンプルがあるが、ここに書くには余白が狭すぎる See samples

予定

  • PlantUMLからの逆変換: 明日やる
  • ドキュメントの充実: 明日やる
  • CLIからの遷移図エクスポート: 明日やる
  • 休憩: 毎日
  • おふとん: 毎日
  • 有能な怠け者になる: もうすぐ
  • 無能な働き者になる: 一昨日

ご満足いただけなかった場合