@@ -35,9 +35,12 @@ export class PEWSClient {
35
35
36
36
// Earthqukae info
37
37
private eqkInfo ?: EarthquakeInfo
38
- private readonly cachedEqkInfo ?: EarthquakeInfo
39
38
private gridArr : number [ ] = [ ]
40
39
40
+ // Caching
41
+ private _cachedPhase2Info ?: EarthquakeInfo
42
+ private _cachedPhase3Info ?: EarthquakeInfo
43
+
41
44
constructor ( wrapper : PEWS ) {
42
45
this . Wrapper = wrapper
43
46
this . tide = this . delay
@@ -226,6 +229,7 @@ export class PEWSClient {
226
229
}
227
230
228
231
const bitEqkData = binDataBits . slice ( - 75 )
232
+ let needCaching = false
229
233
230
234
switch ( this . phase ) {
231
235
case 1 :
@@ -238,13 +242,23 @@ export class PEWSClient {
238
242
case 3 :
239
243
if ( this . _phase !== this . _cachedPhase ) {
240
244
this . renewGrid = true
241
-
245
+ needCaching = true
242
246
// TODO: eqkInfo 캐싱 도입 후,
243
247
// 이전 event ID 와 다를 때에 renewGrid = true 처리 필요.
244
248
}
245
-
246
249
await this . eqkHandler ( bitEqkData )
250
+
251
+ if ( needCaching ) {
252
+ switch ( this . phase ) {
253
+ case 2 :
254
+ this . _cachedPhase2Info = this . eqkInfo
255
+ break
256
+ case 3 :
257
+ this . _cachedPhase3Info = this . eqkInfo
258
+ }
259
+ }
247
260
break
261
+
248
262
case 4 :
249
263
if ( this . eqkInfo != null ) {
250
264
this . eqkInfo . eqkID =
@@ -267,6 +281,37 @@ export class PEWSClient {
267
281
}
268
282
269
283
private async eqkHandler ( eqkData : Uint8Array ) : Promise < void > {
284
+ // bit 69~94: eqkID
285
+ const eqkID =
286
+ ( ( ( eqkData [ 8 ] & 0b111 ) << 23 ) |
287
+ ( eqkData [ 9 ] << 15 ) |
288
+ ( eqkData [ 10 ] << 7 ) |
289
+ ( eqkData [ 11 ] >> 1 ) ) +
290
+ 2000000000
291
+
292
+ // Check with cached data
293
+ switch ( this . phase ) {
294
+ case 2 :
295
+ if ( this . _cachedPhase2Info ?. eqkID === eqkID ) {
296
+ this . eqkInfo = this . _cachedPhase2Info
297
+ this . logger . debug (
298
+ "eqkHandler: eqkID hasn't changed, using cached data" ,
299
+ )
300
+ return
301
+ }
302
+ break
303
+
304
+ case 3 :
305
+ if ( this . _cachedPhase3Info ?. eqkID === eqkID ) {
306
+ this . eqkInfo = this . _cachedPhase3Info
307
+ this . logger . debug (
308
+ "eqkHandler: eqkID hasn't changed, using cached data" ,
309
+ )
310
+ return
311
+ }
312
+ break
313
+ }
314
+
270
315
// bit 0~9: latitude
271
316
const lat = 30 + ( ( eqkData [ 0 ] << 2 ) | ( eqkData [ 1 ] >> 6 ) ) / 100
272
317
@@ -288,14 +333,6 @@ export class PEWSClient {
288
333
( eqkData [ 8 ] >> 3 ) ) *
289
334
1000
290
335
291
- // bit 69~94: eqkID
292
- const eqkID =
293
- ( ( ( eqkData [ 8 ] & 0b111 ) << 23 ) |
294
- ( eqkData [ 9 ] << 15 ) |
295
- ( eqkData [ 10 ] << 7 ) |
296
- ( eqkData [ 11 ] >> 1 ) ) +
297
- 2000000000
298
-
299
336
// bit 95~98: max intensity
300
337
const maxIntensity = ( ( eqkData [ 11 ] & 0b1 ) << 3 ) | ( eqkData [ 12 ] >> 5 )
301
338
@@ -332,7 +369,6 @@ export class PEWSClient {
332
369
dep,
333
370
eqkID,
334
371
}
335
- console . log ( this . eqkInfo )
336
372
337
373
if ( ( this . phase === 2 || this . phase === 3 ) && this . renewGrid ) {
338
374
if ( this . eqkInfo ?. eqkID !== undefined ) {
0 commit comments