|
75 | 75 | location.reload();
|
76 | 76 | }
|
77 | 77 |
|
| 78 | + function bindToElement(instance) { |
| 79 | + instance.target = instance.getRootNode().querySelector(instance.targetSelector) || document.querySelector(instance.targetSelector); |
| 80 | + } |
| 81 | + |
| 82 | + function subscribeToEvents(instance) { |
| 83 | + unsubscribeToEvents(instance); |
| 84 | + instance.target.addEventListener('generic-error', instance.handleGenericError); |
| 85 | + instance.target.addEventListener('connection-error', instance.handleConnectionError); |
| 86 | + instance.target.addEventListener('reconnection-countdown', instance.handleReconnectionCountdownTick); |
| 87 | + instance.target.addEventListener('reconnection-end', instance.handleReconnectionEnd); |
| 88 | + instance.cancelReloadingBtn.addEventListener('click', instance.cancelReloading); |
| 89 | + instance.reloadNowBtn.addEventListener('click', reload); |
| 90 | + instance.genericErrorPane.addEventListener('click', reload); |
| 91 | + instance.subscribed = true; |
| 92 | + } |
| 93 | + |
| 94 | + function unsubscribeToEvents(instance) { |
| 95 | + if (instance.subscribed) { |
| 96 | + instance.target.removeEventListener('generic-error', instance.handleGenericError); |
| 97 | + instance.target.removeEventListener('connection-error', instance.handleConnectionError); |
| 98 | + instance.target.removeEventListener('reconnection-countdown', instance.handleReconnectionCountdownTick); |
| 99 | + instance.target.removeEventListener('reconnection-end', instance.handleReconnectionEnd); |
| 100 | + instance.genericErrorPane.removeEventListener('click', reload); |
| 101 | + instance.cancelReloadingBtn.removeEventListener('click', instance.cancelReloading); |
| 102 | + instance.reloadNowBtn.removeEventListener('click', reload); |
| 103 | + instance.subscribed = false; |
| 104 | + } |
| 105 | + } |
| 106 | + |
78 | 107 | class PalindromErrorCatcher extends HTMLElement {
|
79 | 108 | static get observedAttributes() {
|
80 | 109 | return ['target-selector']
|
81 | 110 | }
|
| 111 | + |
82 | 112 | constructor() {
|
83 | 113 | super();
|
84 | 114 |
|
|
95 | 125 |
|
96 | 126 | bindCallbacks(this);
|
97 | 127 | }
|
98 |
| - _bindToElement() { |
99 |
| - this.target = this.getRootNode().querySelector(this.targetSelector) || document.querySelector(this.targetSelector); |
100 |
| - } |
101 | 128 |
|
102 |
| - _subscribeToEvents() { |
103 |
| - this._unsubscribeToEvents(); |
104 |
| - this.target.addEventListener('generic-error', this.handleGenericError); |
105 |
| - this.target.addEventListener('connection-error', this.handleConnectionError); |
106 |
| - this.target.addEventListener('reconnection-countdown', this.handleReconnectionCountdownTick); |
107 |
| - this.target.addEventListener('reconnection-end', this.handleReconnectionEnd); |
108 |
| - this.cancelReloadingBtn.addEventListener('click', this.cancelReloading); |
109 |
| - this.reloadNowBtn.addEventListener('click', reload); |
110 |
| - this.genericErrorPane.addEventListener('click', reload); |
111 |
| - this.subscribed = true; |
112 |
| - } |
113 |
| - _unsubscribeToEvents() { |
114 |
| - if (this.subscribed) { |
115 |
| - this.target.removeEventListener('generic-error', this.handleGenericError); |
116 |
| - this.target.removeEventListener('connection-error', this.handleConnectionError); |
117 |
| - this.target.removeEventListener('reconnection-countdown', this.handleReconnectionCountdownTick); |
118 |
| - this.target.removeEventListener('reconnection-end', this.handleReconnectionEnd); |
119 |
| - this.genericErrorPane.removeEventListener('click', reload); |
120 |
| - this.cancelReloadingBtn.removeEventListener('click', this.cancelReloading); |
121 |
| - this.reloadNowBtn.removeEventListener('click', reload); |
122 |
| - this.subscribed = false; |
123 |
| - } |
124 |
| - } |
| 129 | + |
125 | 130 | connectedCallback() {
|
126 | 131 | this.targetSelector = this.getAttribute('target-selector') || 'palindrom-client';
|
127 |
| - this._bindToElement(); |
128 |
| - this._subscribeToEvents(); |
| 132 | + bindToElement(this); |
| 133 | + subscribeToEvents(this); |
129 | 134 | }
|
| 135 | + |
130 | 136 | disconnectedCallback() {
|
131 |
| - this._unsubscribeToEvents(); |
| 137 | + unsubscribeToEvents(this); |
132 | 138 | }
|
| 139 | + |
133 | 140 | handleGenericError(event) {
|
134 | 141 | this.genericErrorPane.removeAttribute('hidden');
|
135 | 142 | console.error(event.detail.error);
|
136 | 143 | }
|
| 144 | + |
137 | 145 | cancelReloading() {
|
138 | 146 | this.connectionErrorPane.setAttribute('hidden', '');
|
139 | 147 | clearInterval(this.reloadInterval);
|
140 | 148 | delete this.reloadInterval;
|
141 | 149 | }
|
| 150 | + |
142 | 151 | handleConnectionError(event) {
|
143 | 152 | this.connectionErrorPane.removeAttribute('hidden');
|
144 | 153 | let timeout = 4;
|
|
155 | 164 | }
|
156 | 165 | handleReconnectionCountdownTick(event) {
|
157 | 166 | /* you can use this method to add your logic to handle reconnection timer ticks */
|
158 |
| - console.log('Reconnection tick', event) |
| 167 | + console.info('Reconnection tick', event) |
159 | 168 | }
|
160 | 169 | handleReconnectionEnd() {
|
161 | 170 | /* you can use this method to add your logic to handle reconnection initiation. */
|
162 |
| - console.log('Reconnection will happen now...') |
| 171 | + console.info('Reconnection will happen now...') |
163 | 172 | }
|
164 | 173 | attributeChangedCallback(name, oldVal, newVal) {
|
165 | 174 | this.targetSelector = newVal;
|
166 |
| - this._bindToElement(); |
167 |
| - this._subscribeToEvents(); |
| 175 | + this.bindToElement(this); |
| 176 | + this.subscribeToEvents(this); |
168 | 177 | }
|
169 | 178 | }
|
170 | 179 | customElements.define('palindrom-error-catcher', PalindromErrorCatcher)
|
|
0 commit comments