botbuilder-core
Version:
Core components for Microsoft Bot Builder. Components in this library can run either in a browser or on the server.
82 lines • 3.44 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.BotStatePropertyAccessor = void 0;
/**
* A `BotState` specific implementation of the `StatePropertyAccessor` interface.
*
* @remarks
* Properties can be defined for a given `BotState` instance using `createProperty()`.
*
* ```JavaScript
* const dialogStateProperty = ConversationState.createProperty('dialogState');
* const dialogs = new DialogSet(dialogStateProperty);
* ```
* @param T (Optional) type of property being persisted. Defaults to `any` type.
*/
class BotStatePropertyAccessor {
/**
* Creates a new BotStatePropertyAccessor instance.
*
* @param state Parent BotState instance.
* @param name Unique name of the property for the parent BotState.
*/
constructor(state, name) {
this.state = state;
this.name = name;
}
/**
* Deletes the persisted property from its backing storage object.
*
* @param context [TurnContext](xref:botbuilder-core.TurnContext) object for this turn.
*/
delete(context) {
return __awaiter(this, void 0, void 0, function* () {
const obj = yield this.state.load(context);
if (Object.prototype.hasOwnProperty.call(obj, this.name)) {
delete obj[this.name];
}
});
}
/**
* Reads a persisted property from its backing storage object.
*
* @param context [TurnContext](xref:botbuilder-core.TurnContext) object for this turn.
* @param defaultValue Optional. Default value for the property.
* @returns A JSON representation of the cached state.
*/
get(context, defaultValue) {
return __awaiter(this, void 0, void 0, function* () {
const obj = yield this.state.load(context);
if (!Object.prototype.hasOwnProperty.call(obj, this.name) && defaultValue !== undefined) {
const clone = typeof defaultValue === 'object' || Array.isArray(defaultValue)
? JSON.parse(JSON.stringify(defaultValue))
: defaultValue;
obj[this.name] = clone;
}
return obj[this.name];
});
}
/**
* Assigns a new value to the properties backing storage object.
*
* @param context [TurnContext](xref:botbuilder-core.TurnContext) object for this turn.
* @param value Value to set on the property.
*/
set(context, value) {
return __awaiter(this, void 0, void 0, function* () {
const obj = yield this.state.load(context);
obj[this.name] = value;
});
}
}
exports.BotStatePropertyAccessor = BotStatePropertyAccessor;
//# sourceMappingURL=botStatePropertyAccessor.js.map
;