Skip to content

Commit dc57dbc

Browse files
committed
fix: fix_variable now doesn't update existing variables
1 parent 3e686d1 commit dc57dbc

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/state_handler.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ const create_state_handler = () => {
1111
const state_handler = {
1212
'get_state': () => state,
1313
'init_variable': (variable, value) => {
14-
state[variable] = value
15-
handle_change()
14+
if (state[variable] === undefined) {
15+
state[variable] = value
16+
handle_change()
17+
}
1618
},
1719
'set_variable': (variable, value) => {
1820
state[variable] = value

src/state_handler.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ describe('state handler', () => {
3737
})
3838
})
3939

40+
it('init_variable does not update state if variable is defined', () => {
41+
const state_handler = create_state_handler()
42+
state_handler.init_variable('bar', false)
43+
state_handler.init_variable('pi', 3.14)
44+
state_handler.init_variable('pi', -3.14)
45+
state_handler.get_state().should.deepEqual({
46+
'bar': false,
47+
'pi': 3.14
48+
})
49+
})
50+
4051
it('set_variable should update state', () => {
4152
const state_handler = create_state_handler()
4253
state_handler.set_variable('foo', 42)
@@ -90,6 +101,15 @@ describe('state handler', () => {
90101
my_callback.should.be.calledOnce()
91102
})
92103

104+
it('no state change if init_variable is called twice on the same variable', () => {
105+
const my_callback = sinon.spy(function() {state_handler.subscribe(my_callback)})
106+
const state_handler = create_state_handler()
107+
state_handler.subscribe(my_callback)
108+
state_handler.init_variable('foo', 42)
109+
state_handler.init_variable('foo', 42)
110+
my_callback.should.be.calledOnce()
111+
})
112+
93113
it('callback should be called again with re-subscribe', () => {
94114
const my_callback = sinon.spy(function() {state_handler.subscribe(my_callback)})
95115
const state_handler = create_state_handler()

0 commit comments

Comments
 (0)