パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細v3.0.3

state_machine

有限状態機械ライブラリ。簡単に正当な状態遷移を定義できます。状態の入力、出力、遷移をリスンします。

AndroidiOSWebmacOSWindowsLinux
30日間ダウンロード
2.4万
いいね数
41
Pub ポイント
120

パッケージをインストール

flutter pub add state_machine

パッケージ概要

公開情報に基づく選定ガイドです。参考としてご利用ください。

有限状態機械を実装するためのFlutter/Dartライブラリで、開発者が簡単に合法な状態遷移を定義および管理できるようにします。明確なイベント駆動型の状態変更を提供し、状態への入力、出力、遷移イベントのリスニングをサポートしています。状態の一貫性と予測可能な振る舞いが重要な複雑なワークフローのモデル化に最適です。パッケージはプラットフォーム非依存であり、すべての主要なFlutterプラットフォームをサポートしています。状態ロジックをUIコンポーネントから分離することで、クリーンで保守しやすいコードを促進します。

用途

  • 複数ステップのユーザー・フローの管理
  • アプリケーションライフサイクル状態のモデリング
  • 非同期ワークフローの処理
  • フォーム検証ステートマシンの実装
  • ビジネスルールの遵守の強制

主な機能

  • イベント駆動型の状態遷移
  • 状態のエントリ/エグジット/遷移用コールバック
  • 型安全な状態定義
  • ネストされたおよび階層的な状態のサポート
  • 拡張可能でテスト可能な設計

適している対象

  • 複雑な状態を持つアプリを開発する開発者
  • 決定論的な状態管理が必要なチーム
  • 状態変更の監査が必要なプロジェクト
  • 厳格なワークフロールールを持つアプリケーション

注意事項

  • 初心者には学習曲線が急峻です
  • 状態グラフの慎重な計画が必要です
  • 単純な状態シナリオではオーバーヘッドが発生する可能性があります
  • 限定された組み込みUI統合

元の README

関連するソースリポジトリの README 原文のスナップショットです。最新の内容はリポジトリをご覧ください。

Dart State Machine

Pub Build Status codecov.io documentation

Easily create a finite state machine and define legal state transitions. Listen to state entrances, departures, and transitions.

Getting Started

Import the state_machine package:

import 'package:state_machine/state_machine.dart';
Create a Machine

Once created, the StateMachine will be used to create states and state transitions.

StateMachine light = new StateMachine('light');
Define a Set of States

Use the machine to create all required states. A string name is required for ease of debugging.

State isOn = light.newState('on');
State isOff = light.newState('off');

It's recommended that states be named in the format "is[State]". This may seem strange at first, but it has two main benefits:

  1. It helps differentiate states from transitions, which can be confusing since many words in English are the same as a verb and an adjective ("open" or "secure", for example).
  2. It reads better when calling the state to determine if it's active, as will be demonstrated later.
Define the Legal State Transitions

By defining legal state transitions, you can prevent certain actions based on the current state of the machine. Defining a state transition requires a name (again for ease of debugging), a list of valid "from" states, and the state to transition the machine to.

StateTransition turnOn = light.newStateTransition('turnOn', [isOff], isOn);
StateTransition turnOff = light.newStateTransition('turnOff', [isOn], isOff);
Start the Machine

Before executing any state transitions, the machine should be started at a specific starting state.

light.start(isOff);
Executing a State Transition

The StateTransition class implements Function so that you can simply call a transition to execute it.

light.start(isOff);
turnOn(); // transitions machine from "isOff" to "isOn"
Determining the Active State

The StateMachine instance exposes a current state property which allows you to retrieve the machine's current state at any time.

light.start(isOff);
light.current == isOff; // true

Additionally, the State class implements Function so that you can simply call a state to determine if it's active.

light.start(isOff);
isOff(); // true
isOn();  // false
Listening to State Transitions

The StateTransition class exposes a listen() method that allows you to listen to the transition and receive an event every time the transition executes.

turnOn.listen((StateChange change) {
  print('Light transitioned from ${change.from.name} to ${change.to.name}');
});
light.start(isOff);
turnOn(); // "Light transitioned from off to on"
Passing Data with a State Transition

State transitions accept an optional payload in case you need to pass data along to listeners.

turnOn.listen((StateChange change) {
  print('Light turned on. Wattage: ${change.payload}');
});
light.start(isOff);
turnOn('15w'); // "Light turned on. Wattage: 15w"
Listening for State Entrances and Departures

The State class exposes two streams so that you can listen for the state being entered and the state being left.

isOff.onLeave.listen((StateChange change) {
  print('Left: off');
});
isOn.onEnter.listen((StateChange change) {
  print('Entered: on');
});
light.start(isOff);
turnOn(); // "Left: off"
          // "Entered: on"
Wildcard State and State Transitions

The State class exposes a static instance State.any that can be used as a wildcard when defining a state transition.

StateMachine machine = new StateMachine('machine');
State isFailed = machine.newState('failed');

// This transition will be valid regardless of which state the machine is in.
StateTransition fail = machine.newStateTransition('fail', [State.any], isFailed);
Illegal State Transitions

When you create state transitions, you must define the list of valid "from" states. The machine must be in one of these states in order to execute the transition. If that's not the case, an IllegalStateTransition exception will be thrown.

// Consider a door with the following states and transitions.
StateMachine door = new StateMachine('door');

State isOpen = door.newState('open');
State isClosed = door.newState('closed');
State isLocked = door.newState('locked');

StateTransition open = door.newStateTransition('open', [isClosed], isOpen);
StateTransition close = door.newStateTransition('close', [isOpen], isClosed);
StateTransition lock = door.newStateTransition('lock', [isClosed], isLocked);
StateTransition unlock = door.newStateTransition('unlock', [isLocked], isClosed);

// Let's transition the door from open, to closed, to locked.
door.start(isOpen);
close();
lock();

// In order to open the door, we must first unlock it.
// If we try to open it first, an exception will be thrown.
open(); // throws IllegalStateTransition
Canceling State Transitions

State machines have a set of legal state transitions that are set in stone and provide the required structure. But, there may be scenarios where a state transition may or may not be desirable based on additional logic. To handle this, state transitions support cancellation conditions.

// Consider two state machines - a person and a door.
// The door can be locked or unlocked and the person
// can be with or without a key.
StateMachine door = new StateMachine('door');
State isLocked = door.newState('locked');
State isUnlocked = door.newState('unlocked');
StateTransition unlock = door.newStateTransition('unlock', [isLocked], isUnlocked);

StateMachine person = new StateMachine('person');
State isWithKey = person.newState('withKey');
State isWithoutKey = person.newState('withoutKey');
StateTransition obtainKey = person.newStateTransition('obtainKey', [isWithoutKey], isWithKey);

door.start(isLocked);
person.start(isWithoutKey);

// Add a cancellation condition for unlocking the door:
// If the person is without a key, cancel the unlock transition.
unlock.cancelIf((StateChange change) => isWithoutKey());

unlock(); // false (canceled)
isUnlocked(); // false
obtainKey();
unlock(); // true (not canceled)
isUnlocked(); // true

Example

To run the example:

dart pub global activate webdev
webdev serve example:8080
open http://localhost:8080/

カテゴリー

依存関係

依存関係一覧 1 件

  • w_common>=3.0.0 <5.0.0

ソースリポジトリ

このパッケージを使用しているプロジェクト

データはまだありません。