Skip to content

Commit

Permalink
added function in constructor Transitions
Browse files Browse the repository at this point in the history
  • Loading branch information
panManfredini committed Feb 16, 2020
1 parent 97d2e32 commit 81fb505
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions build/stateElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ class BaseState {
* states are always well defined, it guarantees that UI updates are made at transition completion (final state) only.
*/
export class StateTransition extends BaseState {
constructor(NAME, func) {
super(NAME);
if (typeof func === "function")
this.usrDefined_transition = func;
}
/**
* User defined transition to be overwritten.
* @param input Any meaningfull data.
Expand Down
7 changes: 7 additions & 0 deletions src/stateElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,19 @@ class BaseState{

}


type usrCallback = (input?: any) => void;

/**
* A stateTransition is a global function that is meant to apply simultaneously an overall state change,
* this can be made of just one variable change or multiple stateVariables changes at the same time, so that the initial and final
* states are always well defined, it guarantees that UI updates are made at transition completion (final state) only.
*/
export class StateTransition extends BaseState{
constructor(NAME:string,func?:usrCallback){
super(NAME);
if(typeof func === "function") this.usrDefined_transition = func;
}
/**
* User defined transition to be overwritten.
* @param input Any meaningfull data.
Expand Down
11 changes: 11 additions & 0 deletions test/stateTransition.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default function (){

let counter_st = 0;
let counter_gb = 0;
let counter_st_init = 0;

describe('Transition',()=>{

Expand All @@ -14,6 +15,11 @@ export default function (){
let mess = new Message("pollo");
let message = "ciao";

let st_init = new StateTransition("test",(i)=>{
if(i) counter_st_init=i;
else counter_st_init++;
});


let test_target = document.createElement("h1");
let test_target2 = document.createElement("h2");
Expand Down Expand Up @@ -103,6 +109,11 @@ export default function (){
chai.assert.Throw(func, "Forbidden multiple-update during an update callback loop");
chai.assert.Throw(func2, "Target is undefined");

st_init.applyTransition();
chai.assert.equal(counter_st_init, 1, "standalone init ok");
st_init.applyTransition(7);
chai.assert.equal(counter_st_init, 7, "standalone init ok 2");

});
it('Detaches',()=>{

Expand Down

0 comments on commit 81fb505

Please sign in to comment.