Skip to content

Commit 8fae622

Browse files
committed
adds .value for canjs/can-value#19
1 parent 85e3a31 commit 8fae622

File tree

8 files changed

+124
-30
lines changed

8 files changed

+124
-30
lines changed

async/async.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function AsyncObservable(fn, context, initialValue) {
3434
// otherwise, if `resolve` was called synchronously in the getter,
3535
// resolve with the value passed to `resolve`
3636
else if (this.resolveCalled) {
37-
this.resolve(this.value);
37+
this.resolve(this._value);
3838
}
3939

4040
// if bound, the handlers will be called by `resolve`
@@ -80,14 +80,14 @@ var peek = ObservationRecorder.ignore(canReflect.getValue.bind(canReflect));
8080
AsyncObservable.prototype.activate = function() {
8181
canReflect.onValue(this.observation, this.handler, "notify");
8282
if (!this.resolveCalled) {
83-
this.value = peek(this.observation);
83+
this._value = peek(this.observation);
8484
}
8585
};
8686

8787
AsyncObservable.prototype.resolve = function resolve(newVal) {
8888
this.resolveCalled = true;
89-
var old = this.value;
90-
this.value = newVal;
89+
var old = this._value;
90+
this._value = newVal;
9191

9292
//!steal-remove-start
9393
if (process.env.NODE_ENV !== 'production') {

can-simple-observable-test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,30 @@ QUnit.test('basics', function(){
3434
QUnit.equal(canReflect.getValue(obs), 'four', 'getValue after offValue');
3535
});
3636

37+
QUnit.test('basics with .value', function(){
38+
expect(5);
39+
var obs = new SimpleObservable('one');
40+
41+
QUnit.equal(obs.value, 'one', 'getValue');
42+
43+
obs.value = 'two';
44+
ObservationRecorder.start();
45+
QUnit.equal(obs.value, 'two', 'setValue');
46+
var dependencies = ObservationRecorder.stop();
47+
QUnit.ok(dependencies.valueDependencies.has(obs), "was recorded");
48+
49+
var handler = function(newValue) {
50+
QUnit.equal(newValue, 'three', 'onValue');
51+
};
52+
canReflect.onValue(obs, handler);
53+
obs.value = 'three';
54+
55+
canReflect.offValue(obs, handler);
56+
obs.value = 'four';
57+
58+
QUnit.equal(obs.value, 'four', 'getValue after offValue');
59+
});
60+
3761
skipProduction("log observable changes", function(assert) {
3862
var done = assert.async();
3963
var obs = new SimpleObservable("one");

can-simple-observable.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,33 @@ var dispatchSymbol = canSymbol.for("can.dispatch");
4545
* ```
4646
*/
4747
function SimpleObservable(initialValue) {
48-
this.value = initialValue;
48+
this._value = initialValue;
4949
}
5050

5151
// mix in the value-like object event bindings
5252
valueEventBindings(SimpleObservable.prototype);
5353

54-
Object.assign(SimpleObservable.prototype, {
54+
canReflect.assignMap(SimpleObservable.prototype, {
5555
log: log,
5656
get: function(){
5757
ObservationRecorder.add(this);
58-
return this.value;
58+
return this._value;
5959
},
6060
set: function(value){
61-
var old = this.value;
62-
this.value = value;
61+
var old = this._value;
62+
this._value = value;
6363

6464
this[dispatchSymbol](value, old);
6565
}
6666
});
67+
Object.defineProperty(SimpleObservable.prototype,"value",{
68+
set: function(value){
69+
return this.set(value);
70+
},
71+
get: function(){
72+
return this.get();
73+
}
74+
});
6775

6876
var simpleObservableProto = {
6977
"can.getValue": SimpleObservable.prototype.get,
@@ -77,7 +85,7 @@ var simpleObservableProto = {
7785
//!steal-remove-start
7886
if (process.env.NODE_ENV !== 'production') {
7987
simpleObservableProto["can.getName"] = function() {
80-
var value = this.value;
88+
var value = this._value;
8189
if (typeof value !== 'object' || value === null) {
8290
value = JSON.stringify(value);
8391
}

resolver/resolver-test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ QUnit.test('basics listenTo', 14, function(assert){
8080
assert.equal(obs.get(), 6, "got unbound value");
8181
listenHandlers = obs.binder[ canSymbol.for("can.meta") ].listenHandlers;
8282
QUnit.equal(listenHandlers.size(), 1, "1 handlers after bind");
83-
queues.log("flush");
8483
number.set(2);
8584

8685
assert.equal(obs.get(), 5, "got updated value");

resolver/resolver.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function ResolverObservable(resolver, context) {
1414
// we don't want reads leaking out. We should be binding to all of this ourselves.
1515
this.resolver = ObservationRecorder.ignore(resolver);
1616
this.context = context;
17-
this.valueOptions = {
17+
this._valueOptions = {
1818
resolve: this.resolve.bind(this),
1919
listenTo: this.listenTo.bind(this),
2020
stopListening: this.stopListening.bind(this),
@@ -43,7 +43,7 @@ function ResolverObservable(resolver, context) {
4343
value: canReflect.getName(this) + ".update"
4444
});
4545

46-
canReflect.assignSymbols(this.valueOptions.lastSet, {
46+
canReflect.assignSymbols(this._valueOptions.lastSet, {
4747
"can.getName": function() {
4848
return (
4949
canReflect.getName(this.constructor) +"::lastSet"+
@@ -118,16 +118,16 @@ canReflect.assignMap(ResolverObservable.prototype, {
118118
return this;
119119
},
120120
resolve: function(newVal) {
121-
this.value = newVal;
121+
this._value = newVal;
122122
// if we are setting up the initial binding and we get a resolved value
123123
// do not emit events for it.
124124

125125
if(this.isBinding) {
126-
this.lastValue = this.value;
126+
this.lastValue = this._value;
127127
return newVal;
128128
}
129129

130-
if(this.value !== this.lastValue) {
130+
if(this._value !== this.lastValue) {
131131
var enqueueMeta = {};
132132

133133
//!steal-remove-start
@@ -140,7 +140,7 @@ canReflect.assignMap(ResolverObservable.prototype, {
140140
/* jshint laxcomma: false */
141141
}
142142
//!steal-remove-end
143-
143+
144144
queues.batch.start();
145145
queues.deriveQueue.enqueue(
146146
this.update,
@@ -154,14 +154,14 @@ canReflect.assignMap(ResolverObservable.prototype, {
154154
},
155155
update: function(){
156156

157-
if(this.lastValue !== this.value) {
157+
if(this.lastValue !== this._value) {
158158

159159
var old = this.lastValue;
160-
this.lastValue = this.value;
160+
this.lastValue = this._value;
161161
//!steal-remove-start
162162
if (process.env.NODE_ENV !== 'production') {
163163
if (typeof this._log === "function") {
164-
this._log(old, this.value);
164+
this._log(old, this._value);
165165
}
166166
}
167167
//!steal-remove-end
@@ -170,13 +170,13 @@ canReflect.assignMap(ResolverObservable.prototype, {
170170
queues.enqueueByQueue(
171171
this.handlers.getNode([]),
172172
this,
173-
[this.value, old]
173+
[this._value, old]
174174
);
175175
}
176176
},
177177
activate: function() {
178178
this.isBinding = true;
179-
this.teardown = this.resolver.call(this.context, this.valueOptions);
179+
this.teardown = this.resolver.call(this.context, this._valueOptions);
180180
this.isBinding = false;
181181
},
182182
onUnbound: function() {
@@ -188,7 +188,7 @@ canReflect.assignMap(ResolverObservable.prototype, {
188188
}
189189
},
190190
set: function(value) {
191-
this.valueOptions.lastSet.set(value);
191+
this._valueOptions.lastSet.set(value);
192192

193193
/*if (newVal !== this.lastSetValue.get()) {
194194
this.lastSetValue.set(newVal);
@@ -203,11 +203,11 @@ canReflect.assignMap(ResolverObservable.prototype, {
203203
}
204204

205205
if (this.bound === true) {
206-
return this.value;
206+
return this._value;
207207
} else {
208208
var handler = function(){};
209209
this.on(handler);
210-
var val = this.value;
210+
var val = this._value;
211211
this.off(handler);
212212
return val;
213213
}

settable/settable-test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,43 @@ QUnit.test('basics', function(){
4949

5050
});
5151

52+
QUnit.test('basics with .value', function(){
53+
54+
var value = new SimpleObservable(2);
55+
56+
57+
var obs = new SettableObservable(function(lastSet){
58+
return lastSet * value.value;
59+
}, null, 1);
60+
61+
// Unbound and unobserved behavior
62+
QUnit.equal(obs.value, 2, 'getValue unbound');
63+
64+
65+
66+
var changes = 0;
67+
var handler = function(newValue) {
68+
changes++;
69+
if(changes === 1) {
70+
QUnit.equal(newValue, 4, 'set observable');
71+
obs.value = (3);
72+
} else if(changes === 2){
73+
QUnit.equal(newValue, 6, 'set observable in handler');
74+
value.value = (3);
75+
} else {
76+
QUnit.equal(newValue, 9, 'set source');
77+
}
78+
};
79+
canReflect.onValue(obs, handler);
80+
obs.value = 2;
81+
82+
QUnit.equal( obs.value, 9, "after bound");
83+
canReflect.offValue(obs, handler);
84+
obs.value = 5;
85+
QUnit.equal( obs.value, 15, "after unbound");
86+
87+
});
88+
5289
QUnit.test("get and set Priority", function(){
5390
var value = new SimpleObservable(2);
5491

settable/settable.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ function SettableObservable(fn, context, initialValue) {
5050

5151
valueEventBindings(SettableObservable.prototype);
5252

53-
Object.assign(SettableObservable.prototype, {
53+
canReflect.assignMap(SettableObservable.prototype, {
5454
// call `obs.log()` to log observable changes to the browser console
5555
// The observable has to be bound for `.log` to be called
5656
log: log,
5757
constructor: SettableObservable,
5858
handler: function(newVal) {
59-
var old = this.value;
60-
this.value = newVal;
59+
var old = this._value;
60+
this._value = newVal;
6161

6262
//!steal-remove-start
6363
if (process.env.NODE_ENV !== 'production') {
@@ -87,7 +87,7 @@ Object.assign(SettableObservable.prototype, {
8787
},
8888
activate: function(){
8989
canReflect.onValue(this.observation, this.handler, "notify");
90-
this.value = peek(this.observation);
90+
this._value = peek(this.observation);
9191
},
9292
onUnbound: function() {
9393
this.bound = false;
@@ -118,7 +118,7 @@ Object.assign(SettableObservable.prototype, {
118118
}
119119

120120
if (this.bound === true) {
121-
return this.value;
121+
return this._value;
122122
} else {
123123
return this.observation.get();
124124
}
@@ -131,6 +131,15 @@ Object.assign(SettableObservable.prototype, {
131131
}
132132
});
133133

134+
Object.defineProperty(SettableObservable.prototype,"value",{
135+
set: function(value){
136+
return this.set(value);
137+
},
138+
get: function(){
139+
return this.get();
140+
}
141+
});
142+
134143
canReflect.assignSymbols(SettableObservable.prototype, {
135144
"can.getValue": SettableObservable.prototype.get,
136145
"can.setValue": SettableObservable.prototype.set,

setter/setter-test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,23 @@ QUnit.test('basics', function(assert){
2222
assert.equal(canReflect.getValue(obs), 3, 'getValue unbound');
2323
});
2424

25+
QUnit.test('basics with .value', function(assert){
26+
var value = new SimpleObservable(2);
27+
28+
var obs = new SetterObservable(function(){
29+
return value.value;
30+
}, function(newVal){
31+
value.value = (newVal);
32+
});
33+
34+
// Unbound and unobserved behavior
35+
assert.equal(obs.value, 2, 'getValue unbound');
36+
37+
obs.value = 3;
38+
assert.equal(value.value, 3, 'value set');
39+
assert.equal(obs.value, 3, 'getValue unbound');
40+
});
41+
2542
QUnit.test("get and set Priority", function(assert){
2643
var value = new SimpleObservable(2);
2744

0 commit comments

Comments
 (0)