ccxt 4.4.20 → 4.4.22

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.
Files changed (77) hide show
  1. package/README.md +4 -4
  2. package/dist/ccxt.browser.min.js +5 -5
  3. package/dist/cjs/ccxt.js +1 -1
  4. package/dist/cjs/src/alpaca.js +1 -0
  5. package/dist/cjs/src/base/Exchange.js +34 -0
  6. package/dist/cjs/src/base/ws/Future.js +3 -1
  7. package/dist/cjs/src/bigone.js +3 -0
  8. package/dist/cjs/src/binance.js +103 -10
  9. package/dist/cjs/src/bingx.js +6 -1
  10. package/dist/cjs/src/bitflyer.js +57 -0
  11. package/dist/cjs/src/bitget.js +77 -0
  12. package/dist/cjs/src/bybit.js +143 -3
  13. package/dist/cjs/src/cex.js +1307 -1385
  14. package/dist/cjs/src/cryptocom.js +1 -1
  15. package/dist/cjs/src/gate.js +103 -3
  16. package/dist/cjs/src/htx.js +29 -7
  17. package/dist/cjs/src/hyperliquid.js +15 -12
  18. package/dist/cjs/src/kucoin.js +43 -95
  19. package/dist/cjs/src/kucoinfutures.js +2 -2
  20. package/dist/cjs/src/okx.js +82 -10
  21. package/dist/cjs/src/paradex.js +1 -2
  22. package/dist/cjs/src/static_dependencies/noble-hashes/_sha2.js +1 -1
  23. package/dist/cjs/src/static_dependencies/noble-hashes/hmac.js +1 -1
  24. package/dist/cjs/src/static_dependencies/noble-hashes/sha3.js +1 -1
  25. package/dist/cjs/src/static_dependencies/watchable/src/unpromise.js +298 -0
  26. package/js/ccxt.d.ts +3 -3
  27. package/js/ccxt.js +1 -1
  28. package/js/src/abstract/bitflyer.d.ts +1 -0
  29. package/js/src/abstract/bitget.d.ts +3 -0
  30. package/js/src/abstract/bybit.d.ts +1 -0
  31. package/js/src/abstract/cex.d.ts +28 -29
  32. package/js/src/abstract/gate.d.ts +5 -0
  33. package/js/src/abstract/gateio.d.ts +5 -0
  34. package/js/src/abstract/kucoin.d.ts +2 -0
  35. package/js/src/abstract/kucoinfutures.d.ts +2 -0
  36. package/js/src/abstract/okx.d.ts +4 -0
  37. package/js/src/alpaca.js +1 -0
  38. package/js/src/base/Exchange.d.ts +11 -3
  39. package/js/src/base/Exchange.js +34 -0
  40. package/js/src/base/types.d.ts +8 -0
  41. package/js/src/base/ws/Future.js +2 -1
  42. package/js/src/bigone.js +3 -0
  43. package/js/src/binance.d.ts +3 -2
  44. package/js/src/binance.js +103 -10
  45. package/js/src/bingx.js +6 -1
  46. package/js/src/bitflyer.d.ts +3 -1
  47. package/js/src/bitflyer.js +57 -0
  48. package/js/src/bitget.d.ts +3 -1
  49. package/js/src/bitget.js +77 -0
  50. package/js/src/bybit.d.ts +4 -1
  51. package/js/src/bybit.js +143 -3
  52. package/js/src/cex.d.ts +34 -20
  53. package/js/src/cex.js +1308 -1386
  54. package/js/src/cryptocom.js +1 -1
  55. package/js/src/gate.d.ts +2 -0
  56. package/js/src/gate.js +103 -3
  57. package/js/src/htx.d.ts +2 -2
  58. package/js/src/htx.js +29 -7
  59. package/js/src/hyperliquid.js +15 -12
  60. package/js/src/kucoin.d.ts +0 -2
  61. package/js/src/kucoin.js +43 -95
  62. package/js/src/kucoinfutures.js +2 -2
  63. package/js/src/okx.d.ts +3 -2
  64. package/js/src/okx.js +83 -11
  65. package/js/src/paradex.js +1 -1
  66. package/js/src/static_dependencies/noble-hashes/_blake2.js +1 -1
  67. package/js/src/static_dependencies/noble-hashes/_sha2.js +1 -1
  68. package/js/src/static_dependencies/noble-hashes/hmac.js +1 -1
  69. package/js/src/static_dependencies/noble-hashes/sha3-addons.js +5 -5
  70. package/js/src/static_dependencies/noble-hashes/sha3.js +1 -1
  71. package/js/src/static_dependencies/watchable/src/index.d.ts +2 -0
  72. package/js/src/static_dependencies/watchable/src/index.js +7 -0
  73. package/js/src/static_dependencies/watchable/src/types.d.ts +28 -0
  74. package/js/src/static_dependencies/watchable/src/types.js +8 -0
  75. package/js/src/static_dependencies/watchable/src/unpromise.d.ts +120 -0
  76. package/js/src/static_dependencies/watchable/src/unpromise.js +297 -0
  77. package/package.json +1 -1
