chain-db-ts 0.0.1 → 1.0.0-rc.1

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.
@@ -1,10 +1,10 @@
1
- import { Access } from './access';
2
- import { BasicResponse, SignedUserAccount, TransferUnitsRegistry } from './types';
1
+ import Table from './table';
2
+ import { Connection, EventCallback } from './types';
3
3
  export declare class ChainDB {
4
- api: string;
5
- name: string;
6
- access: Access | null;
7
- access_key: string;
4
+ server: string;
5
+ database: string;
6
+ auth: string;
7
+ private _events;
8
8
  /**
9
9
  * Connection information.
10
10
  * @param server Server location. If the `server` parameter is empty, then "http://localhost" will be used.
@@ -12,63 +12,28 @@ export declare class ChainDB {
12
12
  * @param user User to access the data base
13
13
  * @param password Password to access the data base
14
14
  */
15
- connect(server: string | null, data_base: string, user: string, password: string): void;
16
- /**
17
- * Create a new user account inside the connected table
18
- * @param user_name
19
- * @param password
20
- * @param units
21
- * @param password_hint
22
- * @returns
23
- */
24
- create_user_account(user_name: string, password: string, units?: number, password_hint?: string): Promise<BasicResponse<SignedUserAccount>>;
25
- /**
26
- * Get user account info (login method)
27
- * @param user_name
28
- * @param password
29
- * @returns
30
- */
31
- get_user_account(user_name: string, password: string): Promise<BasicResponse<SignedUserAccount>>;
32
- /**
33
- * Get user account info by its id
34
- * @param user_id
35
- * @returns
36
- */
37
- get_user_account_by_id(user_id: string): Promise<BasicResponse<SignedUserAccount>>;
38
- /**
39
- * Check if user_name is already taken
40
- * @param user_name
41
- */
42
- check_user_name(user_name: string): Promise<BasicResponse<string>>;
43
- /**
44
- * Transfer units between users
45
- * @param from user_id
46
- * @param to user_id
47
- * @param units
48
- * @returns
49
- */
50
- transfer_units(from: string, to: string, units: number): Promise<BasicResponse<null>>;
51
- /**
52
- * Fetch the last Transference of units Records by User
53
- * @param user_id
54
- * @returns
55
- */
56
- get_transfer_by_user_id(user_id: string): Promise<BasicResponse<TransferUnitsRegistry>>;
57
- /**
58
- * Fetch all Transference of units Records by User
59
- * @param user_id
60
- * @returns
61
- */
62
- get_all_transfers_by_user_id(user_id: string): Promise<BasicResponse<TransferUnitsRegistry[]>>;
15
+ connect(connection: Connection): Promise<void>;
63
16
  /**
64
17
  * Initialize a table, fetching its more updated data
65
18
  */
66
- get_table<Model>(table_name: string, model: Model): Promise<{
67
- table: Model;
19
+ getTable<Model>(table_name: string): Promise<Table<Model>>;
20
+ events(): {
21
+ /**
22
+ * Subscribe to an event
23
+ * @param event Event name to subscribe to @see {EventTypes}
24
+ * @param callback Function to call when the event is received
25
+ */
26
+ subscribe: (event: string, callback: EventCallback) => void;
27
+ /**
28
+ * Unsubscribe from an event
29
+ * @param event Event name to unsubscribe from @see {EventTypes}
30
+ * @param callback Optional callback to remove. If not provided, all callbacks for the event will be removed.
31
+ */
32
+ unsubscribe: (event: string, callback?: EventCallback) => void;
68
33
  /**
69
- * Persist table data on chain
34
+ * Close the events transmission
70
35
  */
71
- persist: () => Promise<void>;
72
- }>;
36
+ closeEvents: () => void;
37
+ };
73
38
  }
74
- export declare const connect: (server: string | null, data_base: string, user: string, password: string) => ChainDB;
39
+ export declare const connect: (connection: Connection) => Promise<ChainDB>;
@@ -34,18 +34,16 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
34
34
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
35
  }
36
36
  };
37
- import sha256 from 'sha256';
38
- import axios from 'axios';
39
- import { Access } from './access';
40
- import { API, CHECK_USER_NAME, CREATE_USER_ACCOUNT, GET_ALL_TRANSFER_BY_USER_ID, GET_TRANSFER_BY_USER_ID, GET_USER_ACCOUNT, GET_USER_ACCOUNT_BY_ID, TRANSFER_UNITS, } from './constants';
37
+ import { DEFAULT_API_SERVER, CONNECT, WEB_SOCKET_EVENTS } from './constants';
41
38
  import { post } from './utils';
