Getters, Setters and Lazy Values?

So I've encountered a few situations in which I've needed capabilities that Enyo currently lacks, specifically getters and setters.

For example, say I have a type for which a value's creation is costly, and I want to do it lazily; I've ended up modelling getters for this purpose. Same with handling values that need to be modified before storing them (i.e- prior to a fooChanged() invocation).

So I've ended up with a type that looks a bit like this contrived example:
var kind = require('enyo/kind');
var Foo = module.exports = kind({
name: 'Foo',

published: {
clampedValue: 0,
lazyValue: undefined,
storedAsHex: '',
},

protectedStatics: {
/** Functions for transforming values on retrieval */
getters: {
storedAsHex: function(theValue) {
return parseInt(theValue, 16);
}
},

/** Functions for transforming values before storing */
setters: {
clampedValue: function(theValue) {
if (theValue < 0) { theValue = 0; }
else if (theValue > 10) { theValue = 10; }
return theValue;
},
storedAsHex: function(theValue) {
return Int(theValue).toString(16);
},
},

/** Functions for lazily initialising values */
lazy: {
lazyValue: function() {
var theValue = [];
for (var i = 0; i < 10000; i += 1) { theValue.push(i); }
return theValue;
},
},
},

get: kind.inherit(function(parent) { return function(theKey) {
var theValue = parent.call(this, theKey), theFunction;
if (theFunction = Foo.lazy[theKey]) {
if (theValue === undefined) {
theValue = theFunction.call(this);
this.set(theKey, theValue);
}
}
if (theFunction = Foo.getters[theKey]) {
theValue = theFunction.call(this, theValue);
}
return theValue;
}}),

set: kind.inherit(function(parent) { return function(theKey, theNewValue) {
var theFunction = Foo.setters[theKey];
if (theFunction) { theNewValue = theFunction.call(this, theNewValue); }
parent.call(this, theKey, theNewValue);
}}),
});
A bit rough but it hopefully gives the idea. Now, first of all I'm wondering if there might be a better way to do this? Is it possible to move capabilities like these into a mixin (bearing in mind that I've never written one for Enyo)?

However, what I really want to discuss is, are there any reasons why Enyo doesn't already have capabilities like these? We only have fooChanged(), which is a bit limited.

I realise that these extra lookups obviously cost performance, but actually it seems like the kind() method could ensure this is only the case on kinds that actually use the feature(s), so in normal cases there would be no added overhead.

With the trend in Enyo 2.7.0 towards using .get() and .set() it seems like these are capabilities we could have now. Is anything planned? Would it be worth my developing it?
Sign In or Register to comment.