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

Are State Classes Required To Extend 'this' and to be declared inside of the static _states() method? #43

Closed
im-bob-loucans opened this issue Sep 13, 2019 · 3 comments
Labels

Comments

@im-bob-loucans
Copy link

I would like to extract state classes into their own file, but the examples all show the state class being declared on the fly and extending this. Is this a required pattern?

@im-bob-loucans
Copy link
Author

If its required request that the documentation be updated to reflect this constraint, Thanks.

@g-zachar
Copy link
Contributor

Hi @im-bob-loucans,

while Lightning does enforce some of its patterns, as long as you follow JS semantics you should be fine.

So for example:

{color: 0xff000000}

would be semantically equivalent to:

const COLOR_BLACK = 0xff000000;
...
{color: COLOR_BLACK}

Coming back to states, they mainly depend on class expressions and context resolution of static methods:

static _states() {
    return [
        class MyFirstState extends this {
            _handleEnter() {
                // overwrites root function when we are on this state
            }
        }
    ]
}

which could also be represented like that:

const stateFactory = function(cls) {
    return class MyFirstState extends cls {
        _handleEnter() {
            // overwrites root function when we are on this state
        }
    }
}

static _states() {
  return [stateFactory(this)]
}

So, while following examples is probably safest way to avoid potential issues e.g. with context resolution, it is by no means enforced.

(More extensive example: https://gist.github.com/g-zachar/580094a60c0e27a0396e0745fae6cd2c)

Let me know if this answers your question.

@im-bob-loucans
Copy link
Author

Thanks for the info. I followed the factory pattern you provided.

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

No branches or pull requests

2 participants