Skip to content

Commit

Permalink
feat: added methods to store and retrieve state in the context store (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
bflorian authored Oct 5, 2023
1 parent 5bac3c6 commit 1d35407
Show file tree
Hide file tree
Showing 10 changed files with 1,217 additions and 859 deletions.
62 changes: 62 additions & 0 deletions docs/interfaces/_util_smart_app_context_d_.smartappcontext.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
* [configNumberValue](_util_smart_app_context_d_.smartappcontext.md#confignumbervalue)
* [configStringValue](_util_smart_app_context_d_.smartappcontext.md#configstringvalue)
* [configTimeString](_util_smart_app_context_d_.smartappcontext.md#configtimestring)
* [getItem](_util_smart_app_context_d_.smartappcontext.md#getitem)
* [isAuthenticated](_util_smart_app_context_d_.smartappcontext.md#isauthenticated)
* [removeAllItems](_util_smart_app_context_d_.smartappcontext.md#removeallitems)
* [removeItem](_util_smart_app_context_d_.smartappcontext.md#removeitem)
* [setItem](_util_smart_app_context_d_.smartappcontext.md#setitem)
* [retrieveTokens](_util_smart_app_context_d_.smartappcontext.md#retrievetokens)
* [setLocationId](_util_smart_app_context_d_.smartappcontext.md#setlocationid)

Expand Down Expand Up @@ -206,3 +210,61 @@ Name | Type | Description |

**Returns:** *void*

___

### getItem

**getItem**(`key`: string): *Promise‹any›*

Returns the value of the specified key from the app context state store

**Parameters:**

Name | Type | Description |
------ | ------ |--------------------------------------|
`key` | string | the name of the property to retrieve |

**Returns:** *Promise‹any›*

### setItem

**setItem**(`key`: string, `value`: any): *Promise‹void›*

Add or replaces the value of the specified key in the app context state store. The value can be any
JSON-serializable type.

**Parameters:**

Name | Type | Description |
------ |--------|--------------------------------------|
`key` | string | the name of the property to retrieve |
`value` | any | the value to be stored |

**Returns:** *Promise‹void›*

___

### removeItem

**removeItem**(`key`: string): *Promise‹void›*

Removes the specified entry from the app context state store

**Parameters:**

Name | Type | Description |
------ | ------ |------------------------------------|
`key` | string | the name of the property to remove |

**Returns:** *Promise‹void›*

___

### removeAllItems

**removeAllItems**(): *Promise‹void›*

Removes all items from the app context state store

**Returns:** *Promise‹void›*

2 changes: 1 addition & 1 deletion lib/pages/email-setting.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

module.exports = class EmailSetting extends SectionSetting { // TODO - is this right?
module.exports = class EmailSetting extends SectionSetting {
constructor(section, id) {
super(section, id)
this._type = 'EMAIL'
Expand Down
25 changes: 20 additions & 5 deletions lib/smart-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,12 +463,13 @@ class SmartApp {
ctx.setLocationId(isa.locationId)

if (this._contextStore) {
await this._contextStore.put({
await this._contextStore.update(ctx.installedAppId, {
installedAppId: ctx.installedAppId,
locationId: ctx.locationId,
authToken: auth.access_token,
refreshToken: auth.refresh_token,
config: ctx.config
config: ctx.config,
locale: ctx.locale,
})
}

Expand Down Expand Up @@ -532,6 +533,18 @@ class SmartApp {
disableRemoveApp: this._disableRemoveApp
}

if (this._contextStore) {
const storedContext = await this._contextStore.get(context.installedAppId)
if (!storedContext) {
await this._contextStore.put({
installedAppId: context.installedAppId,
locationId: context.locationId,
config: context.config,
locale: context.locale,
})
}
}

if (this._initializedHandler) {
await this._initializedHandler(context, new Initialization(initialize), configurationData)
}
Expand Down Expand Up @@ -585,7 +598,8 @@ class SmartApp {
locationId: context.locationId,
authToken: context.authToken,
refreshToken: context.refreshToken,
config: context.config
config: context.config,
locale: context.locale,
})
}

Expand All @@ -597,12 +611,13 @@ class SmartApp {
this._log.event(evt)
await this._updatedHandler(context, evt.updateData)
if (this._contextStore) {
await this._contextStore.put({
await this._contextStore.update(context.installedAppId, {
installedAppId: context.installedAppId,
locationId: context.locationId,
authToken: context.authToken,
refreshToken: context.refreshToken,
config: context.config
config: context.config,
locale: context.locale,
})
}

Expand Down
24 changes: 24 additions & 0 deletions lib/util/smart-app-context.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,28 @@ export interface SmartAppContext {
* @param id the location UUID
*/
setLocationId(id: string): void

/**
* Returns the value of a property stored in the installed app state. The property is identified by a key.
* @param key the name of the property
*/
getItem(key: string): Promise<any>

/**
* Stores a property in the installed app state. The property is identified by a key.
* @param key name of the property
* @param value of the property
*/
setItem(key: string, value: any): Promise<any>

/**
* Removes a property from the installed app state. The property is identified by a key.
* @param key
*/
removeItem(key: string): Promise<any>

/**
* Removes all properties from the installed app state.
*/
removeAllItems(): Promise<void>
}
56 changes: 50 additions & 6 deletions lib/util/smart-app-context.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict'

const i18n = require('i18n')
const {Mutex} = require('async-mutex')
const {
Expand Down Expand Up @@ -73,6 +71,12 @@ module.exports = class SmartAppContext {
this.locale = data.executeData.parameters.locale
break

case 'OAUTH_CALLBACK':
this.executionId = data.executionId
this.installedAppId = data.oauthCallbackData.installedAppId
this.locale = data.locale
break

// For constructing context for proactive API calls not in response to a lifecycle event
default:
this.authToken = data.authToken
Expand Down Expand Up @@ -269,7 +273,7 @@ module.exports = class SmartAppContext {
return
}

entry.forEach(item => {
for (const item of entry) {
const {componentId} = item.deviceConfig
const promise = this.api.devices.get(item.deviceConfig.deviceId).then(device => {
return {
Expand All @@ -280,7 +284,7 @@ module.exports = class SmartAppContext {
}
})
list.push(promise)
})
}
return Promise.all(list)
}

Expand All @@ -291,7 +295,7 @@ module.exports = class SmartAppContext {
return
}

entry.forEach(item => {
for (const item of entry) {
const {componentId} = item.deviceConfig
const promise = this.api.devices.get(item.deviceConfig.deviceId).then(device => {
return {
Expand All @@ -307,8 +311,48 @@ module.exports = class SmartAppContext {
})
})
list.push(promise)
})
}
return Promise.all(list)
}

getItem(key) {
if (this.app._contextStore) {
if (this.app._contextStore.getItem) {
return this.app._contextStore.getItem(this.installedAppId, key)
}
throw new Error('Context store does not support getting item.')
}
throw new Error('Context store not configured. Cannot get item.')
}

setItem(key, value) {
if (this.app._contextStore) {
if (this.app._contextStore.setItem) {
return this.app._contextStore.setItem(this.installedAppId, key, value)
}
throw new Error('Context store does not support setting item.')
}
throw new Error('Context store not configured. Cannot set item.')
}

removeItem(key) {
if (this.app._contextStore) {
if (this.app._contextStore.clearItem) {
return this.app._contextStore.clearItem(this.installedAppId, key)
}
throw new Error('Context store does not support clearing individual item.')
}
throw new Error('Context store not configured. Cannot clear item.')
}

removeAllItems() {
if (this.app._contextStore) {
if (this.app._contextStore.clearAllItems) {
return this.app._contextStore.clearAllItems(this.installedAppId)
}
throw new Error('Context store does not support clearing all items.')
}
throw new Error('Context store not configured. Cannot clear items.')
}
}

Loading

0 comments on commit 1d35407

Please sign in to comment.