Skip to content

Commit dc50fa9

Browse files
committed
✨ feat: Introduce caching in the getMMI
1 parent 14ac7c2 commit dc50fa9

File tree

1 file changed

+48
-12
lines changed

1 file changed

+48
-12
lines changed

src/client/client.ts

+48-12
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,12 @@ export class PEWSClient {
3535

3636
// Earthqukae info
3737
private eqkInfo?: EarthquakeInfo
38-
private readonly cachedEqkInfo?: EarthquakeInfo
3938
private gridArr: number[] = []
4039

40+
// Caching
41+
private _cachedPhase2Info?: EarthquakeInfo
42+
private _cachedPhase3Info?: EarthquakeInfo
43+
4144
constructor(wrapper: PEWS) {
4245
this.Wrapper = wrapper
4346
this.tide = this.delay
@@ -226,6 +229,7 @@ export class PEWSClient {
226229
}
227230

228231
const bitEqkData = binDataBits.slice(-75)
232+
let needCaching = false
229233

230234
switch (this.phase) {
231235
case 1:
@@ -238,13 +242,23 @@ export class PEWSClient {
238242
case 3:
239243
if (this._phase !== this._cachedPhase) {
240244
this.renewGrid = true
241-
245+
needCaching = true
242246
// TODO: eqkInfo 캐싱 도입 후,
243247
// 이전 event ID 와 다를 때에 renewGrid = true 처리 필요.
244248
}
245-
246249
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+
}
247260
break
261+
248262
case 4:
249263
if (this.eqkInfo != null) {
250264
this.eqkInfo.eqkID =
@@ -267,6 +281,37 @@ export class PEWSClient {
267281
}
268282

269283
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+
270315
// bit 0~9: latitude
271316
const lat = 30 + ((eqkData[0] << 2) | (eqkData[1] >> 6)) / 100
272317

@@ -288,14 +333,6 @@ export class PEWSClient {
288333
(eqkData[8] >> 3)) *
289334
1000
290335

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-
299336
// bit 95~98: max intensity
300337
const maxIntensity = ((eqkData[11] & 0b1) << 3) | (eqkData[12] >> 5)
301338

@@ -332,7 +369,6 @@ export class PEWSClient {
332369
dep,
333370
eqkID,
334371
}
335-
console.log(this.eqkInfo)
336372

337373
if ((this.phase === 2 || this.phase === 3) && this.renewGrid) {
338374
if (this.eqkInfo?.eqkID !== undefined) {

0 commit comments

Comments
 (0)