1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const composer_1 = require("../composer");
|
4 | const debug_1 = require("debug");
|
5 | const debug = (0, debug_1.default)('telegraf:scenes:context');
|
6 | const noop = () => Promise.resolve();
|
7 | const now = () => Math.floor(Date.now() / 1000);
|
8 | class SceneContextScene {
|
9 | constructor(ctx, scenes, options) {
|
10 | this.ctx = ctx;
|
11 | this.scenes = scenes;
|
12 | this.leaving = false;
|
13 |
|
14 | const fallbackSessionDefault = {};
|
15 | this.options = { defaultSession: fallbackSessionDefault, ...options };
|
16 | }
|
17 | get session() {
|
18 | var _a, _b;
|
19 | const defaultSession = this.options.defaultSession;
|
20 | let session = (_b = (_a = this.ctx.session) === null || _a === void 0 ? void 0 : _a.__scenes) !== null && _b !== void 0 ? _b : defaultSession;
|
21 | if (session.expires !== undefined && session.expires < now()) {
|
22 | session = defaultSession;
|
23 | }
|
24 | if (this.ctx.session === undefined) {
|
25 | this.ctx.session = { __scenes: session };
|
26 | }
|
27 | else {
|
28 | this.ctx.session.__scenes = session;
|
29 | }
|
30 | return session;
|
31 | }
|
32 | get state() {
|
33 | var _a;
|
34 | var _b;
|
35 | return ((_a = (_b = this.session).state) !== null && _a !== void 0 ? _a : (_b.state = {}));
|
36 | }
|
37 | set state(value) {
|
38 | this.session.state = { ...value };
|
39 | }
|
40 | get current() {
|
41 | var _a;
|
42 | const sceneId = (_a = this.session.current) !== null && _a !== void 0 ? _a : this.options.default;
|
43 | return sceneId === undefined || !this.scenes.has(sceneId)
|
44 | ? undefined
|
45 | : this.scenes.get(sceneId);
|
46 | }
|
47 | reset() {
|
48 | if (this.ctx.session !== undefined)
|
49 | this.ctx.session.__scenes = this.options.defaultSession;
|
50 | }
|
51 | async enter(sceneId, initialState = {}, silent = false) {
|
52 | var _a, _b;
|
53 | if (!this.scenes.has(sceneId)) {
|
54 | throw new Error(`Can't find scene: ${sceneId}`);
|
55 | }
|
56 | if (!silent) {
|
57 | await this.leave();
|
58 | }
|
59 | debug('Entering scene', sceneId, initialState, silent);
|
60 | this.session.current = sceneId;
|
61 | this.state = initialState;
|
62 | const ttl = (_b = (_a = this.current) === null || _a === void 0 ? void 0 : _a.ttl) !== null && _b !== void 0 ? _b : this.options.ttl;
|
63 | if (ttl !== undefined) {
|
64 | this.session.expires = now() + ttl;
|
65 | }
|
66 | if (this.current === undefined || silent) {
|
67 | return;
|
68 | }
|
69 | const handler = 'enterMiddleware' in this.current &&
|
70 | typeof this.current.enterMiddleware === 'function'
|
71 | ? this.current.enterMiddleware()
|
72 | : this.current.middleware();
|
73 | return await handler(this.ctx, noop);
|
74 | }
|
75 | reenter() {
|
76 | return this.session.current === undefined
|
77 | ? undefined
|
78 | : this.enter(this.session.current, this.state);
|
79 | }
|
80 | async leave() {
|
81 | if (this.leaving)
|
82 | return;
|
83 | debug('Leaving scene');
|
84 | try {
|
85 | this.leaving = true;
|
86 | if (this.current === undefined) {
|
87 | return;
|
88 | }
|
89 | const handler = 'leaveMiddleware' in this.current &&
|
90 | typeof this.current.leaveMiddleware === 'function'
|
91 | ? this.current.leaveMiddleware()
|
92 | : composer_1.default.passThru();
|
93 | await handler(this.ctx, noop);
|
94 | return this.reset();
|
95 | }
|
96 | finally {
|
97 | this.leaving = false;
|
98 | }
|
99 | }
|
100 | }
|
101 | exports.default = SceneContextScene;
|