-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
84 lines (73 loc) · 2.43 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import {html, LitElement} from '@polymer/lit-element'
const isSpecDecorator = (args) => {
return args.length === 1 && typeof args[0].kind === 'string'
}
// alternative way of creating the property
function createSpecElementDescriptor({kind, key, placement, descriptor, initializer}, options) {
const valueKey = typeof key === 'symbol' ? Symbol() : `__${key}`;
let underlyingDescriptor = { enumerable: false, configurable: false, writable: true };
let underlying = { kind, key: valueKey, placement, descriptor: underlyingDescriptor, initializer };
return {
kind: "method",
key,
placement,
descriptor: {
get() { return this[valueKey]; },
set(value) {
const oldValue = this[valueKey];
this[valueKey] = value;
this._requestPropertyUpdate(name, oldValue, options);
},
enumerable: descriptor.enumerable,
configurable: descriptor.configurable
},
extras: [underlying]
};
}
const property = (options) => (...args) => {
if (isSpecDecorator(args)) {
// code extracted from https://github.com/Polymer/lit-element/issues/205#issuecomment-427719522
const elementDescriptor = args[0]
const name = elementDescriptor.key
// key generation code copied from https://github.com/Polymer/lit-element/blob/master/src/lib/updating-element.ts
const key = typeof name === 'symbol' ? Symbol() : `__${name}`
return {
// We are creating an own property and using the original initializer, but changing the key,
// so foo becomes __foo. The getter and setter methods are created by createProperty().
...elementDescriptor,
key,
finisher(ctor) {
ctor.createProperty(name, options)
}
}
} else {
const proto = args[0]
const name = args[1]
proto.constructor.createProperty(name, options);
}
};
class MyApp extends LitElement {
static properties = {
count: {type: Number}
}
count = 0
increment = () => {
this.count++
}
render() {
return html`<h3>Decorator test</h3>
<button @click=${this.increment}>Increment</button>
<div>App value: ${this.count}</div>
<my-counter .value=${this.count}></my-counter>
`
}
}
class MyCounter extends LitElement {
@property({type: Number})
value = 0
render() {
return html`<div>Component value: ${this.value}</div>`
}
}
customElements.define('my-app', MyApp)
customElements.define('my-counter', MyCounter)