Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(define): rewire own props values after upgrade #115

Merged
merged 1 commit into from May 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 28 additions & 5 deletions src/define.js
Expand Up @@ -9,11 +9,19 @@ try { process.env.NODE_ENV } catch(e) { var process = { env: { NODE_ENV: 'produc

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

const callbacksMap = new WeakMap();
const propsMap = new WeakMap();

function compile(Hybrid, descriptors) {
Hybrid.hybrids = descriptors;
Hybrid.callbacks = [];

Object.keys(descriptors).forEach(key => {
const callbacks = [];
const props = Object.keys(descriptors);

callbacksMap.set(Hybrid, callbacks);
propsMap.set(Hybrid, props);

props.forEach(key => {
const desc = descriptors[key];
const type = typeof desc;

Expand Down Expand Up @@ -46,13 +54,13 @@ function compile(Hybrid, descriptors) {
});

if (config.observe) {
Hybrid.callbacks.unshift(host =>
callbacks.unshift(host =>
cache.observe(host, key, config.get, config.observe),
);
}

if (config.connect) {
Hybrid.callbacks.push(host =>
callbacks.push(host =>
config.connect(host, key, () => {
cache.invalidate(host, key);
}),
Expand Down Expand Up @@ -143,8 +151,23 @@ function defineElement(tagName, hybridsOrConstructor) {
return tagName;
}

constructor() {
super();

const props = propsMap.get(Hybrid);

for (let index = 0; index < props.length; index += 1) {
const key = props[index];
if (Object.prototype.hasOwnProperty.call(this, key)) {
const value = this[key];
delete this[key];
this[key] = value;
}
}
}

connectedCallback() {
const { callbacks } = this.constructor;
const callbacks = callbacksMap.get(Hybrid);
const list = [];

for (let index = 0; index < callbacks.length; index += 1) {
Expand Down
11 changes: 11 additions & 0 deletions test/spec/property.js
Expand Up @@ -73,6 +73,17 @@ describe("property:", () => {
el.stringProp = "new value";
expect(el.stringProp).toBe("new value");
}));

it(
"uses property from the host, before it is upgraded",
test(`<test-property-upgrade></test-property-upgrade>`)(el => {
el.value = "test";
define("test-property-upgrade", { value: property("") });
expect(el.value).toBe("test");
el.value = 5;
expect(el.value).toBe("5");
}),
);
});

describe("string type", () => {
Expand Down