node-nlp
Version:
Library for NLU (Natural Language Understanding) done in Node.js
70 lines • 3.03 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
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 AutoSaveStateiMiddleware 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);
}
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.
*/
add(...botStates) {
botStateSet_1.BotStateSet.prototype.add.apply(this.botStateSet, botStates);
return this;
}
}
exports.AutoSaveStateMiddleware = AutoSaveStateMiddleware;
//# sourceMappingURL=autoSaveStateMiddleware.js.map
;