seal the state

This commit is contained in:
Michael Rawlings 2017-02-01 16:11:35 -08:00
parent 2737509765
commit 0de07b5c8d
3 changed files with 26 additions and 6 deletions

View File

@ -0,0 +1,9 @@
class {
onInput(input) {
this.state = {}
}
}
<div>
Hello ${state.name || ''}!
</div>

View File

@ -0,0 +1,11 @@
'use strict';
var expect = require('chai').expect;
module.exports = function(helpers) {
var widget = helpers.mount(require('./index'), {});
expect(() => {
widget.state.foo = 'bar';
}).to.throw(TypeError, 'Can\'t add property foo');
};

View File

@ -2,7 +2,7 @@ var extend = require('raptor-util/extend');
function ensure(state, propertyName) {
var proto = state.constructor.prototype;
if (!proto.hasOwnProperty(propertyName)) {
if (!(propertyName in proto)) {
Object.defineProperty(proto, propertyName, {
get: function() {
return this.$__raw[propertyName];
@ -25,11 +25,13 @@ function State(widget, initialState) {
if (initialState) {
for(var key in initialState) {
if (initialState.hasOwnProperty(key)) {
if (initialState[HAS_OWN_PROP](key)) {
ensure(this, key);
}
}
}
Object.seal(this);
}
State.prototype = {
@ -49,15 +51,13 @@ State.prototype = {
var rawState = this.$__raw;
for (key in rawState) {
if (rawState.hasOwnProperty(key) && !newState.hasOwnProperty(key)) {
if (!(key in newState)) {
state.$__set(key, undefined, false /* ensure:false */, false /* forceDirty:false */);
}
}
for (key in newState) {
if (newState.hasOwnProperty(key)) {
state.$__set(key, newState[key], true /* ensure:true */, false /* forceDirty:false */);
}
state.$__set(key, newState[key], true /* ensure:true */, false /* forceDirty:false */);
}
},
$__set: function(name, value, shouldEnsure, forceDirty) {