2
0
mirror of https://github.com/yagop/node-telegram-bot-api synced 2025-08-28 04:47:38 +00:00

src/telegram: Deprecate auto-enabling Promise cancellation

Side-effects:

  src/telegram: Allow providing custom Promise constructor

References:

  * BR: https://github.com/yagop/node-telegram-bot-api/issues/319
This commit is contained in:
GochoMugo 2017-08-25 18:39:13 +03:00
parent 28160c70cd
commit ef48af49f5
No known key found for this signature in database
GPG Key ID: 7B6A01CB57AA39E4
3 changed files with 126 additions and 67 deletions

View File

@ -12,6 +12,7 @@ TelegramBot
* [TelegramBot](#TelegramBot)
* [new TelegramBot(token, [options])](#new_TelegramBot_new)
* _instance_
* [.startPolling([options])](#TelegramBot+startPolling) ⇒ <code>Promise</code>
* ~~[.initPolling([options])](#TelegramBot+initPolling) ⇒ <code>Promise</code>~~
* [.stopPolling()](#TelegramBot+stopPolling) ⇒ <code>Promise</code>
@ -74,6 +75,8 @@ TelegramBot
* [.sendInvoice(chatId, title, description, payload, providerToken, startParameter, currency, prices, [options])](#TelegramBot+sendInvoice) ⇒ <code>Promise</code>
* [.answerShippingQuery(shippingQueryId, ok, [options])](#TelegramBot+answerShippingQuery) ⇒ <code>Promise</code>
* [.answerPreCheckoutQuery(preCheckoutQueryId, ok, [options])](#TelegramBot+answerPreCheckoutQuery) ⇒ <code>Promise</code>
* _static_
* [.Promise](#TelegramBot.Promise)
<a name="new_TelegramBot_new"></a>
@ -974,6 +977,23 @@ Use this method to confirm shipping of a product.
| ok | <code>Boolean</code> | Specify if every order details are ok |
| [options] | <code>Object</code> | Additional Telegram query options |
<a name="TelegramBot.Promise"></a>
### TelegramBot.Promise
Change Promise library used internally, for all existing and new
instances.
**Kind**: static property of <code>[TelegramBot](#TelegramBot)</code>
| Param | Type |
| --- | --- |
| customPromise | <code>function</code> |
**Example**
```js
const TelegramBot = require('node-telegram-bot-api');
TelegramBot.Promise = myPromise;
```
* * *

View File

@ -7,7 +7,6 @@ const TelegramBotPolling = require('./telegramPolling');
const debug = require('debug')('node-telegram-bot-api');
const EventEmitter = require('eventemitter3');
const fileType = require('file-type');
const Promise = require('bluebird');
const request = require('request-promise');
const streamedRequest = require('request');
const qs = require('querystring');
@ -18,6 +17,7 @@ const URL = require('url');
const fs = require('fs');
const pump = require('pump');
const deprecate = require('depd')('node-telegram-bot-api');
let Promise = require('bluebird');
const _messageTypes = [
'audio',
@ -49,10 +49,27 @@ const _deprecatedMessageTypes = [
'new_chat_participant', 'left_chat_participant'
];
// enable cancellation
Promise.config({
// Enable Promise cancellation.
try {
const msg =
'Automatic enabling of cancellation of promises is deprecated.\n' +
'In the future, you will have to enable it yourself.\n' +
'See https://github.com/yagop/node-telegram-bot-api/issues/319.';
deprecate(msg);
Promise.config({
cancellation: true,
});
});
} catch (ex) {
/* eslint-disable no-console */
const msg =
'error: Enabling Promise cancellation failed.\n' +
' Temporary fix is to load/require this library as early as possible before using any Promises.';
console.error(msg);
throw ex;
/* eslint-enable no-console */
}
class TelegramBot extends EventEmitter {
@ -64,6 +81,19 @@ class TelegramBot extends EventEmitter {
return _messageTypes;
}
/**
* Change Promise library used internally, for all existing and new
* instances.
* @param {Function} customPromise
*
* @example
* const TelegramBot = require('node-telegram-bot-api');
* TelegramBot.Promise = myPromise;
*/
static set Promise(customPromise) {
Promise = customPromise;
}
on(event, listener) {
if (_deprecatedMessageTypes.indexOf(event) !== -1) {
const url = 'https://github.com/yagop/node-telegram-bot-api/blob/master/doc/usage.md#events';

View File

@ -109,6 +109,15 @@ describe('TelegramBot', function telegramSuite() {
});
});
it('allows providing custom Promise library', function test() {
TelegramBot.Promise = global.Promise;
const promise = bot.stopPolling();
assert.ok(promise instanceof global.Promise);
assert.ok(!(promise instanceof Promise));
// revert
TelegramBot.Promise = Promise;
});
it('automatically starts polling', function test() {
assert.equal(botPolling.isPolling(), true);
return utils.isPollingMockServer(pollingPort2);