Skip to content

Commit b1ac682

Browse files
authored
Merge pull request #8 from theSherwood/subscription
Adds subscriptions to variables
2 parents aede21e + b914042 commit b1ac682

File tree

6 files changed

+106
-1
lines changed

6 files changed

+106
-1
lines changed

dist/variable.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ export declare class Variable {
4545
* @private
4646
*/
4747
setValue(value: number): void;
48+
/**
49+
* Set a callback for whenever the value changes.
50+
*
51+
* @param {function(number,number):void} callback to call whenever the variable value changes
52+
*/
53+
subscribe(callback: (value: number, previousValue: number) => void): void;
54+
/**
55+
* Stops the variable from calling the callback when the variable value
56+
* changes.
57+
*/
58+
unsubscribe(): void;
4859
/**
4960
* Creates a new Expression by adding a number, variable or expression
5061
* to the variable.
@@ -85,5 +96,6 @@ export declare class Variable {
8596
private _value;
8697
private _context;
8798
private _id;
99+
private _callback;
88100
}
89101
//# sourceMappingURL=variable.d.ts.map

dist/variable.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/variable.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,26 @@ export class Variable {
5959
* @private
6060
*/
6161
setValue(value) {
62+
var previousValue = this._value;
6263
this._value = value;
64+
if (this._callback && previousValue !== value) {
65+
this._callback(value, previousValue);
66+
}
67+
}
68+
/**
69+
* Set a callback for whenever the value changes.
70+
*
71+
* @param {function(number,number):void} callback to call whenever the variable value changes
72+
*/
73+
subscribe(callback) {
74+
this._callback = callback;
75+
}
76+
/**
77+
* Stops the variable from calling the callback when the variable value
78+
* changes.
79+
*/
80+
unsubscribe() {
81+
this._callback = null;
6382
}
6483
/**
6584
* Creates a new Expression by adding a number, variable or expression
@@ -116,6 +135,7 @@ export class Variable {
116135
_value = 0.0;
117136
_context = null;
118137
_id = VarId++;
138+
_callback;
119139
}
120140
/**
121141
* The internal variable id counter.

docs/Kiwi.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ console.assert(centerX.value() === 250)
4848
- [.name()](#module_@lume/kiwi..Variable+name) ⇒ <code>String</code>
4949
- [.setName(name)](#module_@lume/kiwi..Variable+setName)
5050
- [.value()](#module_@lume/kiwi..Variable+value) ⇒ <code>Number</code>
51+
- [.subscribe(callback)](#module_@lume/kiwi..Variable+subscribe)
52+
- [.unsubscribe()](#module_@lume/kiwi..Variable+unsubscribe)
5153
- [.plus(value)](#module_@lume/kiwi..Variable+plus) ⇒ <code>Expression</code>
5254
- [.minus(value)](#module_@lume/kiwi..Variable+minus) ⇒ <code>Expression</code>
5355
- [.multiply(coefficient)](#module_@lume/kiwi..Variable+multiply) ⇒ <code>Expression</code>
@@ -99,6 +101,8 @@ The primary user constraint variable.
99101
- [.name()](#module_@lume/kiwi..Variable+name) ⇒ <code>String</code>
100102
- [.setName(name)](#module_@lume/kiwi..Variable+setName)
101103
- [.value()](#module_@lume/kiwi..Variable+value) ⇒ <code>Number</code>
104+
- [.subscribe(callback)](#module_@lume/kiwi..Variable+subscribe)
105+
- [.unsubscribe()](#module_@lume/kiwi..Variable+unsubscribe)
102106
- [.plus(value)](#module_@lume/kiwi..Variable+plus) ⇒ <code>Expression</code>
103107
- [.minus(value)](#module_@lume/kiwi..Variable+minus) ⇒ <code>Expression</code>
104108
- [.multiply(coefficient)](#module_@lume/kiwi..Variable+multiply) ⇒ <code>Expression</code>
@@ -140,6 +144,26 @@ Returns the value of the variable.
140144

141145
**Kind**: instance method of [<code>Variable</code>](#module_@lume/kiwi..Variable)
142146
**Returns**: <code>Number</code> - Calculated value
147+
<a name="module_@lume/kiwi..Variable+subscribe"></a>
148+
149+
### variable.subscribe(callback)
150+
151+
Set a callback for whenever the value changes.
152+
153+
**Kind**: instance method of [<code>Variable</code>](#module_@lume/kiwi..Variable)
154+
155+
| Param | Type | Description |
156+
| -------- | --------------------- | ------------------------------------------- |
157+
| callback | <code>function</code> | to call whenever the variable value changes |
158+
159+
<a name="module_@lume/kiwi..Variable+unsubscribe"></a>
160+
161+
### variable.unsubscribe()
162+
163+
Stops the variable from calling the callback when the variable value
164+
changes.
165+
166+
**Kind**: instance method of [<code>Variable</code>](#module_@lume/kiwi..Variable)
143167
<a name="module_@lume/kiwi..Variable+plus"></a>
144168

145169
### variable.plus(value) ⇒ <code>Expression</code>

src/variable.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,28 @@ export class Variable {
6767
* @private
6868
*/
6969
public setValue(value: number): void {
70+
var previousValue = this._value
7071
this._value = value
72+
if (this._callback && previousValue !== value) {
73+
this._callback(value, previousValue)
74+
}
75+
}
76+
77+
/**
78+
* Set a callback for whenever the value changes.
79+
*
80+
* @param {function(number,number):void} callback to call whenever the variable value changes
81+
*/
82+
public subscribe(callback: (value: number, previousValue: number) => void): void {
83+
this._callback = callback
84+
}
85+
86+
/**
87+
* Stops the variable from calling the callback when the variable value
88+
* changes.
89+
*/
90+
public unsubscribe(): void {
91+
this._callback = null
7192
}
7293

7394
/**
@@ -131,6 +152,7 @@ export class Variable {
131152
private _value: number = 0.0
132153
private _context: any = null
133154
private _id: number = VarId++
155+
private _callback: (value: number, previousValue: number) => void
134156
}
135157

136158
/**

test/main.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,33 @@ describe('import kiwi', function () {
3939
solver.updateVariables()
4040
assert.equal(200, variable.value())
4141
})
42+
it('variable.subscribe(callback) => value: 400', function () {
43+
var var2 = new kiwi.Variable()
44+
var val, previousVal
45+
var2.subscribe((value, previousValue) => ((val = value), (previousVal = previousValue)))
46+
assert.equal(val, undefined)
47+
assert.equal(previousVal, undefined)
48+
solver.addEditVariable(var2, kiwi.Strength.strong)
49+
solver.suggestValue(var2, 400)
50+
solver.updateVariables()
51+
assert.equal(val, 400)
52+
assert.equal(previousVal, 0)
53+
solver.suggestValue(var2, 500)
54+
solver.updateVariables()
55+
assert.equal(val, 500)
56+
assert.equal(previousVal, 400)
57+
})
58+
it('variable.unsubscribe()', function () {
59+
var var2 = new kiwi.Variable()
60+
var val, previousVal
61+
var2.subscribe((value, previousValue) => ((val = value), (previousVal = previousValue)))
62+
solver.addEditVariable(var2, kiwi.Strength.strong)
63+
solver.suggestValue(var2, 300)
64+
var2.unsubscribe()
65+
solver.updateVariables()
66+
assert.equal(val, undefined)
67+
assert.equal(previousVal, undefined)
68+
})
4269
it('solver.removeEditVariable(variable) => solver.hasEditVariable(): false', function () {
4370
assert(solver.hasEditVariable(variable))
4471
solver.removeEditVariable(variable)

0 commit comments

Comments
 (0)