First commit
This commit is contained in:
154
framework/client/stateMachine.js
Normal file
154
framework/client/stateMachine.js
Normal file
@@ -0,0 +1,154 @@
|
||||
|
||||
|
||||
class stateEvent{
|
||||
|
||||
object;
|
||||
|
||||
method;
|
||||
|
||||
args;
|
||||
|
||||
}
|
||||
|
||||
class state{
|
||||
|
||||
events = new Array();
|
||||
|
||||
registerEvent( object, method, args ) {
|
||||
|
||||
var event = new stateEvent();
|
||||
|
||||
event.object = object;
|
||||
|
||||
event.method = method;
|
||||
|
||||
event.args = args;
|
||||
|
||||
this.events.push( event );
|
||||
|
||||
}
|
||||
|
||||
async triggerEvents() {
|
||||
|
||||
var events = this.events;
|
||||
|
||||
for ( var i = 0; i < events.length; i++ ) {
|
||||
|
||||
var event = events[ i ];
|
||||
|
||||
var object = event.object;
|
||||
|
||||
var method = event.method;
|
||||
|
||||
var args = event.args;
|
||||
|
||||
object["__" + method]( args[0], args[1], args[2], args[3], args[4] );
|
||||
|
||||
//object[ method ]()
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default class stateMachine{
|
||||
|
||||
states = new Array();
|
||||
|
||||
currentState;
|
||||
|
||||
stateID = 0;
|
||||
|
||||
|
||||
constructor() {
|
||||
|
||||
var state = this.createState();
|
||||
|
||||
//history.pushState( { id: state.id }, "Unify" );
|
||||
|
||||
}
|
||||
|
||||
createState() {
|
||||
|
||||
var newState = new state();
|
||||
|
||||
newState.id = this.stateID++;
|
||||
|
||||
this.states.push( newState );
|
||||
|
||||
this.currentState = newState;
|
||||
|
||||
return newState;
|
||||
|
||||
}
|
||||
|
||||
registerEvent( object, method, args ) {
|
||||
|
||||
var state = this.currentState;
|
||||
|
||||
state.registerEvent( object, method, args )
|
||||
|
||||
//console.log( "event registered in stateMachine", this );
|
||||
|
||||
}
|
||||
|
||||
async composeState( ...args ) {
|
||||
|
||||
this.createState();
|
||||
|
||||
var state = this.currentState;
|
||||
|
||||
if( args.length == 0 ) {
|
||||
|
||||
history.pushState( { id: state.id }, "Unify" );
|
||||
|
||||
} else if( args.length == 1 ) {
|
||||
|
||||
var title = args.pop().replaceAll( " ", "_" );
|
||||
|
||||
//if( !title ) {
|
||||
|
||||
history.pushState( { id: state.id }, "localhost", title + "/" );
|
||||
|
||||
//} else {
|
||||
|
||||
// history.pushState( { id: state.id }, "Unify", "/" + title + ".html" );
|
||||
|
||||
//}
|
||||
|
||||
|
||||
|
||||
} else {
|
||||
|
||||
var title = args.pop();
|
||||
|
||||
title = title.toString().replaceAll( " ", "_" )
|
||||
|
||||
if(title == "") {
|
||||
|
||||
title = "page"
|
||||
|
||||
}
|
||||
|
||||
history.pushState( { id: state.id }, "Unify", "/" + args.join("/") + "/" + title + ".html" );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
generateUrl( ...args ) {
|
||||
|
||||
var title = args.pop().replaceAll( " ", "_" );
|
||||
|
||||
return "https://unifyjs.org" + "/" + args.join("/") + "/" + title + ".html";
|
||||
|
||||
}
|
||||
|
||||
getState( id ) {
|
||||
|
||||
return this.states[ id ];
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user