Skip to content

Commit 4201f97

Browse files
committed
fn -> to
1 parent 818de98 commit 4201f97

File tree

6 files changed

+60
-11
lines changed

6 files changed

+60
-11
lines changed

packages/core/src/StateNode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ export class StateNode<
369369
transition.target ||
370370
// transition.actions.length ||
371371
transition.reenter ||
372-
transition.fn
372+
transition.to
373373
);
374374
})
375375
);

packages/core/src/stateUtils.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ export function getDelayedTransitions(
270270
typeof configTransition === 'string'
271271
? { target: configTransition }
272272
: typeof configTransition === 'function'
273-
? { fn: configTransition }
273+
? { to: configTransition }
274274
: configTransition;
275275
const resolvedDelay = Number.isNaN(+delay) ? delay : +delay;
276276
const eventType = mutateEntryExitWithDelay(resolvedDelay);
@@ -1254,7 +1254,7 @@ export function getTransitionResult(
12541254
reenter?: boolean;
12551255
internalEvents: EventObject[] | undefined;
12561256
} {
1257-
if (transition.fn) {
1257+
if (transition.to) {
12581258
const actions: UnknownAction[] = [];
12591259
const internalEvents: EventObject[] = [];
12601260
const enqueue = createEnqueueObject(
@@ -1339,7 +1339,7 @@ export function getTransitionResult(
13391339
}
13401340
);
13411341

1342-
const res = transition.fn(
1342+
const res = transition.to(
13431343
{
13441344
context: snapshot.context,
13451345
event,
@@ -2167,7 +2167,7 @@ export function hasEffect(
21672167
snapshot: AnyMachineSnapshot,
21682168
self: AnyActorRef
21692169
): boolean {
2170-
if (transition.fn) {
2170+
if (transition.to) {
21712171
let hasEffect = false;
21722172
let res;
21732173

@@ -2176,7 +2176,7 @@ export function hasEffect(
21762176
hasEffect = true;
21772177
throw new Error('Effect triggered');
21782178
};
2179-
res = transition.fn(
2179+
res = transition.to(
21802180
{
21812181
context,
21822182
event,
@@ -2224,7 +2224,7 @@ export function evaluateCandidate(
22242224
stateNode: AnyStateNode,
22252225
self: AnyActorRef
22262226
): boolean {
2227-
if (candidate.fn) {
2227+
if (candidate.to) {
22282228
let hasEffect = false;
22292229
let res;
22302230
const context = snapshot.context;
@@ -2234,7 +2234,7 @@ export function evaluateCandidate(
22342234
hasEffect = true;
22352235
throw new Error('Effect triggered');
22362236
};
2237-
res = candidate.fn(
2237+
res = candidate.to(
22382238
{
22392239
context,
22402240
event,

packages/core/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ export interface TransitionConfig<
252252
actions?: never;
253253
reenter?: boolean;
254254
target?: TransitionTarget | undefined;
255-
fn?: TransitionConfigFunction<
255+
to?: TransitionConfigFunction<
256256
TContext,
257257
TExpressionEvent,
258258
TEvent,

packages/core/src/types.v6.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,28 @@ export type Next_TransitionConfigOrTarget<
417417
> =
418418
| string
419419
| undefined
420-
| { target?: string | string[]; description?: string; reenter?: boolean }
420+
| {
421+
target?: string | string[];
422+
description?: string;
423+
reenter?: boolean;
424+
meta?: TMeta;
425+
}
426+
| {
427+
to?: TransitionConfigFunction<
428+
TContext,
429+
TExpressionEvent,
430+
TEvent,
431+
TEmitted,
432+
TActionMap,
433+
TActorMap,
434+
TGuardMap,
435+
TDelayMap,
436+
TMeta
437+
>;
438+
description?: string;
439+
reenter?: boolean;
440+
meta?: TMeta;
441+
}
421442
| TransitionConfigFunction<
422443
TContext,
423444
TExpressionEvent,

packages/core/src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ export function toTransitionConfigArray(
232232
}
233233

234234
if (typeof transitionLike === 'function') {
235-
return { fn: transitionLike };
235+
return { to: transitionLike };
236236
}
237237

238238
return transitionLike;

packages/core/test/transition.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,4 +584,32 @@ describe('transition function', () => {
584584
await sleep(10);
585585
expect(JSON.parse(db.state).value).toBe('finish');
586586
});
587+
588+
it('should support transition functions', () => {
589+
const fn = vi.fn();
590+
const machine = createMachine({
591+
initial: 'a',
592+
states: {
593+
a: {
594+
on: {
595+
NEXT: {
596+
description: 'next',
597+
to: (_, enq) => {
598+
enq(fn);
599+
return {
600+
target: 'b'
601+
};
602+
}
603+
}
604+
}
605+
},
606+
b: {}
607+
}
608+
});
609+
610+
const [init] = initialTransition(machine);
611+
const [s1, actions] = transition(machine, init, { type: 'NEXT' });
612+
expect(s1.value).toEqual('b');
613+
expect(actions.length).toEqual(1);
614+
});
587615
});

0 commit comments

Comments
 (0)