xstate icon indicating copy to clipboard operation
xstate copied to clipboard

Bug: Actor System v5 sentTo actions types are not working in provide

Open ShravanSunder opened this issue 2 years ago • 4 comments

Description

The following actor system senTo action is working in an on transition

'job.self.saveJobRunData': {
			description: 'Send a message to WorkflowControlActor to save job run data',
			actions: [
				sendTo(
					({ system }) => {
						// eslint-disable-next-line @typescript-eslint/no-unsafe-return
						return system.get(JobWorkflowControlMachineId);
					},
					({ event }) => {
						return {
							type: 'control.self.saveJobRecord',
							payload: event.payload,
						} satisfies ControlWorkflowEvents;
					}
				),
			],
		},

however putting it in a provide as an action in provide has typing errors


	const machine = fromJobMachine.provide({
		actors: {
			// checkpointActor,
			prepareActor,
			runJobActor,
			postRunActor,
		},
		actions: {
			persistWorkflow: sendTo(
				({ system }) => {
					// eslint-disable-next-line @typescript-eslint/no-unsafe-return
					return system.get(JobWorkflowControlMachineId);
				},
				() => {
					return {
						type: 'control.self.persistWorkflow',
					} satisfies ControlWorkflowEvents;
				}
			),
      saveJobRunData: sendTo(
        ({ system }) => {
          // eslint-disable-next-line @typescript-eslint/no-unsafe-return
          return system.get(JobWorkflowControlMachineId);
        },
        ({ event }) => {
          return {
            type: 'control.self.saveJobRecord',
            payload: event.payload,
          } satisfies ControlWorkflowEvents;
        }
      ),
		},
    
	});

The errors are due to typing of event, it seems to be different in the provide block. The event doesn't have a type that works

Expected result

The typing should also work with provide

Also, system seems to have any type.

Actual result

Typing does not work

Reproduction

n/a

Additional context

No response

ShravanSunder avatar Nov 28 '23 01:11 ShravanSunder

Please always try to share a repro case in a runnable form - either by providing a git repository to clone or a codesandbox. I can't quickly check this out if those code snippets rely on variables that are not present in them etc

Andarist avatar Nov 28 '23 07:11 Andarist

The event is different in the provide block because we can't predict what event the action will be called with in there (at least not yet). I would recommend asserting the event type (if (event.type === 'expectedType') ...) before using the event.

davidkpiano avatar Nov 28 '23 13:11 davidkpiano

The event is different in the provide block because we can't predict what event the action will be called with in there (at least not yet). I would recommend asserting the event type (if (event.type === 'expectedType') ...) before using the event.

The type issue seems to be the parameter input to the second arg of sendTo. I tried the event type check, but event itself not typed.

sendTo(
				({ system }) => {
					// eslint-disable-next-line @typescript-eslint/no-unsafe-return
					return system.get(JobWorkflowControlMachineId);
				},
				({ event }) => {
					if (event.type === 'job.self.saveJobRunData') {
						return {
							type: 'control.self.saveJobRecord',
							payload: event.payload,
						} satisfies ControlWorkflowEvents;
					}
				}
			),

2023-11-28 1133 Brave Browser Bug Actor System v5 sentTo actions types are not working in provide · Issue #4508 · statelyaixstate

ShravanSunder avatar Nov 28 '23 15:11 ShravanSunder

Please always try to share a repro case in a runnable form - either by providing a git repository to clone or a codesandbox. I can't quickly check this out if those code snippets rely on variables that are not present in them etc

@Andarist I totally understand.

However i wanted to raise that its a bit cumbersome to create sandboxes when it comes to complicated edge cases. It would be good to have a code sandbox template that targets a v5 with a built in example that uses actors, systems etc that we could easily use for bug recreation.

I submitted a few bugs with the recreation, but takes a bit of time for each.

ShravanSunder avatar Nov 28 '23 15:11 ShravanSunder