Skip to content
This repository has been archived by the owner on Mar 5, 2023. It is now read-only.

Fix typing for callback services #90

Closed
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/plenty-bobcats-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'xstate-codegen': patch
---

Fix type definition for callback services
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Machine, interpret } from '@xstate/compiled';

interface Context {}

type Event =
| { type: 'INIT', data: string }
| { type: 'LOG' }

const machine = Machine<Context, Event, 'callbackServiceMachine'>({
initial: 'idle',
states: {
idle: {
on: {
INIT: 'active'
}
},
active: {
invoke: [
{
src: 'listen',
},
],
on: {
LOG: {
actions: 'logAction'
}
}
},
},
});

interpret(
machine.withConfig({
services: {
listen: (_context, _event) => (send, onReceive) => {
send({ type: 'LOG' })

onReceive(e => {
if (e.type === 'LOG') {
console.log('log here too')
}
})
}
},
actions: {
logAction: (_context, _event) => console.log('received event')
},
}),
);
89 changes: 88 additions & 1 deletion packages/xstate-compiled/examples/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/xstate-compiled/examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"license": "MIT",
"repository": "https://github.com/mattpocock/xstate-codegen",
"dependencies": {
"xstate": "^4.12.0",
"@xstate/react": "0.8.1",
"react": "16.13.1",
"typescript": "3.9.7"
"typescript": "3.9.7",
"xstate": "^4.12.0"
},
"scripts": {
"build": "tsc"
Expand Down
27 changes: 26 additions & 1 deletion packages/xstate-compiled/src/templates/index.d.ts.hbs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
AnyEventObject,
EventObject,
SingleOrArray,
InvokeConfig,
Expand All @@ -21,7 +22,11 @@ import {
assign,
send,
Expr,
Subscribable,
Sender,
Receiver,
InterpreterOptions,
InvokeMeta,
} from 'xstate';
import {
StateWithMatches,
Expand All @@ -36,6 +41,25 @@ import { StateNode } from 'xstate/lib/StateNode';
declare module '@xstate/compiled' {
type TwoLevelPartial<T extends object> = { [K in keyof T]?: Partial<T[K]>};

type InvokeCallback<
TEvent extends EventObject,
TSentEvent extends EventObject
> = (
callback: Sender<TSentEvent>,
onReceive: Receiver<TEvent>
) => (() => void) | Promise<any> | void;

type NarrowedInvokeCreator<TContext,
TEvent extends EventObject,
TInitEvent extends EventObject,
TFinalContext = any
> =
(context: TContext, event: TInitEvent, meta: InvokeMeta) =>
| PromiseLike<TFinalContext>
| StateMachine<TFinalContext, any, any>
| Subscribable<EventObject>
| InvokeCallback<any, TEvent>;

/** Generated Types */
{{#each machines}}
export class {{capitalize this.id}}StateMachine<
Expand Down Expand Up @@ -114,8 +138,9 @@ declare module '@xstate/compiled' {
{{#if this.machine.services}}
services{{#unless this.machine.services.required}}?{{/unless}}: {
{{#each this.machine.services.lines}}
{{this.name}}{{#unless this.required}}?{{/unless}}: InvokeCreator<
{{this.name}}{{#unless this.required}}?{{/unless}}: NarrowedInvokeCreator<
TContext,
TEvent,
{{#if this.events}}
Extract<TEvent,
{{#each this.events}}
Expand Down