|
| 1 | +import { BaseScheduler } from './scheduler'; |
| 2 | +const toCamelCase = (val = '') => val.replace(/-+([a-z])?/g, (_, char) => char ? char.toUpperCase() : ''); |
| 3 | +function makeComponent(render) { |
| 4 | + class Scheduler extends BaseScheduler { |
| 5 | + frag; |
| 6 | + constructor(renderer, frag, host) { |
| 7 | + super(renderer, (host || frag)); |
| 8 | + this.frag = frag; |
| 9 | + } |
| 10 | + commit(result) { |
| 11 | + render(result, this.frag); |
| 12 | + } |
| 13 | + } |
| 14 | + function component(renderer, baseElementOrOptions, options) { |
| 15 | + const BaseElement = (options || baseElementOrOptions || {}).baseElement || HTMLElement; |
| 16 | + const { observedAttributes = [], useShadowDOM = true, shadowRootInit = {} } = options || baseElementOrOptions || {}; |
| 17 | + class Element extends BaseElement { |
| 18 | + _scheduler; |
| 19 | + static get observedAttributes() { |
| 20 | + return renderer.observedAttributes || observedAttributes || []; |
| 21 | + } |
| 22 | + constructor() { |
| 23 | + super(); |
| 24 | + if (useShadowDOM === false) { |
| 25 | + this._scheduler = new Scheduler(renderer, this); |
| 26 | + } |
| 27 | + else { |
| 28 | + this.attachShadow({ mode: 'open', ...shadowRootInit }); |
| 29 | + this._scheduler = new Scheduler(renderer, this.shadowRoot, this); |
| 30 | + } |
| 31 | + } |
| 32 | + connectedCallback() { |
| 33 | + this._scheduler.update(); |
| 34 | + } |
| 35 | + disconnectedCallback() { |
| 36 | + this._scheduler.teardown(); |
| 37 | + } |
| 38 | + attributeChangedCallback(name, oldValue, newValue) { |
| 39 | + if (oldValue === newValue) { |
| 40 | + return; |
| 41 | + } |
| 42 | + let val = newValue === '' ? true : newValue; |
| 43 | + Reflect.set(this, toCamelCase(name), val); |
| 44 | + } |
| 45 | + } |
| 46 | + ; |
| 47 | + function reflectiveProp(initialValue) { |
| 48 | + let value = initialValue; |
| 49 | + let isSetup = false; |
| 50 | + return Object.freeze({ |
| 51 | + enumerable: true, |
| 52 | + configurable: true, |
| 53 | + get() { |
| 54 | + return value; |
| 55 | + }, |
| 56 | + set(newValue) { |
| 57 | + // Avoid scheduling update when prop value hasn't changed |
| 58 | + if (isSetup && value === newValue) |
| 59 | + return; |
| 60 | + isSetup = true; |
| 61 | + value = newValue; |
| 62 | + if (this._scheduler) { |
| 63 | + this._scheduler.update(); |
| 64 | + } |
| 65 | + } |
| 66 | + }); |
| 67 | + } |
| 68 | + const proto = new Proxy(BaseElement.prototype, { |
| 69 | + getPrototypeOf(target) { |
| 70 | + return target; |
| 71 | + }, |
| 72 | + set(target, key, value, receiver) { |
| 73 | + let desc; |
| 74 | + if (key in target) { |
| 75 | + desc = Object.getOwnPropertyDescriptor(target, key); |
| 76 | + if (desc && desc.set) { |
| 77 | + desc.set.call(receiver, value); |
| 78 | + return true; |
| 79 | + } |
| 80 | + Reflect.set(target, key, value, receiver); |
| 81 | + return true; |
| 82 | + } |
| 83 | + if (typeof key === 'symbol' || key[0] === '_') { |
| 84 | + desc = { |
| 85 | + enumerable: true, |
| 86 | + configurable: true, |
| 87 | + writable: true, |
| 88 | + value |
| 89 | + }; |
| 90 | + } |
| 91 | + else { |
| 92 | + desc = reflectiveProp(value); |
| 93 | + } |
| 94 | + Object.defineProperty(receiver, key, desc); |
| 95 | + if (desc.set) { |
| 96 | + desc.set.call(receiver, value); |
| 97 | + } |
| 98 | + return true; |
| 99 | + } |
| 100 | + }); |
| 101 | + Object.setPrototypeOf(Element.prototype, proto); |
| 102 | + return Element; |
| 103 | + } |
| 104 | + return component; |
| 105 | +} |
| 106 | +export { makeComponent }; |
0 commit comments