UNPKG

@puregram/scenes

Version:

Simple implementation of middleware-based scene management for puregram

62 lines (61 loc) 1.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.StepSceneContext = void 0; const scene_types_1 = require("./scene.types"); class StepSceneContext { constructor(options) { this.stepChanged = false; this.context = options.context; this.steps = options.steps; } /** The first enter to the handler */ get firstTime() { const { firstTime = true } = this.context.scene.session; return firstTime; } /** Returns current `stepId` */ get stepId() { return this.context.scene.session.stepId || 0; } /** Sets current `stepId` */ set stepId(stepId) { const { session } = this.context.scene; session.stepId = stepId; session.firstTime = true; this.stepChanged = true; } /** Returns current handler */ get current() { return this.steps[this.stepId]; } /** Reenter current step handler */ async reenter() { const { current } = this; if (!current) { await this.context.scene.leave(); return; } this.stepChanged = false; await current(this.context); if (this.context.scene.lastAction !== scene_types_1.LastAction.LEAVE && !this.stepChanged) { this.context.scene.session.firstTime = false; } } /** The `go` method goes to a specific step */ go(stepId, { silent = false } = {}) { this.stepId = stepId; if (silent) { return Promise.resolve(); } return this.reenter(); } /** Move to the next handler */ next(options) { return this.go(this.stepId + 1, options); } /** Move to the previous handler */ previous(options) { return this.go(this.stepId - 1, options); } } exports.StepSceneContext = StepSceneContext;