Skip to content

Commit

Permalink
Merge branch 'next' into v5/history
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkpiano committed Feb 15, 2020
2 parents 078fad5 + 159e3af commit d1690b4
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 37 deletions.
5 changes: 5 additions & 0 deletions .changeset/witty-hounds-invent.md
@@ -0,0 +1,5 @@
---
'xstate': major
---

Removed support for `service.send(type, payload)`. We are using `send` API at multiple places and this was the only one supporting this shape of parameters. Additionally, it had not strict TS types and using it was unsafe (type-wise).
2 changes: 1 addition & 1 deletion docs/guides/communication.md
Expand Up @@ -19,10 +19,10 @@ An invocation is defined in a state node's configuration with the `invoke` prope

- `src` - the source of the service to invoke, which can be:
- a machine
- a string, which refers to a machine defined in this machine's `options.services`
- a function that returns a `Promise`
- a function that returns a "callback handler"
- a function that returns an observable
- a string, which refers to any of the 4 listed options defined in this machine's `options.services`
- `id` - the unique identifier for the invoked service
- `onDone` - (optional) the [transition](./transitions.md) to be taken when:
- the child machine reaches its [final state](./final.md), or
Expand Down
10 changes: 10 additions & 0 deletions docs/guides/guards.md
Expand Up @@ -218,6 +218,9 @@ const doorMachine = Machine(
SET_ADMIN: {
actions: assign({ level: 'admin' })
},
SET_ALARM: {
actions: assign({ alert: true })
},
OPEN: [
// Transitions are tested one at a time.
// The first valid transition will be taken.
Expand Down Expand Up @@ -247,6 +250,13 @@ const doorService = interpret(doorMachine)
.start();
// => { closed: 'idle' }

doorService.send('OPEN');
// => { closed: 'idle' }

doorService.send('SET_ALARM');
// => { closed: 'idle' }
// (state does not change, but context changes)

doorService.send('OPEN');
// => { closed: 'error' }

Expand Down
12 changes: 5 additions & 7 deletions packages/core/src/interpreter.ts
Expand Up @@ -23,7 +23,6 @@ import {
MachineOptions,
ActionFunctionMap,
SCXML,
EventData,
Observer,
Spawnable,
Typestate
Expand All @@ -43,7 +42,6 @@ import {
isObservable,
uniqueId,
isMachineNode,
toEventObject,
toSCXMLEvent,
reportUnhandledExceptionOnInvocation,
symbolObservable
Expand Down Expand Up @@ -532,15 +530,14 @@ export class Interpreter<
* @param event The event(s) to send
*/
public send = (
event: SingleOrArray<Event<TEvent>> | SCXML.Event<TEvent>,
payload?: EventData
event: SingleOrArray<Event<TEvent>> | SCXML.Event<TEvent>
): State<TContext, TEvent> => {
if (isArray(event)) {
this.batch(event);
return this.state;
}

const _event = toSCXMLEvent(toEventObject(event as Event<TEvent>, payload));
const _event = toSCXMLEvent(event);

if (this._status === InterpreterStatus.Stopped) {
// do nothing
Expand Down Expand Up @@ -956,10 +953,11 @@ export class Interpreter<

if (resolvedOptions.sync) {
childService.onTransition(state => {
this.send(actionTypes.update as any, {
this.send({
type: actionTypes.update,
state,
id: childService.id
});
} as any);
});
}

Expand Down
2 changes: 0 additions & 2 deletions packages/core/src/types.ts
Expand Up @@ -40,8 +40,6 @@ export interface ActionObject<TContext, TEvent extends EventObject> {

export type DefaultContext = Record<string, any> | undefined;

export type EventData = Record<string, any> & { type?: never };

/**
* The specified string event types or the specified event objects.
*/
Expand Down
9 changes: 3 additions & 6 deletions packages/core/src/utils.ts
Expand Up @@ -14,7 +14,6 @@ import {
ConditionPredicate,
SCXML,
StateLike,
EventData,
TransitionConfig,
TransitionConfigTargetShortcut,
NullEvent,
Expand Down Expand Up @@ -514,12 +513,10 @@ export const uniqueId = (() => {
})();

export function toEventObject<TEvent extends EventObject>(
event: Event<TEvent>,
payload?: EventData
// id?: TEvent['type']
event: Event<TEvent>
): TEvent {
if (isString(event) || typeof event === 'number') {
return { type: event, ...payload } as TEvent;
if (isString(event)) {
return { type: event } as TEvent;
}

return event;
Expand Down
4 changes: 2 additions & 2 deletions packages/core/test/actions.test.ts
Expand Up @@ -939,7 +939,7 @@ describe('forwardTo()', () => {
.onDone(() => done())
.start();

service.send('EVENT', { value: 42 });
service.send({ type: 'EVENT', value: 42 });
});

it('should forward an event to a service (dynamic)', done => {
Expand Down Expand Up @@ -986,7 +986,7 @@ describe('forwardTo()', () => {
.onDone(() => done())
.start();

service.send('EVENT', { value: 42 });
service.send({ type: 'EVENT', value: 42 });
});
});

Expand Down
6 changes: 3 additions & 3 deletions packages/core/test/actor.test.ts
Expand Up @@ -146,8 +146,8 @@ describe('spawning machines', () => {
})
.start();

service.send('ADD', { id: 42 });
service.send('SET_COMPLETE', { id: 42 });
service.send({ type: 'ADD', id: 42 });
service.send({ type: 'SET_COMPLETE', id: 42 });
});

it('should invoke actors (when sending batch)', done => {
Expand All @@ -158,7 +158,7 @@ describe('spawning machines', () => {
.start();

service.send([{ type: 'ADD', id: 42 }]);
service.send('SET_COMPLETE', { id: 42 });
service.send({ type: 'SET_COMPLETE', id: 42 });
});

it('should invoke a null actor if spawned outside of a service', () => {
Expand Down
15 changes: 0 additions & 15 deletions packages/core/test/interpreter.test.ts
Expand Up @@ -1168,21 +1168,6 @@ Event: {\\"type\\":\\"SOME_EVENT\\"}"
service.send({ type: 'EVENT', id: 42 });
});

it('can send events with a string and object payload', done => {
let state: any;
const service = interpret(sendMachine)
.onTransition(s => {
state = s;
})
.onDone(() => {
expect(state.event).toEqual({ type: 'EVENT', id: 42 });
done();
})
.start();

service.send('EVENT', { id: 42 });
});

it('should receive and process all events sent simultaneously', done => {
const toggleMachine = Machine({
id: 'toggle',
Expand Down
2 changes: 1 addition & 1 deletion packages/xstate-react/package.json
@@ -1,6 +1,6 @@
{
"name": "@xstate/react",
"version": "1.0.0-rc.2",
"version": "1.0.0-rc.3",
"description": "XState tools for React",
"keywords": [
"state",
Expand Down

0 comments on commit d1690b4

Please sign in to comment.