Skip to content

Commit

Permalink
Tweak types in a few actor-related tests (#4304)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist authored Sep 23, 2023
1 parent 7375350 commit 5fd8f26
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 88 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/actors/callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export type InvokeCallback<
receive: Receiver<TEvent>;
}) => (() => void) | void;

export function fromCallback<TEvent extends EventObject, TInput>(
export function fromCallback<TEvent extends EventObject, TInput = unknown>(
invokeCallback: InvokeCallback<TEvent, AnyEventObject, TInput>
): CallbackActorLogic<TEvent, TInput> {
return {
Expand Down
118 changes: 31 additions & 87 deletions packages/core/test/actor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import {
fromEventObservable,
fromObservable
} from '../src/actors/observable.ts';
import { PromiseActorRef, fromPromise } from '../src/actors/promise.ts';
import {
PromiseActorLogic,
PromiseActorRef,
fromPromise
} from '../src/actors/promise.ts';
import { fromTransition } from '../src/actors/transition.ts';
import {
ActorLogic,
Expand Down Expand Up @@ -289,6 +293,10 @@ describe('spawning promises', () => {
{
types: {} as {
context: { promiseRef?: PromiseActorRef<string> };
actors: {
src: 'somePromise';
logic: PromiseActorLogic<string>;
};
},
id: 'promise',
initial: 'idle',
Expand All @@ -299,7 +307,6 @@ describe('spawning promises', () => {
idle: {
entry: assign({
promiseRef: ({ spawn }) =>
// TODO: this is typed as AnyActorRef instead of PromiseActorRef<string>
spawn('somePromise', { id: 'my-promise' })
}),
on: {
Expand Down Expand Up @@ -341,7 +348,9 @@ describe('spawning callbacks', () => {
it('should be able to spawn an actor from a callback', (done) => {
const callbackMachine = createMachine({
types: {} as {
context: { callbackRef?: CallbackActorRef<{ type: 'START' }> };
context: {
callbackRef?: CallbackActorRef<{ type: 'START' }>;
};
},
id: 'callback',
initial: 'idle',
Expand All @@ -352,9 +361,8 @@ describe('spawning callbacks', () => {
idle: {
entry: assign({
callbackRef: ({ spawn }) =>
// TODO: this is typed as AnyActorRef instead of CallbackActorRef<{ type: 'START' }>
spawn(
fromCallback(({ sendBack, receive }) => {
fromCallback<{ type: 'START' }>(({ sendBack, receive }) => {
receive((event) => {
if (event.type === 'START') {
setTimeout(() => {
Expand Down Expand Up @@ -787,77 +795,6 @@ describe('communicating with spawned actors', () => {

parentService.start();
});

it.skip('should be able to name existing actors', (done) => {
const existingMachine = createMachine({
types: {
events: {} as {
type: 'ACTIVATE';
origin: AnyActorRef;
}
},
initial: 'inactive',
states: {
inactive: {
on: { ACTIVATE: 'active' }
},
active: {
entry: sendTo(({ event }) => event.origin, { type: 'EXISTING.DONE' })
}
}
});

const existingService = createActor(existingMachine).start();

const parentMachine = createMachine({
types: {} as {
context: { existingRef: typeof existingService | undefined };
},
initial: 'pending',
context: {
existingRef: undefined
},
states: {
pending: {
entry: assign({
// TODO: fix (spawn existing service)
// @ts-expect-error
existingRef: ({ spawn }) =>
// @ts-expect-error
spawn(existingService, {
id: 'existing'
})
}),
on: {
'EXISTING.DONE': 'success'
},
after: {
100: {
actions: sendTo(
({ context }) => context.existingRef!,
({ self }) => ({
type: 'ACTIVATE',
origin: self
})
)
}
}
},
success: {
type: 'final'
}
}
});

const parentService = createActor(parentMachine);
parentService.subscribe({
complete: () => {
done();
}
});

parentService.start();
});
});

describe('actors', () => {
Expand Down Expand Up @@ -1229,10 +1166,9 @@ describe('actors', () => {

it('should be able to spawn callback actors in (lazy) initial context', (done) => {
const machine = createMachine({
types: {} as { context: { ref: CallbackActorRef<{ type: 'TEST' }> } },
types: {} as { context: { ref: CallbackActorRef<EventObject> } },
context: ({ spawn }) => ({
ref: spawn(
// TODO: this is typed as CallbackActorRef<EventObject> instead of CallbackActorRef<{ type: 'TEST' }>
fromCallback(({ sendBack }) => {
sendBack({ type: 'TEST' });
})
Expand Down Expand Up @@ -1331,14 +1267,18 @@ describe('actors', () => {
});

it('should not crash on child promise-like sync completion during self-initialization', () => {
const promiseLogic = fromPromise(
() => ({ then: (fn: any) => fn(null) } as any)
);
const parentMachine = createMachine({
types: {} as { context: { child: ActorRef<never, any> | null } },
types: {} as {
context: { child: ActorRefFrom<typeof promiseLogic> | null };
},
context: {
child: null
},
entry: assign({
child: ({ spawn }) =>
spawn(fromPromise(() => ({ then: (fn: any) => fn(null) } as any)))
child: ({ spawn }) => spawn(promiseLogic)
})
});
const service = createActor(parentMachine);
Expand All @@ -1356,13 +1296,17 @@ describe('actors', () => {
}
});

const emptyObservableLogic = fromObservable(createEmptyObservable);

const parentMachine = createMachine({
types: {} as { context: { child: ActorRef<never, any> | null } },
types: {} as {
context: { child: ActorRefFrom<typeof emptyObservableLogic> | null };
},
context: {
child: null
},
entry: assign({
child: ({ spawn }) => spawn(fromObservable(createEmptyObservable))
child: ({ spawn }) => spawn(emptyObservableLogic)
})
});
const service = createActor(parentMachine);
Expand Down Expand Up @@ -1456,7 +1400,7 @@ describe('actors', () => {
const machine = createMachine({
types: {} as {
context: {
actorRef: CallbackActorRef<any>; // TODO: fix this
actorRef: CallbackActorRef<EventObject>;
};
},
initial: 'active',
Expand Down Expand Up @@ -1523,7 +1467,7 @@ describe('actors', () => {
const machine = createMachine({
types: {} as {
context: {
actorRef: CallbackActorRef<any>; // TODO: fix this
actorRef: CallbackActorRef<EventObject>;
};
},
initial: 'active',
Expand Down Expand Up @@ -1588,7 +1532,7 @@ describe('actors', () => {
const machine = createMachine({
types: {} as {
context: {
actorRef: CallbackActorRef<never>;
actorRef: CallbackActorRef<EventObject>;
};
},
initial: 'active',
Expand Down Expand Up @@ -1653,7 +1597,7 @@ describe('actors', () => {
const machine = createMachine({
types: {} as {
context: {
actorRef: CallbackActorRef<never>;
actorRef: CallbackActorRef<EventObject>;
};
},
initial: 'active',
Expand Down

0 comments on commit 5fd8f26

Please sign in to comment.