UNPKG

@spectrum-web-components/base

Version:

The `SpectrumElement` base class as created by mixing `SpectrumMixin` onto `LitElement` adopts `dir` values from the `document` at connection time with a fallback to `lrt`. In a TypeScript context, it also enforces the presence of `this.shadowRoot` on ext

3 lines (2 loc) 2.18 kB
"use strict";import{nothing as a}from"lit";import{AsyncDirective as o,directive as h}from"lit/async-directive.js";const i=["",()=>{}];class m extends o{constructor(){super(...arguments);this.start=i;this.streamInside=i;this.end=i;this.streamOutside=i;this.state="off";this.handleStart=e=>{this.clearStream(),this.callHandler(this.start[1],e),!e.defaultPrevented&&(this.removeListeners(),this.addListeners("on"))};this.handleInside=e=>{this.handleStream(this.streamInside[1],e)};this.handleEnd=e=>{this.clearStream(),this.callHandler(this.end[1],e),this.removeListeners(),this.addListeners("off")};this.handleOutside=e=>{this.handleStream(this.streamOutside[1],e)}}render(e){return a}update(e,[{start:t,end:s,streamInside:r=i,streamOutside:d=i}]){var n;this.element!==e.element&&(this.element=e.element,this.removeListeners()),this.host=((n=e.options)==null?void 0:n.host)||this.element,this.start=t,this.end=s,this.streamInside=r,this.streamOutside=d,this.addListeners()}addListeners(e){this.state=e||this.state,this.state==="off"?(this.addListener(this.streamOutside[0],this.handleOutside),this.addListener(this.start[0],this.handleStart)):this.state==="on"&&(this.addListener(this.streamInside[0],this.handleInside),this.addListener(this.end[0],this.handleEnd))}callHandler(e,t){typeof e=="function"?e.call(this.host,t):e.handleEvent(t)}handleStream(e,t){this.stream||(this.callHandler(e,t),this.stream=requestAnimationFrame(()=>{this.stream=void 0}))}clearStream(){this.stream!=null&&(cancelAnimationFrame(this.stream),this.stream=void 0)}addListener(e,t){Array.isArray(e)?e.map(s=>{this.element.addEventListener(s,t)}):this.element.addEventListener(e,t)}removeListener(e,t){Array.isArray(e)?e.map(s=>{this.element.removeEventListener(s,t)}):this.element.removeEventListener(e,t)}removeListeners(){this.removeListener(this.start[0],this.handleStart),this.removeListener(this.streamInside[0],this.handleInside),this.removeListener(this.end[0],this.handleEnd),this.removeListener(this.streamOutside[0],this.handleOutside)}disconnected(){this.removeListeners()}reconnected(){this.addListeners()}}export const streamingListener=h(m); //# sourceMappingURL=streaming-listener.js.map