Skip to content

Commit 6b96198

Browse files
authored
Merge pull request #21 from canjs/temp-bind
temporarily activating observable without having to temporarily bind
2 parents 7f5f89e + 628be54 commit 6b96198

File tree

6 files changed

+110
-8
lines changed

6 files changed

+110
-8
lines changed

async/async-test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,42 @@ QUnit.test("resolving, then later returning should not cause duplicate events (#
267267

268268
assert.equal(count, 2, "2 change events");
269269
});
270+
271+
QUnit.test("proactive binding doesn't last past binding (can-stache#486)", function(){
272+
var value = new SimpleObservable(2);
273+
274+
var readCount = 0;
275+
276+
277+
var obs = new AsyncObservable(function(lastSet, resolve){
278+
279+
readCount++;
280+
if(!resolve) {
281+
return "default";
282+
}
283+
if(value.get() === 1) {
284+
setTimeout(function(){
285+
resolve("a");
286+
}, 1);
287+
} else {
288+
setTimeout(function(){
289+
resolve("b");
290+
}, 1);
291+
}
292+
});
293+
294+
var outer = new Observation(function(){
295+
return obs.get();
296+
});
297+
298+
function handler(){}
299+
300+
outer.on(handler);
301+
302+
outer.off(handler);
303+
304+
value.set(3);
305+
306+
QUnit.equal(readCount, 1, "internal observation only updated once");
307+
308+
});

async/async.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ AsyncObservable.prototype.handler = function(newVal) {
7474
};
7575

7676
var peek = ObservationRecorder.ignore(canReflect.getValue.bind(canReflect));
77-
AsyncObservable.prototype.onBound = function() {
78-
this.bound = true;
77+
AsyncObservable.prototype.activate = function() {
7978
canReflect.onValue(this.observation, this.handler, "notify");
8079
if (!this.resolveCalled) {
8180
this.value = peek(this.observation);

resolver/resolver-test.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var SimpleObservable = require('../can-simple-observable');
44
var mapEventMixin = require("can-event-queue/map/map");
55
var canSymbol = require("can-symbol");
66
var canReflect = require("can-reflect");
7-
var queues = require("can-queues");
7+
var Observation = require("can-observation");
88

99
QUnit.module('can-simple-observable/resolver');
1010

@@ -221,3 +221,31 @@ QUnit.test("getWhatIChange", function(assert) {
221221
canReflect.onValue(obs, function() {});
222222
assert.ok(canReflect.getWhatIChange(dep).derive.valueDependencies.has(obs));
223223
});
224+
225+
QUnit.test("proactive binding doesn't last past binding (can-stache#486)", function(){
226+
var v = new SimpleObservable(2);
227+
228+
var readCount = 0;
229+
230+
var obs = new ResolverObservable(function(value) {
231+
value.listenTo(v, function(newVal) {
232+
readCount++;
233+
value.resolve(newVal);
234+
});
235+
});
236+
237+
var outer = new Observation(function(){
238+
return obs.get();
239+
});
240+
241+
function handler(){}
242+
243+
outer.on(handler);
244+
245+
outer.off(handler);
246+
247+
v.set(3);
248+
249+
QUnit.equal(readCount, 0, "internal observation only updated once");
250+
251+
});

resolver/resolver.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,7 @@ canReflect.assignMap(ResolverObservable.prototype, {
145145
);
146146
}
147147
},
148-
onBound: function() {
149-
this.bound = true;
148+
activate: function() {
150149
this.isBinding = true;
151150
this.teardown = this.resolver.call(this.context, this.valueOptions);
152151
this.isBinding = false;
@@ -170,7 +169,7 @@ canReflect.assignMap(ResolverObservable.prototype, {
170169
if (ObservationRecorder.isRecording()) {
171170
ObservationRecorder.add(this);
172171
if (!this.bound) {
173-
Observation.temporarilyBind(this);
172+
this.onBound();
174173
}
175174
}
176175

settable/settable-test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ var QUnit = require('steal-qunit');
33
var SettableObservable = require('./settable');
44
var SimpleObservable = require('../can-simple-observable');
55
var canReflect = require('can-reflect');
6+
var Observation = require("can-observation");
7+
8+
9+
var ObservationRecorder = require("can-observation-recorder");
610

711
QUnit.module('can-simple-observable/settable');
812

@@ -152,3 +156,28 @@ QUnit.test("setting an observable to Settable observable works", function(assert
152156
"should replace the internal observable with 'two'"
153157
);
154158
});
159+
160+
QUnit.test("proactive binding doesn't last past binding (can-stache#486)", function(){
161+
var value = new SimpleObservable(2);
162+
163+
var readCount = 0;
164+
var obs = new SettableObservable(function(lastSet){
165+
readCount++;
166+
return lastSet * value.get();
167+
}, null, 1);
168+
169+
var outer = new Observation(function(){
170+
return obs.get();
171+
});
172+
173+
function handler(){}
174+
175+
outer.on(handler);
176+
177+
outer.off(handler);
178+
179+
value.set(3);
180+
181+
QUnit.equal(readCount, 1, "internal observation only updated once");
182+
183+
});

settable/settable.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,14 @@ Object.assign(SettableObservable.prototype, {
7373
);
7474
},
7575
onBound: function() {
76-
this.bound = true;
76+
// onBound can be called by `.get` and then later called through
77+
// a keyTree binding.
78+
if(!this.bound) {
79+
this.bound = true;
80+
this.activate();
81+
}
82+
},
83+
activate: function(){
7784
canReflect.onValue(this.observation, this.handler, "notify");
7885
this.value = peek(this.observation);
7986
},
@@ -100,7 +107,8 @@ Object.assign(SettableObservable.prototype, {
100107
if (ObservationRecorder.isRecording()) {
101108
ObservationRecorder.add(this);
102109
if (!this.bound) {
103-
Observation.temporarilyBind(this);
110+
// proactively setup bindings
111+
this.onBound();
104112
}
105113
}
106114

0 commit comments

Comments
 (0)