Skip to content

Commit 015926f

Browse files
committed
Transition, message, tested
1 parent 16ffab0 commit 015926f

File tree

4 files changed

+125
-44
lines changed

4 files changed

+125
-44
lines changed

build/stateElement.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ export class StateTransition {
2727
_isCallback_locked = false;
2828
}
2929
updateHandler(event) {
30-
console.log('Handling event UPDATE from stateTransition: ' + this.name);
3130
this.lock_callbacks();
3231
this.usrDefined_transition(event);
3332
// loop over watchers callbacks
@@ -44,12 +43,14 @@ export class StateTransition {
4443
this.unlock_callbacks();
4544
}
4645
watchHanlder(event) {
47-
//console.log('Adding element to watchlist of: '+this.name);
46+
if (event.target === null || event.target === undefined)
47+
throw Error("Target is undefined? WTF?");
4848
// add element to the watcher list
4949
this.callbackMap.set(event.target, event.detail.update);
5050
}
5151
detachHanlder(event) {
52-
//console.log('Removing element from watchlist of: '+this.name);
52+
if (event.target === null || event.target === undefined)
53+
throw Error("Target is undefined? WTF?");
5354
// remove element from watcher list
5455
this.callbackMap.delete(event.target);
5556
}
@@ -98,7 +99,6 @@ export class StateVariable extends StateTransition {
9899
_transitions_callbackMap.set(this.callbackMap, val);
99100
}
100101
updateHandler(event) {
101-
console.log('Handling event UPDATE from state variable: ' + this.name);
102102
this.lock_callbacks();
103103
this.value = event.detail.value;
104104
// loop over watchers callbacks
@@ -110,14 +110,10 @@ export class StateVariable extends StateTransition {
110110
}
111111
export class Message extends StateTransition {
112112
updateHandler(event) {
113-
console.log('Handling event MESSAGE from message: ' + this.name);
114-
// (_statewatchdog >= 10000) ? _statewatchdog = 0 : _statewatchdog++;
115-
/// let sanity_check = _statewatchdog;
116113
// loop over watchers callbacks
117114
for (let message_callback of this.callbackMap.values()) {
118115
message_callback(event.detail);
119116
}
120-
// if(sanity_check !== _statewatchdog) throw Error('State variables update is forbidden within a data update callback.');
121117
}
122118
}
123119
// FIXME:

src/stateElement.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ export class StateTransition {
3636

3737
updateHandler( event:CustomEvent) :void {
3838

39-
console.log('Handling event UPDATE from stateTransition: '+this.name);
4039
this.lock_callbacks();
4140

4241
this.usrDefined_transition(event);
@@ -57,15 +56,15 @@ export class StateTransition {
5756
}
5857

5958
watchHanlder( event:CustomEvent) :void {
60-
//console.log('Adding element to watchlist of: '+this.name);
61-
59+
if(event.target === null || event.target === undefined )
60+
throw Error("Target is undefined? WTF?")
6261
// add element to the watcher list
6362
this.callbackMap.set(event.target, event.detail.update);
6463
}
6564

6665
detachHanlder( event:CustomEvent) :void {
67-
//console.log('Removing element from watchlist of: '+this.name);
68-
66+
if(event.target === null || event.target === undefined )
67+
throw Error("Target is undefined? WTF?")
6968
// remove element from watcher list
7069
this.callbackMap.delete(event.target);
7170
}
@@ -127,7 +126,6 @@ export class StateVariable extends StateTransition{
127126

128127
updateHandler( event:CustomEvent) :void {
129128

130-
console.log('Handling event UPDATE from state variable: '+this.name);
131129
this.lock_callbacks();
132130

133131
this.value = event.detail.value;
@@ -146,15 +144,10 @@ export class StateVariable extends StateTransition{
146144
export class Message extends StateTransition{
147145
updateHandler( event:CustomEvent) :void {
148146

149-
console.log('Handling event MESSAGE from message: '+this.name);
150-
// (_statewatchdog >= 10000) ? _statewatchdog = 0 : _statewatchdog++;
151-
152-
/// let sanity_check = _statewatchdog;
153147
// loop over watchers callbacks
154148
for( let message_callback of this.callbackMap.values()){
155149
message_callback(event.detail);
156150
}
157-
// if(sanity_check !== _statewatchdog) throw Error('State variables update is forbidden within a data update callback.');
158151

159152
}
160153
}

test/stateTransitions.test.js

Lines changed: 103 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,112 @@
1-
import {StateTransition} from '../build/stateElement.js';
1+
import {StateTransition, StateVariable, Message} from '../build/stateElement.js';
22

33
export default function (){
44
// this is a must ;)
55
localStorage.clear();
66

7-
// create single transition and show that can attach, update, detach event
8-
// watch handler
7+
let counter_st = 0;
8+
let counter_gb = 0;
99

10-
// detach handler
11-
12-
// update handler
13-
// calles the user defined transition
14-
// calls its own updates
15-
// calls the global updates (autovalue works)
16-
// global map is cleared, if run multiple times the result is predictable
10+
describe('Transition',()=>{
11+
12+
let st = new StateTransition("test");
13+
let st_t = new StateTransition("test_trow");
14+
let mess = new Message("pollo");
15+
let message = "ciao";
1716

17+
let test_target = document.createElement("h1");
18+
let test_target2 = document.createElement("h2");
19+
let test_target4 = document.createElement("h4");
20+
let test_target3 = document.createElement("h3");
21+
let test_target5 = document.createElement("h5");
22+
23+
let state_var = new StateVariable("test_var", "string","ciao");
24+
let state_num = new StateVariable("test_num", "number", 765);
25+
26+
let ev5 = {detail:{'update':(a)=>{ st_t.updateHandler(); }}};
27+
let ev6 = {detail:{'update':(a)=>{ st_t.updateHandler(); }}};
28+
let ev7 = {detail:{'update':(a)=>{ message = a.message ; }}};
29+
30+
ev6.target = test_target5;
31+
ev7.target = test_target5;
32+
33+
mess.watchHanlder(ev7);
34+
35+
36+
it('Attach only once and override if same target',()=>{
37+
38+
let ev = {detail:{'update':(a)=>{ counter_st++; }}};
39+
let ev2 = {detail:{'update':(a)=>{ counter_st++; }}};
40+
let ev3 = {detail:{'update':(a)=>{ counter_gb++; }}};
41+
let ev4 = {detail:{'update':(a)=>{ counter_gb++; }}};
42+
43+
ev.target = test_target;
44+
ev2.target = test_target2;
45+
ev3.target = test_target3;
46+
ev4.target = test_target4;
47+
48+
st.watchHanlder(ev);
49+
state_var.watchHanlder(ev3);
50+
chai.assert.hasAllDeepKeys(st.callbackMap, test_target);
51+
chai.assert.equal(st.callbackMap.size, 1, "size of map");
52+
53+
st.watchHanlder(ev2);
54+
state_num.watchHanlder(ev4);
55+
chai.assert.hasAllDeepKeys(st.callbackMap, [test_target,test_target2]);
56+
chai.assert.equal(st.callbackMap.size, 2, "size of map");
57+
58+
});
59+
60+
it('Update Handler: user transition, own and global updates, global map is cleared, lock',()=>{
61+
62+
st.usrDefined_transition = (evt)=>{
63+
if(state_num.value === 123)
64+
state_num.auto_value = 321;
65+
66+
state_var.auto_value = "bella";
67+
}
68+
69+
chai.assert.equal(state_var.value, "ciao", "xcheck");
70+
chai.assert.equal(state_num.value, 765, "xcheck");
71+
st.updateHandler({ciao:"nonsense"});
72+
73+
chai.assert.equal(counter_st, 2, "Two watchers of Transition");
74+
chai.assert.equal(counter_gb, 1, "Only one global update");
75+
chai.assert.equal(state_var.value, "bella", "user defined update works");
76+
chai.assert.equal(state_num.value, 765, "xcheck");
77+
78+
state_num.value = 123;
79+
st.updateHandler({ciao:"nonsense"});
80+
chai.assert.equal(counter_st, 4, "Two watchers of Transition");
81+
chai.assert.equal(counter_gb, 3, "Only one global update");
82+
chai.assert.equal(state_var.value, "bella", "user defined update works");
83+
chai.assert.equal(state_num.value, 321, "xcheck");
84+
85+
st.watchHanlder(ev6);
86+
let func = ()=>{ st.updateHandler({ciao:"ciao"}); };
87+
let func2 = ()=>{ st.watchHanlder(ev5); };
88+
89+
chai.assert.Throw(func, "Forbidden multiple-update during an update callback loop");
90+
chai.assert.Throw(func2, "Target is undefined");
91+
92+
});
93+
it('Detaches',()=>{
94+
95+
let func3 = ()=>{ st.detachHanlder(ev5); };
96+
chai.assert.Throw(func3, "Target is undefined")
97+
98+
st.detachHanlder(ev6);
99+
chai.assert.hasAllDeepKeys(st.callbackMap, [test_target,test_target2]);
100+
chai.assert.equal(st.callbackMap.size, 2, "size of map");
101+
});
102+
describe('Message',()=>{
103+
it('Update Handler pass the message',()=>{
104+
let ev8 = {detail:{message:"cazzone"}};
105+
106+
mess.updateHandler(ev8);
107+
chai.assert.equal(message, "cazzone", "message doesn't work");
108+
});
109+
});
110+
});
18111

19-
// lock and unlock was tested
20112
}

test/stateVariable.test.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ export default function (){
4040
let test_function4 = () =>{let a = new StateVariable("test_function",'string',8); };
4141
let test_function7 = () =>{let a = new StateVariable("test_function",'string',pollo); };
4242

43-
chai.assert.Throw(test_function);
44-
chai.assert.Throw(test_function2);
45-
chai.assert.Throw(test_function3);
46-
chai.assert.Throw(test_function4);
47-
chai.assert.Throw(test_function5);
48-
chai.assert.Throw(test_function6);
49-
chai.assert.Throw(test_function7);
43+
chai.assert.Throw(test_function, "Wrong type assignment to state variable");
44+
chai.assert.Throw(test_function2,"Wrong type assignment to state variable");
45+
chai.assert.Throw(test_function3, "Wrong type assignment to state variable");
46+
chai.assert.Throw(test_function4, "Wrong type assignment to state variable");
47+
chai.assert.Throw(test_function5, "Wrong type assignment to state variable");
48+
chai.assert.Throw(test_function6, "Wrong type assignment to state variable");
49+
chai.assert.Throw(test_function7, "Wrong type assignment to state variable");
5050
});
5151
});
5252
describe('Input Output',()=>{
@@ -77,9 +77,9 @@ export default function (){
7777
let test_function2 = () =>{ test_bool.value = 89;};
7878
let test_function3 = () =>{ test_number.value = undefined;};
7979

80-
chai.assert.Throw(test_function);
81-
chai.assert.Throw(test_function2);
82-
chai.assert.Throw(test_function3);
80+
chai.assert.Throw(test_function, "Wrong type assignment to state variable");
81+
chai.assert.Throw(test_function2,"Wrong type assignment to state variable");
82+
chai.assert.Throw(test_function3, "Wrong type assignment to state variable");
8383

8484
localStorage.setItem("test_object",'\"ciao\"');
8585
localStorage.setItem("test_bool",'\"ciao\"');
@@ -88,9 +88,9 @@ export default function (){
8888
let test_function5 = () =>{ let ciao = test_bool.value;};
8989
let test_function6 = () =>{ let ciao = test_number.value;};
9090

91-
chai.assert.Throw(test_function4);
92-
chai.assert.Throw(test_function5);
93-
chai.assert.Throw(test_function6);
91+
chai.assert.Throw(test_function4, "corrupted");
92+
chai.assert.Throw(test_function5, "corrupted");
93+
chai.assert.Throw(test_function6, "corrupted");
9494
});
9595

9696
});
@@ -103,7 +103,7 @@ export default function (){
103103

104104
let throw_lock = ()=>{ let pippo = new CustomEvent("bordello",{ bubbles:true, detail:{'value':"ggg"}}); test_string.updateHandler(pippo)};
105105

106-
chai.assert.Throw(throw_lock);
106+
chai.assert.Throw(throw_lock, "Forbidden multiple-update");
107107
});
108108

109109
it('It updates all values and only once and unlocks',()=>{

0 commit comments

Comments
 (0)