@@ -24,7 +24,7 @@ import './components/moon-phase-calendar';
24
24
// styles
25
25
import style from './css/style.css' ;
26
26
27
- const BASE_REFRESH_INTERVAL = 15 * 1000 ;
27
+ const BASE_REFRESH_INTERVAL = 60 * 1000 ;
28
28
const LOADING_TIMEOUT = 1500 ;
29
29
30
30
@customElement ( 'lunar-phase-card' )
@@ -44,12 +44,12 @@ export class LunarPhaseCard extends LitElement {
44
44
@property ( { type : Object } ) protected moon ! : Moon ;
45
45
@state ( ) _activeCard : PageType | null = null ;
46
46
@state ( ) selectedDate : Date | undefined ;
47
- @state ( ) _refreshInterval : number | undefined ;
48
47
49
48
@state ( ) _calendarPopup : boolean = false ;
50
- @state ( ) _state : MoonState = MoonState . READY ;
51
49
@state ( ) _resizeInitiated = false ;
52
50
@state ( ) public _cardWidth = 0 ;
51
+ @state ( ) private _state : MoonState = MoonState . READY ;
52
+ @state ( ) private _refreshInterval : number | undefined ;
53
53
54
54
@state ( ) _activeEditorPage ?: PageType ;
55
55
@state ( ) _resizeObserver : ResizeObserver | null = null ;
@@ -83,13 +83,14 @@ export class LunarPhaseCard extends LitElement {
83
83
window . LunarCard = this ;
84
84
window . Moon = this . moon ;
85
85
}
86
+ this . startRefreshInterval ( ) ;
87
+
86
88
if ( isEditorMode ( this ) ) {
87
89
document . addEventListener ( 'toggle-graph-editor' , ( ev ) => this . _handleEditorEvent ( ev as CustomEvent ) ) ;
88
90
}
89
91
if ( ! this . _resizeInitiated && ! this . _resizeObserver ) {
90
92
this . delayedAttachResizeObserver ( ) ;
91
93
}
92
- this . startRefreshInterval ( ) ;
93
94
}
94
95
95
96
disconnectedCallback ( ) : void {
@@ -107,16 +108,14 @@ export class LunarPhaseCard extends LitElement {
107
108
}
108
109
109
110
attachResizeObserver ( ) : void {
110
- if ( this . _resizeObserver ) {
111
- return ;
112
- }
113
- this . _resizeObserver = new ResizeObserver ( ( ) => {
111
+ const ro = new ResizeObserver ( ( ) => {
114
112
this . measureCard ( ) ;
115
113
} ) ;
116
114
117
115
const card = this . shadowRoot ?. querySelector ( 'ha-card' ) as HTMLElement ;
118
116
if ( card ) {
119
- this . _resizeObserver . observe ( card ) ;
117
+ ro . observe ( card ) ;
118
+ this . _resizeObserver = ro ;
120
119
}
121
120
}
122
121
@@ -132,11 +131,7 @@ export class LunarPhaseCard extends LitElement {
132
131
const header = this . shadowRoot ?. getElementById ( 'lpc-header' ) as HTMLElement ;
133
132
if ( card ) {
134
133
this . _cardWidth = card . clientWidth ;
135
- }
136
-
137
- if ( header ) {
138
- const sizes = header . getBoundingClientRect ( ) ;
139
- // console.log(JSON.stringify(sizes, null, 2));
134
+ // console.log('card width', this._cardWidth);
140
135
}
141
136
}
142
137
@@ -257,19 +252,25 @@ export class LunarPhaseCard extends LitElement {
257
252
if ( this . _refreshInterval !== undefined ) {
258
253
clearInterval ( this . _refreshInterval ) ;
259
254
}
260
-
255
+ // Calculate the remaining time until the next full minute
256
+ const now = new Date ( ) ;
257
+ const remainingMs = ( 60 - now . getSeconds ( ) ) * 1000 ;
261
258
// Set up a new interval
262
- this . _refreshInterval = window . setInterval ( ( ) => {
263
- if ( this . _activeCard === PageType . BASE || this . _activeCard === PageType . HORIZON ) {
264
- this . _state = MoonState . LOADING ;
265
- setTimeout ( ( ) => {
266
- this . _state = MoonState . READY ;
267
- } , LOADING_TIMEOUT ) ;
268
- this . requestUpdate ( ) ;
269
- } else {
270
- this . clearRefreshInterval ( ) ;
271
- }
272
- } , BASE_REFRESH_INTERVAL ) ;
259
+ setTimeout ( ( ) => {
260
+ this . _refreshInterval = window . setInterval ( ( ) => {
261
+ if ( this . _activeCard === PageType . BASE || this . _activeCard === PageType . HORIZON ) {
262
+ if ( this . _state !== MoonState . LOADING ) {
263
+ this . _state = MoonState . LOADING ;
264
+ setTimeout ( ( ) => {
265
+ // console.log('Refresh interval triggered');
266
+ this . _state = MoonState . READY ;
267
+ this . requestUpdate ( ) ;
268
+ } , LOADING_TIMEOUT ) ;
269
+ }
270
+ }
271
+ } , BASE_REFRESH_INTERVAL ) ;
272
+ // console.log('Triggering first refresh');
273
+ } , remainingMs ) ;
273
274
}
274
275
275
276
private clearRefreshInterval ( ) {
@@ -437,7 +438,12 @@ export class LunarPhaseCard extends LitElement {
437
438
}
438
439
439
440
private renderHorizon ( ) : TemplateResult | void {
440
- return html `< moon-horizon .hass =${ this . hass } .moon =${ this . moon } .card=${ this as any } > </ moon-horizon > ` ;
441
+ return html `< moon-horizon
442
+ .hass =${ this . hass }
443
+ .moon =${ this . moon }
444
+ .card=${ this as any }
445
+ .cardWidth=${ this . _cardWidth }
446
+ > </ moon-horizon > ` ;
441
447
}
442
448
443
449
private updateDate ( action ?: 'next' | 'prev' ) {
0 commit comments