botbuilder-dialogs
Version:
A dialog stack based conversation manager for Microsoft BotBuilder.
80 lines • 3.03 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.DialogMemoryScope = void 0;
/**
* @module botbuilder-dialogs
*/
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
const memoryScope_1 = require("./memoryScope");
const scopePath_1 = require("../scopePath");
const dialogContainer_1 = require("../../dialogContainer");
/**
* DialogMemoryScope maps "dialog" -> dc.parent.activeDialog.state || dc.activeDialog.state
*/
class DialogMemoryScope extends memoryScope_1.MemoryScope {
/**
* Initializes a new instance of the [DialogMemoryScope](xref:botbuilder-dialogs.DialogMemoryScope) class.
*/
constructor() {
super(scopePath_1.ScopePath.dialog);
}
/**
* Gets the backing memory for this scope.
*
* @param dc The [DialogContext](xref:botbuilder-dialogs.DialogContext) object for this turn.
* @returns The memory for the scope.
*/
getMemory(dc) {
// If active dialog is a container dialog then "dialog" binds to it.
// Otherwise the "dialog" will bind to the dialogs parent assuming it
// is a container.
let parent = dc;
if (!this.isContainer(parent) && this.isContainer(parent.parent)) {
parent = parent.parent;
}
// If there's no active dialog then return undefined.
return parent.activeDialog ? parent.activeDialog.state : undefined;
}
/**
* Changes the backing object for the memory scope.
*
* @param dc The [DialogContext](xref:botbuilder-dialogs.DialogContext) object for this turn.
* @param memory Memory object to set for the scope.
*/
setMemory(dc, memory) {
if (memory == undefined) {
throw new Error('DialogMemoryScope.setMemory: undefined memory object passed in.');
}
// If active dialog is a container dialog then "dialog" binds to it.
// Otherwise the "dialog" will bind to the dialogs parent assuming it
// is a container.
let parent = dc;
if (!this.isContainer(parent) && this.isContainer(parent.parent)) {
parent = parent.parent;
}
// If there's no active dialog then throw an error.
if (!parent.activeDialog) {
throw new Error('DialogMemoryScope.setMemory: no active dialog found.');
}
parent.activeDialog.state = memory;
}
/**
* @private
* @param dc The [DialogContext](xref:botbuilder-dialogs.DialogContext) object for this turn.
* @returns A boolean indicating whether is a cointainer or not.
*/
isContainer(dc) {
if (dc != undefined && dc.activeDialog != undefined) {
const dialog = dc.findDialog(dc.activeDialog.id);
if (dialog instanceof dialogContainer_1.DialogContainer) {
return true;
}
}
return false;
}
}
exports.DialogMemoryScope = DialogMemoryScope;
//# sourceMappingURL=dialogMemoryScope.js.map
;