Skip to content

Commit 5fec16e

Browse files
committed
feat: add utils
Signed-off-by: Umar Ali <mailtoumar014@gmail.com>
1 parent a767588 commit 5fec16e

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

src/actors/utils.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// disbale stict no any for now for full file
2+
/* eslint-disable @typescript-eslint/no-explicit-any */
3+
4+
import { AnyActorRef, AnyEventObject, enqueueActions, sendTo } from 'xstate';
5+
import { AnyActorSystem } from 'xstate/dist/declarations/src/system';
6+
7+
type ContextWithReturnAddress = { returnAddress: AnyActorRef };
8+
9+
export const sendToActor = (actor: string, event: AnyEventObject) =>
10+
sendTo(({ system }: { system: AnyActorSystem }) => {
11+
console.log('sendToActor', actor, event);
12+
return system.get(actor);
13+
}, event);
14+
15+
export const sendToActors = (
16+
actorSystemIds: string[],
17+
eventCreator: (actionArgs: any, params: any) => AnyEventObject
18+
) =>
19+
enqueueActions(({ enqueue, ...actionArgs }, params) => {
20+
actorSystemIds.forEach((actorSystemId) => {
21+
const actor = actionArgs.system.get(actorSystemId);
22+
if (!actor) {
23+
console.log('actor not found --sendToActors', actorSystemId);
24+
return;
25+
}
26+
enqueue.sendTo(actor, eventCreator(actionArgs, params));
27+
});
28+
});
29+
30+
export const forwardToActors = (actorSystemIds: string[]) =>
31+
enqueueActions(({ enqueue, event, system }) => {
32+
actorSystemIds.forEach((actorSystemId) => {
33+
const actor = system.get(actorSystemId);
34+
if (actor) {
35+
enqueue.sendTo(actor, event);
36+
}
37+
});
38+
});
39+
40+
export const deadLetter = (event: AnyEventObject) => ({ type: 'DEAD_LETTER', event });
41+
42+
export const reply = (eventFn: (actionArgs: any, params: any) => AnyEventObject) =>
43+
sendTo(({ context }: { context: ContextWithReturnAddress }) => context.returnAddress, eventFn);
44+
45+
export const XSTATE_DEBUG_EVENT = 'XSTATE_DEBUG_EVENT';

0 commit comments

Comments
 (0)