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

[core] Modeling events with createModel() #1955

Merged
merged 15 commits into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
64 changes: 61 additions & 3 deletions packages/core/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,82 @@ import type {
ExtractEvent,
EventObject
} from './types';
import { mapValues } from './utils';

export interface ContextModel<TContext, TEvent extends EventObject> {
export interface ContextModel<
TContext,
TEvent extends EventObject,
TEM extends EventCreatorMap<TEvent> = EventCreatorMap<TEvent>
> {
initialContext: TContext;
assign: <TEventType extends TEvent['type'] = TEvent['type']>(
assigner:
| Assigner<TContext, ExtractEvent<TEvent, TEventType>>
| PropertyAssigner<TContext, ExtractEvent<TEvent, TEventType>>,
eventType?: TEventType
) => AssignAction<TContext, ExtractEvent<TEvent, TEventType>>;
events: FullEventCreatorMap<TEM>;
types: {
context: TContext;
events: TEvent;
};
reset: () => AssignAction<TContext, any>;
}

type EventCreatorMap<TEvent extends EventObject> = {
[key in TEvent['type']]: (
...args: any[]
) => Omit<TEvent & { type: key }, 'type'>;
};

type FullEventCreatorMap<
TEM extends EventCreatorMap<any>,
TE extends EventObject = GetEvents<TEM>
> = TEM extends any
? {
[K in keyof TEM]: (
...args: Parameters<TEM[K]>
) => TE extends { type: K } ? TE : never;
}
: never;

type Expand<T> = T extends infer O ? { [K in keyof O]: O[K] } : never;
type Distribute<U, V extends Record<string, () => object>> = U extends keyof V &
string
? Expand<{ type: U } & ReturnType<V[U]>>
: never;

type GetEvents<T extends EventCreatorMap<any>> = Distribute<keyof T, T>;

export function createModel<TContext, TEvent extends EventObject>(
initialContext: TContext
): ContextModel<TContext, TEvent> {
const model: ContextModel<TContext, TEvent> = {
): ContextModel<TContext, TEvent, never>;
export function createModel<
TContext,
TEM extends EventCreatorMap<any>,
TEvent extends EventObject = GetEvents<TEM>
>(
initialContext: TContext,
eventCreators: TEM
): ContextModel<TContext, TEvent, TEM>;
export function createModel<
TContext,
TEM extends EventCreatorMap<any>,
TEvent extends EventObject = GetEvents<TEM>
>(initialContext: TContext, eventCreators?) {
const model: ContextModel<TContext, TEvent, TEM> = {
initialContext,
assign,
events: (eventCreators
? mapValues(
eventCreators,
(fn, eventType) => (...args: Parameters<typeof fn>) => ({
type: eventType,
...fn(...args)
})
)
: undefined) as FullEventCreatorMap<TEM>,
types: {} as any,
reset: () => assign(initialContext)
};

Expand Down
64 changes: 64 additions & 0 deletions packages/core/test/model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,68 @@ describe('createModel', () => {

expect(resetState.context).toEqual(userModel.initialContext);
});

it('can model events', () => {
const userModel = createModel(
{
name: 'David',
age: 30
},
{
updateName: (value: string) => ({ value }),
updateAge: (value: number) => ({ value }),
anotherEvent: () => ({})
}
);

// Example of an externally-defined assign action
const assignName = userModel.assign(
{
name: (_, event) => {
return event.value;
}
},
'updateName'
);

const machine = createMachine<
typeof userModel['types']['context'],
typeof userModel['types']['events']
>({
context: userModel.initialContext,
initial: 'active',
states: {
active: {
on: {
updateName: {
// pre-defined assign action
actions: assignName
},
updateAge: {
// inline assign action
actions: userModel.assign((_, e) => {
return {
age: e.value
};
})
}
}
}
}
});

let updatedState = machine.transition(
undefined,
userModel.events.updateName('Anyone')
);

expect(updatedState.context.name).toEqual('Anyone');

updatedState = machine.transition(
updatedState,
userModel.events.updateAge(42)
);

expect(updatedState.context.age).toEqual(42);
});
});