Skip to content

Commit

Permalink
Clean transition implementation based on LCA algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
Mesmo committed Nov 25, 2013
1 parent 4dcd2ea commit 2d24fde
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Welcome to state.js

The current stable release is 3.1.2.
The current stable release is 3.1.3.

If you're using state.js I'd love to hear about it; please e-mail me at [email protected]

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "state.js",
"version": "3.1.2",
"version": "3.1.3",
"author": "Steelbreeze <[email protected]>",
"description": "Hierarchical finite state machine library for JavaScript",
"main": "./src/state",
Expand Down
24 changes: 15 additions & 9 deletions src/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ function initStateJS(exports) {
return this.regions.reduce(function (result, region) {return region.process(context, message) || result; }, false);
};

function lca(sourceAncestors, targetAncestors) {
function LCA(sourceAncestors, targetAncestors) {
var common = 0;

while (sourceAncestors.length > common && targetAncestors.length > common && sourceAncestors[common] === targetAncestors[common]) {
Expand All @@ -620,23 +620,27 @@ function initStateJS(exports) {
this.guard = guard || function (message) { return true; };

// evaluate path for non-internal transitions
if (target && target !== null) {
var sourceAncestors = source.ancestors(),
targetAncestors = target.ancestors(),
ignoreAncestors = lca(sourceAncestors, targetAncestors) + (source === target ? 0 : 1);
if (target && (target !== null)) {
var sourceAncestors = source.owner.ancestors(),
targetAncestors = target.owner.ancestors(),
lca = LCA(sourceAncestors, targetAncestors);

this.exit = sourceAncestors.slice(ignoreAncestors);
this.enter = targetAncestors.slice(ignoreAncestors);
this.exit = sourceAncestors.slice(lca + 1);
this.enter = targetAncestors.slice(lca + 1);

this.exit.reverse();

this.source = source;
this.target = target;
}

source[guard && guard.length > 0 ? "transitions" : "completions"].push(this);
}

Transition.prototype.traverse = function (context, message) {
if (this.exit) {
this.exit[0].beginExit(context);
this.source.beginExit(context);
this.source.endExit(context);

this.exit.forEach(function (element) { element.endExit(context); });
}
Expand All @@ -647,7 +651,9 @@ function initStateJS(exports) {

if (this.enter) {
this.enter.forEach(function (element) { element.beginEnter(context); });
this.enter[this.enter.length - 1].endEnter(context, false);

this.target.beginEnter(context);
this.target.endEnter(context, false);
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/state_min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2d24fde

Please sign in to comment.