Skip to content

Commit

Permalink
switch from generating handlers from schema to spawned actors
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaAmaju committed Jan 7, 2022
1 parent 20aa109 commit ffb8afb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
24 changes: 15 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,16 @@ export type FormState =
| 'submitted'
| 'error';

export type SubscriptionValue<T, D, E, Es> = Omit<
export type SubscriptionValue<T, D, E, Es> = Pick<
Context<T, D, E, Es>,
'__ignore' | 'actors' | 'schema' | '__validationMarker'
| 'data'
| 'error'
| 'values'
| 'states'
| 'errors'
| 'failureCount'
| 'dataUpdatedAt'
| 'errorUpdatedAt'
> & {
state: FormState;
isIdle: boolean;
Expand Down Expand Up @@ -136,19 +143,18 @@ export const createForm = <T = any, D = any, E = any, Es = any, TData = D>({

const generate: Generate<T, TData, E, Es> = ({
states,
schema,
values,
errors,
schema,
actors = {},
}: Context<T, TData, E, Es>) => {
if (!schema || typeof schema === 'boolean') {
if (__DEV__) {
console.warn('Cannot generate handlers without schema defined');
if (__DEV__) {
if (!schema || typeof schema === 'boolean') {
console.warn('No schema defined to generate handlers from');
}

return;
}

const entries = Object.keys(schema).map((k) => {
const entries = Object.keys(actors).map((k) => {
const id = k as keyof T;
const value = values[id];
const error = errors.get(id);
Expand Down
17 changes: 8 additions & 9 deletions src/machine/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,9 @@ export const machine = <T, D, E, Es>() => {
actions: assign({
__ignore: (_, { ignore = [] }) => new Set(ignore),
}),
cond: ({ schema }, { ignore = [] }) => {
if (!schema || typeof schema === 'boolean') return false;
const schemaLength = Object.values(schema).length;
return schemaLength - ignore.length > 0;
cond: ({ actors = {} }, { ignore = [] }) => {
const length = Object.values(actors).length;
return length - ignore.length > 0;
},
},
{
Expand Down Expand Up @@ -236,8 +235,8 @@ export const machine = <T, D, E, Es>() => {

entry: [
assign({ errors: (_) => new Map() }),
pure(({ schema, values, __ignore }) => {
return Object.keys(schema as Schema)
pure(({ actors, values, __ignore }) => {
return Object.keys(actors)
.filter((key) => !__ignore.has(key as keyof T))
.map((key) => {
const value = values[key as keyof T];
Expand All @@ -256,16 +255,16 @@ export const machine = <T, D, E, Es>() => {
return (
ctx.errors.size > 0 &&
ctx.__validationMarker.size >=
Object.keys(ctx.schema as Schema).length - ctx.__ignore.size
Object.keys(ctx.actors).length - ctx.__ignore.size
);
},
},
{
target: 'submitting',
cond: ({ schema, __ignore, __validationMarker }) => {
cond: ({ actors, __ignore, __validationMarker }) => {
return (
__validationMarker.size >=
Object.keys(schema as Schema).length - __ignore.size
Object.keys(actors).length - __ignore.size
);
},
},
Expand Down

0 comments on commit ffb8afb

Please sign in to comment.