# StateMachineFragment

Base class for reusable state machine fragments.

Extends: Construct › StateMachineFragment
Implements: IChainable, IConstruct, IDependable

- Source: [`aws-stepfunctions/lib/state-machine-fragment.ts`](https://github.com/aws/aws-cdk/blob/v2.275.0/packages/aws-cdk-lib/aws-stepfunctions/lib/state-machine-fragment.ts#L11)

## Example

```ts
import { Stack } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as sfn from 'aws-cdk-lib/aws-stepfunctions';

interface MyJobProps {
  jobFlavor: string;
}

class MyJob extends sfn.StateMachineFragment {
  public readonly startState: sfn.State;
  public readonly endStates: sfn.INextable[];

  constructor(parent: Construct, id: string, props: MyJobProps) {
    super(parent, id);

    const choice = new sfn.Choice(this, 'Choice')
      .when(sfn.Condition.stringEquals('$.branch', 'left'), new sfn.Pass(this, 'Left Branch'))
      .when(sfn.Condition.stringEquals('$.branch', 'right'), new sfn.Pass(this, 'Right Branch'));

    // ...

    this.startState = choice;
    this.endStates = choice.afterwards().endStates;
  }
}

class MyStack extends Stack {
  constructor(scope: Construct, id: string) {
    super(scope, id);
    // Do 3 different variants of MyJob in parallel
    const parallel = new sfn.Parallel(this, 'All jobs')
      .branch(new MyJob(this, 'Quick', { jobFlavor: 'quick' }).prefixStates())
      .branch(new MyJob(this, 'Medium', { jobFlavor: 'medium' }).prefixStates())
      .branch(new MyJob(this, 'Slow', { jobFlavor: 'slow' }).prefixStates());

    new sfn.StateMachine(this, 'MyStateMachine', {
      definitionBody: sfn.DefinitionBody.fromChainable(parallel),
    });
  }
}
```

## Initializer

```ts
new StateMachineFragment(scope: Construct, id: string)
```

## Properties

- `endStates`: [INextable](/cdk/api/v2/docs/aws-cdk-lib.aws_stepfunctions.INextable.html)[] — The states to chain onto if this fragment is used.

- `id`: string — Descriptive identifier for this chainable.

- `node`: [Node](/cdk/api/v2/docs/constructs.Node.html) — The tree node.

- `startState`: [State](/cdk/api/v2/docs/aws-cdk-lib.aws_stepfunctions.State.html) — The start state of this state machine fragment.

## Methods

- `isConstruct(x: any): boolean` (static) — Checks if `x` is a construct.

  Use this method instead of `instanceof` to properly detect `Construct`
  instances, even when the construct library is symlinked.

  Explanation: in JavaScript, multiple copies of the `constructs` library on
  disk are seen as independent, completely different libraries. As a
  consequence, the class `Construct` in each copy of the `constructs` library
  is seen as a different class, and an instance of one class will not test as
  `instanceof` the other class. `npm install` will not create installations
  like this, but users may manually symlink construct libraries together or
  use a monorepo tool: in those cases, multiple copies of the `constructs`
  library can be accidentally installed, and `instanceof` will behave
  unpredictably. It is safest to avoid using `instanceof`, and using
  this type-testing method instead.

  **Parameters**

  - `x` — Any object.

  **Returns:** true if `x` is an object created from a class which extends `Construct`.

- `next(next: IChainable): Chain` — Continue normal execution with the given state.

- `prefixStates(prefix?: string): StateMachineFragment // chainable` — Prefix the IDs of all states in this state machine fragment.

  Use this to avoid multiple copies of the state machine all having the
  same state IDs.

  **Parameters**

  - `prefix` — The prefix to add.

- `toSingleState(options?: SingleStateOptions): Parallel` — Wrap all states in this state machine fragment up into a single state.

  This can be used to add retry or error handling onto this state
  machine fragment.

  Be aware that this changes the result of the inner state machine
  to be an array with the result of the state machine in it. Adjust
  your paths accordingly. For example, change 'outputPath' to
  '$[0]'.

- `toString(): string` — Returns a string representation of this construct.

- `with(...mixins: IMixin[]): IConstruct` — Applies one or more mixins to this construct.

  Mixins are applied in order. The list of constructs is captured at the
  start of the call, so constructs added by a mixin will not be visited.
  Use multiple `with()` calls if subsequent mixins should apply to added
  constructs.

  **Parameters**

  - `mixins` — The mixins to apply.

  **Returns:** This construct for chaining
