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

[WIP] Fixes actions on same-state transitions #75

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

good-idea
Copy link

Just giving this a shot. This makes a 'fix' to work as I'd expect (see #74) but now there is a failing test.

Needs (from my perspective):

[ ] machineDidUpdate utility probably needs to be improved to better detect changes
[ ] Transitioning to a nonexistent state (transition(FOO)) should act as it did before.

Let me know if this is helpful, I'm happy to continue working on this. If not, I can just call transition with an empty object.

Copy link
Owner

@MicheleBertoli MicheleBertoli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much for submitting this PR, @good-idea.
I'm all-in to get this merged, and I left a few comments.

For now, it really seems value and actions is what we need to check to detect changes, and I agree that a transition to an invalid state should work as it does now.

@@ -30,3 +30,12 @@ export const matches = (patterns, value) => {

return patterns.some(pattern => values.some(val => pattern.test(val)))
}

export const machineDidUpdate = (oldMachine, newMachine) => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be:
(prevMachineState.value !== machineState.value) || machineState.actions.length

Otherwise, this would be true when the previous machine has actions, and the current one doesn't (i.e. it's invalid).

I would also replace machine with state or, better, with machineState, and rename it to something like hasMachineStateChanged.

@@ -118,7 +119,7 @@ const withStateMachine = (statechart, options = {}) => Component => {

handleComponentDidUpdate(prevProps, prevState) {
this.isTransitioning = false

/* WIP - Should this logic be the same as what is used in handleTransition? */
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, it definitely should.

@@ -30,3 +30,12 @@ export const matches = (patterns, value) => {

return patterns.some(pattern => values.some(val => pattern.test(val)))
}

export const machineDidUpdate = (oldMachine, newMachine) => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although this is pretty simple, I would add a couple of tests.

@@ -105,6 +111,7 @@ test('state', () => {
test('actions', () => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't repurpose this test, but I suggest we create a separate one (like "invalid transition with extended state") or, add a new case to "render".

@MicheleBertoli
Copy link
Owner

Ping @good-idea :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants