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

ability to move states in other coffee scripts #13

Open
codervinod opened this issue Jul 17, 2014 · 4 comments
Open

ability to move states in other coffee scripts #13

codervinod opened this issue Jul 17, 2014 · 4 comments

Comments

@codervinod
Copy link

Hi there, We have a very big statemachine in our use case and wondering if its possible to have individual states being exported into separate files. Can you please provide suggestions?

Thanks,Vinod

@ichernev
Copy link
Owner

The states property is an object. So you can put different pieces of it in different files, then merge them together in the main file. Also you can add methods to the state machine (not states - just regular methods), after the fact (modifying the StateMachine.prototype (if you state machine is called StateMachine)).

So in this regard I would say the user has full control on where/how is stuff defined.

I'd imagine splitting the states (and helper functions) in separate file, where each file will return part of the states object (some keys), and part of the prototype (helper methods). Then the main file just iterates over all plugins and merges the states and prototype objects and creates an object with all of those:

plugins = []
# collect plugins -- each returns an object with {states: {}, helpers: {}} properties
states = {}
helpers = {}
for plugin in plugins
  _.extend(states, plugin.states)
  _.extend(helpers, plugin.helpers)

class StateMachine extend NodeState
  states: states

  constructor: ->
    super
    # whatever

  # other global helper methods here

_.extend(StateMachine.prototype, helpers)

# now you can use StateMachine
sm = new StateMachine

_.extend just copies all properties from the second object to the first object.

@codervinod
Copy link
Author

Hi Iskren,

Thanks for quick reply. Would you be kind enough to also show us how a plugin file will look like? I am novice in js/coffeescript and just getting started.

Appreciate your help and quick reply.

Thanks,

Vinod

From: Iskren Ivov Chernev [mailto:[email protected]]
Sent: Thursday, July 17, 2014 4:07 PM
To: ichernev/node-state
Cc: Vinod Gupta
Subject: Re: [node-state] ability to move states in other coffee scripts (#13)

The states property is an object. So you can put different pieces of it in different files, then merge them together in the main file. Also you can add methods to the state machine (not states - just regular methods), after the fact (modifying the StateMachine.prototype (if you state machine is called StateMachine)).

So in this regard I would say the user has full control on where/how is stuff defined.

I'd imagine splitting the states (and helper functions) in separate file, where each file will return part of the states object (some keys), and part of the prototype (helper methods). Then the main file just iterates over all plugins and merges the states and prototype objects and creates an object with all of those:

plugins = []

collect plugins -- each returns an object with {states: {}, helpers: {}} properties

states = {}
helpers = {}
for plugin in plugins
_.extend(states, plugin.states)
_.extend(helpers, plugin.helpers)

class StateMachine extend NodeState
states: states

constructor: ->
super
# whatever

other global helper methods here

_.extend(StateMachine.prototype, helpers)

now you can use StateMachine

sm = new StateMachine

_.extend just copies all properties from the second object to the first object.


Reply to this email directly or view it on GitHub #13 (comment) . https:/notifications/beacon/7387542__eyJzY29wZSI6Ik5ld3NpZXM6QmVhY29uIiwiZXhwaXJlcyI6MTcyMTI1NzYwNCwiZGF0YSI6eyJpZCI6MzczMjM3NzR9fQ==--7c62c3b22363af109ccf2227571717599853f619.gif

@ichernev
Copy link
Owner

So you should be using coffee-script with node.js I guess:

Directory structure should be something like: where first and second are two plugins.

.
├── state_machine
│   ├── first.coffee
│   └── second.coffee
└── state_machine.coffee

Then inside the plugin file you say:

# state_machine/first.coffee
exports.states =
  A:
    Enter: ->
      @helperA()
  B:
    Enter: ->

exports.helpers =
  helperA: ->

And the main file would be

# state_machine.coffee
plugins = [
  require('state_machine/first')
  require('state_machine/second')
]
# continue using plugins as in my first example

You might want to add transitions to the picture -- another property of each file, that gets merged the same way as states and then gets added to the object same as states.

@codervinod
Copy link
Author

Iskren,

Thanks a lot for your help.

I have sent you a linked in request. Let me know if you are interested in moving to San Francisco Bay Area for full time opportunity. We would be glad to talk to you about some of openings in our company. You can read more about my company at www.efi.com http://www.efi.com

Thanks,

Vinod

From: Iskren Ivov Chernev [mailto:[email protected]]
Sent: Thursday, July 17, 2014 4:59 PM
To: ichernev/node-state
Cc: Vinod Gupta
Subject: Re: [node-state] ability to move states in other coffee scripts (#13)

So you should be using coffee-script with node.js I guess:

Directory structure should be something like: where first and second are two plugins.

.
├── state_machine
│ ├── first.coffee
│ └── second.coffee
└── state_machine.coffee

Then inside the plugin file you say:

state_machine/first.coffee

exports.states =
A:
Enter: ->
@HelperA()
B:
Enter: ->

exports.helpers =
helperA: ->

And the main file would be

state_machine.coffee

plugins = [
require('state_machine/first')
require('state_machine/second')
]

continue using plugins as in my first example

You might want to add transitions to the picture -- another property of each file, that gets merged the same way as states and then gets added to the object same as states.


Reply to this email directly or view it on GitHub #13 (comment) . https:/notifications/beacon/7387542__eyJzY29wZSI6Ik5ld3NpZXM6QmVhY29uIiwiZXhwaXJlcyI6MTcyMTI2MDc1NSwiZGF0YSI6eyJpZCI6MzczMjM3NzR9fQ==--5b448c9aa8415cb9b25fe8ba83a8667d534dbc7b.gif

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

No branches or pull requests

2 participants