@mtkruto/node 0.0.994 → 0.0.996

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.
@@ -5,7 +5,7 @@ export declare const PUBLIC_KEYS: PublicKeys;
5
5
  export declare const VECTOR_CONSTRUCTOR = 481674261;
6
6
  export declare const INITIAL_DC: DC;
7
7
  export declare const LAYER = 161;
8
- export declare const APP_VERSION = "MTKruto 0.0.994";
8
+ export declare const APP_VERSION = "MTKruto 0.0.996";
9
9
  export declare const DEVICE_MODEL: string;
10
10
  export declare const LANG_CODE: string;
11
11
  export declare const LANG_PACK = "";
@@ -54,7 +54,7 @@ export const PUBLIC_KEYS = Object.freeze([
54
54
  export const VECTOR_CONSTRUCTOR = 0x1CB5C415;
55
55
  export const INITIAL_DC = "2-test";
56
56
  export const LAYER = 161;
57
- export const APP_VERSION = "MTKruto 0.0.994";
57
+ export const APP_VERSION = "MTKruto 0.0.996";
58
58
  // @ts-ignore: lib
59
59
  export const DEVICE_MODEL = typeof dntShim.Deno === "undefined" ? typeof navigator === "undefined" ? typeof process === "undefined" ? "Unknown" : process.platform + "-" + process.arch : navigator.userAgent.split(" ")[0] : dntShim.Deno.build.os + "-" + dntShim.Deno.build.arch;
60
60
  export const LANG_CODE = typeof navigator === "undefined" ? "en" : navigator.language.split("-")[0];
package/esm/5_client.d.ts CHANGED
@@ -1,3 +1,2 @@
1
1
  export * from "./client/2_client_plain.js";
2
- export { Composer } from "./client/4_composer.js";
3
- export * from "./client/5_client.js";
2
+ export * from "./client/4_client.js";
package/esm/5_client.js CHANGED
@@ -1,3 +1,2 @@
1
1
  export * from "./client/2_client_plain.js";
2
- export { Composer } from "./client/4_composer.js";
3
- export * from "./client/5_client.js";
2
+ export * from "./client/4_client.js";
@@ -211,11 +211,9 @@ export interface Update {
211
211
  inlineQuery?: InlineQuery;
212
212
  }
213
213
  export type NextFn = () => Promise<void>;
214
- export interface HandlerObj<U extends Partial<Update> = Partial<Update>> {
215
- handle(update: U, next: NextFn): MaybePromise<void>;
214
+ export interface Handler<U extends Partial<Update> = Partial<Update>> {
215
+ (update: U, next: NextFn): MaybePromise<void>;
216
216
  }
217
- export type HandlerFn<U extends Partial<Update> = Partial<Update>> = HandlerObj<U>["handle"];
218
- export type Handler<U extends Partial<Update> = Partial<Update>> = HandlerObj<U> | HandlerFn<U>;
219
217
  export type FilterUpdate<U extends Update, T extends keyof U, F extends keyof NonNullable<U[T]>> = With<U, T> & Pick<{
220
218
  [P in T]-?: With<NonNullable<U[T]>, F>;
221
219
  }, T>;
@@ -1,10 +1,11 @@
1
+ import { MaybePromise } from "../1_utilities.js";
1
2
  import { functions, ReadObject, types } from "../2_tl.js";
2
3
  import { Storage } from "../3_storage.js";
3
4
  import { DC } from "../3_transport.js";
4
5
  import { Message } from "../3_types.js";
5
6
  import { With } from "./0_utilities.js";
6
7
  import { ClientAbstract } from "./1_client_abstract.js";
7
- import { AnswerCallbackQueryParams, AuthorizeUserParams, ChatID, ClientParams, EditMessageParams, ForwardMessagesParams, Handler, ParseMode, SendMessagesParams, SendPollParams } from "./3_types.js";
8
+ import { AnswerCallbackQueryParams, AuthorizeUserParams, ChatID, ClientParams, EditMessageParams, FilterableUpdates, FilterUpdate, ForwardMessagesParams, Handler, ParseMode, SendMessagesParams, SendPollParams, Update } from "./3_types.js";
8
9
  export declare const getEntity: unique symbol;
9
10
  export declare const getStickerSetName: unique symbol;
10
11
  export declare const handleMigrationError: unique symbol;
@@ -186,8 +187,6 @@ export declare class Client extends ClientAbstract {
186
187
  */
187
188
  getMe(): Promise<import("../3_types.js").User>;
188
189
  private handleUpdate;
189
- private _handler;
190
- set handler(handler: Handler);
191
190
  /**
192
191
  * Answer a callback query. Bot-only.
193
192
  *
@@ -204,4 +203,10 @@ export declare class Client extends ClientAbstract {
204
203
  * @param options The poll's options.
205
204
  */
206
205
  sendPoll(chatId: ChatID, question: string, options: [string, string, ...string[]], params?: SendPollParams): Promise<With<Message, "poll">>;
206
+ private handle;
207
+ use(handler: Handler): void;
208
+ branch(predicate: (upd: Update) => MaybePromise<boolean>, trueHandler: Handler, falseHandler: Handler): void;
209
+ filter<D extends Update>(predicate: (ctx: Update) => ctx is D, handler: Handler<D>): void;
210
+ filter(predicate: (ctx: Update) => MaybePromise<boolean>, handler: Handler): void;
211
+ on<T extends keyof Update, F extends keyof NonNullable<Update[T]>>(filter: T extends FilterableUpdates ? T | [T, F, ...F[]] : T, handler: Handler<FilterUpdate<Update, T, F>>): void;
207
212
  }
@@ -10,7 +10,6 @@ import { checkPassword } from "./0_password.js";
10
10
  import { parseHtml } from "./0_html.js";
11
11
  import { ClientPlain } from "./2_client_plain.js";
12
12
  import { ClientAbstract } from "./1_client_abstract.js";
13
- import { call } from "./4_composer.js";
14
13
  const d = debug("Client");
15
14
  const dGap = debug("Client/recoverUpdateGap");
16
15
  const dGapC = debug("Client/recoverChannelUpdateGap");
@@ -213,13 +212,11 @@ export class Client extends ClientAbstract {
213
212
  writable: true,
214
213
  value: new Queue("processUpdates")
215
214
  });
216
- Object.defineProperty(this, "_handler", {
215
+ Object.defineProperty(this, "handle", {
217
216
  enumerable: true,
218
217
  configurable: true,
219
218
  writable: true,
220
- value: (_upd, next) => {
221
- next();
222
- }
219
+ value: resolve
223
220
  });
224
221
  this.parseMode = params?.parseMode ?? "none";
225
222
  this.appVersion = params?.appVersion ?? APP_VERSION;
@@ -232,7 +229,7 @@ export class Client extends ClientAbstract {
232
229
  this.autoStart = params?.autoStart ?? true;
233
230
  }
234
231
  propagateConnectionState(connectionState) {
235
- return this._handler({ connectionState }, resolve);
232
+ return this.handle({ connectionState }, resolve);
236
233
  }
237
234
  /**
238
235
  * Sets the DC and resets the auth key stored in the session provider
@@ -359,7 +356,7 @@ export class Client extends ClientAbstract {
359
356
  }
360
357
  async propagateAuthorizationState(authorized) {
361
358
  if (this.lastPropagatedAuthorizationState != authorized) {
362
- await this._handler({ authorizationState: { authorized } }, resolve);
359
+ await this.handle({ authorizationState: { authorized } }, resolve);
363
360
  this.lastPropagatedAuthorizationState = authorized;
364
361
  }
365
362
  }
@@ -1450,7 +1447,7 @@ export class Client extends ClientAbstract {
1450
1447
  update instanceof types.UpdateEditChannelMessage) {
1451
1448
  const key = update instanceof types.UpdateNewMessage || update instanceof types.UpdateNewChannelMessage ? "message" : "editedMessage";
1452
1449
  const message = await constructMessage(update.message, this[getEntity].bind(this), this.getMessage.bind(this), this[getStickerSetName].bind(this));
1453
- await this._handler({ [key]: message }, resolve);
1450
+ await this.handle({ [key]: message }, resolve);
1454
1451
  }
1455
1452
  if (update instanceof types.UpdateDeleteMessages) {
1456
1453
  const deletedMessages = new Array();
@@ -1465,7 +1462,7 @@ export class Client extends ClientAbstract {
1465
1462
  }
1466
1463
  }
1467
1464
  if (deletedMessages.length > 0) {
1468
- await this._handler({ deletedMessages: deletedMessages }, resolve);
1465
+ await this.handle({ deletedMessages: deletedMessages }, resolve);
1469
1466
  }
1470
1467
  }
1471
1468
  else if (update instanceof types.UpdateDeleteChannelMessages) {
@@ -1479,19 +1476,16 @@ export class Client extends ClientAbstract {
1479
1476
  await this.storage.setMessage(chatId, messageId, null);
1480
1477
  }
1481
1478
  if (deletedMessages.length > 0) {
1482
- await this._handler({ deletedMessages: deletedMessages }, resolve);
1479
+ await this.handle({ deletedMessages: deletedMessages }, resolve);
1483
1480
  }
1484
1481
  }
1485
1482
  if (update instanceof types.UpdateBotCallbackQuery || update instanceof types.UpdateInlineBotCallbackQuery) {
1486
- await this._handler({ callbackQuery: await constructCallbackQuery(update, this[getEntity].bind(this), this[getMessageWithReply].bind(this)) }, resolve);
1483
+ await this.handle({ callbackQuery: await constructCallbackQuery(update, this[getEntity].bind(this), this[getMessageWithReply].bind(this)) }, resolve);
1487
1484
  }
1488
1485
  else if (update instanceof types.UpdateBotInlineQuery) {
1489
- await this._handler({ inlineQuery: await constructInlineQuery(update, this[getEntity].bind(this)) }, resolve);
1486
+ await this.handle({ inlineQuery: await constructInlineQuery(update, this[getEntity].bind(this)) }, resolve);
1490
1487
  }
1491
1488
  }
1492
- set handler(handler) {
1493
- this._handler = call(handler);
1494
- }
1495
1489
  /**
1496
1490
  * Answer a callback query. Bot-only.
1497
1491
  *
@@ -1589,4 +1583,50 @@ export class Client extends ClientAbstract {
1589
1583
  const message = await this.updatesToMessages(chatId, result).then((v) => v[0]);
1590
1584
  return Client.assertMsgHas(message, "poll");
1591
1585
  }
1586
+ use(handler) {
1587
+ const handle = this.handle;
1588
+ this.handle = async (upd, next) => {
1589
+ let called = false;
1590
+ await handle(upd, async () => {
1591
+ if (called)
1592
+ return;
1593
+ called = true;
1594
+ await handler(upd, next);
1595
+ });
1596
+ };
1597
+ }
1598
+ branch(predicate, trueHandler, falseHandler) {
1599
+ this.use(async (upd, next) => {
1600
+ if (await predicate(upd)) {
1601
+ await trueHandler(upd, next);
1602
+ }
1603
+ else {
1604
+ await falseHandler(upd, next);
1605
+ }
1606
+ });
1607
+ }
1608
+ filter(predicate, handler) {
1609
+ this.branch(predicate, handler, resolve);
1610
+ }
1611
+ on(filter, handler) {
1612
+ const type = typeof filter === "string" ? filter : filter[0];
1613
+ const keys = Array.isArray(filter) ? filter.slice(1) : [];
1614
+ this.filter((update) => {
1615
+ if (type in update) {
1616
+ if (keys.length > 0) {
1617
+ for (const key of keys) {
1618
+ // deno-lint-ignore ban-ts-comment
1619
+ // @ts-ignore
1620
+ if (!(key in update[type])) {
1621
+ return false;
1622
+ }
1623
+ }
1624
+ }
1625
+ return true;
1626
+ }
1627
+ else {
1628
+ return false;
1629
+ }
1630
+ }, handler);
1631
+ }
1592
1632
  }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "module": "./esm/mod.js",
3
3
  "main": "./script/mod.js",
4
4
  "name": "@mtkruto/node",
5
- "version": "0.0.994",
5
+ "version": "0.0.996",
6
6
  "description": "MTKruto for Node.js",
7
7
  "author": "Roj <rojvv@icloud.com>",
8
8
  "license": "LGPL-3.0-or-later",
@@ -5,7 +5,7 @@ export declare const PUBLIC_KEYS: PublicKeys;
5
5
  export declare const VECTOR_CONSTRUCTOR = 481674261;
6
6
  export declare const INITIAL_DC: DC;
7
7
  export declare const LAYER = 161;
8
- export declare const APP_VERSION = "MTKruto 0.0.994";
8
+ export declare const APP_VERSION = "MTKruto 0.0.996";
9
9
  export declare const DEVICE_MODEL: string;
10
10
  export declare const LANG_CODE: string;
11
11
  export declare const LANG_PACK = "";
@@ -80,7 +80,7 @@ exports.PUBLIC_KEYS = Object.freeze([
80
80
  exports.VECTOR_CONSTRUCTOR = 0x1CB5C415;
81
81
  exports.INITIAL_DC = "2-test";
82
82
  exports.LAYER = 161;
83
- exports.APP_VERSION = "MTKruto 0.0.994";
83
+ exports.APP_VERSION = "MTKruto 0.0.996";
84
84
  // @ts-ignore: lib
85
85
  exports.DEVICE_MODEL = typeof dntShim.Deno === "undefined" ? typeof navigator === "undefined" ? typeof process === "undefined" ? "Unknown" : process.platform + "-" + process.arch : navigator.userAgent.split(" ")[0] : dntShim.Deno.build.os + "-" + dntShim.Deno.build.arch;
86
86
  exports.LANG_CODE = typeof navigator === "undefined" ? "en" : navigator.language.split("-")[0];
@@ -1,3 +1,2 @@
1
1
  export * from "./client/2_client_plain.js";
2
- export { Composer } from "./client/4_composer.js";
3
- export * from "./client/5_client.js";
2
+ export * from "./client/4_client.js";
@@ -14,8 +14,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.Composer = void 0;
18
17
  __exportStar(require("./client/2_client_plain.js"), exports);
19
- var _4_composer_js_1 = require("./client/4_composer.js");
20
- Object.defineProperty(exports, "Composer", { enumerable: true, get: function () { return _4_composer_js_1.Composer; } });
21
- __exportStar(require("./client/5_client.js"), exports);
18
+ __exportStar(require("./client/4_client.js"), exports);
@@ -211,11 +211,9 @@ export interface Update {
211
211
  inlineQuery?: InlineQuery;
212
212
  }
213
213
  export type NextFn = () => Promise<void>;
214
- export interface HandlerObj<U extends Partial<Update> = Partial<Update>> {
215
- handle(update: U, next: NextFn): MaybePromise<void>;
214
+ export interface Handler<U extends Partial<Update> = Partial<Update>> {
215
+ (update: U, next: NextFn): MaybePromise<void>;
216
216
  }
217
- export type HandlerFn<U extends Partial<Update> = Partial<Update>> = HandlerObj<U>["handle"];
218
- export type Handler<U extends Partial<Update> = Partial<Update>> = HandlerObj<U> | HandlerFn<U>;
219
217
  export type FilterUpdate<U extends Update, T extends keyof U, F extends keyof NonNullable<U[T]>> = With<U, T> & Pick<{
220
218
  [P in T]-?: With<NonNullable<U[T]>, F>;
221
219
  }, T>;
@@ -1,10 +1,11 @@
1
+ import { MaybePromise } from "../1_utilities.js";
1
2
  import { functions, ReadObject, types } from "../2_tl.js";
2
3
  import { Storage } from "../3_storage.js";
3
4
  import { DC } from "../3_transport.js";
4
5
  import { Message } from "../3_types.js";
5
6
  import { With } from "./0_utilities.js";
6
7
  import { ClientAbstract } from "./1_client_abstract.js";
7
- import { AnswerCallbackQueryParams, AuthorizeUserParams, ChatID, ClientParams, EditMessageParams, ForwardMessagesParams, Handler, ParseMode, SendMessagesParams, SendPollParams } from "./3_types.js";
8
+ import { AnswerCallbackQueryParams, AuthorizeUserParams, ChatID, ClientParams, EditMessageParams, FilterableUpdates, FilterUpdate, ForwardMessagesParams, Handler, ParseMode, SendMessagesParams, SendPollParams, Update } from "./3_types.js";
8
9
  export declare const getEntity: unique symbol;
9
10
  export declare const getStickerSetName: unique symbol;
10
11
  export declare const handleMigrationError: unique symbol;
@@ -186,8 +187,6 @@ export declare class Client extends ClientAbstract {
186
187
  */
187
188
  getMe(): Promise<import("../3_types.js").User>;
188
189
  private handleUpdate;
189
- private _handler;
190
- set handler(handler: Handler);
191
190
  /**
192
191
  * Answer a callback query. Bot-only.
193
192
  *
@@ -204,4 +203,10 @@ export declare class Client extends ClientAbstract {
204
203
  * @param options The poll's options.
205
204
  */
206
205
  sendPoll(chatId: ChatID, question: string, options: [string, string, ...string[]], params?: SendPollParams): Promise<With<Message, "poll">>;
206
+ private handle;
207
+ use(handler: Handler): void;
208
+ branch(predicate: (upd: Update) => MaybePromise<boolean>, trueHandler: Handler, falseHandler: Handler): void;
209
+ filter<D extends Update>(predicate: (ctx: Update) => ctx is D, handler: Handler<D>): void;
210
+ filter(predicate: (ctx: Update) => MaybePromise<boolean>, handler: Handler): void;
211
+ on<T extends keyof Update, F extends keyof NonNullable<Update[T]>>(filter: T extends FilterableUpdates ? T | [T, F, ...F[]] : T, handler: Handler<FilterUpdate<Update, T, F>>): void;
207
212
  }
@@ -13,7 +13,6 @@ const _0_password_js_1 = require("./0_password.js");
13
13
  const _0_html_js_1 = require("./0_html.js");
14
14
  const _2_client_plain_js_1 = require("./2_client_plain.js");
15
15
  const _1_client_abstract_js_1 = require("./1_client_abstract.js");
16
- const _4_composer_js_1 = require("./4_composer.js");
17
16
  const d = (0, _0_deps_js_1.debug)("Client");
18
17
  const dGap = (0, _0_deps_js_1.debug)("Client/recoverUpdateGap");
19
18
  const dGapC = (0, _0_deps_js_1.debug)("Client/recoverChannelUpdateGap");
@@ -216,13 +215,11 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
216
215
  writable: true,
217
216
  value: new _1_utilities_js_1.Queue("processUpdates")
218
217
  });
219
- Object.defineProperty(this, "_handler", {
218
+ Object.defineProperty(this, "handle", {
220
219
  enumerable: true,
221
220
  configurable: true,
222
221
  writable: true,
223
- value: (_upd, next) => {
224
- next();
225
- }
222
+ value: _0_utilities_js_1.resolve
226
223
  });
227
224
  this.parseMode = params?.parseMode ?? "none";
228
225
  this.appVersion = params?.appVersion ?? _4_constants_js_1.APP_VERSION;
@@ -235,7 +232,7 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
235
232
  this.autoStart = params?.autoStart ?? true;
236
233
  }
237
234
  propagateConnectionState(connectionState) {
238
- return this._handler({ connectionState }, _0_utilities_js_1.resolve);
235
+ return this.handle({ connectionState }, _0_utilities_js_1.resolve);
239
236
  }
240
237
  /**
241
238
  * Sets the DC and resets the auth key stored in the session provider
@@ -362,7 +359,7 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
362
359
  }
363
360
  async propagateAuthorizationState(authorized) {
364
361
  if (this.lastPropagatedAuthorizationState != authorized) {
365
- await this._handler({ authorizationState: { authorized } }, _0_utilities_js_1.resolve);
362
+ await this.handle({ authorizationState: { authorized } }, _0_utilities_js_1.resolve);
366
363
  this.lastPropagatedAuthorizationState = authorized;
367
364
  }
368
365
  }
@@ -1453,7 +1450,7 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
1453
1450
  update instanceof _2_tl_js_1.types.UpdateEditChannelMessage) {
1454
1451
  const key = update instanceof _2_tl_js_1.types.UpdateNewMessage || update instanceof _2_tl_js_1.types.UpdateNewChannelMessage ? "message" : "editedMessage";
1455
1452
  const message = await (0, _3_types_js_1.constructMessage)(update.message, this[exports.getEntity].bind(this), this.getMessage.bind(this), this[exports.getStickerSetName].bind(this));
1456
- await this._handler({ [key]: message }, _0_utilities_js_1.resolve);
1453
+ await this.handle({ [key]: message }, _0_utilities_js_1.resolve);
1457
1454
  }
1458
1455
  if (update instanceof _2_tl_js_1.types.UpdateDeleteMessages) {
1459
1456
  const deletedMessages = new Array();
@@ -1468,7 +1465,7 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
1468
1465
  }
1469
1466
  }
1470
1467
  if (deletedMessages.length > 0) {
1471
- await this._handler({ deletedMessages: deletedMessages }, _0_utilities_js_1.resolve);
1468
+ await this.handle({ deletedMessages: deletedMessages }, _0_utilities_js_1.resolve);
1472
1469
  }
1473
1470
  }
1474
1471
  else if (update instanceof _2_tl_js_1.types.UpdateDeleteChannelMessages) {
@@ -1482,19 +1479,16 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
1482
1479
  await this.storage.setMessage(chatId, messageId, null);
1483
1480
  }
1484
1481
  if (deletedMessages.length > 0) {
1485
- await this._handler({ deletedMessages: deletedMessages }, _0_utilities_js_1.resolve);
1482
+ await this.handle({ deletedMessages: deletedMessages }, _0_utilities_js_1.resolve);
1486
1483
  }
1487
1484
  }
1488
1485
  if (update instanceof _2_tl_js_1.types.UpdateBotCallbackQuery || update instanceof _2_tl_js_1.types.UpdateInlineBotCallbackQuery) {
1489
- await this._handler({ callbackQuery: await (0, _3_types_js_1.constructCallbackQuery)(update, this[exports.getEntity].bind(this), this[exports.getMessageWithReply].bind(this)) }, _0_utilities_js_1.resolve);
1486
+ await this.handle({ callbackQuery: await (0, _3_types_js_1.constructCallbackQuery)(update, this[exports.getEntity].bind(this), this[exports.getMessageWithReply].bind(this)) }, _0_utilities_js_1.resolve);
1490
1487
  }
1491
1488
  else if (update instanceof _2_tl_js_1.types.UpdateBotInlineQuery) {
1492
- await this._handler({ inlineQuery: await (0, _3_types_js_1.constructInlineQuery)(update, this[exports.getEntity].bind(this)) }, _0_utilities_js_1.resolve);
1489
+ await this.handle({ inlineQuery: await (0, _3_types_js_1.constructInlineQuery)(update, this[exports.getEntity].bind(this)) }, _0_utilities_js_1.resolve);
1493
1490
  }
1494
1491
  }
1495
- set handler(handler) {
1496
- this._handler = (0, _4_composer_js_1.call)(handler);
1497
- }
1498
1492
  /**
1499
1493
  * Answer a callback query. Bot-only.
1500
1494
  *
@@ -1592,5 +1586,51 @@ class Client extends _1_client_abstract_js_1.ClientAbstract {
1592
1586
  const message = await this.updatesToMessages(chatId, result).then((v) => v[0]);
1593
1587
  return Client.assertMsgHas(message, "poll");
1594
1588
  }
1589
+ use(handler) {
1590
+ const handle = this.handle;
1591
+ this.handle = async (upd, next) => {
1592
+ let called = false;
1593
+ await handle(upd, async () => {
1594
+ if (called)
1595
+ return;
1596
+ called = true;
1597
+ await handler(upd, next);
1598
+ });
1599
+ };
1600
+ }
1601
+ branch(predicate, trueHandler, falseHandler) {
1602
+ this.use(async (upd, next) => {
1603
+ if (await predicate(upd)) {
1604
+ await trueHandler(upd, next);
1605
+ }
1606
+ else {
1607
+ await falseHandler(upd, next);
1608
+ }
1609
+ });
1610
+ }
1611
+ filter(predicate, handler) {
1612
+ this.branch(predicate, handler, _0_utilities_js_1.resolve);
1613
+ }
1614
+ on(filter, handler) {
1615
+ const type = typeof filter === "string" ? filter : filter[0];
1616
+ const keys = Array.isArray(filter) ? filter.slice(1) : [];
1617
+ this.filter((update) => {
1618
+ if (type in update) {
1619
+ if (keys.length > 0) {
1620
+ for (const key of keys) {
1621
+ // deno-lint-ignore ban-ts-comment
1622
+ // @ts-ignore
1623
+ if (!(key in update[type])) {
1624
+ return false;
1625
+ }
1626
+ }
1627
+ }
1628
+ return true;
1629
+ }
1630
+ else {
1631
+ return false;
1632
+ }
1633
+ }, handler);
1634
+ }
1595
1635
  }
1596
1636
  exports.Client = Client;
@@ -1,13 +0,0 @@
1
- import { MaybePromise } from "../1_utilities.js";
2
- import { FilterableUpdates, FilterUpdate, Handler, HandlerFn, HandlerObj, Update } from "./3_types.js";
3
- export declare function call(handler: Handler): HandlerFn;
4
- export declare class Composer<U extends Update = Update> implements HandlerObj<U> {
5
- constructor(...handlers: Handler<U>[]);
6
- get handle(): HandlerFn;
7
- private _handle;
8
- use(...handlers: Handler<U>[]): this;
9
- branch(predicate: (upd: U) => MaybePromise<boolean>, trueHandler: Handler<U>, falseHandler: Handler<U>): this;
10
- filter<D extends U>(predicate: (ctx: U) => ctx is D, ...middleware: Handler<D>[]): Composer<D>;
11
- filter(predicate: (ctx: U) => MaybePromise<boolean>, ...middleware: Handler<U>[]): Composer<U>;
12
- on<T extends keyof U, F extends keyof NonNullable<U[T]>>(filter: T extends FilterableUpdates ? T | [T, F, ...F[]] : T, ...handlers: Handler<FilterUpdate<U, T, F>>[]): Composer<FilterUpdate<U, T, F>>;
13
- }
@@ -1,80 +0,0 @@
1
- import { resolve } from "./0_utilities.js";
2
- export function call(handler) {
3
- if ("handle" in handler) {
4
- return handler.handle;
5
- }
6
- else {
7
- return handler;
8
- }
9
- }
10
- function concat(what, with_) {
11
- return async (upd, next) => {
12
- let called = false;
13
- await call(what)(upd, async () => {
14
- if (called)
15
- return;
16
- called = true;
17
- await call(with_)(upd, next);
18
- });
19
- };
20
- }
21
- function reduce(...handlers) {
22
- return handlers.reduce((a, b) => concat(a, b));
23
- }
24
- export class Composer {
25
- constructor(...handlers) {
26
- Object.defineProperty(this, "_handle", {
27
- enumerable: true,
28
- configurable: true,
29
- writable: true,
30
- value: resolve
31
- });
32
- this.use(...handlers);
33
- }
34
- get handle() {
35
- return this._handle;
36
- }
37
- use(...handlers) {
38
- const handle = this.handle;
39
- this._handle = concat(handle, reduce(...handlers));
40
- return this;
41
- }
42
- branch(predicate, trueHandler, falseHandler) {
43
- return this.use(async (upd, next) => {
44
- if (await predicate(upd)) {
45
- await call(trueHandler)(upd, next);
46
- }
47
- else {
48
- await call(falseHandler)(upd, next);
49
- }
50
- });
51
- }
52
- filter(predicate, ...middleware) {
53
- const composer = new Composer(...middleware);
54
- this.branch(predicate, composer, resolve);
55
- return composer;
56
- }
57
- on(filter, ...handlers) {
58
- // deno-lint-ignore ban-ts-comment
59
- // @ts-ignore
60
- const type = typeof filter === "string" ? filter : filter[0];
61
- const keys = Array.isArray(filter) ? filter.slice(1) : [];
62
- return this.filter((update) => {
63
- if (type in update) {
64
- if (keys.length > 0) {
65
- for (const key of keys) {
66
- // deno-lint-ignore ban-ts-comment
67
- // @ts-ignore
68
- if (!(key in update[type])) {
69
- return false;
70
- }
71
- }
72
- }
73
- return true;
74
- }
75
- else {
76
- return false;
77
- }
78
- }, ...handlers);
79
- }
80
- }
@@ -1,13 +0,0 @@
1
- import { MaybePromise } from "../1_utilities.js";
2
- import { FilterableUpdates, FilterUpdate, Handler, HandlerFn, HandlerObj, Update } from "./3_types.js";
3
- export declare function call(handler: Handler): HandlerFn;
4
- export declare class Composer<U extends Update = Update> implements HandlerObj<U> {
5
- constructor(...handlers: Handler<U>[]);
6
- get handle(): HandlerFn;
7
- private _handle;
8
- use(...handlers: Handler<U>[]): this;
9
- branch(predicate: (upd: U) => MaybePromise<boolean>, trueHandler: Handler<U>, falseHandler: Handler<U>): this;
10
- filter<D extends U>(predicate: (ctx: U) => ctx is D, ...middleware: Handler<D>[]): Composer<D>;
11
- filter(predicate: (ctx: U) => MaybePromise<boolean>, ...middleware: Handler<U>[]): Composer<U>;
12
- on<T extends keyof U, F extends keyof NonNullable<U[T]>>(filter: T extends FilterableUpdates ? T | [T, F, ...F[]] : T, ...handlers: Handler<FilterUpdate<U, T, F>>[]): Composer<FilterUpdate<U, T, F>>;
13
- }
@@ -1,85 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Composer = exports.call = void 0;
4
- const _0_utilities_js_1 = require("./0_utilities.js");
5
- function call(handler) {
6
- if ("handle" in handler) {
7
- return handler.handle;
8
- }
9
- else {
10
- return handler;
11
- }
12
- }
13
- exports.call = call;
14
- function concat(what, with_) {
15
- return async (upd, next) => {
16
- let called = false;
17
- await call(what)(upd, async () => {
18
- if (called)
19
- return;
20
- called = true;
21
- await call(with_)(upd, next);
22
- });
23
- };
24
- }
25
- function reduce(...handlers) {
26
- return handlers.reduce((a, b) => concat(a, b));
27
- }
28
- class Composer {
29
- constructor(...handlers) {
30
- Object.defineProperty(this, "_handle", {
31
- enumerable: true,
32
- configurable: true,
33
- writable: true,
34
- value: _0_utilities_js_1.resolve
35
- });
36
- this.use(...handlers);
37
- }
38
- get handle() {
39
- return this._handle;
40
- }
41
- use(...handlers) {
42
- const handle = this.handle;
43
- this._handle = concat(handle, reduce(...handlers));
44
- return this;
45
- }
46
- branch(predicate, trueHandler, falseHandler) {
47
- return this.use(async (upd, next) => {
48
- if (await predicate(upd)) {
49
- await call(trueHandler)(upd, next);
50
- }
51
- else {
52
- await call(falseHandler)(upd, next);
53
- }
54
- });
55
- }
56
- filter(predicate, ...middleware) {
57
- const composer = new Composer(...middleware);
58
- this.branch(predicate, composer, _0_utilities_js_1.resolve);
59
- return composer;
60
- }
61
- on(filter, ...handlers) {
62
- // deno-lint-ignore ban-ts-comment
63
- // @ts-ignore
64
- const type = typeof filter === "string" ? filter : filter[0];
65
- const keys = Array.isArray(filter) ? filter.slice(1) : [];
66
- return this.filter((update) => {
67
- if (type in update) {
68
- if (keys.length > 0) {
69
- for (const key of keys) {
70
- // deno-lint-ignore ban-ts-comment
71
- // @ts-ignore
72
- if (!(key in update[type])) {
73
- return false;
74
- }
75
- }
76
- }
77
- return true;
78
- }
79
- else {
80
- return false;
81
- }
82
- }, ...handlers);
83
- }
84
- }
85
- exports.Composer = Composer;