oira666_tg 1.0.13 → 1.0.14

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.
package/index.js CHANGED
@@ -4,6 +4,7 @@ const { redirect_handler, apply_handler_to_cron_task } = require('./middleware/a
4
4
  const { find_in_handlers } = require('./utils/find_in_handlers');
5
5
  const { send_empty_message } = require('./utils/send_empty_message');
6
6
  const { remove_html_tags } = require('./utils/remove_html_tags');
7
+ const { exclusive_locker } = require('./utils/exclusive_locker');
7
8
  const { error_handler, notifyAdmins, sendErrorToDevelopers, error_403_handler, sendErrorToUser } = require('./middleware/error_handler');
8
9
 
9
10
  const { TelegramSession } = require('./source/classes/TelegramSession');
@@ -36,3 +37,4 @@ module.exports.WaitMessage = WaitMessage;
36
37
 
37
38
  // utils
38
39
  module.exports.remove_html_tags = remove_html_tags;
40
+ module.exports.exclusive_locker = exclusive_locker;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oira666_tg",
3
- "version": "1.0.13",
3
+ "version": "1.0.14",
4
4
  "description": "tg framework for oira666",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -0,0 +1,41 @@
1
+ const tracked_functions = {};
2
+
3
+ const exclusive_locker = (max_counter = 30, callback) => {
4
+ const key = String(callback);
5
+ if (!tracked_functions[key]) {
6
+ tracked_functions[key] = {
7
+ is_locked: false,
8
+ counter: 0,
9
+ };
10
+ }
11
+
12
+ const locked = tracked_functions[key];
13
+
14
+ return async (...args) => {
15
+ if (locked.is_locked) {
16
+ locked.counter++;
17
+ if (locked.counter > max_counter) {
18
+ locked.is_locked = false;
19
+ locked.counter = 0;
20
+ }
21
+ return;
22
+ }
23
+
24
+ locked.is_locked = true;
25
+ locked.counter = 0;
26
+
27
+ try {
28
+ await callback(...args);
29
+ }
30
+ catch (err) {
31
+ locked.is_locked = false;
32
+ locked.counter = 0;
33
+ throw err;
34
+ }
35
+
36
+ locked.is_locked = false;
37
+ locked.counter = 0;
38
+ }
39
+ };
40
+
41
+ module.exports.exclusive_locker = exclusive_locker;
File without changes