Skip to content

Commit

Permalink
port: settings scope from configuration
Browse files Browse the repository at this point in the history
Fixes #3461
  • Loading branch information
Josh Gummersall committed Mar 29, 2021
1 parent 07dda24 commit a503f13
Show file tree
Hide file tree
Showing 6 changed files with 719 additions and 694 deletions.
1 change: 1 addition & 0 deletions libraries/botbuilder-dialogs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"botframework-connector": "4.1.6",
"globalize": "^1.4.2",
"lodash": "^4.17.21",
"runtypes": "^5.0.1",
"uuid": "^8.3.2"
},
"devDependencies": {
Expand Down
8 changes: 6 additions & 2 deletions libraries/botbuilder-dialogs/src/dialogsBotComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import { BotComponent } from 'botbuilder-core';
import { Configuration, ServiceCollection } from 'botbuilder-runtime-core';
import { Dictionary, Unknown } from 'runtypes';
import { MemoryScope, PathResolver } from './memory';

import {
Expand All @@ -26,11 +27,14 @@ import {
} from './memory/pathResolvers';

export class DialogsBotComponent extends BotComponent {
configureServices(services: ServiceCollection, _configuration: Configuration): void {
configureServices(services: ServiceCollection, configuration: Configuration): void {
services.composeFactory<MemoryScope[]>('memoryScopes', (memoryScopes) => {
const rootConfiguration = configuration.get([]);
const initialSettings = Dictionary(Unknown).guard(rootConfiguration) ? rootConfiguration : {};

return memoryScopes.concat(
new TurnMemoryScope(),
new SettingsMemoryScope(),
new SettingsMemoryScope(initialSettings),
new DialogMemoryScope(),
new DialogContextMemoryScope(),
new DialogClassMemoryScope(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ class Node {
export class SettingsMemoryScope extends MemoryScope {
/**
* Initializes a new instance of the [SettingsMemoryScope](xref:botbuilder-dialogs.SettingsMemoryScope) class.
*
* @param initialSettings initial settings to derive from
*/
public constructor() {
public constructor(private readonly initialSettings?: Record<string, unknown>) {
super(ScopePath.settings, false);
}

Expand All @@ -56,20 +58,23 @@ export class SettingsMemoryScope extends MemoryScope {
*/
public getMemory(dc: DialogContext): Record<string, unknown> {
if (dc.context.turnState.has(ScopePath.settings)) {
return dc.context.turnState.get(ScopePath.settings) ?? {};
} else {
const configuration = dc.context.turnState.get(DialogTurnStateConstants.configuration) ?? {};
return dc.context.turnState.get<Record<string, unknown>>(ScopePath.settings) ?? {};
}

Object.entries(process.env).reduce((result, [key, value]) => {
result[`${key}`] = value;
return result;
}, configuration);
let settings: Record<string, unknown>;

const settings = SettingsMemoryScope.loadSettings(configuration);
dc.context.turnState.set(ScopePath.settings, settings);
const configuration = dc.context.turnState.get<Record<string, string>>(DialogTurnStateConstants.configuration);
if (configuration != null) {
settings = SettingsMemoryScope.loadSettings(
Object.entries(process.env).reduce((acc, [key, value]) => ({ ...acc, [key]: value }), configuration)
);

return settings;
dc.context.turnState.set(ScopePath.settings, settings);
} else if (this.initialSettings != null) {
settings = this.initialSettings;
}

return settings ?? {};
}

/**
Expand Down
Loading

0 comments on commit a503f13

Please sign in to comment.