Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: @xstate/store - emits doesn't work #5196

Closed
nmonastyrskyi opened this issue Feb 13, 2025 · 4 comments · Fixed by #5197
Closed

Bug: @xstate/store - emits doesn't work #5196

nmonastyrskyi opened this issue Feb 13, 2025 · 4 comments · Fixed by #5197

Comments

@nmonastyrskyi
Copy link

XState version

XState version 5

Description

emits simply doesn't work. Check the attached stackblitz

import { createStore } from '@xstate/store'

type Context = {
  drawer?: Drawer | null
}

type Drawer = {
  id: string
}

const context: Context = {
  drawer: null,
}

export const drawersBridgeStore = createStore({
  context,
  on: {
    openDrawer: (context, event: { drawer: Drawer }, enqueue) => {
      console.log('OPEN_DRAWER')
      enqueue.emit.drawerOpened(event)

      return {
        ...context,
        drawer: event.drawer,
      }
    },
  },
  emits: {
    drawerOpened: (_payload: { drawer: Drawer }) => {
      console.log('DRAWER_OPENED')
    },
  },
})

drawersBridgeStore.inspect(console.log)

drawersBridgeStore.on('drawerOpened', event => {
  console.log('🚀 ~ connectXstateStore ~ event:', event)
})

Expected result

When I send openDrawer, drawerOpened should be emitted

Actual result

When I send openDrawer, drawerOpened is not emmited

Reproduction

https://stackblitz.com/edit/github-w4mpbuok?file=package.json

Additional context

No response

@davidkpiano
Copy link
Member

It does work; there was a typo in your main.ts file unrelated to XState Store.

Image

@davidkpiano davidkpiano closed this as not planned Won't fix, can't repro, duplicate, stale Feb 13, 2025
@nmonastyrskyi
Copy link
Author

nmonastyrskyi commented Feb 13, 2025

@davidkpiano Unfortunately, it doesn't. if it worked, you would see 'DRAWER_OPENED' in the console, logged by the 'emits'.

@davidkpiano davidkpiano reopened this Feb 13, 2025
@davidkpiano
Copy link
Member

Ah, you're overriding the event type. Will fix this soon, but you can workaround this by only passing in the payload, without type.

@nmonastyrskyi
Copy link
Author

Thanks for the hint!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants