UNPKG

node-nlp

Version:

Library for NLU (Natural Language Understanding) done in Node.js

72 lines 2.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const internal_1 = require("./internal"); const middlewareSet_1 = require("./middlewareSet"); /** * Abstract base class for all adapter plugins. * * @remarks * Adapters manage the communication between the bot and a user over a specific channel, or set * of channels. */ class BotAdapter { constructor() { this.middleware = new middlewareSet_1.MiddlewareSet(); } /** * Gets/sets a error handler that will be called anytime an uncaught exception is raised during * a turn. */ get onTurnError() { return this.turnError; } set onTurnError(value) { this.turnError = value; } /** * Registers middleware handlers(s) with the adapter. * @param middleware One or more middleware handlers(s) to register. */ use(...middleware) { middlewareSet_1.MiddlewareSet.prototype.use.apply(this.middleware, middleware); return this; } /** * Executes the adapters middleware chain. * * @remarks * This should be be called by the parent class to run the adapters middleware chain. The * `next()` handler passed to the method will be called at the end of the chain. * * While the context object is passed in from the caller is created by the caller, what gets * passed to the `next()` handler is a wrapped version of the context which will automatically * be revoked upon completion of the turn. This causes the bots logic to throw an error if it * tries to use the context object after the turn completes. * @param context Context for the current turn of conversation with the user. * @param next Function to call at the end of the middleware chain. * @param next.revocableContext A revocable version of the context object. */ runMiddleware(context, next) { // Wrap context with revocable proxy const pContext = internal_1.makeRevocable(context); return new Promise((resolve, reject) => { this.middleware.run(pContext.proxy, () => { // Call next with revocable context return next(pContext.proxy); }).then(resolve, (err) => { if (this.onTurnError) { this.onTurnError(pContext.proxy, err) .then(resolve, reject); } else { reject(err); } }); }).then(() => pContext.revoke(), (err) => { pContext.revoke(); throw err; }); } } exports.BotAdapter = BotAdapter; //# sourceMappingURL=botAdapter.js.map