Xstate: why is Machine just a function, not a constructor?

Created on 12 Jul 2019  路  3Comments  路  Source: davidkpiano/xstate

Question

Description:

Is there any particular reason Machine is not a constructor? The fact that it's capitalized but not actually a constructor means that I have to work around lint rules and style guidelines and litter my code with eslint comments to use it with the rest of my code or else turn off the useful lint warnings.

import { Machine, interpret } from 'xstate';

// eslint-disable-next-line new-cap
const machine = Machine({...});

If it's just a personal preference that you want to be different than 99% of JS code that's totally fine but I thought I'd ask if maybe there was some more concrete reason like maybe this is ES8 style or something new I'm not aware of.

Thanks

enhancement question

Most helpful comment

Nothing's stopping you from having const machine = new Machine(...); it'll work.

It's just a convention that Machine(...) returns a machine object that is "static" - it never changes its internal state.

However, I can/will add createMachine as an alias to the API:

import { createMachine } from 'xstate';

const machine = createMachine(...);

Better?

All 3 comments

Nothing's stopping you from having const machine = new Machine(...); it'll work.

It's just a convention that Machine(...) returns a machine object that is "static" - it never changes its internal state.

However, I can/will add createMachine as an alias to the API:

import { createMachine } from 'xstate';

const machine = createMachine(...);

Better?

createMachine is much better 馃憤

The createMachine function is now in xstate@next.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

suku-h picture suku-h  路  3Comments

mreinstein picture mreinstein  路  3Comments

kurtmilam picture kurtmilam  路  3Comments

laurentpierson picture laurentpierson  路  3Comments

rodinhart picture rodinhart  路  3Comments