@talismn/util 0.0.0-pr2079-20250710050859 → 0.0.0-pr2091-20250715012835

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.
@@ -30,3 +30,4 @@ export * from "./isNotNil";
30
30
  export * from "./isTruthy";
31
31
  export * from "./isAbortError";
32
32
  export * from "./getSharedObservable";
33
+ export * from "./keepAlive";
@@ -0,0 +1,17 @@
1
+ import type { OperatorFunction } from "rxjs";
2
+ /**
3
+ * An RxJS operator that keeps the source observable alive for a specified duration
4
+ * after all subscribers have unsubscribed. This prevents expensive re-subscriptions
5
+ * when subscribers come and go frequently.
6
+ *
7
+ * @param keepAliveMs - Duration in milliseconds to keep the source alive after last unsubscription
8
+ * @returns MonoTypeOperatorFunction that can be used in pipe()
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * const data$ = expensive_api_call$.pipe(
13
+ * keepAlive(3000) // Keep alive for 3 seconds
14
+ * );
15
+ * ```
16
+ */
17
+ export declare const keepAlive: <T>(timeout: number) => OperatorFunction<T, T>;
@@ -345,6 +345,40 @@ const getSharedObservable = (namespace, args, createObservable, serializer = arg
345
345
  return sharedObs;
346
346
  };
347
347
 
348
+ /**
349
+ * An RxJS operator that keeps the source observable alive for a specified duration
350
+ * after all subscribers have unsubscribed. This prevents expensive re-subscriptions
351
+ * when subscribers come and go frequently.
352
+ *
353
+ * @param keepAliveMs - Duration in milliseconds to keep the source alive after last unsubscription
354
+ * @returns MonoTypeOperatorFunction that can be used in pipe()
355
+ *
356
+ * @example
357
+ * ```typescript
358
+ * const data$ = expensive_api_call$.pipe(
359
+ * keepAlive(3000) // Keep alive for 3 seconds
360
+ * );
361
+ * ```
362
+ */
363
+ const keepAlive = timeout => {
364
+ let release;
365
+ return source => source.pipe(rxjs.tap({
366
+ subscribe: () => {
367
+ release = keepSourceSubscribed(source, timeout);
368
+ },
369
+ unsubscribe: () => {
370
+ release?.();
371
+ }
372
+ }), rxjs.shareReplay({
373
+ refCount: true,
374
+ bufferSize: 1
375
+ }));
376
+ };
377
+ const keepSourceSubscribed = (observable, ms) => {
378
+ const sub = observable.subscribe();
379
+ return () => setTimeout(() => sub.unsubscribe(), ms);
380
+ };
381
+
348
382
  exports.BigMath = BigMath;
349
383
  exports.Deferred = Deferred;
350
384
  exports.MAX_DECIMALS_FORMAT = MAX_DECIMALS_FORMAT;
@@ -370,6 +404,7 @@ exports.isEthereumAddress = isEthereumAddress;
370
404
  exports.isNotNil = isNotNil;
371
405
  exports.isTruthy = isTruthy;
372
406
  exports.isValidSubstrateAddress = isValidSubstrateAddress;
407
+ exports.keepAlive = keepAlive;
373
408
  exports.normalizeAddress = normalizeAddress;
374
409
  exports.planckToTokens = planckToTokens;
375
410
  exports.sleep = sleep;
@@ -345,6 +345,40 @@ const getSharedObservable = (namespace, args, createObservable, serializer = arg
345
345
  return sharedObs;
346
346
  };
347
347
 
348
+ /**
349
+ * An RxJS operator that keeps the source observable alive for a specified duration
350
+ * after all subscribers have unsubscribed. This prevents expensive re-subscriptions
351
+ * when subscribers come and go frequently.
352
+ *
353
+ * @param keepAliveMs - Duration in milliseconds to keep the source alive after last unsubscription
354
+ * @returns MonoTypeOperatorFunction that can be used in pipe()
355
+ *
356
+ * @example
357
+ * ```typescript
358
+ * const data$ = expensive_api_call$.pipe(
359
+ * keepAlive(3000) // Keep alive for 3 seconds
360
+ * );
361
+ * ```
362
+ */
363
+ const keepAlive = timeout => {
364
+ let release;
365
+ return source => source.pipe(rxjs.tap({
366
+ subscribe: () => {
367
+ release = keepSourceSubscribed(source, timeout);
368
+ },
369
+ unsubscribe: () => {
370
+ release?.();
371
+ }
372
+ }), rxjs.shareReplay({
373
+ refCount: true,
374
+ bufferSize: 1
375
+ }));
376
+ };
377
+ const keepSourceSubscribed = (observable, ms) => {
378
+ const sub = observable.subscribe();
379
+ return () => setTimeout(() => sub.unsubscribe(), ms);
380
+ };
381
+
348
382
  exports.BigMath = BigMath;
