# How to implement state pattern in match api?

**URL:** <https://forum.heroiclabs.com/t/how-to-implement-state-pattern-in-match-api/3297>\
**Category:** Runtime Framework\
**Tags:** matches\
**Created:** [November 27, 2022, 10:40am UTC](https://forum.heroiclabs.com/t/how-to-implement-state-pattern-in-match-api/3297 "2022-11-27T10:40:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tom-willmowski](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/tom-willmowski/32/1415_2.png) [@tom-willmowski](https://forum.heroiclabs.com/u/tom-willmowski)\
**Post date:** [November 27, 2022, 10:40am UTC](https://forum.heroiclabs.com/t/how-to-implement-state-pattern-in-match-api/3297/1 "2022-11-27T10:40:34Z")

</div>

I would like to pass my StateMachine class as MatchState parameter to make my life easier,  
but I’m getting stack overflow.

Here is the code snipped and error log:

> <https://gist.github.com/tom-willmowski/37f5771c9983ba03546cb37909b2d139>

Here is my tsconfig.json:

```auto
{
  "compilerOptions": {
    /* Visit https://aka.ms/tsconfig to read more about this file */
    "typeRoots": ["./node_modules"],
    "target": "ES5", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
    "outFile": "./build/index.js", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
    "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
    // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
    "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */

    /* Type Checking */
    "strict": true, /* Enable all strict type-checking options. */
    "strictPropertyInitialization": true
  },
  "include": [
    "./src/**/*"
  ],
  "exclude": [
    "node_modules",
    "build"
  ],
}

```

---

<div class="post-metadata">

**Author:** ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)\
**Post date:** [November 28, 2022, 11:40am UTC](https://forum.heroiclabs.com/t/how-to-implement-state-pattern-in-match-api/3297/2 "2022-11-28T11:40:03Z")

</div>

It looks like you’re creating a self-referential loop as you’re passing `machine` to itself within the `.change()` method.  
This causes issues because Nakama has to walk the state object to apply some reference changes. You’ll need to change your code to prevent this self-reference.

Storing objects in the `state` is also not recommended as it’s not fully supported and can cause issues in some cases, it would be best to make it a POJO and have some class or plain functions to manipulate it.

Hope this helps

---

<div class="post-metadata">

**Author:** ![tom-willmowski](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/tom-willmowski/32/1415_2.png) [@tom-willmowski](https://forum.heroiclabs.com/u/tom-willmowski)\
**Post date:** [December 2, 2022, 5:51pm UTC](https://forum.heroiclabs.com/t/how-to-implement-state-pattern-in-match-api/3297/3 "2022-12-02T17:51:09Z")

</div>

Thank you for the answer! It make sense.
