botbuilder-dialogs-adaptive
Version:
Rule system for the Microsoft BotBuilder dialog system.
200 lines • 8.86 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ForEachPage = void 0;
/**
* @module botbuilder-dialogs-adaptive
*/
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
const actionScope_1 = require("./actionScope");
const adaptive_expressions_1 = require("adaptive-expressions");
const FOREACHPAGE = 'dialog.foreach.page';
const FOREACHPAGEINDEX = 'dialog.foreach.pageindex';
/**
* Executes a set of actions once for each page of results in an in-memory list or collection.
*
* @remarks
* The list or collection at [property](#property) will be broken up into pages and stored in
* `dialog.page` for each iteration of the loop. The size of each page is determined by [maxSize](#maxsize)
* and defaults to a size of 10. The loop can be exited early by including either a `EndDialog` or
* `GotoDialog` action.
*/
class ForEachPage extends actionScope_1.ActionScope {
/**
* Initializes a new instance of the [ForeachPage](xref:botbuilder-dialogs-adaptive.ForeachPage) class.
*
* @param itemsProperty Optional. Expression used to compute the list that should be enumerated.
* @param pageSize Default = `10`. Page size.
*/
constructor(itemsProperty, pageSize = 10) {
super();
/**
* Expression used to compute the list that should be enumerated.
*/
this.page = new adaptive_expressions_1.StringExpression(FOREACHPAGE);
/**
* Expression used to compute the list that should be enumerated.
*/
this.pageIndex = new adaptive_expressions_1.StringExpression(FOREACHPAGEINDEX);
/**
* Page size, default to 10.
*/
this.pageSize = new adaptive_expressions_1.IntExpression(10);
if (itemsProperty) {
this.itemsProperty = new adaptive_expressions_1.StringExpression(itemsProperty);
}
this.pageSize = new adaptive_expressions_1.IntExpression(pageSize);
}
/**
* @param property The key of the conditional selector configuration.
* @returns The converter for the selector configuration.
*/
getConverter(property) {
switch (property) {
case 'itemsProperty':
return new adaptive_expressions_1.StringExpressionConverter();
case 'page':
return new adaptive_expressions_1.StringExpressionConverter();
case 'pageIndex':
return new adaptive_expressions_1.StringExpressionConverter();
case 'pageSize':
return new adaptive_expressions_1.IntExpressionConverter();
case 'disabled':
return new adaptive_expressions_1.BoolExpressionConverter();
default:
return super.getConverter(property);
}
}
/**
* Gets the child [Dialog](xref:botbuilder-dialogs.Dialog) dependencies so they can be added to the containers [Dialog](xref:botbuilder-dialogs.Dialog) set.
*
* @returns The child [Dialog](xref:botbuilder-dialogs.Dialog) dependencies.
*/
getDependencies() {
return this.actions;
}
/**
* Starts a new [Dialog](xref:botbuilder-dialogs.Dialog) and pushes it onto the dialog stack.
*
* @param dc The [DialogContext](xref:botbuilder-dialogs.DialogContext) for the current turn of conversation.
* @param _options Optional. Initial information to pass to the dialog.
* @returns A `Promise` representing the asynchronous operation.
*/
beginDialog(dc, _options) {
return __awaiter(this, void 0, void 0, function* () {
if (this.disabled && this.disabled.getValue(dc.state)) {
return yield dc.endDialog();
}
dc.state.setValue(this.pageIndex.getValue(dc.state), 0);
return yield this.nextPage(dc);
});
}
/**
* @protected
* Called when the [Dialog](xref:botbuilder-dialogs.Dialog) continues to the next action.
* @param dc The [DialogContext](xref:botbuilder-dialogs.DialogContext) for the current turn of conversation.
* @param _result Optional. Value returned from the dialog that was called. The type
* of the value returned is dependent on the child dialog.
* @returns A `Promise` representing the asynchronous operation.
*/
onEndOfActions(dc, _result) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.nextPage(dc);
});
}
/**
* @protected
* Called when returning control to this [Dialog](xref:botbuilder-dialogs.Dialog) with an [ActionScopeResult](xref:botbuilder-dialogs-adaptive.ActionScopeResult)
* with the property `ActionCommand` set to `BreakLoop`.
* @param dc The [DialogContext](xref:botbuilder-dialogs.DialogContext) for the current turn of conversation.
* @param _actionScopeResult [ActionScopeResult](xref:botbuilder-dialogs-adaptive.ActionScopeResult), contains the actions scope result.
* @returns A `Promise` representing the asynchronous operation.
*/
onBreakLoop(dc, _actionScopeResult) {
return __awaiter(this, void 0, void 0, function* () {
return yield dc.endDialog();
});
}
/**
* @protected
* Called when returning control to this [Dialog](xref:botbuilder-dialogs.Dialog) with an [ActionScopeResult](xref:botbuilder-dialogs-adaptive.ActionScopeResult)
* with the property `ActionCommand` set to `ContinueLoop`.
* @param dc The [DialogContext](xref:botbuilder-dialogs.DialogContext) for the current turn of conversation.
* @param _actionScopeResult [ActionScopeResult](xref:botbuilder-dialogs-adaptive.ActionScopeResult), contains the actions scope result.
* @returns A `Promise` representing the asynchronous operation.
*/
onContinueLoop(dc, _actionScopeResult) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.nextPage(dc);
});
}
/**
* @protected
* Builds the compute Id for the [Dialog](xref:botbuilder-dialogs.Dialog).
* @returns A `string` representing the compute Id.
*/
onComputeId() {
return `ForEachPage[${this.itemsProperty.toString()}]`;
}
/**
* @private
* @param dc The [DialogContext](xref:botbuilder-dialogs.DialogContext) for the current turn of conversation.
* @returns A `Promise` representing the asynchronous operation.
*/
nextPage(dc) {
return __awaiter(this, void 0, void 0, function* () {
let pageIndex = dc.state.getValue(this.pageIndex.getValue(dc.state), 0);
const pageSize = this.pageSize.getValue(dc.state);
const itemOffset = pageSize * pageIndex;
const itemsProperty = this.itemsProperty.getValue(dc.state);
const items = dc.state.getValue(itemsProperty, []);
if (items.length > 0) {
const page = this.getPage(items, itemOffset, pageSize);
if (page && page.length > 0) {
dc.state.setValue(this.page.getValue(dc.state), page);
dc.state.setValue(this.pageIndex.getValue(dc.state), ++pageIndex);
return yield this.beginAction(dc, 0);
}
}
return yield dc.endDialog();
});
}
/**
* @private
*/
getPage(list, index, pageSize) {
const page = [];
const end = index + pageSize;
if (Array.isArray(list)) {
for (let i = index; i < list.length && i < end; i++) {
page.push(list[i]);
}
}
else if (typeof list === 'object') {
let i = index;
for (const key in list) {
if (Object.hasOwnProperty.call(list, key)) {
if (i < end) {
page.push(list[key]);
}
i++;
}
}
}
return page;
}
}
exports.ForEachPage = ForEachPage;
ForEachPage.$kind = 'Microsoft.ForeachPage';
//# sourceMappingURL=forEachPage.js.map