package/js/src/okx.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import Exchange from './abstract/okx.js';
2
- import type { TransferEntry, Int, OrderSide, OrderType, Trade, OHLCV, Order, FundingRateHistory, OrderRequest, FundingHistory, Str, Transaction, Ticker, OrderBook, Balances, Tickers, Market, Greeks, Strings, MarketInterface, Currency, Leverage, Num, Account, OptionChain, Option, MarginModification, TradingFeeInterface, Currencies, Conversion, CancellationRequest, Dict, Position, CrossBorrowRate, CrossBorrowRates, LeverageTier, int, LedgerEntry, FundingRate, DepositAddress } from './base/types.js';
2
+ import type { TransferEntry, Int, OrderSide, OrderType, Trade, OHLCV, Order, FundingRateHistory, OrderRequest, FundingHistory, Str, Transaction, Ticker, OrderBook, Balances, Tickers, Market, Greeks, Strings, MarketInterface, Currency, Leverage, Num, Account, OptionChain, Option, MarginModification, TradingFeeInterface, Currencies, Conversion, CancellationRequest, Dict, Position, CrossBorrowRate, CrossBorrowRates, LeverageTier, int, LedgerEntry, FundingRate, DepositAddress, LongShortRatio } from './base/types.js';
3
3
  /**
4
4
  * @class okx
5
5
  * @augments Exchange
@@ -107,7 +107,6 @@ export default class okx extends Exchange {
107
107
  info: any;
108
108
  };
109
109
  parseBorrowRateHistories(response: any, codes: any, since: any, limit: any): Dict;
110
- parseBorrowRateHistory(response: any, code: any, since: any, limit: any): any;
111
110
  fetchBorrowRateHistories(codes?: any, since?: Int, limit?: Int, params?: {}): Promise<Dict>;
112
111
  fetchBorrowRateHistory(code: string, since?: Int, limit?: Int, params?: {}): Promise<any>;
113
112
  modifyMarginHelper(symbol: string, amount: any, type: any, params?: {}): Promise<MarginModification>;
@@ -186,4 +185,6 @@ export default class okx extends Exchange {
186
185
  handleErrors(httpCode: int, reason: string, url: string, method: string, headers: Dict, body: string, response: any, requestHeaders: any, requestBody: any): any;
187
186
  fetchMarginAdjustmentHistory(symbol?: Str, type?: Str, since?: Num, limit?: Num, params?: {}): Promise<MarginModification[]>;
188
187
  fetchPositionsHistory(symbols?: Strings, since?: Int, limit?: Int, params?: {}): Promise<Position[]>;
188
+ fetchLongShortRatioHistory(symbol?: Str, timeframe?: Str, since?: Int, limit?: Int, params?: {}): Promise<LongShortRatio[]>;
189
+ parseLongShortRatio(info: Dict, market?: Market): LongShortRatio;
189
190
  }
package/js/src/okx.js CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  // ---------------------------------------------------------------------------
8
8
  import Exchange from './abstract/okx.js';
9
- import { ExchangeError, ExchangeNotAvailable, OnMaintenance, ArgumentsRequired, BadRequest, AccountSuspended, InvalidAddress, DDoSProtection, PermissionDenied, InsufficientFunds, InvalidNonce, InvalidOrder, OrderNotFound, AuthenticationError, RequestTimeout, BadSymbol, RateLimitExceeded, NetworkError, CancelPending, NotSupported, AccountNotEnabled, ContractUnavailable, ManualInteractionNeeded } from './base/errors.js';
9
+ import { ExchangeError, ExchangeNotAvailable, OnMaintenance, ArgumentsRequired, BadRequest, AccountSuspended, InvalidAddress, DDoSProtection, PermissionDenied, InsufficientFunds, InvalidNonce, InvalidOrder, OrderNotFound, AuthenticationError, RequestTimeout, BadSymbol, RateLimitExceeded, NetworkError, CancelPending, NotSupported, AccountNotEnabled, ContractUnavailable, ManualInteractionNeeded, OperationRejected } from './base/errors.js';
10
10
  import { Precise } from './base/Precise.js';
11
11
  import { TICK_SIZE } from './base/functions/number.js';
12
12
  import { sha256 } from './static_dependencies/noble-hashes/sha256.js';
@@ -96,6 +96,8 @@ export default class okx extends Exchange {
96
96
  'fetchLedgerEntry': undefined,
97
97
  'fetchLeverage': true,
98
98
  'fetchLeverageTiers': false,
99
+ 'fetchLongShortRatio': false,
100
+ 'fetchLongShortRatioHistory': true,
99
101
  'fetchMarginAdjustmentHistory': true,
100
102
  'fetchMarketLeverageTiers': true,
101
103
  'fetchMarkets': true,
@@ -238,6 +240,7 @@ export default class okx extends Exchange {
238
240
  'rubik/stat/margin/loan-ratio': 4,
239
241
  // long/short
240
242
  'rubik/stat/contracts/long-short-account-ratio': 4,
243
+ 'rubik/stat/contracts/long-short-account-ratio-contract': 4,
241
244
  'rubik/stat/contracts/open-interest-volume': 4,
242
245
  'rubik/stat/option/open-interest-volume': 4,
243
246
  // put/call
@@ -356,6 +359,9 @@ export default class okx extends Exchange {
356
359
  'account/fixed-loan/borrowing-limit': 4,
357
360
  'account/fixed-loan/borrowing-quote': 5,
358
361
  'account/fixed-loan/borrowing-orders-list': 5,
362
+ 'account/spot-manual-borrow-repay': 10,
363
+ 'account/set-auto-repay': 4,
364
+ 'account/spot-borrow-repay-history': 4,
359
365
  // subaccount
360
366
  'users/subaccount/list': 10,
361
367
  'account/subaccount/balances': 10 / 3,
@@ -887,6 +893,11 @@ export default class okx extends Exchange {
887
893
  '59301': ExchangeError,
888
894
  '59313': ExchangeError,
889
895
  '59401': ExchangeError,
896
+ '59410': OperationRejected,
897
+ '59411': InsufficientFunds,
898
+ '59412': OperationRejected,
899
+ '59413': OperationRejected,
900
+ '59414': BadRequest,
890
901
  '59500': ExchangeError,
891
902
  '59501': ExchangeError,
892
903
  '59502': ExchangeError,
@@ -6676,16 +6687,6 @@ export default class okx extends Exchange {
6676
6687
  }
6677
6688
  return borrowRateHistories;
6678
6689
  }
6679
- parseBorrowRateHistory(response, code, since, limit) {
6680
- const result = [];
6681
- for (let i = 0; i < response.length; i++) {
6682
- const item = response[i];
6683
- const borrowRate = this.parseBorrowRate(item);
6684
- result.push(borrowRate);
6685
- }
6686
- const sorted = this.sortBy(result, 'timestamp');
6687
- return this.filterByCurrencySinceLimit(sorted, code, since, limit);
6688
- }
6689
6690
  async fetchBorrowRateHistories(codes = undefined, since = undefined, limit = undefined, params = {}) {
6690
6691
  /**
6691
6692
  * @method
@@ -8502,4 +8503,75 @@ export default class okx extends Exchange {
8502
8503
  const positions = this.parsePositions(data, symbols, params);
8503
8504
  return this.filterBySinceLimit(positions, since, limit);
8504
8505
  }
8506
+ async fetchLongShortRatioHistory(symbol = undefined, timeframe = undefined, since = undefined, limit = undefined, params = {}) {
8507
+ /**
8508
+ * @method
8509
+ * @name okx#fetchLongShortRatioHistory
8510
+ * @description fetches the long short ratio history for a unified market symbol
8511
+ * @see https://www.okx.com/docs-v5/en/#trading-statistics-rest-api-get-contract-long-short-ratio
8512
+ * @param {string} symbol unified symbol of the market to fetch the long short ratio for
8513
+ * @param {string} [timeframe] the period for the ratio
8514
+ * @param {int} [since] the earliest time in ms to fetch ratios for
8515
+ * @param {int} [limit] the maximum number of long short ratio structures to retrieve
8516
+ * @param {object} [params] extra parameters specific to the exchange API endpoint
8517
+ * @param {int} [params.until] timestamp in ms of the latest ratio to fetch
8518
+ * @returns {object[]} an array of [long short ratio structures]{@link https://docs.ccxt.com/#/?id=long-short-ratio-structure}
8519
+ */
8520
+ await this.loadMarkets();
8521
+ const market = this.market(symbol);
8522
+ const request = {
8523
+ 'instId': market['id'],
8524
+ };
8525
+ const until = this.safeString2(params, 'until', 'end');
8526
+ params = this.omit(params, 'until');
8527
+ if (until !== undefined) {
8528
+ request['end'] = until;
8529
+ }
8530
+ if (timeframe !== undefined) {
8531
+ request['period'] = timeframe;
8532
+ }
8533
+ if (since !== undefined) {
8534
+ request['begin'] = since;
8535
+ }
8536
+ if (limit !== undefined) {
8537
+ request['limit'] = limit;
8538
+ }
8539
+ const response = await this.publicGetRubikStatContractsLongShortAccountRatioContract(this.extend(request, params));
8540
+ //
8541
+ // {
8542
+ // "code": "0",
8543
+ // "data": [
8544
+ // ["1729323600000", "0.9398602814619824"],
8545
+ // ["1729323300000", "0.9398602814619824"],
8546
+ // ["1729323000000", "0.9398602814619824"],
8547
+ // ],
8548
+ // "msg": ""
8549
+ // }
8550
+ //
8551
+ const data = this.safeList(response, 'data', []);
8552
+ const result = [];
8553
+ for (let i = 0; i < data.length; i++) {
8554
+ const entry = data[i];
8555
+ result.push({
8556
+ 'timestamp': this.safeString(entry, 0),
8557
+ 'longShortRatio': this.safeString(entry, 1),
8558
+ });
8559
+ }
8560
+ return this.parseLongShortRatioHistory(result, market);
8561
+ }
8562
+ parseLongShortRatio(info, market = undefined) {
8563
+ const timestamp = this.safeInteger(info, 'timestamp');
8564
+ let symbol = undefined;
8565
+ if (market !== undefined) {
8566
+ symbol = market['symbol'];
8567
+ }
8568
+ return {
8569
+ 'info': info,
8570
+ 'symbol': symbol,
8571
+ 'timestamp': timestamp,
8572
+ 'datetime': this.iso8601(timestamp),
8573
+ 'timeframe': undefined,
8574
+ 'longShortRatio': this.safeNumber(info, 'longShortRatio'),
8575
+ };
8576
+ }
8505
8577
  }
