Skip to content

Commit 1d35407

Browse files
authored
feat: added methods to store and retrieve state in the context store (#249)
1 parent 5bac3c6 commit 1d35407

File tree

10 files changed

+1217
-859
lines changed

10 files changed

+1217
-859
lines changed

docs/interfaces/_util_smart_app_context_d_.smartappcontext.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
* [configNumberValue](_util_smart_app_context_d_.smartappcontext.md#confignumbervalue)
1616
* [configStringValue](_util_smart_app_context_d_.smartappcontext.md#configstringvalue)
1717
* [configTimeString](_util_smart_app_context_d_.smartappcontext.md#configtimestring)
18+
* [getItem](_util_smart_app_context_d_.smartappcontext.md#getitem)
1819
* [isAuthenticated](_util_smart_app_context_d_.smartappcontext.md#isauthenticated)
20+
* [removeAllItems](_util_smart_app_context_d_.smartappcontext.md#removeallitems)
21+
* [removeItem](_util_smart_app_context_d_.smartappcontext.md#removeitem)
22+
* [setItem](_util_smart_app_context_d_.smartappcontext.md#setitem)
1923
* [retrieveTokens](_util_smart_app_context_d_.smartappcontext.md#retrievetokens)
2024
* [setLocationId](_util_smart_app_context_d_.smartappcontext.md#setlocationid)
2125

@@ -206,3 +210,61 @@ Name | Type | Description |
206210

207211
**Returns:** *void*
208212

213+
___
214+
215+
### getItem
216+
217+
**getItem**(`key`: string): *Promise‹any›*
218+
219+
Returns the value of the specified key from the app context state store
220+
221+
**Parameters:**
222+
223+
Name | Type | Description |
224+
------ | ------ |--------------------------------------|
225+
`key` | string | the name of the property to retrieve |
226+
227+
**Returns:** *Promise‹any›*
228+
229+
### setItem
230+
231+
**setItem**(`key`: string, `value`: any): *Promise‹void›*
232+
233+
Add or replaces the value of the specified key in the app context state store. The value can be any
234+
JSON-serializable type.
235+
236+
**Parameters:**
237+
238+
Name | Type | Description |
239+
------ |--------|--------------------------------------|
240+
`key` | string | the name of the property to retrieve |
241+
`value` | any | the value to be stored |
242+
243+
**Returns:** *Promise‹void›*
244+
245+
___
246+
247+
### removeItem
248+
249+
**removeItem**(`key`: string): *Promise‹void›*
250+
251+
Removes the specified entry from the app context state store
252+
253+
**Parameters:**
254+
255+
Name | Type | Description |
256+
------ | ------ |------------------------------------|
257+
`key` | string | the name of the property to remove |
258+
259+
**Returns:** *Promise‹void›*
260+
261+
___
262+
263+
### removeAllItems
264+
265+
**removeAllItems**(): *Promise‹void›*
266+
267+
Removes all items from the app context state store
268+
269+
**Returns:** *Promise‹void›*
270+

lib/pages/email-setting.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const SectionSetting = require('./section-setting.js')
44

5-
module.exports = class EmailSetting extends SectionSetting { // TODO - is this right?
5+
module.exports = class EmailSetting extends SectionSetting {
66
constructor(section, id) {
77
super(section, id)
88
this._type = 'EMAIL'

lib/smart-app.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -463,12 +463,13 @@ class SmartApp {
463463
ctx.setLocationId(isa.locationId)
464464

465465
if (this._contextStore) {
466-
await this._contextStore.put({
466+
await this._contextStore.update(ctx.installedAppId, {
467467
installedAppId: ctx.installedAppId,
468468
locationId: ctx.locationId,
469469
authToken: auth.access_token,
470470
refreshToken: auth.refresh_token,
471-
config: ctx.config
471+
config: ctx.config,
472+
locale: ctx.locale,
472473
})
473474
}
474475

@@ -532,6 +533,18 @@ class SmartApp {
532533
disableRemoveApp: this._disableRemoveApp
533534
}
534535

536+
if (this._contextStore) {
537+
const storedContext = await this._contextStore.get(context.installedAppId)
538+
if (!storedContext) {
539+
await this._contextStore.put({
540+
installedAppId: context.installedAppId,
541+
locationId: context.locationId,
542+
config: context.config,
543+
locale: context.locale,
544+
})
545+
}
546+
}
547+
535548
if (this._initializedHandler) {
536549
await this._initializedHandler(context, new Initialization(initialize), configurationData)
537550
}
@@ -585,7 +598,8 @@ class SmartApp {
585598
locationId: context.locationId,
586599
authToken: context.authToken,
587600
refreshToken: context.refreshToken,
588-
config: context.config
601+
config: context.config,
602+
locale: context.locale,
589603
})
590604
}
591605

@@ -597,12 +611,13 @@ class SmartApp {
597611
this._log.event(evt)
598612
await this._updatedHandler(context, evt.updateData)
599613
if (this._contextStore) {
600-
await this._contextStore.put({
614+
await this._contextStore.update(context.installedAppId, {
601615
installedAppId: context.installedAppId,
602616
locationId: context.locationId,
603617
authToken: context.authToken,
604618
refreshToken: context.refreshToken,
605-
config: context.config
619+
config: context.config,
620+
locale: context.locale,
606621
})
607622
}
608623

lib/util/smart-app-context.d.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,28 @@ export interface SmartAppContext {
9898
* @param id the location UUID
9999
*/
100100
setLocationId(id: string): void
101+
102+
/**
103+
* Returns the value of a property stored in the installed app state. The property is identified by a key.
104+
* @param key the name of the property
105+
*/
106+
getItem(key: string): Promise<any>
107+
108+
/**
109+
* Stores a property in the installed app state. The property is identified by a key.
110+
* @param key name of the property
111+
* @param value of the property
112+
*/
113+
setItem(key: string, value: any): Promise<any>
114+
115+
/**
116+
* Removes a property from the installed app state. The property is identified by a key.
117+
* @param key
118+
*/
119+
removeItem(key: string): Promise<any>
120+
121+
/**
122+
* Removes all properties from the installed app state.
123+
*/
124+
removeAllItems(): Promise<void>
101125
}

lib/util/smart-app-context.js

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'use strict'
2-
31
const i18n = require('i18n')
42
const {Mutex} = require('async-mutex')
53
const {
@@ -73,6 +71,12 @@ module.exports = class SmartAppContext {
7371
this.locale = data.executeData.parameters.locale
7472
break
7573

74+
case 'OAUTH_CALLBACK':
75+
this.executionId = data.executionId
76+
this.installedAppId = data.oauthCallbackData.installedAppId
77+
this.locale = data.locale
78+
break
79+
7680
// For constructing context for proactive API calls not in response to a lifecycle event
7781
default:
7882
this.authToken = data.authToken
@@ -269,7 +273,7 @@ module.exports = class SmartAppContext {
269273
return
270274
}
271275

272-
entry.forEach(item => {
276+
for (const item of entry) {
273277
const {componentId} = item.deviceConfig
274278
const promise = this.api.devices.get(item.deviceConfig.deviceId).then(device => {
275279
return {
@@ -280,7 +284,7 @@ module.exports = class SmartAppContext {
280284
}
281285
})
282286
list.push(promise)
283-
})
287+
}
284288
return Promise.all(list)
285289
}
286290

@@ -291,7 +295,7 @@ module.exports = class SmartAppContext {
291295
return
292296
}
293297

294-
entry.forEach(item => {
298+
for (const item of entry) {
295299
const {componentId} = item.deviceConfig
296300
const promise = this.api.devices.get(item.deviceConfig.deviceId).then(device => {
297301
return {
@@ -307,8 +311,48 @@ module.exports = class SmartAppContext {
307311
})
308312
})
309313
list.push(promise)
310-
})
314+
}
311315
return Promise.all(list)
312316
}
317+
318+
getItem(key) {
319+
if (this.app._contextStore) {
320+
if (this.app._contextStore.getItem) {
321+
return this.app._contextStore.getItem(this.installedAppId, key)
322+
}
323+
throw new Error('Context store does not support getting item.')
324+
}
325+
throw new Error('Context store not configured. Cannot get item.')
326+
}
327+
328+
setItem(key, value) {
329+
if (this.app._contextStore) {
330+
if (this.app._contextStore.setItem) {
331+
return this.app._contextStore.setItem(this.installedAppId, key, value)
332+
}
333+
throw new Error('Context store does not support setting item.')
334+
}
335+
throw new Error('Context store not configured. Cannot set item.')
336+
}
337+
338+
removeItem(key) {
339+
if (this.app._contextStore) {
340+
if (this.app._contextStore.clearItem) {
341+
return this.app._contextStore.clearItem(this.installedAppId, key)
342+
}
343+
throw new Error('Context store does not support clearing individual item.')
344+
}
345+
throw new Error('Context store not configured. Cannot clear item.')
346+
}
347+
348+
removeAllItems() {
349+
if (this.app._contextStore) {
350+
if (this.app._contextStore.clearAllItems) {
351+
return this.app._contextStore.clearAllItems(this.installedAppId)
352+
}
353+
throw new Error('Context store does not support clearing all items.')
354+
}
355+
throw new Error('Context store not configured. Cannot clear items.')
356+
}
313357
}
314358

0 commit comments

Comments
 (0)