Skip to content

Commit

Permalink
fix: refresh iot client every 8 hours
Browse files Browse the repository at this point in the history
  • Loading branch information
dgreif committed Sep 18, 2021
1 parent 85f8013 commit 1f114a5
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/api.ts
Expand Up @@ -12,6 +12,7 @@ import { RestMini } from './rest-mini'
import { Restore } from './restore'
import { BehaviorSubject } from 'rxjs'
import { IotDevice } from './iot-device'
import { debounceTime } from 'rxjs/operators'

export interface ApiConfig extends EmailAuth {}

Expand All @@ -23,7 +24,8 @@ const productMap = {
knownProducts = Object.keys(productMap),
productFetchQueryString = knownProducts
.map((product) => 'iotProducts=' + product)
.join('&')
.join('&'),
iotClientRefreshPeriod = 8 * 60 * 60 * 1000 // refresh client every 8 hours

export class HatchBabyApi {
restClient = new RestClient(this.config)
Expand Down Expand Up @@ -134,6 +136,13 @@ export class HatchBabyApi {
}

onIotClient = new BehaviorSubject<AwsIotDevice>(await createNewIotClient())

onIotClient.pipe(debounceTime(iotClientRefreshPeriod)).subscribe(() => {
createNewIotClient()
.then((client) => onIotClient?.next(client))
.catch(logError)
})

return onIotClient
}

Expand Down

0 comments on commit 1f114a5

Please sign in to comment.