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

Put all special events under xstate.* namespace #4270

Merged
merged 8 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lazy-panthers-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'xstate': major
---

All events automatically generated by XState will now be prefixed by `xstate.`. Naming scheme changed slightly as well, for example `done.invoke.*` events became `xstate.done.actor.*` events.
2 changes: 1 addition & 1 deletion docs/fr/guides/actors.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ const someMachine = createMachine({

## Spawning Promises

Just like [invoking promises](./communication.md#invoking-promises), promises can be spawned as actors. The event sent back to the machine will be a `'done.invoke.<ID>'` action with the promise response as the `data` property in the payload:
Just like [invoking promises](./communication.md#invoking-promises), promises can be spawned as actors. The event sent back to the machine will be a `'xstate.done.actor.<ID>'` action with the promise response as the `data` property in the payload:

```js {11}
// Returns a promise
Expand Down
12 changes: 6 additions & 6 deletions docs/fr/guides/communication.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ const userMachine = createMachine({
});
```

The resolved data is placed into a `'done.invoke.<id>'` event, under the `data` property, e.g.:
The resolved data is placed into a `'xstate.done.actor.<id>'` event, under the `data` property, e.g.:

```js
{
type: 'done.invoke.getUser',
type: 'xstate.done.actor.getUser',
data: {
name: 'David',
location: 'Florida'
Expand All @@ -128,7 +128,7 @@ The resolved data is placed into a `'done.invoke.<id>'` event, under the `data`

### Promise Rejection

If a Promise rejects, the `onError` transition will be taken with a `{ type: 'error.platform' }` event. The error data is available on the event's `data` property:
If a Promise rejects, the `onError` transition will be taken with a `{ type: 'xstate.error.actor' }` event. The error data is available on the event's `data` property:

```js
const search = (context, event) =>
Expand Down Expand Up @@ -165,7 +165,7 @@ const searchMachine = createMachine({
actions: assign({
errorMessage: (context, event) => {
// event is:
// { type: 'error.platform', data: 'No query specified' }
// { type: 'xstate.error.actor', data: 'No query specified' }
return event.data;
}
})
Expand Down Expand Up @@ -429,7 +429,7 @@ The `data` _replaces_ the default `context` defined on the machine; it is not me

### Done Data

When a child machine reaches its top-level [final state](./final.md), it can send data in the "done" event (e.g., `{ type: 'done.invoke.someId', data: ... }`). This "done data" is specified on the final state's `data` property:
When a child machine reaches its top-level [final state](./final.md), it can send data in the "done" event (e.g., `{ type: 'xstate.done.actor.someId', data: ... }`). This "done data" is specified on the final state's `data` property:

```js
const secretMachine = createMachine({
Expand Down Expand Up @@ -469,7 +469,7 @@ const parentMachine = createMachine({
actions: assign({
revealedSecret: (context, event) => {
// event is:
// { type: 'done.invoke.secret', data: { secret: '42' } }
// { type: 'xstate.done.actor.secret', data: { secret: '42' } }
return event.data.secret;
}
})
Expand Down
2 changes: 1 addition & 1 deletion docs/fr/tutorials/reddit.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ const redditMachine = createMachine({

Notice how we moved the `invoke` config to the `'loading'` state. This is useful because if we want to change the app logic in the future to have some sort of `'paused'` or `'canceled'` child state, the invoked promise will automatically be "canceled" since it's no longer in the `'loading'` state where it was invoked.

When the promise resolves, a special `'done.invoke.<invoke ID>'` event will be sent to the machine, containing the resolved data as `event.data`. For convenience, XState maps the `onDone` property within the `invoke` object to this special event. You can assign the resolved data to `context.posts`:
When the promise resolves, a special `'xstate.done.actor.<invoke ID>'` event will be sent to the machine, containing the resolved data as `event.data`. For convenience, XState maps the `onDone` property within the `invoke` object to this special event. You can assign the resolved data to `context.posts`:

```js {18-20}
const redditMachine = createMachine({
Expand Down
2 changes: 1 addition & 1 deletion docs/guides/actors.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ const someMachine = createMachine({

## Spawning Promises

Just like [invoking promises](./communication.md#invoking-promises), promises can be spawned as actors. The event sent back to the machine will be a `'done.invoke.<ID>'` action with the promise response as the `data` property in the payload:
Just like [invoking promises](./communication.md#invoking-promises), promises can be spawned as actors. The event sent back to the machine will be a `'xstate.done.actor.<ID>'` action with the promise response as the `data` property in the payload:

```js {11}
// Returns a promise
Expand Down
12 changes: 6 additions & 6 deletions docs/guides/communication.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ const userMachine = createMachine({
});
```

The resolved data is placed into a `'done.invoke.<id>'` event, under the `data` property, e.g.:
The resolved data is placed into a `'xstate.done.actor.<id>'` event, under the `data` property, e.g.:

```js
{
type: 'done.invoke.getUser',
type: 'xstate.done.actor.getUser',
data: {
name: 'David',
location: 'Florida'
Expand All @@ -132,7 +132,7 @@ The resolved data is placed into a `'done.invoke.<id>'` event, under the `data`

### Promise Rejection

If a Promise rejects, the `onError` transition will be taken with a `{ type: 'error.platform' }` event. The error data is available on the event's `data` property:
If a Promise rejects, the `onError` transition will be taken with a `{ type: 'xstate.error.actor' }` event. The error data is available on the event's `data` property:

```js
const search = (context, event) =>
Expand Down Expand Up @@ -169,7 +169,7 @@ const searchMachine = createMachine({
actions: assign({
errorMessage: (context, event) => {
// event is:
// { type: 'error.platform', data: 'No query specified' }
// { type: 'xstate.error.actor', data: 'No query specified' }
return event.data;
}
})
Expand Down Expand Up @@ -433,7 +433,7 @@ The `data` _replaces_ the default `context` defined on the machine; it is not me

### Done Data

When a child machine reaches its top-level [final state](./final.md), it can send data in the "done" event (e.g., `{ type: 'done.invoke.someId', data: ... }`). This "done data" is specified on the final state's `data` property:
When a child machine reaches its top-level [final state](./final.md), it can send data in the "done" event (e.g., `{ type: 'xstate.done.actor.someId', data: ... }`). This "done data" is specified on the final state's `data` property:

```js
const secretMachine = createMachine({
Expand Down Expand Up @@ -473,7 +473,7 @@ const parentMachine = createMachine({
actions: assign({
revealedSecret: (context, event) => {
// event is:
// { type: 'done.invoke.secret', data: { secret: '42' } }
// { type: 'xstate.done.actor.secret', data: { secret: '42' } }
return event.data.secret;
}
})
Expand Down
2 changes: 1 addition & 1 deletion docs/tutorials/reddit.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ const redditMachine = createMachine({

Notice how we moved the `invoke` config to the `'loading'` state. This is useful because if we want to change the app logic in the future to have some sort of `'paused'` or `'canceled'` child state, the invoked promise will automatically be "canceled" since it's no longer in the `'loading'` state where it was invoked.

When the promise resolves, a special `'done.invoke.<invoke ID>'` event will be sent to the machine, containing the resolved data as `event.data`. For convenience, XState maps the `onDone` property within the `invoke` object to this special event. You can assign the resolved data to `context.posts`:
When the promise resolves, a special `'xstate.done.actor.<invoke ID>'` event will be sent to the machine, containing the resolved data as `event.data`. For convenience, XState maps the `onDone` property within the `invoke` object to this special event. You can assign the resolved data to `context.posts`:

```js {18-20}
const redditMachine = createMachine({
Expand Down
2 changes: 1 addition & 1 deletion docs/zh/guides/actors.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ const someMachine = createMachine({

## 创建 Promises

就像 [invoking promises](./communication.md#invoking-promises) 一样,promise 可以作为 演员 生成。 发送回状态机的事件将是一个 `'done.invoke.<ID>'` 操作,promise 响应作为有效负载中的 `data` 属性:
就像 [invoking promises](./communication.md#invoking-promises) 一样,promise 可以作为 演员 生成。 发送回状态机的事件将是一个 `'xstate.done.actor.<ID>'` 操作,promise 响应作为有效负载中的 `data` 属性:

```js {11}
// Returns a promise
Expand Down
12 changes: 6 additions & 6 deletions docs/zh/guides/communication.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ const userMachine = createMachine({
});
```

解析后的数据被放置在一个 `'done.invoke.<id>'` 事件中,在 `data` 属性下,例如:
解析后的数据被放置在一个 `'xstate.done.actor.<id>'` 事件中,在 `data` 属性下,例如:

```js
{
type: 'done.invoke.getUser',
type: 'xstate.done.actor.getUser',
data: {
name: 'David',
location: 'Florida'
Expand All @@ -128,7 +128,7 @@ const userMachine = createMachine({

### Promise Rejection

如果 Promise 拒绝,则将使用 `{ type: 'error.platform' }` 事件进行 `onError` 转换。 错误数据在事件的 `data` 属性中可用:
如果 Promise 拒绝,则将使用 `{ type: 'xstate.error.actor' }` 事件进行 `onError` 转换。 错误数据在事件的 `data` 属性中可用:

```js
const search = (context, event) =>
Expand Down Expand Up @@ -165,7 +165,7 @@ const searchMachine = createMachine({
actions: assign({
errorMessage: (context, event) => {
// event is:
// { type: 'error.platform', data: 'No query specified' }
// { type: 'xstate.error.actor', data: 'No query specified' }
return event.data;
}
})
Expand Down Expand Up @@ -428,7 +428,7 @@ data: (context, event) => ({

### 完成数据

当子状态机到达其顶级[最终状态](./final.md)时,它可以在“done”事件中发送数据(例如,`{ type: 'done.invoke.someId', data: .. .}`)。 这个“完成的数据”是在最终状态的`data`属性上指定的:
当子状态机到达其顶级[最终状态](./final.md)时,它可以在“done”事件中发送数据(例如,`{ type: 'xstate.done.actor.someId', data: .. .}`)。 这个“完成的数据”是在最终状态的`data`属性上指定的:

```js
const secretMachine = createMachine({
Expand Down Expand Up @@ -468,7 +468,7 @@ const parentMachine = createMachine({
actions: assign({
revealedSecret: (context, event) => {
// event is:
// { type: 'done.invoke.secret', data: { secret: '42' } }
// { type: 'xstate.done.actor.secret', data: { secret: '42' } }
return event.data.secret;
}
})
Expand Down
2 changes: 1 addition & 1 deletion docs/zh/tutorials/reddit.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ const redditMachine = createMachine({

Notice how we moved the `invoke` config to the `'loading'` state. This is useful because if we want to change the app logic in the future to have some sort of `'paused'` or `'canceled'` child state, the invoked promise will automatically be "canceled" since it's no longer in the `'loading'` state where it was invoked.

When the promise resolves, a special `'done.invoke.<invoke ID>'` event will be sent to the machine, containing the resolved data as `event.data`. For convenience, XState maps the `onDone` property within the `invoke` object to this special event. You can assign the resolved data to `context.posts`:
When the promise resolves, a special `'xstate.done.actor.<invoke ID>'` event will be sent to the machine, containing the resolved data as `event.data`. For convenience, XState maps the `onDone` property within the `invoke` object to this special event. You can assign the resolved data to `context.posts`:

```js {18-20}
const redditMachine = createMachine({
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/StateMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import type {
Equals,
TODO
} from './types.ts';
import { isErrorEvent, resolveReferencedActor } from './utils.ts';
import { isErrorActorEvent, resolveReferencedActor } from './utils.ts';

export const STATE_IDENTIFIER = '#';
export const WILDCARD = '*';
Expand Down Expand Up @@ -245,7 +245,7 @@ export class StateMachine<
): State<TContext, TEvent, TActor, TTag, TOutput, TResolvedTypesMeta> {
// TODO: handle error events in a better way
if (
isErrorEvent(event) &&
isErrorActorEvent(event) &&
!state.nextEvents.some((nextEvent) => nextEvent === event.type)
) {
return cloneState(state, {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/StateNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export class StateNode<
*/
public meta?: any;
/**
* The output data sent with the "done.state._id_" event if this is a final state node.
* The output data sent with the "xstate.done.state._id_" event if this is a final state node.
*/
public output?: Mapper<TContext, TEvent, any>;

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/actions/invoke.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import isDevelopment from '#is-development';
import { cloneState } from '../State.ts';
import { createErrorPlatformEvent } from '../eventUtils.ts';
import { createErrorActorEvent } from '../eventUtils.ts';
import { ActorStatus, createActor } from '../interpreter.ts';
import {
ActionArgs,
Expand Down Expand Up @@ -89,7 +89,7 @@ function execute(
try {
actorRef.start?.();
} catch (err) {
(actorContext.self as AnyActor).send(createErrorPlatformEvent(id, err));
(actorContext.self as AnyActor).send(createErrorActorEvent(id, err));
return;
}
});
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/actions/send.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import isDevelopment from '#is-development';
import { createErrorPlatformEvent } from '../eventUtils.ts';
import { createErrorActorEvent } from '../eventUtils.ts';
import {
ActionArgs,
ActorRef,
Expand Down Expand Up @@ -125,7 +125,7 @@ function execute(
actorContext.defer(() => {
to.send(
event.type === XSTATE_ERROR
? createErrorPlatformEvent(actorContext.self.id, (event as any).data)
? createErrorActorEvent(actorContext.self.id, (event as any).data)
: event
);
});
Expand Down
9 changes: 3 additions & 6 deletions packages/core/src/actors/callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ import {
TODO
} from '../types';
import { isPromiseLike } from '../utils';
import {
createDoneInvokeEvent,
createErrorPlatformEvent
} from '../eventUtils.ts';
import { createDoneActorEvent, createErrorActorEvent } from '../eventUtils.ts';
import { XSTATE_INIT, XSTATE_STOP } from '../constants.ts';

export interface CallbackInternalState<
Expand Down Expand Up @@ -99,12 +96,12 @@ export function fromCallback<TEvent extends EventObject, TInput>(
if (isPromiseLike(state.dispose)) {
state.dispose.then(
(resolved) => {
self._parent?.send(createDoneInvokeEvent(id, resolved));
self._parent?.send(createDoneActorEvent(id, resolved));
state.canceled = true;
},
(errorData) => {
state.canceled = true;
self._parent?.send(createErrorPlatformEvent(id, errorData));
self._parent?.send(createErrorActorEvent(id, errorData));
}
);
}
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ export const TARGETLESS_KEY = '';
export const NULL_EVENT = '';
export const STATE_IDENTIFIER = '#';
export const WILDCARD = '*';
export const ERROR_EXECUTION = 'error.execution'; // TODO: replace with `xstate.error.*`
export const XSTATE_INIT = 'xstate.init';
export const XSTATE_ERROR = 'xstate.error';
export const XSTATE_STOP = 'xstate.stop';
22 changes: 9 additions & 13 deletions packages/core/src/eventUtils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { XSTATE_INIT } from './constants.ts';
import {
DoneInvokeEventObject,
DoneStateEventObject,
ErrorPlatformEvent
} from './types.ts';
import { DoneActorEvent, DoneStateEvent, ErrorActorEvent } from './types.ts';

/**
* Returns an event that represents an implicit event that
Expand All @@ -27,9 +23,9 @@ export function createAfterEvent(delayRef: number | string, id?: string) {
export function createDoneStateEvent(
id: string,
output?: unknown
): DoneStateEventObject {
): DoneStateEvent {
return {
type: `done.state.${id}`,
type: `xstate.done.state.${id}`,
output
};
}
Expand All @@ -43,21 +39,21 @@ export function createDoneStateEvent(
* @param invokeId The invoked service ID
* @param output The data to pass into the event
*/
export function createDoneInvokeEvent(
export function createDoneActorEvent(
invokeId: string,
output?: unknown
): DoneInvokeEventObject {
): DoneActorEvent {
return {
type: `done.invoke.${invokeId}`,
type: `xstate.done.actor.${invokeId}`,
output
};
}

export function createErrorPlatformEvent(
export function createErrorActorEvent(
id: string,
data?: unknown
): ErrorPlatformEvent {
return { type: `error.platform.${id}`, data };
): ErrorActorEvent {
return { type: `xstate.error.actor.${id}`, data };
}

export function createInitEvent(input: unknown) {
Expand Down
Loading