xstate-codegen icon indicating copy to clipboard operation
xstate-codegen copied to clipboard

Callback services not typed correctly

Open bahalperin opened this issue 4 years ago • 4 comments

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 avatar Jul 02 '21 18:07 bahalperin

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

bahalperin avatar Jul 09 '21 21:07 bahalperin

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.

mattpocock avatar Jul 10 '21 07:07 mattpocock

@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 🙌

bahalperin avatar Jul 13 '21 14:07 bahalperin

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

VanTanev avatar Aug 04 '21 10:08 VanTanev