botbuilder-core
Version:
Core components for Microsoft Bot Builder. Components in this library can run either in a browser or on the server.
87 lines • 3.54 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.BotStateSet = void 0;
/**
* A collection of `BotState` plugins that should be loaded or saved in parallel as a single unit.
* See `AutoSaveStateMiddleware` for an implementation of this class.
*/
class BotStateSet {
/**
* Creates a new BotStateSet instance.
*
* @param botStates One or more BotState plugins to register.
*/
constructor(...botStates) {
/**
* Array of the sets `BotState` plugins.
*/
this.botStates = [];
BotStateSet.prototype.add.apply(this, botStates);
}
/**
* Registers one or more `BotState` plugins with the set.
*
* @param botStates One or more BotState plugins to register.
* @returns The updated BotStateSet.
*/
add(...botStates) {
botStates.forEach((botstate) => {
if (typeof botstate.load === 'function' && typeof botstate.saveChanges === 'function') {
this.botStates.push(botstate);
}
else {
throw new Error("BotStateSet: a object was added that isn't an instance of BotState.");
}
});
return this;
}
/**
* Calls `BotState.load()` on all of the BotState plugins in the set.
*
* @remarks
* This will trigger all of the plugins to read in their state in parallel.
*
* ```JavaScript
* await stateSet.readAll(context);
* ```
* @param context Context for current turn of conversation with the user.
* @param force (Optional) If `true` the cache will be bypassed and the state will always be read in directly from storage. Defaults to `false`.
*/
loadAll(context, force = false) {
return __awaiter(this, void 0, void 0, function* () {
const promises = this.botStates.map((botstate) => botstate.load(context, force));
yield Promise.all(promises);
return;
});
}
/**
* Calls `BotState.saveChanges()` on all of the BotState plugins in the set.
*
* @remarks
* This will trigger all of the plugins to write out their state in parallel.
*
* ```JavaScript
* await stateSet.saveAllChanges(context);
* ```
* @param context Context for current turn of conversation with the user.
* @param force (Optional) if `true` the state will always be written out regardless of its change state. Defaults to `false`.
*/
saveAllChanges(context, force = false) {
return __awaiter(this, void 0, void 0, function* () {
const promises = this.botStates.map((botstate) => botstate.saveChanges(context, force));
yield Promise.all(promises);
return;
});
}
}
exports.BotStateSet = BotStateSet;
//# sourceMappingURL=botStateSet.js.map
;