42
- import * as table from './table';
39
+ import Table from './table';
40
+ import Events from './events';
43
41
  var ChainDB = /** @class */ (function () {
44
42
  function ChainDB() {
45
- this.api = '';
46
- this.name = '';
47
- this.access = null;
48
- this.access_key = '';
43
+ this.server = DEFAULT_API_SERVER;
44
+ this.database = '';
45
+ this.auth = '';
46
+ this._events = null;
49
47
  }
50
48
  /**
51
49
  * Connection information.
@@ -54,224 +52,34 @@ var ChainDB = /** @class */ (function () {
54
52
  * @param user User to access the data base
55
53
  * @param password Password to access the data base
56
54
  */
57
- ChainDB.prototype.connect = function (server, data_base, user, password) {
58
- var key_data = "".concat(data_base).concat(user).concat(password);
59
- var key = sha256(key_data);
60
- this.api = server || API;
61
- this.name = data_base;
62
- this.access = new Access(user, password);
63
- this.access_key = key;
64
- };
65
- /**
66
- * Create a new user account inside the connected table
67
- * @param user_name
68
- * @param password
69
- * @param units
70
- * @param password_hint
71
- * @returns
72
- */
73
- ChainDB.prototype.create_user_account = function (user_name, password, units, password_hint) {
55
+ ChainDB.prototype.connect = function (connection) {
74
56
  return __awaiter(this, void 0, void 0, function () {
75
- var url, body, response, _a;
76
- return __generator(this, function (_b) {
77
- switch (_b.label) {
78
- case 0:
79
- url = "".concat(this.api).concat(CREATE_USER_ACCOUNT);
80
- body = {
81
- db_access_key: this.access_key,
82
- user_name: user_name,
83
- password: password,
84
- password_hint: password_hint,
85
- units: units
86
- };
87
- _b.label = 1;
88
- case 1:
89
- _b.trys.push([1, 4, , 5]);
90
- return [4 /*yield*/, post(url, body)];
91
- case 2:
92
- response = _b.sent();
93
- return [4 /*yield*/, response.data];
94
- case 3: return [2 /*return*/, _b.sent()];
95
- case 4:
96
- _a = _b.sent();
97
- throw new Error('Something went wrong!');
98
- case 5: return [2 /*return*/];
99
- }
100
- });
101
- });
102
- };
103
- /**
104
- * Get user account info (login method)
105
- * @param user_name
106
- * @param password
107
- * @returns
108
- */
109
- ChainDB.prototype.get_user_account = function (user_name, password) {
110
- return __awaiter(this, void 0, void 0, function () {
111
- var url, response, _a;
112
- return __generator(this, function (_b) {
113
- switch (_b.label) {
114
- case 0:
115
- url = "".concat(this.api).concat(GET_USER_ACCOUNT, "/").concat(user_name, "/").concat(password, "/").concat(this.access_key);
116
- _b.label = 1;
117
- case 1:
118
- _b.trys.push([1, 4, , 5]);
119
- return [4 /*yield*/, axios.get(url)];
120
- case 2:
121
- response = _b.sent();
122
- return [4 /*yield*/, response.data];
123
- case 3: return [2 /*return*/, _b.sent()];
124
- case 4:
125
- _a = _b.sent();
126
- throw new Error('Something went wrong!');
127
- case 5: return [2 /*return*/];
128
- }
129
- });
130
- });
131
- };
132
- /**
133
- * Get user account info by its id
134
- * @param user_id
135
- * @returns
136
- */
137
- ChainDB.prototype.get_user_account_by_id = function (user_id) {
138
- return __awaiter(this, void 0, void 0, function () {
139
- var url, response, _a;
140
- return __generator(this, function (_b) {
141
- switch (_b.label) {
142
- case 0:
143
- url = "".concat(this.api).concat(GET_USER_ACCOUNT_BY_ID, "/").concat(user_id, "/").concat(this.access_key);
144
- _b.label = 1;
145
- case 1:
146
- _b.trys.push([1, 4, , 5]);
147
- return [4 /*yield*/, axios.get(url)];
148
- case 2:
149
- response = _b.sent();
150
- return [4 /*yield*/, response.data];
151
- case 3: return [2 /*return*/, _b.sent()];
152
- case 4:
153
- _a = _b.sent();
154
- throw new Error('Something went wrong!');
155
- case 5: return [2 /*return*/];
156
- }
157
- });
158
- });
159
- };
160
- /**
161
- * Check if user_name is already taken
162
- * @param user_name
163
- */
164
- ChainDB.prototype.check_user_name = function (user_name) {
165
- return __awaiter(this, void 0, void 0, function () {
166
- var url, response, _a;
167
- return __generator(this, function (_b) {
168
- switch (_b.label) {
169
- case 0:
170
- url = "".concat(this.api).concat(CHECK_USER_NAME, "/").concat(user_name, "/").concat(this.access_key);
171
- _b.label = 1;
172
- case 1:
173
- _b.trys.push([1, 4, , 5]);
174
- return [4 /*yield*/, axios.get(url)];
175
- case 2:
176
- response = _b.sent();
177
- return [4 /*yield*/, response.data];
178
- case 3: return [2 /*return*/, _b.sent()];
179
- case 4:
180
- _a = _b.sent();
181
- throw new Error('Something went wrong!');
182
- case 5: return [2 /*return*/];
183
- }
184
- });
185
- });
186
- };
187
- /**
188
- * Transfer units between users
189
- * @param from user_id
190
- * @param to user_id
191
- * @param units
192
- * @returns
193
- */
194
- ChainDB.prototype.transfer_units = function (from, to, units) {
195
- return __awaiter(this, void 0, void 0, function () {
196
- var url, body, response, _a;
197
- return __generator(this, function (_b) {
198
- switch (_b.label) {
199
- case 0:
200
- url = "".concat(this.api).concat(TRANSFER_UNITS);
201
- body = {
202
- db_access_key: this.access_key,
203
- from: from,
204
- to: to,
205
- units: units
206
- };
207
- _b.label = 1;
208
- case 1:
209
- _b.trys.push([1, 4, , 5]);
210
- return [4 /*yield*/, post(url, body)];
211
- case 2:
212
- response = _b.sent();
213
- return [4 /*yield*/, response.data];
214
- case 3: return [2 /*return*/, _b.sent()];
215
- case 4:
216
- _a = _b.sent();
217
- throw new Error('Something went wrong!');
218
- case 5: return [2 /*return*/];
219
- }
220
- });
221
- });
222
- };
223
- /**
224
- * Fetch the last Transference of units Records by User
225
- * @param user_id
226
- * @returns
227
- */
228
- ChainDB.prototype.get_transfer_by_user_id = function (user_id) {
229
- return __awaiter(this, void 0, void 0, function () {
230
- var url, response, _a;
231
- return __generator(this, function (_b) {
232
- switch (_b.label) {
233
- case 0:
234
- url = "".concat(this.api).concat(GET_TRANSFER_BY_USER_ID, "/").concat(user_id, "/").concat(this.access_key);
235
- _b.label = 1;
236
- case 1:
237
- _b.trys.push([1, 4, , 5]);
238
- return [4 /*yield*/, axios.get(url)];
239
- case 2:
240
- response = _b.sent();
241
- return [4 /*yield*/, response.data];
242
- case 3: return [2 /*return*/, _b.sent()];
243
- case 4:
244
- _a = _b.sent();
245
- throw new Error('Something went wrong!');
246
- case 5: return [2 /*return*/];
247
- }
248
- });
249
- });
250
- };
251
- /**
252
- * Fetch all Transference of units Records by User
253
- * @param user_id
254
- * @returns
255
- */
256
- ChainDB.prototype.get_all_transfers_by_user_id = function (user_id) {
257
- return __awaiter(this, void 0, void 0, function () {
258
- var url, response, _a;
259
- return __generator(this, function (_b) {
260
- switch (_b.label) {
57
+ var server, database, user, password, response, e_1;
58
+ return __generator(this, function (_a) {
59
+ switch (_a.label) {
261
60
  case 0:
262
- url = "".concat(this.api).concat(GET_ALL_TRANSFER_BY_USER_ID, "/").concat(user_id, "/").concat(this.access_key);
263
- _b.label = 1;
61
+ server = connection.server, database = connection.database, user = connection.user, password = connection.password;
62
+ this.server = server || DEFAULT_API_SERVER;
63
+ this.database = database;
64
+ _a.label = 1;
264
65
  case 1:
265
- _b.trys.push([1, 4, , 5]);
266
- return [4 /*yield*/, axios.get(url)];
66
+ _a.trys.push([1, 3, , 4]);
67
+ return [4 /*yield*/, post("".concat(this.server).concat(CONNECT), {
68
+ name: this.database,
69
+ user: user,
70
+ password: password
71
+ })];
267
72
  case 2:
268
- response = _b.sent();
269
- return [4 /*yield*/, response.data];
270
- case 3: return [2 /*return*/, _b.sent()];
271
- case 4:
272
- _a = _b.sent();
273
- throw new Error('Something went wrong!');
274
- case 5: return [2 /*return*/];
73
+ response = _a.sent();
74
+ if (!response.data.success) {
75
+ throw new Error(response.data.message);
76
+ }
77
+ this.auth = response.data.data;
78
+ return [3 /*break*/, 4];
79
+ case 3:
80
+ e_1 = _a.sent();
81
+ throw new Error("Something went wrong! ".concat(e_1.message || String(e_1)));
82
+ case 4: return [2 /*return*/];
275
83
  }
276
84
  });
277
85
  });
@@ -279,39 +87,91 @@ var ChainDB = /** @class */ (function () {
279
87
  /**
280
88
  * Initialize a table, fetching its more updated data
281
89
  */
282
- ChainDB.prototype.get_table = function (table_name, model) {
90
+ ChainDB.prototype.getTable = function (table_name) {
283
91
  return __awaiter(this, void 0, void 0, function () {
284
- var chainDbCopy, table_data;
92
+ var tableData;
285
93
  return __generator(this, function (_a) {
286
94
  switch (_a.label) {
287
95
  case 0:
288
- chainDbCopy = connect(this.api, this.name, this.access.user, this.access.password);
289
- return [4 /*yield*/, table.get(chainDbCopy, table_name, model)
96
+ tableData = new Table(table_name, this);
97
+ return [4 /*yield*/, tableData.refetch()];
98
+ case 1:
99
+ _a.sent();
100
+ return [2 /*return*/, tableData
101
+ // const table_data = await table.get<Model>(this, table_name, model)
290
102
  // NOTE: Although only the "table" and "persist" properties are displayed by
291
103
  // the lint, all Table properties are being exposed.
292
104
  // This is due to a javascript limitation on classes.
293
105
  //
294
106
  // There was an attempt to return a new object with only the required
295
107
  // data, but this generates an error in the "this" instance of the Table.
108
+ // return table_data as {
109
+ // table: Model
110
+ // /**
111
+ // * Persist table data on chain
112
+ // */
113
+ // persist: () => Promise<void>
114
+ // /**
115
+ // * Get the history of changes. A list of transactions from the most recent to the most old
116
+ // * in a range of depth
117
+ // * @param depth
118
+ // */
119
+ // getHistory: (depth: number) => Promise<Model[]>
120
+ // }
296
121
  ];
297
- case 1:
298
- table_data = _a.sent();
299
- // NOTE: Although only the "table" and "persist" properties are displayed by
300
- // the lint, all Table properties are being exposed.
301
- // This is due to a javascript limitation on classes.
302
- //
303
- // There was an attempt to return a new object with only the required
304
- // data, but this generates an error in the "this" instance of the Table.
305
- return [2 /*return*/, table_data];
306
122
  }
307
123
  });
308
124
  });
309
125
  };
126
+ ChainDB.prototype.events = function () {
127
+ var _this = this;
128
+ return {
129
+ /**
130
+ * Subscribe to an event
131
+ * @param event Event name to subscribe to @see {EventTypes}
132
+ * @param callback Function to call when the event is received
133
+ */
134
+ subscribe: function (event, callback) {
135
+ var _a;
136
+ if (_this._events === null) {
137
+ var wsUrl = "".concat(_this.server.replace('http', 'ws')).concat(WEB_SOCKET_EVENTS);
138
+ _this._events = new Events(wsUrl, _this.auth);
139
+ }
140
+ (_a = _this._events) === null || _a === void 0 ? void 0 : _a.subscribe(event, callback);
141
+ },
142
+ /**
143
+ * Unsubscribe from an event
144
+ * @param event Event name to unsubscribe from @see {EventTypes}
145
+ * @param callback Optional callback to remove. If not provided, all callbacks for the event will be removed.
146
+ */
147
+ unsubscribe: function (event, callback) {
148
+ if (!_this._events || !_this._events.isConnected()) {
149
+ return;
150
+ }
151
+ _this._events.unsubscribe(event, callback);
152
+ },
153
+ /**
154
+ * Close the events transmission
155
+ */
156
+ closeEvents: function () {
157
+ var _a;
158
+ (_a = _this._events) === null || _a === void 0 ? void 0 : _a.close();
159
+ }
160
+ };
161
+ };
310
162
  return ChainDB;
311
163
  }());
312
164
  export { ChainDB };
313
- export var connect = function (server, data_base, user, password) {
314
- var chainDb = new ChainDB();
315
- chainDb.connect(server, data_base, user, password);
316
- return chainDb;
317
- };
165
+ export var connect = function (connection) { return __awaiter(void 0, void 0, void 0, function () {
166
+ var chainDb;
167
+ return __generator(this, function (_a) {
168
+ switch (_a.label) {
169
+ case 0:
170
+ chainDb = new ChainDB();
171
+ return [4 /*yield*/, chainDb.connect(connection)];
172
+ case 1:
173
+ _a.sent();
174
+ return [2 /*return*/, chainDb];
175
+ }
176
+ });
177
+ }); };
@@ -1,10 +1,10 @@
1
- export declare const API = "http://localhost:2818";
2
- export declare const CONTRACT_PAYLOAD = "/get_last_contract_transaction";
3
- export declare const CONTRACT_TRANSACTION = "/post_contract_transaction";
4
- export declare const CREATE_USER_ACCOUNT = "/create_user_account";
5
- export declare const GET_USER_ACCOUNT = "/get_user_account";
6
- export declare const GET_USER_ACCOUNT_BY_ID = "/get_user_account_by_id";
7
- export declare const TRANSFER_UNITS = "/transfer_units";
8
- export declare const GET_TRANSFER_BY_USER_ID = "/get_transfer_by_user_id";
9
- export declare const GET_ALL_TRANSFER_BY_USER_ID = "/get_all_transfers_by_user_id";
10
- export declare const CHECK_USER_NAME = "/check_user_name";
1
+ export declare const DEFAULT_API_SERVER = "http://localhost:2818";
2
+ export declare const API_BASE = "/api/v1";
3
+ export declare const CONNECT: string;
4
+ export declare const GET_TABLE: (table: string) => string;
5
+ export declare const UPDATE_LAST_ITEM: (table: string) => string;
6
+ export declare const PERSIST_NEW_DATA: (table: string) => string;
7
+ export declare const GET_HISTORY: (table: string, limit?: number) => string;
8
+ export declare const FIND_WHERE_BASIC: (table: string) => string;
9
+ export declare const FIND_WHERE_ADVANCED: (table: string) => string;
10
+ export declare const WEB_SOCKET_EVENTS: string;
@@ -1,11 +1,14 @@
1
1
  // Contants
2
- export var API = 'http://localhost:2818';
3
- export var CONTRACT_PAYLOAD = '/get_last_contract_transaction';
4
- export var CONTRACT_TRANSACTION = '/post_contract_transaction';
5
- export var CREATE_USER_ACCOUNT = '/create_user_account';
6
- export var GET_USER_ACCOUNT = '/get_user_account';
7
- export var GET_USER_ACCOUNT_BY_ID = '/get_user_account_by_id';
8
- export var TRANSFER_UNITS = '/transfer_units';
9
- export var GET_TRANSFER_BY_USER_ID = '/get_transfer_by_user_id';
10
- export var GET_ALL_TRANSFER_BY_USER_ID = '/get_all_transfers_by_user_id';
11
- export var CHECK_USER_NAME = '/check_user_name';
2
+ export var DEFAULT_API_SERVER = 'http://localhost:2818';
3
+ export var API_BASE = '/api/v1';
4
+ export var CONNECT = "".concat(API_BASE, "/database/connect");
5
+ export var GET_TABLE = function (table) { return "".concat(API_BASE, "/table/").concat(table); };
6
+ export var UPDATE_LAST_ITEM = function (table) { return "".concat(API_BASE, "/table/").concat(table, "/update"); };
7
+ export var PERSIST_NEW_DATA = function (table) { return "".concat(API_BASE, "/table/").concat(table, "/persist"); };
8
+ export var GET_HISTORY = function (table, limit) {
9
+ if (limit === void 0) { limit = 25; }
10
+ return "".concat(API_BASE, "/table/").concat(table, "/history?limit=").concat(limit);
11
+ };
12
+ export var FIND_WHERE_BASIC = function (table) { return "".concat(API_BASE, "/table/").concat(table, "/find"); };
13
+ export var FIND_WHERE_ADVANCED = function (table) { return "".concat(API_BASE, "/table/").concat(table, "/find-advanced"); };
14
+ export var WEB_SOCKET_EVENTS = "".concat(API_BASE, "/events");
@@ -0,0 +1,28 @@
1
+ type EventCallback = (data: any) => void;
2
+ declare class Events {
3
+ private socket;
4
+ private eventListeners;
5
+ private connected;
6
+ constructor(url: string, auth: string);
7
+ /**
8
+ * Subscribe to an event
9
+ * @param event Event name to subscribe to
10
+ * @param callback Function to call when the event is received
11
+ */
12
+ subscribe(event: string, callback: EventCallback): void;
13
+ /**
14
+ * Unsubscribe from an event
15
+ * @param event Event name to unsubscribe from
16
+ * @param callback Optional callback to remove. If not provided, all callbacks for the event will be removed.
17
+ */
18
+ unsubscribe(event: string, callback?: EventCallback): void;
19
+ /**
20
+ * Close the WebSocket connection
21
+ */
22
+ close(): void;
23
+ /**
24
+ * Check if the WebSocket connection is established
25
+ */
26
+ isConnected(): boolean;
27
+ }
28
+ export default Events;
@@ -0,0 +1,84 @@
1
+ import WebSocket from 'ws';
2
+ var Events = /** @class */ (function () {
3
+ function Events(url, auth) {
4
+ var _this = this;
5
+ this.eventListeners = new Map();
6
+ this.connected = false;
7
+ this.socket = new WebSocket(url, {
8
+ headers: {
9
+ Authorization: "Basic ".concat(auth)
10
+ }
11
+ });
12
+ //
13
+ this.socket.onopen = function () {
14
+ _this.connected = true;
15
+ console.log('ChainDB: WebSocket connection established');
16
+ };
17
+ this.socket.on('message', function (data) {
18
+ try {
19
+ var parsedData_1 = JSON.parse(data.toString());
20
+ if (parsedData_1.event_type && _this.eventListeners.has(parsedData_1.event_type)) {
21
+ var listeners = _this.eventListeners.get(parsedData_1.event_type) || [];
22
+ listeners.forEach(function (callback) { return callback(parsedData_1); });
23
+ }
24
+ }
25
+ catch (error) {
26
+ console.error('Error parsing WebSocket message:', error);
27
+ }
28
+ });
29
+ this.socket.on('close', function () {
30
+ _this.connected = false;
31
+ console.log('ChainDB: WebSocket connection closed');
32
+ });
33
+ this.socket.on('error', function (error) {
34
+ console.error('WebSocket error:', error);
35
+ });
36
+ }
37
+ /**
38
+ * Subscribe to an event
39
+ * @param event Event name to subscribe to
40
+ * @param callback Function to call when the event is received
41
+ */
42
+ Events.prototype.subscribe = function (event, callback) {
43
+ if (!this.eventListeners.has(event)) {
44
+ this.eventListeners.set(event, []);
45
+ }
46
+ var listeners = this.eventListeners.get(event) || [];
47
+ listeners.push(callback);
48
+ this.eventListeners.set(event, listeners);
49
+ };
50
+ /**
51
+ * Unsubscribe from an event
52
+ * @param event Event name to unsubscribe from
53
+ * @param callback Optional callback to remove. If not provided, all callbacks for the event will be removed.
54
+ */
55
+ Events.prototype.unsubscribe = function (event, callback) {
56
+ if (!this.eventListeners.has(event)) {
57
+ return;
58
+ }
59
+ if (callback) {
60
+ // Remove specific callback
61
+ var listeners = this.eventListeners.get(event) || [];
62
+ var updatedListeners = listeners.filter(function (cb) { return cb !== callback; });
63
+ this.eventListeners.set(event, updatedListeners);
64
+ }
65
+ else {
66
+ // Remove all callbacks for this event
67
+ this.eventListeners["delete"](event);
68
+ }
69
+ };
70
+ /**
71
+ * Close the WebSocket connection
72
+ */
73
+ Events.prototype.close = function () {
74
+ this.socket.close();
75
+ };
76
+ /**
77
+ * Check if the WebSocket connection is established
78
+ */
79
+ Events.prototype.isConnected = function () {
80
+ return this.connected;
81
+ };
82
+ return Events;
83
+ }());
84
+ export default Events;