@mtcute/dispatcher 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (190) hide show
  1. package/LICENSE +8 -0
  2. package/README.md +26 -0
  3. package/cjs/callback-data-builder.d.ts +43 -0
  4. package/cjs/callback-data-builder.js +99 -0
  5. package/cjs/callback-data-builder.js.map +1 -0
  6. package/cjs/context/base.d.ts +8 -0
  7. package/cjs/context/base.js +3 -0
  8. package/cjs/context/base.js.map +1 -0
  9. package/cjs/context/callback-query.d.ts +27 -0
  10. package/cjs/context/callback-query.js +56 -0
  11. package/cjs/context/callback-query.js.map +1 -0
  12. package/cjs/context/chat-join-request.d.ts +16 -0
  13. package/cjs/context/chat-join-request.js +34 -0
  14. package/cjs/context/chat-join-request.js.map +1 -0
  15. package/cjs/context/chosen-inline-result.d.ts +21 -0
  16. package/cjs/context/chosen-inline-result.js +35 -0
  17. package/cjs/context/chosen-inline-result.js.map +1 -0
  18. package/cjs/context/index.d.ts +8 -0
  19. package/cjs/context/index.js +24 -0
  20. package/cjs/context/index.js.map +1 -0
  21. package/cjs/context/inline-query.d.ts +14 -0
  22. package/cjs/context/inline-query.js +22 -0
  23. package/cjs/context/inline-query.js.map +1 -0
  24. package/cjs/context/message.d.ts +72 -0
  25. package/cjs/context/message.js +142 -0
  26. package/cjs/context/message.js.map +1 -0
  27. package/cjs/context/parse.d.ts +1 -0
  28. package/cjs/context/parse.js +34 -0
  29. package/cjs/context/parse.js.map +1 -0
  30. package/cjs/context/pre-checkout-query.d.ts +16 -0
  31. package/cjs/context/pre-checkout-query.js +26 -0
  32. package/cjs/context/pre-checkout-query.js.map +1 -0
  33. package/cjs/dispatcher.d.ts +641 -0
  34. package/cjs/dispatcher.js +765 -0
  35. package/cjs/dispatcher.js.map +1 -0
  36. package/cjs/filters/bots.d.ts +70 -0
  37. package/cjs/filters/bots.js +129 -0
  38. package/cjs/filters/bots.js.map +1 -0
  39. package/cjs/filters/bundle.d.ts +10 -0
  40. package/cjs/filters/bundle.js +27 -0
  41. package/cjs/filters/bundle.js.map +1 -0
  42. package/cjs/filters/chat.d.ts +27 -0
  43. package/cjs/filters/chat.js +55 -0
  44. package/cjs/filters/chat.js.map +1 -0
  45. package/cjs/filters/group.d.ts +25 -0
  46. package/cjs/filters/group.js +72 -0
  47. package/cjs/filters/group.js.map +1 -0
  48. package/cjs/filters/index.d.ts +3 -0
  49. package/cjs/filters/index.js +29 -0
  50. package/cjs/filters/index.js.map +1 -0
  51. package/cjs/filters/logic.d.ts +29 -0
  52. package/cjs/filters/logic.js +114 -0
  53. package/cjs/filters/logic.js.map +1 -0
  54. package/cjs/filters/message.d.ts +295 -0
  55. package/cjs/filters/message.js +150 -0
  56. package/cjs/filters/message.js.map +1 -0
  57. package/cjs/filters/state.d.ts +15 -0
  58. package/cjs/filters/state.js +32 -0
  59. package/cjs/filters/state.js.map +1 -0
  60. package/cjs/filters/text.d.ts +64 -0
  61. package/cjs/filters/text.js +132 -0
  62. package/cjs/filters/text.js.map +1 -0
  63. package/cjs/filters/types.d.ts +91 -0
  64. package/cjs/filters/types.js +6 -0
  65. package/cjs/filters/types.js.map +1 -0
  66. package/cjs/filters/updates.d.ts +46 -0
  67. package/cjs/filters/updates.js +46 -0
  68. package/cjs/filters/updates.js.map +1 -0
  69. package/cjs/filters/user.d.ts +24 -0
  70. package/cjs/filters/user.js +76 -0
  71. package/cjs/filters/user.js.map +1 -0
  72. package/cjs/handler.d.ts +31 -0
  73. package/cjs/handler.js +4 -0
  74. package/cjs/handler.js.map +1 -0
  75. package/cjs/index.d.ts +8 -0
  76. package/cjs/index.js +25 -0
  77. package/cjs/index.js.map +1 -0
  78. package/cjs/package.json +3 -0
  79. package/cjs/propagation.d.ts +21 -0
  80. package/cjs/propagation.js +26 -0
  81. package/cjs/propagation.js.map +1 -0
  82. package/cjs/state/index.d.ts +3 -0
  83. package/cjs/state/index.js +20 -0
  84. package/cjs/state/index.js.map +1 -0
  85. package/cjs/state/key.d.ts +23 -0
  86. package/cjs/state/key.js +45 -0
  87. package/cjs/state/key.js.map +1 -0
  88. package/cjs/state/storage.d.ts +75 -0
  89. package/cjs/state/storage.js +17 -0
  90. package/cjs/state/storage.js.map +1 -0
  91. package/cjs/state/update-state.d.ts +151 -0
  92. package/cjs/state/update-state.js +211 -0
  93. package/cjs/state/update-state.js.map +1 -0
  94. package/cjs/wizard.d.ts +60 -0
  95. package/cjs/wizard.js +103 -0
  96. package/cjs/wizard.js.map +1 -0
  97. package/esm/callback-data-builder.d.ts +43 -0
  98. package/esm/callback-data-builder.js +95 -0
  99. package/esm/callback-data-builder.js.map +1 -0
  100. package/esm/context/base.d.ts +8 -0
  101. package/esm/context/base.js +2 -0
  102. package/esm/context/base.js.map +1 -0
  103. package/esm/context/callback-query.d.ts +27 -0
  104. package/esm/context/callback-query.js +52 -0
  105. package/esm/context/callback-query.js.map +1 -0
  106. package/esm/context/chat-join-request.d.ts +16 -0
  107. package/esm/context/chat-join-request.js +30 -0
  108. package/esm/context/chat-join-request.js.map +1 -0
  109. package/esm/context/chosen-inline-result.d.ts +21 -0
  110. package/esm/context/chosen-inline-result.js +31 -0
  111. package/esm/context/chosen-inline-result.js.map +1 -0
  112. package/esm/context/index.d.ts +8 -0
  113. package/esm/context/index.js +8 -0
  114. package/esm/context/index.js.map +1 -0
  115. package/esm/context/inline-query.d.ts +14 -0
  116. package/esm/context/inline-query.js +18 -0
  117. package/esm/context/inline-query.js.map +1 -0
  118. package/esm/context/message.d.ts +72 -0
  119. package/esm/context/message.js +138 -0
  120. package/esm/context/message.js.map +1 -0
  121. package/esm/context/parse.d.ts +1 -0
  122. package/esm/context/parse.js +30 -0
  123. package/esm/context/parse.js.map +1 -0
  124. package/esm/context/pre-checkout-query.d.ts +16 -0
  125. package/esm/context/pre-checkout-query.js +22 -0
  126. package/esm/context/pre-checkout-query.js.map +1 -0
  127. package/esm/dispatcher.d.ts +641 -0
  128. package/esm/dispatcher.js +761 -0
  129. package/esm/dispatcher.js.map +1 -0
  130. package/esm/filters/bots.d.ts +70 -0
  131. package/esm/filters/bots.js +125 -0
  132. package/esm/filters/bots.js.map +1 -0
  133. package/esm/filters/bundle.d.ts +10 -0
  134. package/esm/filters/bundle.js +11 -0
  135. package/esm/filters/bundle.js.map +1 -0
  136. package/esm/filters/chat.d.ts +27 -0
  137. package/esm/filters/chat.js +50 -0
  138. package/esm/filters/chat.js.map +1 -0
  139. package/esm/filters/group.d.ts +25 -0
  140. package/esm/filters/group.js +67 -0
  141. package/esm/filters/group.js.map +1 -0
  142. package/esm/filters/index.d.ts +3 -0
  143. package/esm/filters/index.js +3 -0
  144. package/esm/filters/index.js.map +1 -0
  145. package/esm/filters/logic.d.ts +29 -0
  146. package/esm/filters/logic.js +107 -0
  147. package/esm/filters/logic.js.map +1 -0
  148. package/esm/filters/message.d.ts +295 -0
  149. package/esm/filters/message.js +130 -0
  150. package/esm/filters/message.js.map +1 -0
  151. package/esm/filters/state.d.ts +15 -0
  152. package/esm/filters/state.js +27 -0
  153. package/esm/filters/state.js.map +1 -0
  154. package/esm/filters/text.d.ts +64 -0
  155. package/esm/filters/text.js +124 -0
  156. package/esm/filters/text.js.map +1 -0
  157. package/esm/filters/types.d.ts +91 -0
  158. package/esm/filters/types.js +5 -0
  159. package/esm/filters/types.js.map +1 -0
  160. package/esm/filters/updates.d.ts +46 -0
  161. package/esm/filters/updates.js +39 -0
  162. package/esm/filters/updates.js.map +1 -0
  163. package/esm/filters/user.d.ts +24 -0
  164. package/esm/filters/user.js +70 -0
  165. package/esm/filters/user.js.map +1 -0
  166. package/esm/handler.d.ts +31 -0
  167. package/esm/handler.js +3 -0
  168. package/esm/handler.js.map +1 -0
  169. package/esm/index.d.ts +8 -0
  170. package/esm/index.js +9 -0
  171. package/esm/index.js.map +1 -0
  172. package/esm/propagation.d.ts +21 -0
  173. package/esm/propagation.js +23 -0
  174. package/esm/propagation.js.map +1 -0
  175. package/esm/state/index.d.ts +3 -0
  176. package/esm/state/index.js +4 -0
  177. package/esm/state/index.js.map +1 -0
  178. package/esm/state/key.d.ts +23 -0
  179. package/esm/state/key.js +41 -0
  180. package/esm/state/key.js.map +1 -0
  181. package/esm/state/storage.d.ts +75 -0
  182. package/esm/state/storage.js +13 -0
  183. package/esm/state/storage.js.map +1 -0
  184. package/esm/state/update-state.d.ts +151 -0
  185. package/esm/state/update-state.js +206 -0
  186. package/esm/state/update-state.js.map +1 -0
  187. package/esm/wizard.d.ts +60 -0
  188. package/esm/wizard.js +99 -0
  189. package/esm/wizard.js.map +1 -0
  190. package/package.json +21 -0
