Skip to content

Commit

Permalink
Merge pull request #288 from Robert-Frampton/issue_287
Browse files Browse the repository at this point in the history
Ensure that STATE configuration from static hints are properly configured with multiple levels of class inheritance
  • Loading branch information
jbalsas authored Nov 1, 2017
2 parents cbb22f4 + 37c8a21 commit d83b7a6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
23 changes: 19 additions & 4 deletions packages/metal-state/src/State.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,17 +271,20 @@ class State extends EventEmitter {

/**
* Adds state keys from super classes static hint `MyClass.STATE = {};`.
* @param {Object.<string, !Object>=} opt_config An object that maps all the
* configurations for state keys.
* @protected
*/
configStateFromStaticHint_() {
const ctor = this.constructor;
if (ctor !== State) {
let defineContext;
if (this.obj_ === this) {
defineContext = ctor.hasConfiguredState_ ? false : ctor.prototype;
ctor.hasConfiguredState_ = true;
const staticKey = State.STATE_STATIC_HINT_CONFIGURED;

ctor[staticKey] = ctor[staticKey] || {};

defineContext = ctor[staticKey][ctor.name] ? false :
ctor.prototype;
ctor[staticKey][ctor.name] = true;
}
this.configState(State.getStateStatic(ctor), defineContext);
}
Expand Down Expand Up @@ -676,8 +679,20 @@ class State extends EventEmitter {
}
}

/**
* Constant used as key on State instance for storing property definition.
* @type {!string}
*/
State.STATE_REF_KEY = '__METAL_STATE_REF_KEY__';

/**
* Constant used as key on class constructors that extend from State, stores
* which constructors have had their static STATE configured so that
* configuration of STATE is not repeated.
* @type {!string}
*/
State.STATE_STATIC_HINT_CONFIGURED = '__METAL_STATE_STATIC_HINT_CONFIGURED__';

/**
* Constants that represent the states that a state key can be in.
* @type {!Object}
Expand Down
26 changes: 26 additions & 0 deletions packages/metal-state/test/State.js
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,32 @@ describe('State', function() {
assert.strictEqual('foo2', test2.key1);
assert.strictEqual('foo1', test1.key1);
});

it('should configure static STATE with multiple levels of class inheritance', function() {
var Test = createTestClass();
Test.STATE = {
key1: {
}
};

class Child extends Test {
}
Child.STATE = {
key2: {
}
};

var test = new Test({
key1: 'foo1'
});
assert.strictEqual('foo1', test.key1);

var child = new Child({
key2: 'foo2'
});
assert.strictEqual('foo1', test.key1);
assert.strictEqual('foo2', child.key2);
});
});

describe('Separate object', function() {
Expand Down

0 comments on commit d83b7a6

Please sign in to comment.