Skip to content

Commit

Permalink
merge lazy eval stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
justinbmeyer committed Nov 28, 2018
2 parents 5ba0a2d + bdb1676 commit 724b6a4
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 7 deletions.
3 changes: 2 additions & 1 deletion can-view-scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,8 @@ assign(Scope.prototype, {
return res && res._context;
},
// ### scope.getTemplateContext
// Returns the template context
// Returns the template context scope
// This function isn't named right.
getTemplateContext: function() {
var lastScope;

Expand Down
81 changes: 78 additions & 3 deletions scope-key-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var canReflectDeps = require('can-reflect-dependencies');
var valueEventBindings = require("can-event-queue/value/value");
var stacheHelpers = require('can-stache-helpers');
var SimpleObservable = require("can-simple-observable");
var dev = require("can-log/dev/dev");

var dispatchSymbol = canSymbol.for("can.dispatch");

Expand Down Expand Up @@ -67,6 +68,56 @@ function callMutateWithRightArgs(method, mutated, reads, mutator){
}
}




var warnOnUndefinedProperty;
//!steal-remove-start
if (process.env.NODE_ENV !== 'production') {
warnOnUndefinedProperty = function(options) {
if ( options.key !== "debugger" && !options.parentHasKey) {
var filename = options.scope.peek('scope.filename');
var lineNumber = options.scope.peek('scope.lineNumber');

var reads = observeReader.reads(options.key);
var firstKey = reads[0].key;
var key = reads.map(function(read) {
return read.key + (read.at ? "()" : "");
}).join(".");
var pathsForKey = options.scope.getPathsForKey(firstKey);
var paths = Object.keys( pathsForKey );

var includeSuggestions = paths.length && (paths.indexOf(firstKey) < 0);

var warning = [
(filename ? filename + ':' : '') +
(lineNumber ? lineNumber + ': ' : '') +
'Unable to find key "' + key + '".' +
(
includeSuggestions ?
" Did you mean" + (paths.length > 1 ? " one of these" : "") + "?\n" :
"\n"
)
];

if (includeSuggestions) {
paths.forEach(function(path) {
warning.push('\t"' + path + '" which will read from');
warning.push(pathsForKey[path]);
warning.push("\n");
});
}

warning.push("\n");

dev.warn.apply(dev,
warning
);
}
};
}
//!steal-remove-end

// could we make this an observation first ... and have a getter for the compute?

// This is a fast-path enabled Observation wrapper use many places in can-stache.
Expand Down Expand Up @@ -112,7 +163,6 @@ var ScopeKeyData = function(scope, key, options){
// things added later
this.fastPath = undefined;
this.root = undefined;
this.initialValue = undefined;
this.reads = undefined;
this.setRoot = undefined;
// This is read by call expressions so it needs to be observable
Expand Down Expand Up @@ -277,13 +327,25 @@ assign(ScopeKeyData.prototype, {
valueDependencies: rootValueDeps
});
}
if(data.value === undefined && this.options.warnOnMissingKey === true) {
warnOnUndefinedProperty({
scope: this.startingScope,
key: this.key,
parentHasKey: data.parentHasKey
});
}
}
//!steal-remove-end


return this.initialValue = data.value;
return data.value;
},
hasDependencies: function(){
// ScopeKeyData is unique in that when these things are read, it will temporarily bind
// to make sure the right value is returned. This is for can-stache.
// Helpers warns about a missing helper.
if (!this.bound) {
Observation.temporarilyBind(this);
}
return canReflect.valueHasDependencies( this.observation );
}
});
Expand Down Expand Up @@ -336,4 +398,17 @@ Object.defineProperty(ScopeKeyData.prototype, "compute", {
configurable: true
});

Object.defineProperty(ScopeKeyData.prototype, "initialValue", {
get: function(){
if (!this.bound) {
Observation.temporarilyBind(this);
}
return this.get();
},
set: function(){
throw new Error("initialValue should not be set");
},
configurable: true
});

module.exports = ScopeKeyData;
6 changes: 3 additions & 3 deletions test/scope-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -845,9 +845,9 @@ QUnit.test("ScopeKeyData can.valueHasDependencies", function(){
var base = new Scope(map);
var age = base.computeData('age');


QUnit.equal(canReflect.valueHasDependencies(age), undefined, "undefined");
canReflect.onValue(age, function(){});
// The following 2 lines were commented out ... we auto-bind ScopeKeyData
//QUnit.equal(canReflect.valueHasDependencies(age), undefined, "undefined");
//canReflect.onValue(age, function(){});

QUnit.equal(canReflect.valueHasDependencies(age), true, "undefined");
});
Expand Down

0 comments on commit 724b6a4

Please sign in to comment.