Skip to content

Conversation

@davidkpiano
Copy link
Member

@davidkpiano davidkpiano commented Feb 18, 2024

The new emit(…) action creator emits events that can be received by listeners. Actors are now event emitters.

import { emit } from 'xstate';

const machine = createMachine({
  // ...
  on: {
    something: {
      actions: emit({
        type: 'emitted',
        some: 'data'
      })
    }
  }
  // ...
});

const actor = createActor(machine).start();

actor.on('emitted', (event) => {
  console.log(event);
});

actor.send({ type: 'something' });
// logs:
// {
//   type: 'emitted',
//   some: 'data'
// }

@changeset-bot
Copy link

changeset-bot bot commented Feb 18, 2024

🦋 Changeset detected

Latest commit: 10f8dca

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
xstate Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@davidkpiano davidkpiano requested a review from Andarist February 18, 2024 18:59
@codesandbox-ci
Copy link

codesandbox-ci bot commented Feb 18, 2024

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@davidkpiano davidkpiano merged commit b570ba2 into main Mar 1, 2024
@davidkpiano davidkpiano deleted the davidkpiano/event-emitter branch March 1, 2024 18:24
@github-actions github-actions bot mentioned this pull request Mar 1, 2024
oscarmarina pushed a commit to oscarmarina/xstate that referenced this pull request Mar 3, 2024
* Add basic event emitter

* Remove id and delay

* Fix types

* Rename

* Add machine types

* Add TEmitted type... everywhere

* Avoid upsetting devs who rely on order of ActorLogic<…> generics

* Same for ActorScope<…>

* Update packages/core/src/actions/emit.ts

Co-authored-by: Mateusz Burzyński <[email protected]>

* Update packages/core/src/actions/emit.ts

Co-authored-by: Mateusz Burzyński <[email protected]>

* Update packages/core/src/State.ts

Co-authored-by: Mateusz Burzyński <[email protected]>

* Update packages/core/src/actions/emit.ts

Co-authored-by: Mateusz Burzyński <[email protected]>

* Update packages/core/src/actions/emit.ts

Co-authored-by: Mateusz Burzyński <[email protected]>

* Update packages/core/src/actions/emit.ts

Co-authored-by: Mateusz Burzyński <[email protected]>

* Update packages/core/test/types.test.ts

Co-authored-by: Mateusz Burzyński <[email protected]>

* Update packages/core/src/createMachine.ts

Co-authored-by: Mateusz Burzyński <[email protected]>

* Update packages/core/src/actions/emit.ts

Co-authored-by: Mateusz Burzyński <[email protected]>

* Fix TS error

* Add emit to enqueueActions

* Add default

* Wrap handler

* Check for errors

* Add changeset

* Types

* small tweaks

* fix types

* tweak things

* fix small issues around listeners management

* rename stuff

* tighten up one default

* remove unused type

* fixed `MachineImplementationsActions`

* No need for defer

* Add test

* rewrite test to make it fail correctly

* defer again

* Add jsdocs

* Update packages/core/src/actions/emit.ts

---------

Co-authored-by: Mateusz Burzyński <[email protected]>
oscarmarina pushed a commit to oscarmarina/xstate that referenced this pull request Jun 26, 2024
* Add basic event emitter

* Remove id and delay

* Fix types

* Rename

* Add machine types

* Add TEmitted type... everywhere

* Avoid upsetting devs who rely on order of ActorLogic<…> generics

* Same for ActorScope<…>

* Update packages/core/src/actions/emit.ts

Co-authored-by: Mateusz Burzyński <[email protected]>

* Update packages/core/src/actions/emit.ts

Co-authored-by: Mateusz Burzyński <[email protected]>

* Update packages/core/src/State.ts

Co-authored-by: Mateusz Burzyński <[email protected]>

* Update packages/core/src/actions/emit.ts

Co-authored-by: Mateusz Burzyński <[email protected]>

* Update packages/core/src/actions/emit.ts

Co-authored-by: Mateusz Burzyński <[email protected]>

* Update packages/core/src/actions/emit.ts

Co-authored-by: Mateusz Burzyński <[email protected]>

* Update packages/core/test/types.test.ts

Co-authored-by: Mateusz Burzyński <[email protected]>

* Update packages/core/src/createMachine.ts

Co-authored-by: Mateusz Burzyński <[email protected]>

* Update packages/core/src/actions/emit.ts

Co-authored-by: Mateusz Burzyński <[email protected]>

* Fix TS error

* Add emit to enqueueActions

* Add default

* Wrap handler

* Check for errors

* Add changeset

* Types

* small tweaks

* fix types

* tweak things

* fix small issues around listeners management

* rename stuff

* tighten up one default

* remove unused type

* fixed `MachineImplementationsActions`

* No need for defer

* Add test

* rewrite test to make it fail correctly

* defer again

* Add jsdocs

* Update packages/core/src/actions/emit.ts

---------

Co-authored-by: Mateusz Burzyński <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants