Skip to content

Commit

Permalink
Merge pull request #21 from canjs/temp-bind
Browse files Browse the repository at this point in the history
temporarily activating observable without having to temporarily bind
  • Loading branch information
justinbmeyer authored Mar 6, 2018
2 parents 7f5f89e + 628be54 commit 6b96198
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 8 deletions.
39 changes: 39 additions & 0 deletions async/async-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,42 @@ QUnit.test("resolving, then later returning should not cause duplicate events (#

assert.equal(count, 2, "2 change events");
});

QUnit.test("proactive binding doesn't last past binding (can-stache#486)", function(){
var value = new SimpleObservable(2);

var readCount = 0;


var obs = new AsyncObservable(function(lastSet, resolve){

readCount++;
if(!resolve) {
return "default";
}
if(value.get() === 1) {
setTimeout(function(){
resolve("a");
}, 1);
} else {
setTimeout(function(){
resolve("b");
}, 1);
}
});

var outer = new Observation(function(){
return obs.get();
});

function handler(){}

outer.on(handler);

outer.off(handler);

value.set(3);

QUnit.equal(readCount, 1, "internal observation only updated once");

});
3 changes: 1 addition & 2 deletions async/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ AsyncObservable.prototype.handler = function(newVal) {
};

var peek = ObservationRecorder.ignore(canReflect.getValue.bind(canReflect));
AsyncObservable.prototype.onBound = function() {
this.bound = true;
AsyncObservable.prototype.activate = function() {
canReflect.onValue(this.observation, this.handler, "notify");
if (!this.resolveCalled) {
this.value = peek(this.observation);
Expand Down
30 changes: 29 additions & 1 deletion resolver/resolver-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var SimpleObservable = require('../can-simple-observable');
var mapEventMixin = require("can-event-queue/map/map");
var canSymbol = require("can-symbol");
var canReflect = require("can-reflect");
var queues = require("can-queues");
var Observation = require("can-observation");

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

Expand Down Expand Up @@ -221,3 +221,31 @@ QUnit.test("getWhatIChange", function(assert) {
canReflect.onValue(obs, function() {});
assert.ok(canReflect.getWhatIChange(dep).derive.valueDependencies.has(obs));
});

QUnit.test("proactive binding doesn't last past binding (can-stache#486)", function(){
var v = new SimpleObservable(2);

var readCount = 0;

var obs = new ResolverObservable(function(value) {
value.listenTo(v, function(newVal) {
readCount++;
value.resolve(newVal);
});
});

var outer = new Observation(function(){
return obs.get();
});

function handler(){}

outer.on(handler);

outer.off(handler);

v.set(3);

QUnit.equal(readCount, 0, "internal observation only updated once");

});
5 changes: 2 additions & 3 deletions resolver/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,7 @@ canReflect.assignMap(ResolverObservable.prototype, {
);
}
},
onBound: function() {
this.bound = true;
activate: function() {
this.isBinding = true;
this.teardown = this.resolver.call(this.context, this.valueOptions);
this.isBinding = false;
Expand All @@ -170,7 +169,7 @@ canReflect.assignMap(ResolverObservable.prototype, {
if (ObservationRecorder.isRecording()) {
ObservationRecorder.add(this);
if (!this.bound) {
Observation.temporarilyBind(this);
this.onBound();
}
}

Expand Down
29 changes: 29 additions & 0 deletions settable/settable-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ var QUnit = require('steal-qunit');
var SettableObservable = require('./settable');
var SimpleObservable = require('../can-simple-observable');
var canReflect = require('can-reflect');
var Observation = require("can-observation");


var ObservationRecorder = require("can-observation-recorder");

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

Expand Down Expand Up @@ -152,3 +156,28 @@ QUnit.test("setting an observable to Settable observable works", function(assert
"should replace the internal observable with 'two'"
);
});

QUnit.test("proactive binding doesn't last past binding (can-stache#486)", function(){
var value = new SimpleObservable(2);

var readCount = 0;
var obs = new SettableObservable(function(lastSet){
readCount++;
return lastSet * value.get();
}, null, 1);

var outer = new Observation(function(){
return obs.get();
});

function handler(){}

outer.on(handler);

outer.off(handler);

value.set(3);

QUnit.equal(readCount, 1, "internal observation only updated once");

});
12 changes: 10 additions & 2 deletions settable/settable.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,14 @@ Object.assign(SettableObservable.prototype, {
);
},
onBound: function() {
this.bound = true;
// onBound can be called by `.get` and then later called through
// a keyTree binding.
if(!this.bound) {
this.bound = true;
this.activate();
}
},
activate: function(){
canReflect.onValue(this.observation, this.handler, "notify");
this.value = peek(this.observation);
},
Expand All @@ -100,7 +107,8 @@ Object.assign(SettableObservable.prototype, {
if (ObservationRecorder.isRecording()) {
ObservationRecorder.add(this);
if (!this.bound) {
Observation.temporarilyBind(this);
// proactively setup bindings
this.onBound();
}
}

Expand Down

0 comments on commit 6b96198

Please sign in to comment.