mattpocock / xstate-codegen

A codegen tool for 100% TS type-safety in XState
MIT License
245 stars 12 forks source link

Callback services not typed correctly #89

Open bahalperin opened 3 years ago

bahalperin commented 3 years ago

The definitions for InvokeCallback and InvokeCreator are currently:

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

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

xstate-codegen narrows the event type down to the event that caused the transition and invoked the service. This makes the event type for the Sender in the InvokeCallback the same. It should be able to send any of the machine's events back to it, regardless of what event caused the service to be invoked.

bahalperin commented 3 years ago

@mattpocock Just checking, is this library still being maintained? I’ve noticed there hasn’t been a new commit/release in a while.

mattpocock commented 3 years ago

Hey @bahalperin, it's kind of in experiment status at the moment. We're trying out some new parsers in other projects and, while I'm still full-time at Yozobi I don't have that much time to put to it.

However, I'll be joining Stately full-time Monday after next so I'll have more time to put to this. Apologies for the delay in merging your PR.

bahalperin commented 3 years ago

@mattpocock Thanks for the quick response! No worries at all, just wanted to make sure these weren't going to sit around forever. And thanks for making this library by the way, it is super super useful! Makes working with xstate in typescript so much more enjoyable 🙌

VanTanev commented 3 years ago

fwiw:

import {createMachine} from '@xstate/compiled'

interface Context {}

type Event = {type: 'DUMMY_TYPE'};

const machine = createMachine<Context, Event, 'uid'>({
    initial: 'test',
    states: {
        test: {
            invoke: {
                id: 'test',
                src: () => () => {}
            }
        }
    }
})
File Changed:  src/test.machine.ts
Could not complete due to errors in src/test.machine.ts
Error: Could not extract state schema
    at Object.exports.extractSchema (/home/ivan/dev/xstate-codegen-test/node_modules/xstate-codegen/bin/extractor.js:247:15)
    at /home/ivan/dev/xstate-codegen-test/node_modules/xstate-codegen/bin/extractMachines.js:97:41
    at Array.map (<anonymous>)
    at Object.exports.extractMachines (/home/ivan/dev/xstate-codegen-test/node_modules/xstate-codegen/bin/extractMachines.js:75:30)
    at addToCache (/home/ivan/dev/xstate-codegen-test/node_modules/xstate-codegen/bin/index.js:106:46)
    at FSWatcher.<anonymous> (/home/ivan/dev/xstate-codegen-test/node_modules/xstate-codegen/bin/index.js:68:19)
    at FSWatcher.emit (events.js:375:28)
    at FSWatcher.emitWithAll (/home/ivan/dev/xstate-codegen-test/node_modules/chokidar/index.js:541:32)
    at FSWatcher._emit (/home/ivan/dev/xstate-codegen-test/node_modules/chokidar/index.js:632:8)
    at listener (/home/ivan/dev/xstate-codegen-test/node_modules/chokidar/lib/nodefs-handler.js:370:20)

Edit: This was running the version from #78