81 lines
No EOL
3.4 KiB
JavaScript
81 lines
No EOL
3.4 KiB
JavaScript
"use strict";
|
|
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.AutoSaveStateMiddleware = void 0;
|
|
const botStateSet_1 = require("./botStateSet");
|
|
/**
|
|
* Middleware that will automatically save any state changes at the end of the turn.
|
|
*
|
|
* @remarks
|
|
* The `AutoSaveStateMiddleware` class should be added towards the top of your bot's middleware
|
|
* stack, before any other components that use state. Any `BotState` plugins passed to the
|
|
* constructor will have their `BotState.saveChanges()` method called upon successful completion
|
|
* of the turn.
|
|
*
|
|
* This example shows boilerplate code for reading and writing conversation and user state within
|
|
* a bot:
|
|
*
|
|
* ```JavaScript
|
|
* const { AutoSaveStateMiddleware, ConversationState, UserState, MemoryStorage } = require('botbuilder');
|
|
*
|
|
* const storage = new MemoryStorage();
|
|
* const conversationState = new ConversationState(storage);
|
|
* const userState = new UserState(storage);
|
|
* adapter.use(new AutoSaveStateMiddleware(conversationState, userState));
|
|
*
|
|
* server.post('/api/messages', (req, res) => {
|
|
* adapter.processActivity(req, res, async (turnContext) => {
|
|
* // Get state
|
|
* const convo = await conversationState.load(turnContext);
|
|
* const user = await userState.load(turnContext);
|
|
*
|
|
* // ... route activity ...
|
|
* // ...make changes to state objects...
|
|
* // ... no need to call userState.saveChanges() or conversationState.saveChanges() anymore!
|
|
* });
|
|
* });
|
|
* ```
|
|
*/
|
|
class AutoSaveStateMiddleware {
|
|
/**
|
|
* Creates a new AutoSaveStateMiddleware instance.
|
|
*
|
|
* @param botStates One or more BotState plugins to automatically save at the end of the turn.
|
|
*/
|
|
constructor(...botStates) {
|
|
this.botStateSet = new botStateSet_1.BotStateSet();
|
|
botStateSet_1.BotStateSet.prototype.add.apply(this.botStateSet, botStates);
|
|
}
|
|
/**
|
|
* Called by the adapter (for example, a `BotFrameworkAdapter`) at runtime in order to process an inbound [Activity](xref:botframework-schema.Activity).
|
|
*
|
|
* @param context The context object for this turn.
|
|
* @param next {function} The next delegate function.
|
|
*/
|
|
onTurn(context, next) {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
yield next();
|
|
yield this.botStateSet.saveAllChanges(context, false);
|
|
});
|
|
}
|
|
/**
|
|
* Adds additional `BotState` plugins to be saved.
|
|
*
|
|
* @param botStates One or more BotState plugins to add.
|
|
* @returns The updated BotStateSet object.
|
|
*/
|
|
add(...botStates) {
|
|
botStateSet_1.BotStateSet.prototype.add.apply(this.botStateSet, botStates);
|
|
return this;
|
|
}
|
|
}
|
|
exports.AutoSaveStateMiddleware = AutoSaveStateMiddleware;
|
|
//# sourceMappingURL=autoSaveStateMiddleware.js.map
|