Skip to content

Commit e849849

Browse files
authoredMay 9, 2020
fix(define): rewire own props values after upgrade (#115)
1 parent da058c4 commit e849849

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed
 

Diff for: ‎src/define.js

+28-5
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,19 @@ try { process.env.NODE_ENV } catch(e) { var process = { env: { NODE_ENV: 'produc
99

1010
const defaultMethod = (host, value) => value;
1111

12+
const callbacksMap = new WeakMap();
13+
const propsMap = new WeakMap();
14+
1215
function compile(Hybrid, descriptors) {
1316
Hybrid.hybrids = descriptors;
14-
Hybrid.callbacks = [];
1517

16-
Object.keys(descriptors).forEach(key => {
18+
const callbacks = [];
19+
const props = Object.keys(descriptors);
20+
21+
callbacksMap.set(Hybrid, callbacks);
22+
propsMap.set(Hybrid, props);
23+
24+
props.forEach(key => {
1725
const desc = descriptors[key];
1826
const type = typeof desc;
1927

@@ -46,13 +54,13 @@ function compile(Hybrid, descriptors) {
4654
});
4755

4856
if (config.observe) {
49-
Hybrid.callbacks.unshift(host =>
57+
callbacks.unshift(host =>
5058
cache.observe(host, key, config.get, config.observe),
5159
);
5260
}
5361

5462
if (config.connect) {
55-
Hybrid.callbacks.push(host =>
63+
callbacks.push(host =>
5664
config.connect(host, key, () => {
5765
cache.invalidate(host, key);
5866
}),
@@ -143,8 +151,23 @@ function defineElement(tagName, hybridsOrConstructor) {
143151
return tagName;
144152
}
145153

154+
constructor() {
155+
super();
156+
157+
const props = propsMap.get(Hybrid);
158+
159+
for (let index = 0; index < props.length; index += 1) {
160+
const key = props[index];
161+
if (Object.prototype.hasOwnProperty.call(this, key)) {
162+
const value = this[key];
163+
delete this[key];
164+
this[key] = value;
165+
}
166+
}
167+
}
168+
146169
connectedCallback() {
147-
const { callbacks } = this.constructor;
170+
const callbacks = callbacksMap.get(Hybrid);
148171
const list = [];
149172

150173
for (let index = 0; index < callbacks.length; index += 1) {

Diff for: ‎test/spec/property.js

+11
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,17 @@ describe("property:", () => {
7373
el.stringProp = "new value";
7474
expect(el.stringProp).toBe("new value");
7575
}));
76+
77+
it(
78+
"uses property from the host, before it is upgraded",
79+
test(`<test-property-upgrade></test-property-upgrade>`)(el => {
80+
el.value = "test";
81+
define("test-property-upgrade", { value: property("") });
82+
expect(el.value).toBe("test");
83+
el.value = 5;
84+
expect(el.value).toBe("5");
85+
}),
86+
);
7687
});
7788

7889
describe("string type", () => {

0 commit comments

Comments
 (0)
Please sign in to comment.