package/js/src/paradex.js CHANGED
@@ -5,7 +5,7 @@
5
5
  // EDIT THE CORRESPONDENT .ts FILE INSTEAD
6
6
 
7
7
  // ---------------------------------------------------------------------------
8
- import { Precise } from '../ccxt.js';
8
+ import { Precise } from './base/Precise.js';
9
9
  import Exchange from './abstract/paradex.js';
10
10
  import { ExchangeError, PermissionDenied, AuthenticationError, BadRequest, ArgumentsRequired, OperationRejected, InvalidOrder } from './base/errors.js';
11
11
  import { TICK_SIZE } from './base/functions/number.js';
@@ -97,7 +97,7 @@ export class BLAKE2 extends Hash {
97
97
  }
98
98
  _cloneInto(to) {
99
99
  const { buffer, length, finished, destroyed, outputLen, pos } = this;
100
- to || (to = new this.constructor({ dkLen: outputLen }));
100
+ to ||= new this.constructor({ dkLen: outputLen });
101
101
  to.set(...this.get());
102
102
  to.length = length;
103
103
  to.finished = finished;
@@ -105,7 +105,7 @@ export class SHA2 extends Hash {
105
105
  return res;
106
106
  }
107
107
  _cloneInto(to) {
108
- to || (to = new this.constructor());
108
+ to ||= new this.constructor();
109
109
  to.set(...this.get());
110
110
  const { blockLen, buffer, length, finished, destroyed, pos } = this;
111
111
  to.length = length;
@@ -55,7 +55,7 @@ class HMAC extends Hash {
55
55
  }
56
56
  _cloneInto(to) {
57
57
  // Create new instance without calling constructor since key already in state and we don't know it.
58
- to || (to = Object.create(Object.getPrototypeOf(this), {}));
58
+ to ||= Object.create(Object.getPrototypeOf(this), {});
59
59
  const { oHash, iHash, finished, destroyed, blockLen, outputLen } = this;
60
60
  to = to;
61
61
  to.finished = finished;
@@ -113,7 +113,7 @@ class TupleHash extends Keccak {
113
113
  super.finish();
114
114
  }
115
115
  _cloneInto(to) {
116
- to || (to = new TupleHash(this.blockLen, this.outputLen, this.enableXOF));
116
+ to ||= new TupleHash(this.blockLen, this.outputLen, this.enableXOF);
117
117
  return super._cloneInto(to);
118
118
  }
119
119
  clone() {
@@ -142,7 +142,7 @@ class ParallelHash extends Keccak {
142
142
  this.chunksDone = 0; // How many chunks we already have
143
143
  cshakePers(this, { NISTfn: 'ParallelHash', personalization: opts.personalization });
144
144
  let { blockLen: B } = opts;
145
- B || (B = 8);
145
+ B ||= 8;
146
146
  assertNumber(B);
147
147
  this.chunkLen = B;
148
148
  super.update(leftEncode(B));
@@ -179,7 +179,7 @@ class ParallelHash extends Keccak {
179
179
  super.finish();
180
180
  }
181
181
  _cloneInto(to) {
182
- to || (to = new ParallelHash(this.blockLen, this.outputLen, this.leafCons, this.enableXOF));
182
+ to ||= new ParallelHash(this.blockLen, this.outputLen, this.leafCons, this.enableXOF);
183
183
  if (this.leafHash)
184
184
  to.leafHash = this.leafHash._cloneInto(to.leafHash);
185
185
  to.chunkPos = this.chunkPos;
@@ -273,7 +273,7 @@ class KangarooTwelve extends Keccak {
273
273
  }
274
274
  _cloneInto(to) {
275
275
  const { blockLen, leafLen, leafHash, outputLen, rounds } = this;
276
- to || (to = new KangarooTwelve(blockLen, leafLen, outputLen, rounds, {}));
276
+ to ||= new KangarooTwelve(blockLen, leafLen, outputLen, rounds, {});
277
277
  super._cloneInto(to);
278
278
  if (leafHash)
279
279
  to.leafHash = leafHash._cloneInto(to.leafHash);
@@ -340,7 +340,7 @@ class KeccakPRG extends Keccak {
340
340
  }
341
341
  _cloneInto(to) {
342
342
  const { rate } = this;
343
- to || (to = new KeccakPRG(1600 - rate));
343
+ to ||= new KeccakPRG(1600 - rate);
344
344
  super._cloneInto(to);
345
345
  to.rate = rate;
346
346
  return to;
@@ -175,7 +175,7 @@ export class Keccak extends Hash {
175
175
  }
176
176
  _cloneInto(to) {
177
177
  const { blockLen, suffix, outputLen, rounds, enableXOF } = this;
178
- to || (to = new Keccak(blockLen, suffix, outputLen, enableXOF, rounds));
178
+ to ||= new Keccak(blockLen, suffix, outputLen, enableXOF, rounds);
179
179
  to.state32.set(this.state32);
180
180
  to.pos = this.pos;
181
181
  to.posOut = this.posOut;
@@ -0,0 +1,2 @@
1
+ export { Unpromise } from "./unpromise";
2
+ export type { ProxyPromise, SubscribedPromise, PromiseExecutor, PromiseWithResolvers, } from "./types";
@@ -0,0 +1,7 @@
1
+ // ----------------------------------------------------------------------------
2
+
3
+ // PLEASE DO NOT EDIT THIS FILE, IT IS GENERATED AND WILL BE OVERWRITTEN:
4
+ // https://github.com/ccxt/ccxt/blob/master/CONTRIBUTING.md#how-to-contribute-code
5
+ // EDIT THE CORRESPONDENT .ts FILE INSTEAD
6
+
7
+ export { Unpromise } from "./unpromise";
@@ -0,0 +1,28 @@
1
+ /** TYPES */
2
+ /** A promise that exploits a single, memory-safe upstream subscription
3
+ * to a single re-used Unpromise that persists for the VM lifetime of a
4
+ * Promise.
5
+ *
6
+ * Calling unsubscribe() removes the subscription, eliminating
7
+ * all references to the SubscribedPromise. */
8
+ export interface SubscribedPromise<T> extends Promise<T> {
9
+ unsubscribe: () => void;
10
+ }
11
+ /** Duplicate of Promise interface, except each call returns SubscribedPromise */
12
+ export interface ProxyPromise<T> extends Promise<T> {
13
+ subscribe: () => SubscribedPromise<T>;
14
+ then: <TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined) => SubscribedPromise<TResult1 | TResult2>;
15
+ catch: <TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null | undefined) => SubscribedPromise<T | TResult>;
16
+ finally: (onfinally?: (() => void) | null | undefined) => SubscribedPromise<T>;
17
+ }
18
+ export declare type PromiseExecutor<T> = (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void;
19
+ /** A standard pattern for a resolvable, rejectable Promise, based
20
+ * on the emerging ES2023 standard. Type ported from
21
+ * https://github.com/microsoft/TypeScript/pull/56593 */
22
+ export interface PromiseWithResolvers<T> {
23
+ promise: Promise<T>;
24
+ resolve: (value: T | PromiseLike<T>) => void;
25
+ reject: (reason?: any) => void;
26
+ }
27
+ /** Given an array, this is the union of its members' types. */
28
+ export declare type MemberOf<Arr extends readonly unknown[]> = Arr[number];
@@ -0,0 +1,8 @@
1
+ // ----------------------------------------------------------------------------
2
+
3
+ // PLEASE DO NOT EDIT THIS FILE, IT IS GENERATED AND WILL BE OVERWRITTEN:
4
+ // https://github.com/ccxt/ccxt/blob/master/CONTRIBUTING.md#how-to-contribute-code
5
+ // EDIT THE CORRESPONDENT .ts FILE INSTEAD
6
+
7
+ /** TYPES */
8
+ export {};
@@ -0,0 +1,120 @@
1
+ import type { PromiseExecutor, PromiseWithResolvers, ProxyPromise, SubscribedPromise } from "./types";
2
+ /**
3
+ * Every `Promise<T>` can be shadowed by a single `ProxyPromise<T>`. It is
4
+ * created once, cached and reused throughout the lifetime of the Promise. Get a
5
+ * Promise's ProxyPromise using `Unpromise.proxy(promise)`.
6
+ *
7
+ * The `ProxyPromise<T>` attaches handlers to the original `Promise<T>`
8
+ * `.then()` and `.catch()` just once. Promises derived from it use a
9
+ * subscription- (and unsubscription-) based mechanism that monitors these
10
+ * handlers.
11
+ *
12
+ * Every time you call `.subscribe()`, `.then()` `.catch()` or `.finally()` on a
13
+ * `ProxyPromise<T>` it returns a `SubscribedPromise<T>` having an additional
14
+ * `unsubscribe()` method. Calling `unsubscribe()` detaches reference chains
15
+ * from the original, potentially long-lived Promise, eliminating memory leaks.
16
+ *
17
+ * This approach can eliminate the memory leaks that otherwise come about from
18
+ * repeated `race()` or `any()` calls invoking `.then()` and `.catch()` multiple
19
+ * times on the same long-lived native Promise (subscriptions which can never be
20
+ * cleaned up).
21
+ *
22
+ * `Unpromise.race(promises)` is a reference implementation of `Promise.race`
23
+ * avoiding memory leaks when using long-lived unsettled Promises.
24
+ *
25
+ * `Unpromise.any(promises)` is a reference implementation of `Promise.any`
26
+ * avoiding memory leaks when using long-lived unsettled Promises.
27
+ *
28
+ * `Unpromise.resolve(promise)` returns an ephemeral `SubscribedPromise<T>` for
29
+ * any given `Promise<T>` facilitating arbitrary async/await patterns. Behind
30
+ * the scenes, `resolve` is implemented simply as
31
+ * `Unpromise.proxy(promise).subscribe()`. Don't forget to call `.unsubscribe()`
32
+ * to tidy up!
33
+ *
34
+ */
35
+ export declare class Unpromise<T> implements ProxyPromise<T> {
36
+ /** INSTANCE IMPLEMENTATION */
37
+ /** The promise shadowed by this Unpromise<T> */
38
+ protected readonly promise: Promise<T> | PromiseLike<T>;
39
+ /** Promises expecting eventual settlement (unless unsubscribed first). This list is deleted
40
+ * after the original promise settles - no further notifications will be issued. */
41
+ protected subscribers: ReadonlyArray<PromiseWithResolvers<T>> | null;
42
+ /** The Promise's settlement (recorded when it fulfils or rejects). This is consulted when
43
+ * calling .subscribe() .then() .catch() .finally() to see if an immediately-resolving Promise
44
+ * can be returned, and therefore subscription can be bypassed. */
45
+ protected settlement: PromiseSettledResult<T> | null;
46
+ /** Constructor accepts a normal Promise executor function like `new
47
+ * Unpromise((resolve, reject) => {...})` or accepts a pre-existing Promise
48
+ * like `new Unpromise(existingPromise)`. Adds `.then()` and `.catch()`
49
+ * handlers to the Promise. These handlers pass fulfilment and rejection
50
+ * notifications to downstream subscribers and maintains records of value
51
+ * or error if the Promise ever settles. */
52
+ protected constructor(promise: Promise<T>);
53
+ protected constructor(promise: PromiseLike<T>);
54
+ protected constructor(executor: PromiseExecutor<T>);
55
+ /** Create a promise that mitigates uncontrolled subscription to a long-lived
56
+ * Promise via .then() and .catch() - otherwise a source of memory leaks.
57
+ *
58
+ * The returned promise has an `unsubscribe()` method which can be called when
59
+ * the Promise is no longer being tracked by application logic, and which
60
+ * ensures that there is no reference chain from the original promise to the
61
+ * new one, and therefore no memory leak.
62
+ *
63
+ * If original promise has not yet settled, this adds a new unique promise
64
+ * that listens to then/catch events, along with an `unsubscribe()` method to
65
+ * detach it.
66
+ *
67
+ * If original promise has settled, then creates a new Promise.resolve() or
68
+ * Promise.reject() and provided unsubscribe is a noop.
69
+ *
70
+ * If you call `unsubscribe()` before the returned Promise has settled, it
71
+ * will never settle.
72
+ */
73
+ subscribe(): SubscribedPromise<T>;
74
+ /** STANDARD PROMISE METHODS (but returning a SubscribedPromise) */
75
+ then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined): SubscribedPromise<TResult1 | TResult2>;
76
+ catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null | undefined): SubscribedPromise<T | TResult>;
77
+ finally(onfinally?: (() => void) | null | undefined): SubscribedPromise<T>;
78
+ /** TOSTRING SUPPORT */
79
+ readonly [Symbol.toStringTag] = "Unpromise";
80
+ /** Unpromise STATIC METHODS */
81
+ /** Create or Retrieve the proxy Unpromise (a re-used Unpromise for the VM lifetime
82
+ * of the provided Promise reference) */
83
+ static proxy<T>(promise: PromiseLike<T>): ProxyPromise<T>;
84
+ /** Create and store an Unpromise keyed by an original Promise. */
85
+ protected static createSubscribablePromise<T>(promise: PromiseLike<T>): Unpromise<T>;
86
+ /** Retrieve a previously-created Unpromise keyed by an original Promise. */
87
+ protected static getSubscribablePromise<T>(promise: PromiseLike<T>): ProxyPromise<T>;
88
+ /** Promise STATIC METHODS */
89
+ /** Lookup the Unpromise for this promise, and derive a SubscribedPromise from
90
+ * it (that can be later unsubscribed to eliminate Memory leaks) */
91
+ static resolve<T>(value: T | PromiseLike<T>): SubscribedPromise<Awaited<T>>;
92
+ /** Perform Promise.any() via SubscribedPromises, then unsubscribe them.
93
+ * Equivalent to Promise.any but eliminates memory leaks from long-lived
94
+ * promises accumulating .then() and .catch() subscribers. */
95
+ static any<T extends readonly unknown[] | []>(values: T): Promise<Awaited<T[number]>>;
96
+ /** Perform Promise.race via SubscribedPromises, then unsubscribe them.
97
+ * Equivalent to Promise.race but eliminates memory leaks from long-lived
98
+ * promises accumulating .then() and .catch() subscribers. */
99
+ static race<T extends readonly unknown[] | []>(values: T): Promise<Awaited<T[number]>>;
100
+ /** Create a race of SubscribedPromises that will fulfil to a single winning
101
+ * Promise (in a 1-Tuple). Eliminates memory leaks from long-lived promises
102
+ * accumulating .then() and .catch() subscribers. Allows simple logic to
103
+ * consume the result, like...
104
+ * ```ts
105
+ * const [ winner ] = await Unpromise.race([ promiseA, promiseB ]);
106
+ * if(winner === promiseB){
107
+ * const result = await promiseB;
108
+ * // do the thing
109
+ * }
110
+ * ```
111
+ * */
112
+ static raceReferences<P extends Promise<unknown>>(promises: readonly P[]): Promise<readonly [P]>;
113
+ }
114
+ /** Promises a 1-tuple containing the original promise when it resolves. Allows
115
+ * awaiting the eventual Promise ***reference*** (easy to destructure and
116
+ * exactly compare with ===). Avoids resolving to the Promise ***value*** (which
117
+ * may be ambiguous and therefore hard to identify as the winner of a race).
118
+ * You can call unsubscribe on the Promise to mitigate memory leaks.
119
+ * */
120
+ export declare function resolveSelfTuple<P extends Promise<unknown>>(promise: P): SubscribedPromise<readonly [P]>;