Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[poc] Use a functionBrand to make limited callback actors possible #4590

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ import {
TypegenDisabled
} from './typegenTypes.ts';

type Primitive = string | number | boolean | symbol | bigint | null | undefined;

declare const functionBrand: unique symbol;
type NotAFunction<T> = T & { [functionBrand]?: never };
declare global {
interface Function {
[functionBrand]?: true;
}
}

export type Identity<T> = { [K in keyof T]: T[K] };

export type HomomorphicPick<T, K extends keyof any> = {
Expand Down Expand Up @@ -674,7 +684,9 @@ export type InvokeConfig<

input?:
| Mapper<TContext, TEvent, NonReducibleUnknown, TEvent>
| NonReducibleUnknown;
| Primitive
| NotAFunction<{ [k: PropertyKey]: unknown }>
| NotAFunction<object>;
/**
* The transition to take upon the invoked child machine reaching its final top-level state.
*/
Expand Down
53 changes: 53 additions & 0 deletions packages/core/test/setup.types.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import {
ActorRef,
ActorRefFrom,
and,
assign,
createActor,
createMachine,
fromCallback,
fromPromise,
fromTransition,
not,
Expand Down Expand Up @@ -1599,3 +1601,54 @@
});
});
});

type MyCustomEvents = 'MY_EVENT' | 'MY_OTHER_EVENT';

type ParentAcceptingSimpleEvents<Event extends string> = ActorRef<
any,
{ [K in Event]: { type: K } }[Event]
>;

const registerEvent = <Event extends MyCustomEvents>(eventName: Event) => ({
id: `listenerFor:${eventName}` as const,
src: fromCallback(
({
sendBack
}: {
sendBack: ParentAcceptingSimpleEvents<Event>['send'];
}) => {
const handler = (event: CustomEventInit) =>
sendBack({
...(event.detail ?? {}),
type: eventName
});

document.addEventListener(eventName, handler);
return () => {
document.removeEventListener(eventName, handler);
};
}
),
input: ({ self }: { self: ParentAcceptingSimpleEvents<Event> }) => ({
parent: self
})
});

createMachine({
types: {
events: {} as { type: 'MY_EVENT' }
},
invoke: registerEvent('MY_EVENT'),
on: {
MY_EVENT: {
actions: 'myAction'
}
}
});

createMachine({
types: {
events: {} as { type: 'DIFFERENT_EVENT' }
},
invoke: registerEvent('MY_EVENT')

Check failure on line 1653 in packages/core/test/setup.types.test.ts

View workflow job for this annotation

GitHub Actions / build

Type '{ id: "listenerFor:MY_EVENT"; src: CallbackActorLogic<EventObject, NonReducibleUnknown>; input: ({ self }: { self: ParentAcceptingSimpleEvents<"MY_EVENT">; }) => { ...; }; }' is not assignable to type 'SingleOrArray<{ id?: string | undefined; systemId?: string | undefined; src: string | AnyActorLogic; input?: Primitive | NotAFunction<{ [k: string]: unknown; [k: number]: unknown; [k: symbol]: unknown; }> | NotAFunction<...> | Mapper<...>; onDone?: SingleOrArray<...>; onError?: SingleOrArray<...>; onSnapshot?: Singl...'.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the error here is expected, that's the whole point of this PR :P

});
Loading