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

Removed support for service.send(type, payload) #1002

Merged
merged 1 commit into from
Feb 15, 2020
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/witty-hounds-invent.md
Original file line number Diff line number Diff line change
@@ -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).
12 changes: 5 additions & 7 deletions packages/core/src/interpreter.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
ConditionPredicate,
SCXML,
StateLike,
EventData,
TransitionConfig,
TransitionConfigTargetShortcut,
NullEvent,
Expand Down Expand Up @@ -548,12 +547,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -1170,21 +1170,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