Skip to content

Commit e9d2cca

Browse files
committed
fix(builders): patch provide to function correctly
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
1 parent a3fb055 commit e9d2cca

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

src/builders.spec.tsx

+26
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,32 @@ describe("xstate-tree builders", () => {
5353
}}
5454
/>;
5555
});
56+
57+
it("repairs the provide function to not lose the _xstateTree property and return an XstateTreeMachine", () => {
58+
const machine = setup({
59+
actions: {
60+
someAction: () => {},
61+
},
62+
}).createMachine({
63+
initial: "idle",
64+
states: {
65+
idle: {},
66+
},
67+
});
68+
69+
const xstateTreeMachine = createXStateTreeMachine(machine, {
70+
View() {
71+
return <div>hello world</div>;
72+
},
73+
}).provide({
74+
actions: {
75+
someAction: () => {},
76+
},
77+
});
78+
79+
const Root = buildRootComponent({ machine: xstateTreeMachine });
80+
render(<Root />);
81+
});
5682
});
5783

5884
describe("viewToMachine", () => {

src/builders.tsx

+17-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,23 @@ export function createXStateTreeMachine<
5454
slots: (options.slots ?? []) as any,
5555
};
5656

57-
return machineWithMeta;
57+
return fixProvideLosingXstateTreeMeta(machineWithMeta);
58+
}
59+
60+
function fixProvideLosingXstateTreeMeta<
61+
T extends XstateTreeMachine<any, any, any, any>
62+
>(machine: T): T {
63+
const originalProvide = machine.provide.bind(machine);
64+
(machine as any).provide = (impl: any) => {
65+
const result = originalProvide(impl) as T;
66+
67+
result._xstateTree = machine._xstateTree;
68+
fixProvideLosingXstateTreeMeta(result);
69+
70+
return result;
71+
};
72+
73+
return machine;
5874
}
5975

6076
/**

src/types.ts

+23-1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,23 @@ export type XstateTreeMachineInjection<
102102
>;
103103
};
104104

105+
/**
106+
* Repairs the return type of the `provide` function on XstateTreeMachines to correctly return
107+
* an XstateTreeMachine type instead of an xstate StateMachine
108+
*/
109+
type RepairProvideReturnType<
110+
T extends AnyStateMachine,
111+
TSelectorsOutput,
112+
TActionsOutput,
113+
TSlots extends readonly Slot[]
114+
> = {
115+
[K in keyof T]: K extends "provide"
116+
? (
117+
...args: Parameters<T[K]>
118+
) => XstateTreeMachine<T, TSelectorsOutput, TActionsOutput, TSlots>
119+
: T[K];
120+
};
121+
105122
/**
106123
* @public
107124
*/
@@ -110,7 +127,12 @@ export type XstateTreeMachine<
110127
TSelectorsOutput = ContextFrom<TMachine>,
111128
TActionsOutput = Record<never, string>,
112129
TSlots extends readonly Slot[] = Slot[]
113-
> = TMachine &
130+
> = RepairProvideReturnType<
131+
TMachine,
132+
TSelectorsOutput,
133+
TActionsOutput,
134+
TSlots
135+
> &
114136
XstateTreeMachineInjection<
115137
TMachine,
116138
TSelectorsOutput,

0 commit comments

Comments
 (0)