Skip to content

A Stateful class implementation for both NodeJS and Browser environments

License

Notifications You must be signed in to change notification settings

brec-c/stateful

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

#Stateful Designed Classes

When designing certain kinds of classes it can be very useful to have a map or chart of all the potential States that might exist for the class. Not only all the states but also how they are allowed to transition between them. Quite often just the exercise of thinking up the States and their transitions will help you design the class more thoughtfully. Stateful gives you mechanic and then reinforces by preventing any invalid state changes. In addition, Stateful also allows you to have specific implementations by state (ie the doThat (™) method might behave different if your class is "loaded" and not "processing").

Install

npm install stateful

#Usage

Look at this example:

Statechart = require 'stateful'

class BeerMug extends Statechart

@StateChart 
	Empty: 
		transitions: [
			destination: 'Empty/Full'
			action: 'pour'
		]
		methods: 
			pour: -> 
				console.log "I'm pouring..."
				Statechart.Success
		paths: 
			Full:
				transitions: [
					destination: 'Empty'
					action: 'drink'
				]
				methods:
					drink: -> 
						console.log "chug, chug, chug..."
						Statechart.Success

onStateChange: ->
	console.log "I'm now in state: #{@stateName}."
	

mug = new BeerMug

mug.pour()

mug.drink()

About

A Stateful class implementation for both NodeJS and Browser environments

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published