Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist committed Oct 21, 2024
1 parent ab4bad0 commit 73c6b22
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 71 deletions.
4 changes: 3 additions & 1 deletion packages/core/src/actions/cancel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ function resolveCancel(
}

function executeCancel(actorScope: AnyActorScope, resolvedSendId: string) {
actorScope.system.scheduler.cancel(actorScope.self, resolvedSendId);
actorScope.defer(() => {
actorScope.system.scheduler.cancel(actorScope.self, resolvedSendId);
});
}

export interface CancelAction<
Expand Down
13 changes: 10 additions & 3 deletions packages/core/src/actions/raise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,21 @@ function resolveRaise(
}

function executeRaise(
_actorScope: AnyActorScope,
_params: {
actorScope: AnyActorScope,
params: {
event: EventObject;
id: string | undefined;
delay: number | undefined;
}
) {
return;
const { event, delay, id } = params;
if (typeof delay === 'number') {
actorScope.defer(() => {
const self = actorScope.self;
actorScope.system.scheduler.schedule(self, self, event, delay, id);
});
return;
}
}

export interface RaiseAction<
Expand Down
11 changes: 9 additions & 2 deletions packages/core/src/actions/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,15 @@ function executeSendTo(
// this forms an outgoing events queue
// thanks to that the recipient actors are able to read the *updated* snapshot value of the sender
actorScope.defer(() => {
const { to, event, delay, id: _id } = params;
const { to, event, delay, id } = params;
if (typeof delay === 'number') {
actorScope.system.scheduler.schedule(
actorScope.self,
to,
event,
delay,
id
);
return;
}
actorScope.system._relay(
Expand Down Expand Up @@ -240,7 +247,7 @@ export function sendTo<
> {
if (isDevelopment && executingCustomAction) {
console.warn(
'Custom actions should not call `raise()` directly, as it is not imperative. See https://stately.ai/docs/actions#built-in-actions for more details.'
'Custom actions should not call `sendTo()` directly, as it is not imperative. See https://stately.ai/docs/actions#built-in-actions for more details.'
);
}

Expand Down
16 changes: 8 additions & 8 deletions packages/core/src/createActor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ import {
createInitEvent
} from './eventUtils.ts';
import { reportUnhandledError } from './reportUnhandledError.ts';
import { executeAction } from './stateUtils.ts';
import { symbolObservable } from './symbolObservable.ts';
import { AnyActorSystem, Clock, createSystem } from './system.ts';

export let executingCustomAction: ((...args: any[]) => void) | false = false;
export let executingCustomAction: boolean = false;

import type {
ActorScope,
Expand Down Expand Up @@ -186,11 +185,11 @@ export class Actor<TLogic extends AnyActorLogic>
if (!listeners && !wildcardListener) {
return;
}
const allListeners = new Set([
const allListeners = [
...(listeners ? listeners.values() : []),
...(wildcardListener ? wildcardListener.values() : [])
]);
for (const handler of Array.from(allListeners)) {
];
for (const handler of allListeners) {
handler(emittedEvent);
}
},
Expand All @@ -207,11 +206,12 @@ export class Actor<TLogic extends AnyActorLogic>
if (!action.exec) {
return;
}
const saveExecutingCustomAction = executingCustomAction;
try {
executingCustomAction = action.exec;
executeAction(action, this);
executingCustomAction = true;
action.exec(action.info, action.params);
} finally {
executingCustomAction = false;
executingCustomAction = saveExecutingCustomAction;
}
};
if (this._processingStatus === ProcessingStatus.Running) {
Expand Down
43 changes: 12 additions & 31 deletions packages/core/src/stateUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1566,24 +1566,13 @@ function resolveAndExecuteActionsWithContext(
: action.params
: undefined;

function executeAction() {
actorScope.actionExecutor(
{
type: action.type,
info: actionArgs,
params: actionParams,
exec: resolvedAction
},
actorScope.self
);
}

if (!resolvedAction || !('resolve' in resolvedAction)) {
executeAction();
continue;
}

if (!resolvedAction) {
actorScope.actionExecutor({
type: action.type,
info: actionArgs,
params: actionParams,
exec: resolvedAction
});
continue;
}

Expand All @@ -1604,20 +1593,12 @@ function resolveAndExecuteActionsWithContext(
}

if ('execute' in builtinAction) {
actorScope.actionExecutor(
{
type: builtinAction.type,
info: actionArgs,
params,
exec: () => {} // noop
},
actorScope.self
);
if (actorScope.self._processingStatus === ProcessingStatus.Running) {
builtinAction.execute(actorScope, params);
} else {
actorScope.defer(builtinAction.execute.bind(null, actorScope, params));
}
actorScope.actionExecutor({
type: builtinAction.type,
info: actionArgs,
params,
exec: builtinAction.execute.bind(null, actorScope, params)
});
}

if (actions) {
Expand Down
25 changes: 4 additions & 21 deletions packages/core/src/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import {
Observer,
HomomorphicOmit,
EventObject,
Subscription,
AnyMachineSnapshot
Subscription
} from './types.ts';
import { toObserver } from './utils.ts';

Expand All @@ -17,12 +16,7 @@ interface ScheduledEvent {
startedAt: number; // timestamp
delay: number;
source: AnyActorRef;
/**
* The target `ActorRef` of the event.
*
* Can be a `string` (references `snapshot.children[target]`) or an `ActorRef`
*/
target: AnyActorRef | string;
target: AnyActorRef;
}

export interface Clock {
Expand All @@ -33,7 +27,7 @@ export interface Clock {
interface Scheduler {
schedule(
source: AnyActorRef,
target: AnyActorRef | string,
target: AnyActorRef,
event: EventObject,
delay: number,
id: string | undefined
Expand Down Expand Up @@ -131,18 +125,7 @@ export function createSystem<T extends ActorSystemInfo>(
delete timerMap[scheduledEventId];
delete system._snapshot._scheduledEvents[scheduledEventId];

const resolvedTarget =
typeof target === 'string'
? (source.getSnapshot() as AnyMachineSnapshot).children[target]
: target;

if (!resolvedTarget) {
throw new Error(
`Actor with id ${typeof target === 'string' ? target : target.sessionId} not found in the system.`
);
}

system._relay(source, resolvedTarget, event);
system._relay(source, target, event);
}, delay);

timerMap[scheduledEventId] = timeout;
Expand Down
5 changes: 1 addition & 4 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2678,7 +2678,4 @@ export type ExecutableActionsFrom<T extends AnyActorLogic> =
| (string extends TAction['type'] ? never : ToExecutableAction<TAction>)
: never;

export type ActionExecutor = (
actionToExecute: ExecutableActionObject,
actorRef: AnyActorRef
) => void;
export type ActionExecutor = (actionToExecute: ExecutableActionObject) => void;
2 changes: 1 addition & 1 deletion packages/core/test/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4252,7 +4252,7 @@ describe('actions', () => {
"Custom actions should not call \`raise()\` directly, as it is not imperative. See https://stately.ai/docs/actions#built-in-actions for more details.",
],
[
"Custom actions should not call \`raise()\` directly, as it is not imperative. See https://stately.ai/docs/actions#built-in-actions for more details.",
"Custom actions should not call \`sendTo()\` directly, as it is not imperative. See https://stately.ai/docs/actions#built-in-actions for more details.",
],
[
"Custom actions should not call \`emit()\` directly, as it is not imperative. See https://stately.ai/docs/actions#built-in-actions for more details.",
Expand Down

0 comments on commit 73c6b22

Please sign in to comment.