349
383
  exports.Deferred = Deferred;
350
384
  exports.MAX_DECIMALS_FORMAT = MAX_DECIMALS_FORMAT;
@@ -370,6 +404,7 @@ exports.isEthereumAddress = isEthereumAddress;
370
404
  exports.isNotNil = isNotNil;
371
405
  exports.isTruthy = isTruthy;
372
406
  exports.isValidSubstrateAddress = isValidSubstrateAddress;
407
+ exports.keepAlive = keepAlive;
373
408
  exports.normalizeAddress = normalizeAddress;
374
409
  exports.planckToTokens = planckToTokens;
375
410
  exports.sleep = sleep;
@@ -2,7 +2,7 @@ import { u8aToHex, u8aConcat, u8aToU8a, hexToU8a } from '@polkadot/util';
2
2
  import { blake2AsU8a, isEthereumAddress as isEthereumAddress$1, ethereumEncode, decodeAddress as decodeAddress$1, base58Decode, checkAddressChecksum, xxhashAsU8a } from '@polkadot/util-crypto';
3
3
  import { twMerge } from 'tailwind-merge';
4
4
  import { encodeAddress, decodeAddress } from '@polkadot/keyring';
5
- import { concat, take, skip, debounceTime, shareReplay } from 'rxjs';
5
+ import { concat, take, skip, debounceTime, shareReplay, tap } from 'rxjs';
6
6
  import BigNumber from 'bignumber.js';
7
7
 
8
8
  const addTrailingSlash = url => {
@@ -339,4 +339,38 @@ const getSharedObservable = (namespace, args, createObservable, serializer = arg
339
339
  return sharedObs;
340
340
  };
341
341
 
342
- export { BigMath, Deferred, MAX_DECIMALS_FORMAT, addTrailingSlash, blake2Concat, classNames, convertAddress, decodeAnyAddress, decodeSs58Format, encodeAnyAddress, firstThenDebounce, formatDecimals, formatPrice, getSharedObservable, hasOwnProperty, isAbortError, isAddressEqual, isArrayOf, isAscii, isBigInt, isBooleanTrue, isEthereumAddress, isNotNil, isTruthy, isValidSubstrateAddress, normalizeAddress, planckToTokens, sleep, throwAfter, tokensToPlanck, twox64Concat, validateHexString };
342
+ /**
343
+ * An RxJS operator that keeps the source observable alive for a specified duration
344
+ * after all subscribers have unsubscribed. This prevents expensive re-subscriptions
345
+ * when subscribers come and go frequently.
346
+ *
347
+ * @param keepAliveMs - Duration in milliseconds to keep the source alive after last unsubscription
348
+ * @returns MonoTypeOperatorFunction that can be used in pipe()
349
+ *
350
+ * @example
351
+ * ```typescript
352
+ * const data$ = expensive_api_call$.pipe(
353
+ * keepAlive(3000) // Keep alive for 3 seconds
354
+ * );
355
+ * ```
356
+ */
357
+ const keepAlive = timeout => {
358
+ let release;
359
+ return source => source.pipe(tap({
360
+ subscribe: () => {
361
+ release = keepSourceSubscribed(source, timeout);
362
+ },
363
+ unsubscribe: () => {
364
+ release?.();
365
+ }
366
+ }), shareReplay({
367
+ refCount: true,
368
+ bufferSize: 1
369
+ }));
370
+ };
371
+ const keepSourceSubscribed = (observable, ms) => {
372
+ const sub = observable.subscribe();
373
+ return () => setTimeout(() => sub.unsubscribe(), ms);
374
+ };
375
+
376
+ export { BigMath, Deferred, MAX_DECIMALS_FORMAT, addTrailingSlash, blake2Concat, classNames, convertAddress, decodeAnyAddress, decodeSs58Format, encodeAnyAddress, firstThenDebounce, formatDecimals, formatPrice, getSharedObservable, hasOwnProperty, isAbortError, isAddressEqual, isArrayOf, isAscii, isBigInt, isBooleanTrue, isEthereumAddress, isNotNil, isTruthy, isValidSubstrateAddress, keepAlive, normalizeAddress, planckToTokens, sleep, throwAfter, tokensToPlanck, twox64Concat, validateHexString };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@talismn/util",
3
- "version": "0.0.0-pr2079-20250710050859",
3
+ "version": "0.0.0-pr2091-20250715012835",
4
4
  "author": "Talisman",
5
5
  "homepage": "https://talisman.xyz",
6
6
  "license": "GPL-3.0-or-later",