@puregram/scenes
Version:
Simple implementation of middleware-based scene management for puregram
48 lines (47 loc) • 1.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SceneManager = void 0;
const contexts_1 = require("./contexts");
const cache_repository_1 = require("./cache-repository");
class SceneManager {
constructor(rawOptions = {}) {
this.repository = new cache_repository_1.CacheRepository();
const options = Array.isArray(rawOptions)
? { scenes: rawOptions }
: rawOptions;
if (options.scenes) {
this.addScenes(options.scenes);
}
}
/** Checks for has a scene */
hasScene(slug) {
return this.repository.has(slug);
}
/** Adds scenes to the repository */
addScenes(scenes) {
for (const scene of scenes) {
this.repository.set(scene.slug, scene);
}
return this;
}
/** Returns the middleware for embedding */
get middleware() {
return (context, next) => {
context.scene = new contexts_1.SceneContext({
context,
repository: this.repository
});
return next();
};
}
/** Returns the middleware for intercept */
get middlewareIntercept() {
return (context, next) => {
if (!context.scene.current) {
return next();
}
return context.scene.reenter();
};
}
}
exports.SceneManager = SceneManager;