Skip to content

Commit

Permalink
Fixed issue with useService returning an initial state for services…
Browse files Browse the repository at this point in the history
… in their final states
  • Loading branch information
Andarist committed Sep 30, 2020
1 parent 37d5ed8 commit 72b0880
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/poor-flowers-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'xstate': patch
---

Changed `_status` property on the `Interpreter` to be a public one.
5 changes: 5 additions & 0 deletions .changeset/thin-teachers-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@xstate/react': patch
---

Fixed issue with `useService` returning an initial state for services in their final states.
2 changes: 1 addition & 1 deletion packages/core/src/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export class Interpreter<
* Whether the service is started.
*/
public initialized = false;
private _status: InterpreterStatus = InterpreterStatus.NotStarted;
public _status: InterpreterStatus = InterpreterStatus.NotStarted;

// Actor
public parent?: Interpreter<any>;
Expand Down
2 changes: 1 addition & 1 deletion packages/xstate-react/src/useService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function fromService<TContext, TEvent extends EventObject>(
send: service.send.bind(service),
subscribe: service.subscribe.bind(service),
stop: service.stop!,
current: service.initialized ? service.state : machine.initialState,
current: service._status !== 0 ? service.state : machine.initialState,
name: service.sessionId
};
}
Expand Down
31 changes: 31 additions & 0 deletions packages/xstate-react/test/useService.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,35 @@ describe('useService hook', () => {

render(<Test />);
});

it('should render the final state', () => {
const service = interpret(
Machine({
initial: 'first',
states: {
first: {
on: {
NEXT: {
target: 'last'
}
}
},
last: {
type: 'final'
}
}
})
).start();

service.send({ type: 'NEXT' });

const Test = () => {
const [state] = useService(service);
return <>{state.value}</>;
};

const { container } = render(<Test />);

expect(container.textContent).toBe('last');
});
});

0 comments on commit 72b0880

Please sign in to comment.