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

Callback services not typed correctly #89

Open
bahalperin opened this issue Jul 2, 2021 · 4 comments
Open

Callback services not typed correctly #89

bahalperin opened this issue Jul 2, 2021 · 4 comments

Comments

@bahalperin
Copy link

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
Copy link
Author

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

@mattpocock
Copy link
Owner

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
Copy link
Author

@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
Copy link

VanTanev commented Aug 4, 2021

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants