Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Merge pull request #109 from canjs/is-proactively-cacheable" #110

Merged
merged 1 commit into from
Aug 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions can-observation.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ function Observation(func, context, compute){
this.context = context;
this.compute = compute && (compute.updater || ("isObservable" in compute)) ? compute : {updater: compute};
this.isObservable = typeof compute === "object" ? compute.isObservable : true;
this.isProactivelyCacheable = typeof compute === "object" && "isProactivelyCacheable" in compute ? compute.isProactivelyCacheable : this.isObservable;
var observation = this;
this.onDependencyChange = function(value, legacyValue){
observation.dependencyChange(this, value, legacyValue);
Expand Down Expand Up @@ -148,28 +147,27 @@ var remaining = {updates: 0, notifications: 0};
// expose the remaining state
Observation.remaining = remaining;

assign(Observation.prototype, {
assign(Observation.prototype,{
// something is reading the value of this compute
get: function(){

var isRecording = Observation.isRecording();

// If an external observation is tracking observables and
// this compute can be listened to by "function" based computes ....
// This doesn't happen with observations within computes
if (this.isObservable && isRecording) {
if( this.isObservable && Observation.isRecording() ) {


// ... tell the tracking compute to listen to change on this observation.
Observation.add(this);
}

if (this.isProactivelyCacheable && isRecording) {
// ... if we are not bound, we should bind so that
// we don't have to re-read to get the value of this compute.
if (!this.bound) {
Observation.temporarilyBind(this);
}

}


if(this.bound === true) {
// Flush events so this compute should have been notified.
// But we want not only update
Expand Down
102 changes: 6 additions & 96 deletions can-observation_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ var clone = require("steal-clone");
var canReflect = require("can-reflect");
var canSymbol = require("can-symbol");

var onValueSymbol = canSymbol.for("can.onValue");

QUnit.module('can-observation',{
setup: function(){
eventAsync.sync();
}
});



QUnit.test('nested traps are reset onto parent traps', function() {
var obs1 = assign({}, canEvent);
CID(obs1);
Expand Down Expand Up @@ -551,119 +551,29 @@ QUnit.test("Observation can itself be observable", function(){
var v1 = reflectiveObservable(1);
var v2 = reflectiveObservable(2);

var oA = new Observation(function oA(){
var oA = new Observation(function(){
return v1.get() + v2.get();
});
var oB = new Observation(function oB(){
var oB = new Observation(function(){
return oA.get() * 3;
});

canReflect.onValue(oB, function(){
QUnit.ok(true, "this was fired in a batch");
});

canBatch.start();
v1.set(10);
v2.set(20);
canBatch.stop();

QUnit.equal( canReflect.getValue(oA), 30, 'oA updated');
QUnit.equal( canReflect.getValue(oB), 90, 'oB updated');
QUnit.ok(oA.bound, "bound on oA");
});

QUnit.test("Observation can be not observable and be proactively cacheable", function(){
var v1 = reflectiveObservable(1);
var v2 = reflectiveObservable(2);

var oA = new Observation(function oA(){
return v1.get() + v2.get();
}, null, {
isObservable: false,
isProactivelyCacheable: true
});
oA[onValueSymbol] = function() {
QUnit.ok(true, 'oA onValue should be called');
};

var origStack = Observation.observationStack;
Observation.observationStack.push({ ignore: 0, newObserved: {} });
QUnit.equal( canReflect.getValue(oA), 3);
Observation.observationStack = origStack;
});

QUnit.test("Observation can be not observable and not proactively cacheable", function(){
var v1 = reflectiveObservable(1);
var v2 = reflectiveObservable(2);

var oA = new Observation(function oA(){
return v1.get() + v2.get();
}, null, {
isObservable: false,
isProactivelyCacheable: false
});
oA[onValueSymbol] = function() {
QUnit.ok(false, 'oA onValue should not be called');
};

var origStack = Observation.observationStack;
Observation.observationStack.push({ ignore: 0, newObserved: {} });
QUnit.equal( canReflect.getValue(oA), 3);
Observation.observationStack = origStack;
});

QUnit.test("Observation can be observable and not proactively cacheable", function(){
QUnit.expect(5);
var v1 = reflectiveObservable(1);
var v2 = reflectiveObservable(2);

var oA = new Observation(function oA(){
return v1.get() + v2.get();
}, null, {
isObservable: true,
isProactivelyCacheable: false
});
var origOnValue = oA[onValueSymbol];
oA[onValueSymbol] = function() {
QUnit.ok(true, 'oA onValue should be called once');
origOnValue.apply(this, arguments);
};

var oB = new Observation(function oB(){
return oA.get() * 3;
});

canReflect.onValue(oB, function(){
QUnit.ok(true, "this was fired in a batch");
});
QUnit.equal( canReflect.getValue(oB), 9);

canBatch.start();
v1.set(10);
v2.set(20);
canBatch.stop();

QUnit.equal( canReflect.getValue(oA), 30, 'oA updated');
QUnit.equal( canReflect.getValue(oB), 90, 'oB updated');
QUnit.equal( canReflect.getValue(oB), 90);
QUnit.ok(oA.bound, "bound on oA");
});

QUnit.test("compute.isProactivelyCacheable", function() {
var obs = new Observation(function() {});
QUnit.ok(obs.isProactivelyCacheable, 'should be true if compute is null (for backwards compatibility)');

obs = new Observation(function() {}, null, { isObservable: true });
QUnit.ok(obs.isProactivelyCacheable, 'should be true if isObservable is set to true (for backwards compatibility)');

obs = new Observation(function() {}, null, { isObservable: false });
QUnit.ok(!obs.isProactivelyCacheable, 'should be false if isObservable is set to false (for backwards compatibility)');

obs = new Observation(function() {}, null, { isObservable: false, isProactivelyCacheable: true });
QUnit.ok(obs.isProactivelyCacheable, 'should be true if isObservable is set to false and isProactivelyCacheable is explicitly set to true');

obs = new Observation(function() {}, null, { isObservable: false, isProactivelyCacheable: false });
QUnit.ok(!obs.isProactivelyCacheable, 'should be false if isObservable is set to false and isProactivelyCacheable is explicitly set to false');
});

QUnit.test("should be able to bind, unbind, and re-bind to an observation", function() {
var observation = new Observation(function() {
return "Hello";
Expand Down