Skip to content

Commit

Permalink
fix(builders): patch provide to function correctly
Browse files Browse the repository at this point in the history
Due to how provide works it loses the _xstateTree property that is attached to xstate-tree machines, making them unusable.

It also fixes the return type to return an XstateTreeMachine instead of a StateMachine
  • Loading branch information
UberMouse committed Mar 17, 2024
1 parent a3fb055 commit e9d2cca
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
26 changes: 26 additions & 0 deletions src/builders.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,32 @@ describe("xstate-tree builders", () => {
}}
/>;
});

it("repairs the provide function to not lose the _xstateTree property and return an XstateTreeMachine", () => {
const machine = setup({
actions: {
someAction: () => {},
},
}).createMachine({
initial: "idle",
states: {
idle: {},
},
});

const xstateTreeMachine = createXStateTreeMachine(machine, {
View() {
return <div>hello world</div>;
},
}).provide({
actions: {
someAction: () => {},
},
});

const Root = buildRootComponent({ machine: xstateTreeMachine });
render(<Root />);
});
});

describe("viewToMachine", () => {
Expand Down
18 changes: 17 additions & 1 deletion src/builders.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,23 @@ export function createXStateTreeMachine<
slots: (options.slots ?? []) as any,
};

return machineWithMeta;
return fixProvideLosingXstateTreeMeta(machineWithMeta);
}

function fixProvideLosingXstateTreeMeta<
T extends XstateTreeMachine<any, any, any, any>
>(machine: T): T {
const originalProvide = machine.provide.bind(machine);
(machine as any).provide = (impl: any) => {
const result = originalProvide(impl) as T;

result._xstateTree = machine._xstateTree;
fixProvideLosingXstateTreeMeta(result);

return result;
};

return machine;
}

/**
Expand Down
24 changes: 23 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,23 @@ export type XstateTreeMachineInjection<
>;
};

/**
* Repairs the return type of the `provide` function on XstateTreeMachines to correctly return
* an XstateTreeMachine type instead of an xstate StateMachine
*/
type RepairProvideReturnType<
T extends AnyStateMachine,
TSelectorsOutput,
TActionsOutput,
TSlots extends readonly Slot[]
> = {
[K in keyof T]: K extends "provide"
? (
...args: Parameters<T[K]>
) => XstateTreeMachine<T, TSelectorsOutput, TActionsOutput, TSlots>
: T[K];
};

/**
* @public
*/
Expand All @@ -110,7 +127,12 @@ export type XstateTreeMachine<
TSelectorsOutput = ContextFrom<TMachine>,
TActionsOutput = Record<never, string>,
TSlots extends readonly Slot[] = Slot[]
> = TMachine &
> = RepairProvideReturnType<
TMachine,
TSelectorsOutput,
TActionsOutput,
TSlots
> &
XstateTreeMachineInjection<
TMachine,
TSelectorsOutput,
Expand Down

0 comments on commit e9d2cca

Please sign in to comment.