Skip to content

Commit ba37fde

Browse files
committed
- Allow for non blocking token refresh on main app (blocking in other uses)
- Stop doing reresh straight after initial login
1 parent ccc694a commit ba37fde

File tree

10 files changed

+42
-23
lines changed

10 files changed

+42
-23
lines changed

src/app.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,11 @@ export async function createApp(config: Config, bl: Bluelink) {
8282
// if its been at least MIN_API_REFRESH_TIME milliseconds
8383
const cachedStatus = bl.getCachedStatus()
8484
if (!cachedStatus || cachedStatus.status.lastStatusCheck < Date.now() + MIN_API_REFRESH_TIME) {
85-
bl.getStatus(false, true).then(async (status) => {
86-
updateStatus(status)
85+
// non blocking refresh of Auth then status call
86+
bl.refreshAuth().then(async () => {
87+
bl.getStatus(false, true).then(async (status) => {
88+
updateStatus(status)
89+
})
8790
})
8891
}
8992

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ import { confirm, quickOptions } from './lib/scriptable-utils'
2727

2828
// Init Bluelink - Deal with region exceptions if needed
2929
try {
30-
bl = await initRegionalBluelink(blConfig)
30+
// main app handles refreshing auth in non blocking way
31+
bl = await initRegionalBluelink(blConfig, config.runsWithSiri || config.runsInWidget ? true : false)
3132
} catch (e) {
3233
const error = e instanceof Error ? e.message : e
3334
const errorMessage = `Error Initalizing Bluelink: ${error}`

src/lib/bluelink-regions/australia.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ export class BluelinkAustralia extends Bluelink {
8484
this.europeccs2 = undefined
8585
}
8686

87-
static async init(config: Config, vin?: string, statusCheckInterval?: number) {
87+
static async init(config: Config, refreshAuth: boolean, vin?: string, statusCheckInterval?: number) {
8888
const obj = new BluelinkAustralia(config, statusCheckInterval)
89-
await obj.superInit(config)
89+
await obj.superInit(config, refreshAuth)
9090
return obj
9191
}
9292

src/lib/bluelink-regions/base.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,18 +178,20 @@ export class Bluelink {
178178
this.logger = getBluelinkLogger()
179179
}
180180

181-
protected async superInit(config: Config, statusCheckInterval?: number) {
181+
protected async superInit(config: Config, refreshAuth: boolean, statusCheckInterval?: number) {
182182
this.vin = this.config.vin
183183
this.statusCheckInterval = statusCheckInterval || DEFAULT_STATUS_CHECK_INTERVAL
184184

185+
// check for cache - if not this is first login
186+
const existingCache = this.cacheExists()
185187
// loadCache will login user if the cache doesnt exist i.e first app use
186188
const cache = await this.loadCache()
187189
if (!cache) {
188190
this.loginFailure = true
189191
return
190192
}
191193
this.cache = cache
192-
await this.refreshLogin()
194+
if (existingCache && refreshAuth) await this.refreshLogin()
193195
}
194196

195197
protected async refreshLogin(force?: boolean) {
@@ -322,6 +324,10 @@ export class Bluelink {
322324
}
323325
}
324326

327+
public async refreshAuth(force = false): Promise<void> {
328+
return await this.refreshLogin(force)
329+
}
330+
325331
public async getStatus(forceUpdate: boolean, noCache: boolean, location: boolean = false): Promise<Status> {
326332
if (forceUpdate) {
327333
// getCar first then save then get remote status
@@ -439,6 +445,10 @@ export class Bluelink {
439445
Keychain.set(this.getCacheKey(true), JSON.stringify(this.cache))
440446
}
441447

448+
protected cacheExists(): boolean {
449+
return Keychain.contains(this.getCacheKey())
450+
}
451+
442452
protected async loadCache(): Promise<Cache | undefined> {
443453
let cache: Cache | undefined = undefined
444454
if (Keychain.contains(this.getCacheKey())) {

src/lib/bluelink-regions/canada.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ export class BluelinkCanada extends Bluelink {
6969
}
7070
}
7171

72-
static async init(config: Config, vin?: string, statusCheckInterval?: number) {
72+
static async init(config: Config, refreshAuth: boolean, vin?: string, statusCheckInterval?: number) {
7373
const obj = new BluelinkCanada(config, statusCheckInterval)
74-
await obj.superInit(config)
74+
await obj.superInit(config, refreshAuth)
7575
return obj
7676
}
7777

src/lib/bluelink-regions/europe.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ export class BluelinkEurope extends Bluelink {
8989
this.europeccs2 = undefined
9090
}
9191

92-
static async init(config: Config, vin?: string, statusCheckInterval?: number) {
92+
static async init(config: Config, refreshAuth: boolean, vin?: string, statusCheckInterval?: number) {
9393
const obj = new BluelinkEurope(config, statusCheckInterval)
94-
await obj.superInit(config)
94+
await obj.superInit(config, refreshAuth)
9595
return obj
9696
}
9797

src/lib/bluelink-regions/india.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ export class BluelinkIndia extends Bluelink {
7575
this.europeccs2 = undefined
7676
}
7777

78-
static async init(config: Config, vin?: string, statusCheckInterval?: number) {
78+
static async init(config: Config, refreshAuth: boolean, vin?: string, statusCheckInterval?: number) {
7979
const obj = new BluelinkIndia(config, statusCheckInterval)
80-
await obj.superInit(config)
80+
await obj.superInit(config, refreshAuth)
8181
return obj
8282
}
8383

src/lib/bluelink-regions/usa-kia.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ export class BluelinkUSAKia extends Bluelink {
4343
this.authIdHeader = 'vinkey'
4444
}
4545

46-
static async init(config: Config, vin?: string, statusCheckInterval?: number) {
46+
static async init(config: Config, refreshAuth: boolean, vin?: string, statusCheckInterval?: number) {
4747
const obj = new BluelinkUSAKia(config, statusCheckInterval)
48-
await obj.superInit(config)
48+
await obj.superInit(config, refreshAuth)
4949
return obj
5050
}
5151

src/lib/bluelink-regions/usa.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ export class BluelinkUSA extends Bluelink {
4848
this.authHeader = 'accessToken'
4949
}
5050

51-
static async init(config: Config, vin?: string, statusCheckInterval?: number) {
51+
static async init(config: Config, refreshAuth: boolean, vin?: string, statusCheckInterval?: number) {
5252
const obj = new BluelinkUSA(config, statusCheckInterval)
53-
await obj.superInit(config)
53+
await obj.superInit(config, refreshAuth)
5454
return obj
5555
}
5656

src/lib/bluelink.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ const regionSupport = {
1313
genesis: ['canada', 'usa'],
1414
}
1515

16-
export async function initRegionalBluelink(config: Config): Promise<BluelinkCanada | Bluelink | undefined> {
16+
export async function initRegionalBluelink(
17+
config: Config,
18+
refreshAuth = true,
19+
): Promise<BluelinkCanada | Bluelink | undefined> {
1720
for (const [manufacturer, regions] of Object.entries(regionSupport)) {
1821
if (config.manufacturer.toLowerCase() === manufacturer) {
1922
if (!regions.includes(config.auth.region)) {
@@ -24,15 +27,17 @@ export async function initRegionalBluelink(config: Config): Promise<BluelinkCana
2427

2528
switch (config.auth.region) {
2629
case 'canada':
27-
return await BluelinkCanada.init(config)
30+
return await BluelinkCanada.init(config, refreshAuth)
2831
case 'usa':
29-
return config.manufacturer === 'kia' ? await BluelinkUSAKia.init(config) : await BluelinkUSA.init(config)
32+
return config.manufacturer === 'kia'
33+
? await BluelinkUSAKia.init(config, refreshAuth)
34+
: await BluelinkUSA.init(config, refreshAuth)
3035
case 'europe':
31-
return await BluelinkEurope.init(config)
36+
return await BluelinkEurope.init(config, refreshAuth)
3237
case 'india':
33-
return await BluelinkIndia.init(config)
38+
return await BluelinkIndia.init(config, refreshAuth)
3439
case 'australia':
35-
return await BluelinkAustralia.init(config)
40+
return await BluelinkAustralia.init(config, refreshAuth)
3641
default:
3742
throw new Error(
3843
`Something went wrong determining bluelink region! Please raise an issue on GitHub with details of your vehicle and region.`,

0 commit comments

Comments
 (0)