@@ -0,0 +1,31 @@
1
+ import { BotStoppedUpdate, ChatJoinRequestUpdate, ChatMemberUpdate, DeleteMessageUpdate, DeleteStoryUpdate, HistoryReadUpdate, MaybeAsync, PeersIndex, PollUpdate, PollVoteUpdate, StoryUpdate, TelegramClient, tl, UserStatusUpdate, UserTypingUpdate } from '@mtcute/client';
2
+ import { UpdateContext } from './context/base.js';
3
+ import { CallbackQueryContext, ChatJoinRequestUpdateContext, ChosenInlineResultContext, InlineQueryContext, MessageContext, PreCheckoutQueryContext } from './context/index.js';
4
+ import { PropagationAction } from './propagation.js';
5
+ export interface BaseUpdateHandler<Name, Handler, Checker> {
6
+ name: Name;
7
+ callback: Handler;
8
+ check?: Checker;
9
+ }
10
+ export type ParsedUpdateHandler<Name, Update, State = never> = BaseUpdateHandler<Name, (update: Update, state: State) => MaybeAsync<void | PropagationAction>, (update: Update, state: State) => MaybeAsync<boolean>>;
11
+ export type RawUpdateHandler = BaseUpdateHandler<'raw', (client: TelegramClient, update: tl.TypeUpdate | tl.TypeMessage, peers: PeersIndex) => MaybeAsync<void | PropagationAction>, (client: TelegramClient, update: tl.TypeUpdate | tl.TypeMessage, peers: PeersIndex) => MaybeAsync<boolean>>;
12
+ export type NewMessageHandler<T = MessageContext, S = never> = ParsedUpdateHandler<'new_message', T, S>;
13
+ export type EditMessageHandler<T = MessageContext, S = never> = ParsedUpdateHandler<'edit_message', T, S>;
14
+ export type MessageGroupHandler<T = MessageContext, S = never> = ParsedUpdateHandler<'message_group', T, S>;
15
+ export type DeleteMessageHandler<T = UpdateContext<DeleteMessageUpdate>> = ParsedUpdateHandler<'delete_message', T>;
16
+ export type ChatMemberUpdateHandler<T = UpdateContext<ChatMemberUpdate>> = ParsedUpdateHandler<'chat_member', T>;
17
+ export type InlineQueryHandler<T = InlineQueryContext> = ParsedUpdateHandler<'inline_query', T>;
18
+ export type ChosenInlineResultHandler<T = ChosenInlineResultContext> = ParsedUpdateHandler<'chosen_inline_result', T>;
19
+ export type CallbackQueryHandler<T = CallbackQueryContext, S = never> = ParsedUpdateHandler<'callback_query', T, S>;
20
+ export type PollUpdateHandler<T = UpdateContext<PollUpdate>> = ParsedUpdateHandler<'poll', T>;
21
+ export type PollVoteHandler<T = UpdateContext<PollVoteUpdate>> = ParsedUpdateHandler<'poll_vote', T>;
22
+ export type UserStatusUpdateHandler<T = UpdateContext<UserStatusUpdate>> = ParsedUpdateHandler<'user_status', T>;
23
+ export type UserTypingHandler<T = UpdateContext<UserTypingUpdate>> = ParsedUpdateHandler<'user_typing', T>;
24
+ export type HistoryReadHandler<T = UpdateContext<HistoryReadUpdate>> = ParsedUpdateHandler<'history_read', T>;
25
+ export type BotStoppedHandler<T = UpdateContext<BotStoppedUpdate>> = ParsedUpdateHandler<'bot_stopped', T>;
26
+ export type BotChatJoinRequestHandler<T = ChatJoinRequestUpdateContext> = ParsedUpdateHandler<'bot_chat_join_request', T>;
27
+ export type ChatJoinRequestHandler<T = UpdateContext<ChatJoinRequestUpdate>> = ParsedUpdateHandler<'chat_join_request', T>;
28
+ export type PreCheckoutQueryHandler<T = PreCheckoutQueryContext> = ParsedUpdateHandler<'pre_checkout_query', T>;
29
+ export type StoryUpdateHandler<T = UpdateContext<StoryUpdate>> = ParsedUpdateHandler<'story', T>;
30
+ export type DeleteStoryHandler<T = UpdateContext<DeleteStoryUpdate>> = ParsedUpdateHandler<'delete_story', T>;
31
+ export type UpdateHandler = RawUpdateHandler | NewMessageHandler | EditMessageHandler | MessageGroupHandler | DeleteMessageHandler | ChatMemberUpdateHandler | InlineQueryHandler | ChosenInlineResultHandler | CallbackQueryHandler | PollUpdateHandler | PollVoteHandler | UserStatusUpdateHandler | UserTypingHandler | HistoryReadHandler | BotStoppedHandler | BotChatJoinRequestHandler | ChatJoinRequestHandler | PreCheckoutQueryHandler | StoryUpdateHandler | DeleteStoryHandler;
package/cjs/handler.js ADDED
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ // end-codegen
4
+ //# sourceMappingURL=handler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"handler.js","sourceRoot":"","sources":["../../src/handler.ts"],"names":[],"mappings":";;AAqGA,cAAc","sourcesContent":["import {\n BotStoppedUpdate,\n ChatJoinRequestUpdate,\n ChatMemberUpdate,\n DeleteMessageUpdate,\n DeleteStoryUpdate,\n HistoryReadUpdate,\n MaybeAsync,\n PeersIndex,\n PollUpdate,\n PollVoteUpdate,\n StoryUpdate,\n TelegramClient,\n tl,\n UserStatusUpdate,\n UserTypingUpdate,\n} from '@mtcute/client'\n\nimport { UpdateContext } from './context/base.js'\nimport {\n CallbackQueryContext,\n ChatJoinRequestUpdateContext,\n ChosenInlineResultContext,\n InlineQueryContext,\n MessageContext,\n PreCheckoutQueryContext,\n} from './context/index.js'\nimport { PropagationAction } from './propagation.js'\n\nexport interface BaseUpdateHandler<Name, Handler, Checker> {\n name: Name\n callback: Handler\n\n check?: Checker\n}\n\nexport type ParsedUpdateHandler<Name, Update, State = never> = BaseUpdateHandler<\n Name,\n (update: Update, state: State) => MaybeAsync<void | PropagationAction>,\n (update: Update, state: State) => MaybeAsync<boolean>\n>\n\nexport type RawUpdateHandler = BaseUpdateHandler<\n 'raw',\n (\n client: TelegramClient,\n update: tl.TypeUpdate | tl.TypeMessage,\n peers: PeersIndex,\n ) => MaybeAsync<void | PropagationAction>,\n (client: TelegramClient, update: tl.TypeUpdate | tl.TypeMessage, peers: PeersIndex) => MaybeAsync<boolean>\n>\n\n// begin-codegen\nexport type NewMessageHandler<T = MessageContext, S = never> = ParsedUpdateHandler<'new_message', T, S>\nexport type EditMessageHandler<T = MessageContext, S = never> = ParsedUpdateHandler<'edit_message', T, S>\nexport type MessageGroupHandler<T = MessageContext, S = never> = ParsedUpdateHandler<'message_group', T, S>\nexport type DeleteMessageHandler<T = UpdateContext<DeleteMessageUpdate>> = ParsedUpdateHandler<'delete_message', T>\nexport type ChatMemberUpdateHandler<T = UpdateContext<ChatMemberUpdate>> = ParsedUpdateHandler<'chat_member', T>\nexport type InlineQueryHandler<T = InlineQueryContext> = ParsedUpdateHandler<'inline_query', T>\nexport type ChosenInlineResultHandler<T = ChosenInlineResultContext> = ParsedUpdateHandler<'chosen_inline_result', T>\nexport type CallbackQueryHandler<T = CallbackQueryContext, S = never> = ParsedUpdateHandler<'callback_query', T, S>\nexport type PollUpdateHandler<T = UpdateContext<PollUpdate>> = ParsedUpdateHandler<'poll', T>\nexport type PollVoteHandler<T = UpdateContext<PollVoteUpdate>> = ParsedUpdateHandler<'poll_vote', T>\nexport type UserStatusUpdateHandler<T = UpdateContext<UserStatusUpdate>> = ParsedUpdateHandler<'user_status', T>\nexport type UserTypingHandler<T = UpdateContext<UserTypingUpdate>> = ParsedUpdateHandler<'user_typing', T>\nexport type HistoryReadHandler<T = UpdateContext<HistoryReadUpdate>> = ParsedUpdateHandler<'history_read', T>\nexport type BotStoppedHandler<T = UpdateContext<BotStoppedUpdate>> = ParsedUpdateHandler<'bot_stopped', T>\nexport type BotChatJoinRequestHandler<T = ChatJoinRequestUpdateContext> = ParsedUpdateHandler<\n 'bot_chat_join_request',\n T\n>\nexport type ChatJoinRequestHandler<T = UpdateContext<ChatJoinRequestUpdate>> = ParsedUpdateHandler<\n 'chat_join_request',\n T\n>\nexport type PreCheckoutQueryHandler<T = PreCheckoutQueryContext> = ParsedUpdateHandler<'pre_checkout_query', T>\nexport type StoryUpdateHandler<T = UpdateContext<StoryUpdate>> = ParsedUpdateHandler<'story', T>\nexport type DeleteStoryHandler<T = UpdateContext<DeleteStoryUpdate>> = ParsedUpdateHandler<'delete_story', T>\n\nexport type UpdateHandler =\n | RawUpdateHandler\n | NewMessageHandler\n | EditMessageHandler\n | MessageGroupHandler\n | DeleteMessageHandler\n | ChatMemberUpdateHandler\n | InlineQueryHandler\n | ChosenInlineResultHandler\n | CallbackQueryHandler\n | PollUpdateHandler\n | PollVoteHandler\n | UserStatusUpdateHandler\n | UserTypingHandler\n | HistoryReadHandler\n | BotStoppedHandler\n | BotChatJoinRequestHandler\n | ChatJoinRequestHandler\n | PreCheckoutQueryHandler\n | StoryUpdateHandler\n | DeleteStoryHandler\n\n// end-codegen\n"]}
package/cjs/index.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ export * from './callback-data-builder.js';
2
+ export * from './context/index.js';
3
+ export * from './dispatcher.js';
4
+ export * from './filters/index.js';
5
+ export * from './handler.js';
6
+ export * from './propagation.js';
7
+ export * from './state/index.js';
8
+ export * from './wizard.js';
package/cjs/index.js ADDED
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./callback-data-builder.js"), exports);
18
+ __exportStar(require("./context/index.js"), exports);
19
+ __exportStar(require("./dispatcher.js"), exports);
20
+ __exportStar(require("./filters/index.js"), exports);
21
+ __exportStar(require("./handler.js"), exports);
22
+ __exportStar(require("./propagation.js"), exports);
23
+ __exportStar(require("./state/index.js"), exports);
24
+ __exportStar(require("./wizard.js"), exports);
25
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,6DAA0C;AAC1C,qDAAkC;AAClC,kDAA+B;AAC/B,qDAAkC;AAClC,+CAA4B;AAC5B,mDAAgC;AAChC,mDAAgC;AAChC,8CAA2B","sourcesContent":["export * from './callback-data-builder.js'\nexport * from './context/index.js'\nexport * from './dispatcher.js'\nexport * from './filters/index.js'\nexport * from './handler.js'\nexport * from './propagation.js'\nexport * from './state/index.js'\nexport * from './wizard.js'\n"]}
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Propagation action.
3
+ *
4
+ * `Stop`: Stop the propagation of the event through any handler groups
5
+ * in the current dispatcher. Does not prevent child dispatchers from
6
+ * being executed.
7
+ *
8
+ * `StopChildren`: Stop the propagation of the event through any handler groups
9
+ * in the current dispatcher, and any of its children. If current dispatcher
10
+ * is a child, does not prevent from propagating to its siblings.
11
+ *
12
+ * `Continue`: Continue propagating the event inside the same handler group.
13
+ *
14
+ * `ToScene`: Used after using `state.enter()` to dispatch the update to the scene
15
+ */
16
+ export declare enum PropagationAction {
17
+ Stop = "stop",
18
+ StopChildren = "stop-children",
19
+ Continue = "continue",
20
+ ToScene = "scene"
21
+ }
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PropagationAction = void 0;
4
+ /**
5
+ * Propagation action.
6
+ *
7
+ * `Stop`: Stop the propagation of the event through any handler groups
8
+ * in the current dispatcher. Does not prevent child dispatchers from
9
+ * being executed.
10
+ *
11
+ * `StopChildren`: Stop the propagation of the event through any handler groups
12
+ * in the current dispatcher, and any of its children. If current dispatcher
13
+ * is a child, does not prevent from propagating to its siblings.
14
+ *
15
+ * `Continue`: Continue propagating the event inside the same handler group.
16
+ *
17
+ * `ToScene`: Used after using `state.enter()` to dispatch the update to the scene
18
+ */
19
+ var PropagationAction;
20
+ (function (PropagationAction) {
21
+ PropagationAction["Stop"] = "stop";
22
+ PropagationAction["StopChildren"] = "stop-children";
23
+ PropagationAction["Continue"] = "continue";
24
+ PropagationAction["ToScene"] = "scene";
25
+ })(PropagationAction = exports.PropagationAction || (exports.PropagationAction = {}));
26
+ //# sourceMappingURL=propagation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"propagation.js","sourceRoot":"","sources":["../../src/propagation.ts"],"names":[],"mappings":";;;AAAA;;;;;;;;;;;;;;GAcG;AACH,IAAY,iBAKX;AALD,WAAY,iBAAiB;IACzB,kCAAa,CAAA;IACb,mDAA8B,CAAA;IAC9B,0CAAqB,CAAA;IACrB,sCAAiB,CAAA;AACrB,CAAC,EALW,iBAAiB,GAAjB,yBAAiB,KAAjB,yBAAiB,QAK5B","sourcesContent":["/**\n * Propagation action.\n *\n * `Stop`: Stop the propagation of the event through any handler groups\n * in the current dispatcher. Does not prevent child dispatchers from\n * being executed.\n *\n * `StopChildren`: Stop the propagation of the event through any handler groups\n * in the current dispatcher, and any of its children. If current dispatcher\n * is a child, does not prevent from propagating to its siblings.\n *\n * `Continue`: Continue propagating the event inside the same handler group.\n *\n * `ToScene`: Used after using `state.enter()` to dispatch the update to the scene\n */\nexport enum PropagationAction {\n Stop = 'stop',\n StopChildren = 'stop-children',\n Continue = 'continue',\n ToScene = 'scene',\n}\n"]}
@@ -0,0 +1,3 @@
1
+ export * from './key.js';
2
+ export * from './storage.js';
3
+ export * from './update-state.js';
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./key.js"), exports);
18
+ __exportStar(require("./storage.js"), exports);
19
+ __exportStar(require("./update-state.js"), exports);
20
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/state/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAwB;AACxB,+CAA4B;AAC5B,oDAAiC","sourcesContent":["export * from './key.js'\nexport * from './storage.js'\nexport * from './update-state.js'\n"]}
@@ -0,0 +1,23 @@
1
+ import { Chat, MaybeAsync, User } from '@mtcute/client';
2
+ import { CallbackQueryContext, MessageContext } from '../context/index.js';
3
+ /**
4
+ * Function that determines how the state key is derived.
5
+ *
6
+ * The key is additionally prefixed with current scene, if any.
7
+ *
8
+ * @param msg Message or callback from which to derive the key
9
+ * @param scene Current scene UID, or `null` if none
10
+ */
11
+ export type StateKeyDelegate = (upd: MessageContext | CallbackQueryContext | User | Chat) => MaybeAsync<string | null>;
12
+ /**
13
+ * Default state key delegate.
14
+ *
15
+ * Derives key as follows:
16
+ * - If private chat, `msg.chat.id`
17
+ * - If group chat, `msg.chat.id + '_' + msg.sender.id`
18
+ * - If channel, `msg.chat.id`
19
+ * - If non-inline callback query:
20
+ * - If in private chat (i.e. `upd.chatType === 'user'`), `upd.user.id`
21
+ * - If in group/channel/supergroup (i.e. `upd.chatType !== 'user'`), `upd.chatId + '_' + upd.user.id`
22
+ */
23
+ export declare const defaultStateKeyDelegate: StateKeyDelegate;
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.defaultStateKeyDelegate = void 0;
4
+ const client_1 = require("@mtcute/client");
5
+ /**
6
+ * Default state key delegate.
7
+ *
8
+ * Derives key as follows:
9
+ * - If private chat, `msg.chat.id`
10
+ * - If group chat, `msg.chat.id + '_' + msg.sender.id`
11
+ * - If channel, `msg.chat.id`
12
+ * - If non-inline callback query:
13
+ * - If in private chat (i.e. `upd.chatType === 'user'`), `upd.user.id`
14
+ * - If in group/channel/supergroup (i.e. `upd.chatType !== 'user'`), `upd.chatId + '_' + upd.user.id`
15
+ */
16
+ const defaultStateKeyDelegate = (upd) => {
17
+ if ('type' in upd) {
18
+ // User | Chat
19
+ return String(upd.id);
20
+ }
21
+ if (upd._name === 'new_message') {
22
+ switch (upd.chat.chatType) {
23
+ case 'private':
24
+ case 'bot':
25
+ case 'channel':
26
+ return String(upd.chat.id);
27
+ case 'group':
28
+ case 'supergroup':
29
+ case 'gigagroup':
30
+ return `${upd.chat.id}_${upd.sender.id}`;
31
+ default:
32
+ (0, client_1.assertNever)(upd.chat.chatType);
33
+ }
34
+ }
35
+ if (upd._name === 'callback_query') {
36
+ if (upd.isInline)
37
+ return null;
38
+ if (upd.chatType === 'user')
39
+ return `${upd.user.id}`;
40
+ return `${upd.chatId}_${upd.user.id}`;
41
+ }
42
+ return null;
43
+ };
44
+ exports.defaultStateKeyDelegate = defaultStateKeyDelegate;
45
+ //# sourceMappingURL=key.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"key.js","sourceRoot":"","sources":["../../../src/state/key.ts"],"names":[],"mappings":";;;AAAA,2CAAoE;AAcpE;;;;;;;;;;GAUG;AACI,MAAM,uBAAuB,GAAqB,CAAC,GAAG,EAAiB,EAAE;IAC5E,IAAI,MAAM,IAAI,GAAG,EAAE;QACf,cAAc;QACd,OAAO,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC,CAAA;KACxB;IAED,IAAI,GAAG,CAAC,KAAK,KAAK,aAAa,EAAE;QAC7B,QAAQ,GAAG,CAAC,IAAI,CAAC,QAAQ,EAAE;YACvB,KAAK,SAAS,CAAC;YACf,KAAK,KAAK,CAAC;YACX,KAAK,SAAS;gBACV,OAAO,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,CAAA;YAC9B,KAAK,OAAO,CAAC;YACb,KAAK,YAAY,CAAC;YAClB,KAAK,WAAW;gBACZ,OAAO,GAAG,GAAG,CAAC,IAAI,CAAC,EAAE,IAAI,GAAG,CAAC,MAAM,CAAC,EAAE,EAAE,CAAA;YAC5C;gBACI,IAAA,oBAAW,EAAC,GAAG,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;SACrC;KACJ;IAED,IAAI,GAAG,CAAC,KAAK,KAAK,gBAAgB,EAAE;QAChC,IAAI,GAAG,CAAC,QAAQ;YAAE,OAAO,IAAI,CAAA;QAC7B,IAAI,GAAG,CAAC,QAAQ,KAAK,MAAM;YAAE,OAAO,GAAG,GAAG,CAAC,IAAI,CAAC,EAAE,EAAE,CAAA;QAEpD,OAAO,GAAG,GAAG,CAAC,MAAM,IAAI,GAAG,CAAC,IAAI,CAAC,EAAE,EAAE,CAAA;KACxC;IAED,OAAO,IAAI,CAAA;AACf,CAAC,CAAA;AA7BY,QAAA,uBAAuB,2BA6BnC","sourcesContent":["import { assertNever, Chat, MaybeAsync, User } from '@mtcute/client'\n\nimport { CallbackQueryContext, MessageContext } from '../context/index.js'\n\n/**\n * Function that determines how the state key is derived.\n *\n * The key is additionally prefixed with current scene, if any.\n *\n * @param msg Message or callback from which to derive the key\n * @param scene Current scene UID, or `null` if none\n */\nexport type StateKeyDelegate = (upd: MessageContext | CallbackQueryContext | User | Chat) => MaybeAsync<string | null>\n\n/**\n * Default state key delegate.\n *\n * Derives key as follows:\n * - If private chat, `msg.chat.id`\n * - If group chat, `msg.chat.id + '_' + msg.sender.id`\n * - If channel, `msg.chat.id`\n * - If non-inline callback query:\n * - If in private chat (i.e. `upd.chatType === 'user'`), `upd.user.id`\n * - If in group/channel/supergroup (i.e. `upd.chatType !== 'user'`), `upd.chatId + '_' + upd.user.id`\n */\nexport const defaultStateKeyDelegate: StateKeyDelegate = (upd): string | null => {\n if ('type' in upd) {\n // User | Chat\n return String(upd.id)\n }\n\n if (upd._name === 'new_message') {\n switch (upd.chat.chatType) {\n case 'private':\n case 'bot':\n case 'channel':\n return String(upd.chat.id)\n case 'group':\n case 'supergroup':\n case 'gigagroup':\n return `${upd.chat.id}_${upd.sender.id}`\n default:\n assertNever(upd.chat.chatType)\n }\n }\n\n if (upd._name === 'callback_query') {\n if (upd.isInline) return null\n if (upd.chatType === 'user') return `${upd.user.id}`\n\n return `${upd.chatId}_${upd.user.id}`\n }\n\n return null\n}\n"]}
@@ -0,0 +1,75 @@
1
+ import { MaybeAsync } from '@mtcute/client';
2
+ /**
3
+ * Interface for FSM storage for the dispatcher.
4
+ *
5
+ * All of the officially supported storages already implement
6
+ * this interface, so you can just re-use it.
7
+ *
8
+ * Current scene is a special case of a `string` state,
9
+ * Most of the time you can just store it the same way
10
+ * as normal state, prefixing with something like `$current_state_`
11
+ * (scene name can't start with `$`).
12
+ * Alternatively, you can store them as simple strings
13
+ */
14
+ export interface IStateStorage {
15
+ /**
16
+ * Retrieve state from the storage
17
+ *
18
+ * @param key Key of the state, as defined by {@link StateKeyDelegate}
19
+ */
20
+ getState(key: string): MaybeAsync<unknown>;
21
+ /**
22
+ * Save state to the storage
23
+ *
24
+ * @param key Key of the state, as defined by {@link StateKeyDelegate}
25
+ * @param state Object representing the state
26
+ * @param ttl TTL for the state, in seconds
27
+ */
28
+ setState(key: string, state: unknown, ttl?: number): MaybeAsync<void>;
29
+ /**
30
+ * Delete state from the storage
31
+ *
32
+ * @param key Key of the state, as defined by {@link StateKeyDelegate}
33
+ */
34
+ deleteState(key: string): MaybeAsync<void>;
35
+ /**
36
+ * Retrieve the current scene UID from the storage
37
+ *
38
+ * @param key Key of the state, as defined by {@link StateKeyDelegate}
39
+ */
40
+ getCurrentScene(key: string): MaybeAsync<string | null>;
41
+ /**
42
+ * Change current scene's UID from the storage
43
+ *
44
+ * @param key Key of the state, as defined by {@link StateKeyDelegate}
45
+ * @param scene New scene
46
+ * @param ttl TTL for the scene, in seconds
47
+ */
48
+ setCurrentScene(key: string, scene: string, ttl?: number): MaybeAsync<void>;
49
+ /**
50
+ * Delete current scene from the storage, effectively "exiting" to root.
51
+ *
52
+ * @param key Key of the state, as defined by {@link StateKeyDelegate}
53
+ */
54
+ deleteCurrentScene(key: string): MaybeAsync<void>;
55
+ /**
56
+ * Get information about a rate limit.
57
+ *
58
+ * It is recommended that you use sliding window or leaky bucket
59
+ * to implement rate limiting ([learn more](https://konghq.com/blog/how-to-design-a-scalable-rate-limiting-algorithm/)),
60
+ *
61
+ * @param key Key of the rate limit
62
+ * @param limit Maximum number of requests in `window`
63
+ * @param window Window size in seconds
64
+ * @returns Tuple containing the number of remaining and
65
+ * unix time in ms when the user can try again
66
+ */
67
+ getRateLimit(key: string, limit: number, window: number): MaybeAsync<[number, number]>;
68
+ /**
69
+ * Reset a rate limit.
70
+ *
71
+ * @param key Key of the rate limit
72
+ */
73
+ resetRateLimit(key: string): MaybeAsync<void>;
74
+ }
75
+ export declare function isCompatibleStorage(storage: unknown): storage is IStateStorage;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isCompatibleStorage = void 0;
4
+ function isCompatibleStorage(storage) {
5
+ return (typeof storage === 'object' &&
6
+ storage !== null &&
7
+ 'getState' in storage &&
8
+ 'setState' in storage &&
9
+ 'deleteState' in storage &&
10
+ 'getCurrentScene' in storage &&
11
+ 'setCurrentScene' in storage &&
12
+ 'deleteCurrentScene' in storage &&
13
+ 'getRateLimit' in storage &&
14
+ 'resetRateLimit' in storage);
15
+ }
16
+ exports.isCompatibleStorage = isCompatibleStorage;
17
+ //# sourceMappingURL=storage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"storage.js","sourceRoot":"","sources":["../../../src/state/storage.ts"],"names":[],"mappings":";;;AAmFA,SAAgB,mBAAmB,CAAC,OAAgB;IAChD,OAAO,CACH,OAAO,OAAO,KAAK,QAAQ;QAC3B,OAAO,KAAK,IAAI;QAChB,UAAU,IAAI,OAAO;QACrB,UAAU,IAAI,OAAO;QACrB,aAAa,IAAI,OAAO;QACxB,iBAAiB,IAAI,OAAO;QAC5B,iBAAiB,IAAI,OAAO;QAC5B,oBAAoB,IAAI,OAAO;QAC/B,cAAc,IAAI,OAAO;QACzB,gBAAgB,IAAI,OAAO,CAC9B,CAAA;AACL,CAAC;AAbD,kDAaC","sourcesContent":["import { MaybeAsync } from '@mtcute/client'\n\n/**\n * Interface for FSM storage for the dispatcher.\n *\n * All of the officially supported storages already implement\n * this interface, so you can just re-use it.\n *\n * Current scene is a special case of a `string` state,\n * Most of the time you can just store it the same way\n * as normal state, prefixing with something like `$current_state_`\n * (scene name can't start with `$`).\n * Alternatively, you can store them as simple strings\n */\nexport interface IStateStorage {\n /**\n * Retrieve state from the storage\n *\n * @param key Key of the state, as defined by {@link StateKeyDelegate}\n */\n getState(key: string): MaybeAsync<unknown>\n\n /**\n * Save state to the storage\n *\n * @param key Key of the state, as defined by {@link StateKeyDelegate}\n * @param state Object representing the state\n * @param ttl TTL for the state, in seconds\n */\n setState(key: string, state: unknown, ttl?: number): MaybeAsync<void>\n\n /**\n * Delete state from the storage\n *\n * @param key Key of the state, as defined by {@link StateKeyDelegate}\n */\n deleteState(key: string): MaybeAsync<void>\n\n /**\n * Retrieve the current scene UID from the storage\n *\n * @param key Key of the state, as defined by {@link StateKeyDelegate}\n */\n getCurrentScene(key: string): MaybeAsync<string | null>\n\n /**\n * Change current scene's UID from the storage\n *\n * @param key Key of the state, as defined by {@link StateKeyDelegate}\n * @param scene New scene\n * @param ttl TTL for the scene, in seconds\n */\n setCurrentScene(key: string, scene: string, ttl?: number): MaybeAsync<void>\n\n /**\n * Delete current scene from the storage, effectively \"exiting\" to root.\n *\n * @param key Key of the state, as defined by {@link StateKeyDelegate}\n */\n deleteCurrentScene(key: string): MaybeAsync<void>\n\n /**\n * Get information about a rate limit.\n *\n * It is recommended that you use sliding window or leaky bucket\n * to implement rate limiting ([learn more](https://konghq.com/blog/how-to-design-a-scalable-rate-limiting-algorithm/)),\n *\n * @param key Key of the rate limit\n * @param limit Maximum number of requests in `window`\n * @param window Window size in seconds\n * @returns Tuple containing the number of remaining and\n * unix time in ms when the user can try again\n */\n getRateLimit(key: string, limit: number, window: number): MaybeAsync<[number, number]>\n\n /**\n * Reset a rate limit.\n *\n * @param key Key of the rate limit\n */\n resetRateLimit(key: string): MaybeAsync<void>\n}\n\nexport function isCompatibleStorage(storage: unknown): storage is IStateStorage {\n return (\n typeof storage === 'object' &&\n storage !== null &&\n 'getState' in storage &&\n 'setState' in storage &&\n 'deleteState' in storage &&\n 'getCurrentScene' in storage &&\n 'setCurrentScene' in storage &&\n 'deleteCurrentScene' in storage &&\n 'getRateLimit' in storage &&\n 'resetRateLimit' in storage\n )\n}\n"]}
@@ -0,0 +1,151 @@
1
+ import { MtcuteError } from '@mtcute/client';
2
+ import type { Dispatcher } from '../dispatcher.js';
3
+ import { IStateStorage } from './storage.js';
4
+ /**
5
+ * Error thrown by `.rateLimit()`
6
+ */
7
+ export declare class RateLimitError extends MtcuteError {
8
+ readonly reset: number;
9
+ constructor(reset: number);
10
+ }
11
+ /**
12
+ * State of the current update.
13
+ *
14
+ * @template State Type that represents the state
15
+ * @template SceneName Possible scene names
16
+ */
17
+ export declare class UpdateState<State extends object> {
18
+ private _key;
19
+ private _localKey;
20
+ private _storage;
21
+ private _scene;
22
+ private _scoped?;
23
+ private _cached?;
24
+ private _localStorage;
25
+ private _localKeyBase;
26
+ constructor(storage: IStateStorage, key: string, scene: string | null, scoped?: boolean, customStorage?: IStateStorage, customKey?: string);
27
+ /** Name of the current scene */
28
+ get scene(): string | null;
29
+ private _updateLocalKey;
30
+ /**
31
+ * Retrieve the state from the storage, falling back to default
32
+ * if not found
33
+ *
34
+ * @param fallback Default state value
35
+ * @param force Whether to ignore cached state (def. `false`)
36
+ */
37
+ get(fallback: State | (() => State), force?: boolean): Promise<State>;
38
+ /**
39
+ * Retrieve the state from the storage, falling back to default
40
+ * if not found
41
+ *
42
+ * @param fallback Default state value
43
+ * @param force Whether to ignore cached state (def. `false`)
44
+ */
45
+ get(fallback?: State | (() => State), force?: boolean): Promise<State | null>;
46
+ /**
47
+ * Retrieve the state from the storage
48
+ *
49
+ * @param force Whether to ignore cached state (def. `false`)
50
+ */
51
+ get(force?: boolean): Promise<State | null>;
52
+ /**
53
+ * Set new state to the storage
54
+ *
55
+ * @param state New state
56
+ * @param ttl TTL for the new state (in seconds)
57
+ */
58
+ set(state: State, ttl?: number): Promise<void>;
59
+ /**
60
+ * Merge the given object to the current state.
61
+ *
62
+ * > **Note**: If the storage currently has no state,
63
+ * > then `fallback` must be provided.
64
+ *
65
+ * Basically a shorthand to calling `.get()`,
66
+ * modifying and then calling `.set()`
67
+ *
68
+ * @param state State to be merged
69
+ * @param fallback Default state
70
+ * @param ttl TTL for the new state (in seconds)
71
+ * @param forceLoad Whether to force load the old state from storage
72
+ */
73
+ merge(state: Partial<State>, params?: {
74
+ fallback?: State | (() => State);
75
+ ttl?: number;
76
+ forceLoad?: boolean;
77
+ }): Promise<State>;
78
+ /**
79
+ * Delete the state from the storage
80
+ */
81
+ delete(): Promise<void>;
82
+ /**
83
+ * Enter some scene
84
+ */
85
+ enter<SceneState extends object, Scene extends Dispatcher<SceneState>>(scene: Scene, params?: {
86
+ /**
87
+ * Initial state for the scene
88
+ *
89
+ * Note that this will only work if the scene uses the same key delegate as this state.
90
+ */
91
+ with?: SceneState;
92
+ /** TTL for the scene (in seconds) */
93
+ ttl?: number;
94
+ /**
95
+ * If currently in a scoped scene, whether to reset the state
96
+ *
97
+ * @default true
98
+ */
99
+ reset?: boolean;
100
+ }): Promise<void>;
101
+ /**
102
+ * Exit from current scene to the root
103
+ *
104
+ * @param reset
105
+ * Whether to reset scene state (only applicable in case this is a scoped scene)
106
+ */
107
+ exit(reset?: boolean): Promise<void>;
108
+ /**
109
+ * Rate limit some handler.
110
+ *
111
+ * When the rate limit exceeds, {@link RateLimitError} is thrown.
112
+ *
113
+ * This is a simple rate-limiting solution that uses
114
+ * the same key as the state. If you need something more
115
+ * sophisticated and/or customizable, you'll have to implement
116
+ * your own rate-limiter.
117
+ *
118
+ * > **Note**: `key` is used to prefix the local key
119
+ * > derived using the given key delegate.
120
+ *
121
+ * @param key Key of the rate limit
122
+ * @param limit Maximum number of requests in `window`
123
+ * @param window Window size in seconds
124
+ * @returns Tuple containing the number of remaining and
125
+ * unix time in ms when the user can try again
126
+ */
127
+ rateLimit(key: string, limit: number, window: number): Promise<[number, number]>;
128
+ /**
129
+ * Throttle some handler.
130
+ *
131
+ * When the rate limit exceeds, this function waits for it to reset.
132
+ *
133
+ * This is a simple wrapper over {@link rateLimit}, and follows the same logic.
134
+ *
135
+ * > **Note**: `key` is used to prefix the local key
136
+ * > derived using the given key delegate.
137
+ *
138
+ * @param key Key of the rate limit
139
+ * @param limit Maximum number of requests in `window`
140
+ * @param window Window size in seconds
141
+ * @returns Tuple containing the number of remaining and
142
+ * unix time in ms when the user can try again
143
+ */
144
+ throttle(key: string, limit: number, window: number): Promise<[number, number]>;
145
+ /**
146
+ * Reset the rate limit
147
+ *
148
+ * @param key Key of the rate limit
149
+ */
150
+ resetRateLimit(key: string): Promise<void>;
151
+ }