@mtkruto/node 0.1.135 → 0.1.138
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/esm/0_deps.d.ts +1 -1
- package/esm/0_deps.js +1 -1
- package/esm/4_constants.d.ts +1 -1
- package/esm/4_constants.js +1 -1
- package/esm/client/0_utilities.d.ts +38 -23
- package/esm/client/0_utilities.js +27 -0
- package/esm/client/1_composer.d.ts +4 -4
- package/esm/client/1_composer.js +3 -29
- package/esm/client/4_client.d.ts +4 -4
- package/esm/client/4_client.js +3 -29
- package/esm/deps/raw.githubusercontent.com/MTKruto/mutex/main/mod.d.ts +2 -0
- package/esm/deps/raw.githubusercontent.com/MTKruto/mutex/main/mod.js +2 -0
- package/esm/deps/raw.githubusercontent.com/MTKruto/mutex/main/mutex.d.ts +26 -0
- package/esm/deps/raw.githubusercontent.com/MTKruto/mutex/main/mutex.js +32 -0
- package/esm/deps/raw.githubusercontent.com/MTKruto/mutex/main/semaphore.d.ts +41 -0
- package/esm/deps/raw.githubusercontent.com/MTKruto/mutex/main/semaphore.js +113 -0
- package/esm/storage/1_storage_local_storage.js +2 -2
- package/esm/storage/1_storage_session_storage.js +2 -2
- package/esm/types/6_update.d.ts +70 -8
- package/package.json +1 -2
- package/script/0_deps.d.ts +1 -1
- package/script/0_deps.js +6 -6
- package/script/4_constants.d.ts +1 -1
- package/script/4_constants.js +1 -1
- package/script/client/0_utilities.d.ts +38 -23
- package/script/client/0_utilities.js +29 -1
- package/script/client/1_composer.d.ts +4 -4
- package/script/client/1_composer.js +3 -29
- package/script/client/4_client.d.ts +4 -4
- package/script/client/4_client.js +2 -28
- package/script/deps/raw.githubusercontent.com/MTKruto/mutex/main/mod.d.ts +2 -0
- package/script/deps/raw.githubusercontent.com/MTKruto/mutex/main/mod.js +18 -0
- package/script/deps/raw.githubusercontent.com/MTKruto/mutex/main/mutex.d.ts +26 -0
- package/script/deps/raw.githubusercontent.com/MTKruto/mutex/main/mutex.js +36 -0
- package/script/deps/raw.githubusercontent.com/MTKruto/mutex/main/semaphore.d.ts +41 -0
- package/script/deps/raw.githubusercontent.com/MTKruto/mutex/main/semaphore.js +117 -0
- package/script/storage/1_storage_local_storage.js +2 -2
- package/script/storage/1_storage_session_storage.js +2 -2
- package/script/types/6_update.d.ts +70 -8
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Semaphore = exports.E_CANCELED = void 0;
|
|
4
|
+
exports.E_CANCELED = new Error("request for lock canceled");
|
|
5
|
+
class Semaphore {
|
|
6
|
+
constructor(_value, _cancelError = exports.E_CANCELED) {
|
|
7
|
+
Object.defineProperty(this, "_value", {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
configurable: true,
|
|
10
|
+
writable: true,
|
|
11
|
+
value: _value
|
|
12
|
+
});
|
|
13
|
+
Object.defineProperty(this, "_cancelError", {
|
|
14
|
+
enumerable: true,
|
|
15
|
+
configurable: true,
|
|
16
|
+
writable: true,
|
|
17
|
+
value: _cancelError
|
|
18
|
+
});
|
|
19
|
+
Object.defineProperty(this, "_weightedQueues", {
|
|
20
|
+
enumerable: true,
|
|
21
|
+
configurable: true,
|
|
22
|
+
writable: true,
|
|
23
|
+
value: []
|
|
24
|
+
});
|
|
25
|
+
Object.defineProperty(this, "_weightedWaiters", {
|
|
26
|
+
enumerable: true,
|
|
27
|
+
configurable: true,
|
|
28
|
+
writable: true,
|
|
29
|
+
value: []
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
acquire(weight = 1) {
|
|
33
|
+
if (weight <= 0) {
|
|
34
|
+
throw new Error(`invalid weight ${weight}: must be positive`);
|
|
35
|
+
}
|
|
36
|
+
return new Promise((resolve, reject) => {
|
|
37
|
+
if (!this._weightedQueues[weight - 1]) {
|
|
38
|
+
this._weightedQueues[weight - 1] = [];
|
|
39
|
+
}
|
|
40
|
+
this._weightedQueues[weight - 1].push({ resolve, reject });
|
|
41
|
+
this._dispatch();
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
async runExclusive(callback, weight = 1) {
|
|
45
|
+
const [value, release] = await this.acquire(weight);
|
|
46
|
+
try {
|
|
47
|
+
return await callback(value);
|
|
48
|
+
}
|
|
49
|
+
finally {
|
|
50
|
+
release();
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
waitForUnlock(weight = 1) {
|
|
54
|
+
if (weight <= 0) {
|
|
55
|
+
throw new Error(`invalid weight ${weight}: must be positive`);
|
|
56
|
+
}
|
|
57
|
+
return new Promise((resolve) => {
|
|
58
|
+
if (!this._weightedWaiters[weight - 1]) {
|
|
59
|
+
this._weightedWaiters[weight - 1] = [];
|
|
60
|
+
}
|
|
61
|
+
this._weightedWaiters[weight - 1].push(resolve);
|
|
62
|
+
this._dispatch();
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
isLocked() {
|
|
66
|
+
return this._value <= 0;
|
|
67
|
+
}
|
|
68
|
+
getValue() {
|
|
69
|
+
return this._value;
|
|
70
|
+
}
|
|
71
|
+
setValue(value) {
|
|
72
|
+
this._value = value;
|
|
73
|
+
this._dispatch();
|
|
74
|
+
}
|
|
75
|
+
release(weight = 1) {
|
|
76
|
+
if (weight <= 0) {
|
|
77
|
+
throw new Error(`invalid weight ${weight}: must be positive`);
|
|
78
|
+
}
|
|
79
|
+
this._value += weight;
|
|
80
|
+
this._dispatch();
|
|
81
|
+
}
|
|
82
|
+
cancel() {
|
|
83
|
+
this._weightedQueues.forEach((queue) => queue.forEach((entry) => entry.reject(this._cancelError)));
|
|
84
|
+
this._weightedQueues = [];
|
|
85
|
+
}
|
|
86
|
+
_dispatch() {
|
|
87
|
+
for (let weight = this._value; weight > 0; weight--) {
|
|
88
|
+
const queueEntry = this._weightedQueues[weight - 1]?.shift();
|
|
89
|
+
if (!queueEntry)
|
|
90
|
+
continue;
|
|
91
|
+
const previousValue = this._value;
|
|
92
|
+
const previousWeight = weight;
|
|
93
|
+
this._value -= weight;
|
|
94
|
+
weight = this._value + 1;
|
|
95
|
+
queueEntry.resolve([previousValue, this._newReleaser(previousWeight)]);
|
|
96
|
+
}
|
|
97
|
+
this._drainUnlockWaiters();
|
|
98
|
+
}
|
|
99
|
+
_newReleaser(weight) {
|
|
100
|
+
let called = false;
|
|
101
|
+
return () => {
|
|
102
|
+
if (called)
|
|
103
|
+
return;
|
|
104
|
+
called = true;
|
|
105
|
+
this.release(weight);
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
_drainUnlockWaiters() {
|
|
109
|
+
for (let weight = this._value; weight > 0; weight--) {
|
|
110
|
+
if (!this._weightedWaiters[weight - 1])
|
|
111
|
+
continue;
|
|
112
|
+
this._weightedWaiters[weight - 1].forEach((waiter) => waiter());
|
|
113
|
+
this._weightedWaiters[weight - 1] = [];
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
exports.Semaphore = Semaphore;
|
|
@@ -59,7 +59,7 @@ class StorageLocalStorage extends _0_storage_js_1.Storage {
|
|
|
59
59
|
if (params?.limit !== undefined) {
|
|
60
60
|
entries = entries.slice(0, params.limit <= 0 ? 1 : params.limit);
|
|
61
61
|
}
|
|
62
|
-
for (let [key, value] of entries) {
|
|
62
|
+
entries: for (let [key, value] of entries) {
|
|
63
63
|
if (key.startsWith(this.prefix)) {
|
|
64
64
|
key = key.slice(this.prefix.length);
|
|
65
65
|
}
|
|
@@ -68,7 +68,7 @@ class StorageLocalStorage extends _0_storage_js_1.Storage {
|
|
|
68
68
|
if ("prefix" in filter) {
|
|
69
69
|
for (const [i, p] of filter.prefix.entries()) {
|
|
70
70
|
if ((0, _0_utilities_js_1.toString)(p) != (0, _0_utilities_js_1.toString)(parts[i])) {
|
|
71
|
-
continue;
|
|
71
|
+
continue entries;
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
74
|
}
|
|
@@ -59,7 +59,7 @@ class StorageSessionStorage extends _0_storage_js_1.Storage {
|
|
|
59
59
|
if (params?.limit !== undefined) {
|
|
60
60
|
entries = entries.slice(0, params.limit <= 0 ? 1 : params.limit);
|
|
61
61
|
}
|
|
62
|
-
for (let [key, value] of entries) {
|
|
62
|
+
entries: for (let [key, value] of entries) {
|
|
63
63
|
if (key.startsWith(this.prefix)) {
|
|
64
64
|
key = key.slice(this.prefix.length);
|
|
65
65
|
}
|
|
@@ -68,7 +68,7 @@ class StorageSessionStorage extends _0_storage_js_1.Storage {
|
|
|
68
68
|
if ("prefix" in filter) {
|
|
69
69
|
for (const [i, p] of filter.prefix.entries()) {
|
|
70
70
|
if ((0, _0_utilities_js_1.toString)(p) != (0, _0_utilities_js_1.toString)(parts[i])) {
|
|
71
|
-
continue;
|
|
71
|
+
continue entries;
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
74
|
}
|
|
@@ -9,7 +9,13 @@ import { Message } from "./4_message.js";
|
|
|
9
9
|
import { CallbackQuery } from "./5_callback_query.js";
|
|
10
10
|
import { Chat } from "./5_chat.js";
|
|
11
11
|
/**
|
|
12
|
-
*
|
|
12
|
+
* A client's connection state was changed.
|
|
13
|
+
*
|
|
14
|
+
* ```
|
|
15
|
+
* client.on("connectionState", (ctx) => {
|
|
16
|
+
* console.log("The client's connection state is now:", ctx.connectionState);
|
|
17
|
+
* });
|
|
18
|
+
* ```
|
|
13
19
|
* @unlisted
|
|
14
20
|
*/
|
|
15
21
|
export interface UpdateConnectionState {
|
|
@@ -17,7 +23,18 @@ export interface UpdateConnectionState {
|
|
|
17
23
|
connectionState: ConnectionState;
|
|
18
24
|
}
|
|
19
25
|
/**
|
|
20
|
-
*
|
|
26
|
+
* A client's authorization state was changed.
|
|
27
|
+
*
|
|
28
|
+
* ```
|
|
29
|
+
* client.on("authorizationState", (ctx) => {
|
|
30
|
+
* if (ctx.authorizationState.authorized) {
|
|
31
|
+
* const me = await ctx.client.getMe();
|
|
32
|
+
* console.log("The client is now authorized as", me.firstName);
|
|
33
|
+
* } else {
|
|
34
|
+
* console.log("The client is no longer authorized.")
|
|
35
|
+
* }
|
|
36
|
+
* });
|
|
37
|
+
* ```
|
|
21
38
|
* @unlisted
|
|
22
39
|
*/
|
|
23
40
|
export interface UpdateAuthorizationState {
|
|
@@ -26,6 +43,23 @@ export interface UpdateAuthorizationState {
|
|
|
26
43
|
}
|
|
27
44
|
/**
|
|
28
45
|
* A message was sent or received.
|
|
46
|
+
*
|
|
47
|
+
* ```
|
|
48
|
+
* // Handle text messages
|
|
49
|
+
* client.on("message:text", (ctx) => {
|
|
50
|
+
* const receivedOrSent = ctx.message.out ? "sent" : "received";
|
|
51
|
+
* console.log("Just", receivedOrSent, "a text message:", ctx.message.text);
|
|
52
|
+
* });
|
|
53
|
+
*
|
|
54
|
+
* // Handle other messages
|
|
55
|
+
* client.on("message", (ctx) => {
|
|
56
|
+
* if (ctx.message.out) {
|
|
57
|
+
* console.log("Just sent a message.");
|
|
58
|
+
* }
|
|
59
|
+
* });
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* Note that updates on outgoing messages are disabled by default for bots.
|
|
29
63
|
* @unlisted
|
|
30
64
|
*/
|
|
31
65
|
export interface UpdateNewMessage {
|
|
@@ -34,6 +68,13 @@ export interface UpdateNewMessage {
|
|
|
34
68
|
}
|
|
35
69
|
/**
|
|
36
70
|
* A message was edited.
|
|
71
|
+
*
|
|
72
|
+
* ```
|
|
73
|
+
* client.on("editedMessage", (ctx) => {
|
|
74
|
+
* console.log("A message was just edited.");
|
|
75
|
+
* // ctx.editedMessage
|
|
76
|
+
* });
|
|
77
|
+
* ```
|
|
37
78
|
* @unlisted
|
|
38
79
|
*/
|
|
39
80
|
export interface UpdateEditedMessage {
|
|
@@ -42,6 +83,14 @@ export interface UpdateEditedMessage {
|
|
|
42
83
|
}
|
|
43
84
|
/**
|
|
44
85
|
* One or more messages were deleted.
|
|
86
|
+
*
|
|
87
|
+
* ```
|
|
88
|
+
* client.on("deletedMessages", (ctx) => {
|
|
89
|
+
* for (const deletedMessage of ctx.deletedMessages) {
|
|
90
|
+
* console.log(deletedMessage);
|
|
91
|
+
* }
|
|
92
|
+
* });
|
|
93
|
+
* ```
|
|
45
94
|
* @unlisted
|
|
46
95
|
*/
|
|
47
96
|
export interface UpdateDeletedMessages {
|
|
@@ -49,7 +98,13 @@ export interface UpdateDeletedMessages {
|
|
|
49
98
|
deletedMessages: MessageIdentifier[];
|
|
50
99
|
}
|
|
51
100
|
/**
|
|
52
|
-
* A callback query was
|
|
101
|
+
* A callback query was made (a user presses an inline button). Bot-only.
|
|
102
|
+
*
|
|
103
|
+
* ```
|
|
104
|
+
* client.on("callbackQuery", async (ctx) => {
|
|
105
|
+
* await ctx.answerCallbackQuery(ctx.callbackQuery.data, { alert: true });
|
|
106
|
+
* });
|
|
107
|
+
* ```
|
|
53
108
|
* @unlisted
|
|
54
109
|
*/
|
|
55
110
|
export interface UpdateCallbackQuery {
|
|
@@ -58,6 +113,13 @@ export interface UpdateCallbackQuery {
|
|
|
58
113
|
}
|
|
59
114
|
/**
|
|
60
115
|
* An inline query was received. Bot-only.
|
|
116
|
+
*
|
|
117
|
+
* ```
|
|
118
|
+
* client.on("inlineQuery", (ctx) => {
|
|
119
|
+
* const { from, query } = ctx.inlineQuery;
|
|
120
|
+
* console.log("User", from.id, "sent an inline query:", query);
|
|
121
|
+
* });
|
|
122
|
+
* ```
|
|
61
123
|
* @unlisted
|
|
62
124
|
*/
|
|
63
125
|
export interface UpdateInlineQuery {
|
|
@@ -65,15 +127,15 @@ export interface UpdateInlineQuery {
|
|
|
65
127
|
inlineQuery: InlineQuery;
|
|
66
128
|
}
|
|
67
129
|
/**
|
|
68
|
-
* An inline result was chosen. Bot-only.
|
|
130
|
+
* An inline query result was chosen. Bot-only.
|
|
69
131
|
* @unlisted
|
|
70
132
|
*/
|
|
71
133
|
export interface UpdateChosenInlineResult {
|
|
72
|
-
/** The chosen inline result. */
|
|
134
|
+
/** The chosen inline query result. */
|
|
73
135
|
chosenInlineResult: ChosenInlineResult;
|
|
74
136
|
}
|
|
75
137
|
/**
|
|
76
|
-
* A new chat was added. User-only.
|
|
138
|
+
* A new chat was added to the chat list. User-only.
|
|
77
139
|
* @unlisted
|
|
78
140
|
*/
|
|
79
141
|
export interface UpdateNewChat {
|
|
@@ -81,14 +143,14 @@ export interface UpdateNewChat {
|
|
|
81
143
|
newChat: Chat;
|
|
82
144
|
}
|
|
83
145
|
/**
|
|
84
|
-
* A chat was
|
|
146
|
+
* A chat in the chat list was edited. User-only.
|
|
85
147
|
* @unlisted
|
|
86
148
|
*/
|
|
87
149
|
export interface UpdateEditedChat {
|
|
88
150
|
editedChat: Chat;
|
|
89
151
|
}
|
|
90
152
|
/**
|
|
91
|
-
* A chat was
|
|
153
|
+
* A chat was removed from the chat list. User-only.
|
|
92
154
|
* @unlisted
|
|
93
155
|
*/
|
|
94
156
|
export interface UpdateDeletedChat {
|