From 8a885b1db92160fed02c87be25b3064f7cb057da Mon Sep 17 00:00:00 2001 From: Simone Romano Date: Sun, 13 Oct 2019 14:04:10 +0200 Subject: [PATCH] Add support for async modifications --- lib/methods.js | 4 ++-- lib/telebot.js | 18 +++++++++--------- lib/updates.js | 6 +++--- test/index.js | 12 ++++++++---- 4 files changed, 22 insertions(+), 18 deletions(-) diff --git a/lib/methods.js b/lib/methods.js index cd54318..3b17bb5 100644 --- a/lib/methods.js +++ b/lib/methods.js @@ -492,9 +492,9 @@ function editObject(obj, form) { return form; } -function sendFile(type, file, opt = {}, methodForm = {}, methodUrl) { +async function sendFile(type, file, opt = {}, methodForm = {}, methodUrl) { - const form = this.properties(methodForm, opt); + const form = await this.properties(methodForm, opt); const defaultName = `file.${ DEFAULT_FILE_EXTENSIONS[type] }`; const url = methodUrl ? methodUrl : 'send' + type.charAt(0).toUpperCase() + type.slice(1); diff --git a/lib/telebot.js b/lib/telebot.js index 451cf5f..c0633a1 100644 --- a/lib/telebot.js +++ b/lib/telebot.js @@ -252,10 +252,10 @@ class TeleBot { if (!updateList.length) return promise; // We have updates - return this.event('update', updateList).then(eventProps => { + return this.event('update', updateList).then(async eventProps => { // Run update list modifiers - mod = this.modRun('updateList', { + mod = await this.modRun('updateList', { updateList, props: extendProps(props, eventProps) }); @@ -270,7 +270,7 @@ class TeleBot { if (this.updateId < nextId) this.updateId = nextId; // Run update modifiers - mod = this.modRun('update', {update, props}); + mod = await this.modRun('update', {update, props}); update = mod.update; props = mod.props; @@ -340,10 +340,10 @@ class TeleBot { return fn; } - modRun(name, data) { + async modRun(name, data) { const list = this.modList[name]; if (!list || !list.length) return data; - for (let fn of list) data = fn.call(this, data); + for (let fn of list) data = await fn.call(this, data); return data; } @@ -481,7 +481,7 @@ class TeleBot { /* Process global properties */ - properties(form = {}, opt = {}) { + async properties(form = {}, opt = {}) { const parseMode = opt.parseMode || opt.parse; const replyToMessage = opt.replyToMessage || opt.reply; @@ -508,7 +508,7 @@ class TeleBot { } } - return (this.modRun('property', {form, options: opt})).form; + return (await this.modRun('property', {form, options: opt})).form; } @@ -545,14 +545,14 @@ class TeleBot { let optFn = method.options; // Create method - this.prototype[id] = this.prototype[name] = function () { + this.prototype[id] = this.prototype[name] = async function () { this.event([id, name], arguments); let form = {}, args = [].slice.call(arguments); let options = args[args.length - 1], fnOptions = {}; if (typeof options !== 'object') options = {}; if (argFn) form = argFn.apply(this, args); if (optFn) fnOptions = optFn.apply(this, [].concat(form, options)); - form = this.properties(form, Object.assign(options, fnOptions)); + form = await this.properties(form, Object.assign(options, fnOptions)); return this.request(`/${id}`, form).then(method.then || (re => re && re.result)); }; diff --git a/lib/updates.js b/lib/updates.js index 98b0051..6653c19 100644 --- a/lib/updates.js +++ b/lib/updates.js @@ -33,13 +33,13 @@ const SHORTCUTS = { const updateFunctions = { // Message - message(update, props) { + async message(update, props) { // Any event status: ['*', '/*'] let anyEventFlags = [false, false]; let promise = Promise.resolve(); - let mod = this.modRun('message', {message: update, props}); + let mod = await this.modRun('message', {message: update, props}); update = mod.message; props = mod.props; @@ -57,7 +57,7 @@ const updateFunctions = { props.type = type; // Run message type mod - mod = this.modRun(type, {message: update, props}); + mod = await this.modRun(type, {message: update, props}); update = mod.message; props = mod.props; diff --git a/test/index.js b/test/index.js index 7165a16..b31cc46 100644 --- a/test/index.js +++ b/test/index.js @@ -100,7 +100,7 @@ test('events', t => { }); -test('mods', t => { +test('mods', async t => { const defModCount = bot.buildInPlugins.length + bot.usePlugins.length; @@ -109,6 +109,7 @@ test('mods', t => { } var delMe = x => x; + var delMeAsync = async x => x; t.is(all(bot.modList), defModCount); @@ -117,20 +118,23 @@ test('mods', t => { bot.mod('custom', x => ++x); bot.mod('custom', delMe); bot.mod('custom', x => ++x); + bot.mod('custom', delMeAsync); + bot.mod('custom', async x => ++x); // Count - t.is(len('custom'), 4); + t.is(len('custom'), 6); t.is(all(bot.modList), 1 + defModCount); // Run - t.is(bot.modRun('custom', 5), 8); + t.is(await bot.modRun('custom', 5), 9); // Remove t.true(bot.removeMod('custom', delMe)); + t.true(bot.removeMod('custom', delMeAsync)); t.false(bot.removeMod('custom')); t.false(bot.removeMod('not_found')); - t.is(len('custom'), 3); + t.is(len('custom'), 4); t.is(all(bot.modList), 1 + defModCount); });